mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
added support on avalache
This commit is contained in:
parent
054f95a92d
commit
7edaa872e7
12
contracts/avalanche/connectors/0x/events.sol
Normal file
12
contracts/avalanche/connectors/0x/events.sol
Normal file
|
@ -0,0 +1,12 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
contract Events {
|
||||
event LogSwap(
|
||||
address indexed buyToken,
|
||||
address indexed sellToken,
|
||||
uint256 buyAmt,
|
||||
uint256 sellAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
}
|
68
contracts/avalanche/connectors/0x/helpers.sol
Normal file
68
contracts/avalanche/connectors/0x/helpers.sol
Normal file
|
@ -0,0 +1,68 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import {TokenInterface} from "../../common/interfaces.sol";
|
||||
import {DSMath} from "../../common/math.sol";
|
||||
import {Basic} from "../../common/basic.sol";
|
||||
import {ZeroExData, zeroExInterface} from "./interface.sol";
|
||||
|
||||
contract Helpers is DSMath, Basic {
|
||||
/**
|
||||
* @dev 0x Address
|
||||
*/
|
||||
address internal constant zeroExAddr =
|
||||
0xDef1C0ded9bec7F1a1670819833240f027b25EfF;
|
||||
|
||||
function _swapHelper(ZeroExData memory zeroExData, uint256 avaxAmt)
|
||||
internal
|
||||
returns (uint256 buyAmt)
|
||||
{
|
||||
TokenInterface buyToken = zeroExData.buyToken;
|
||||
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(
|
||||
buyToken,
|
||||
zeroExData.sellToken
|
||||
);
|
||||
uint256 _sellAmt18 = convertTo18(_sellDec, zeroExData._sellAmt);
|
||||
uint256 _slippageAmt = convert18ToDec(
|
||||
_buyDec,
|
||||
wmul(zeroExData.unitAmt, _sellAmt18)
|
||||
);
|
||||
|
||||
uint256 initalBal = getTokenBal(buyToken);
|
||||
|
||||
// solium-disable-next-line security/no-call-value
|
||||
(bool success, ) = zeroExAddr.call{value: avaxAmt}(zeroExData.callData);
|
||||
if (!success) revert("0x-swap-failed");
|
||||
|
||||
uint256 finalBal = getTokenBal(buyToken);
|
||||
|
||||
buyAmt = sub(finalBal, initalBal);
|
||||
|
||||
require(_slippageAmt <= buyAmt, "Too much slippage");
|
||||
}
|
||||
|
||||
function _swap(ZeroExData memory zeroExData, uint256 setId)
|
||||
internal
|
||||
returns (ZeroExData memory)
|
||||
{
|
||||
TokenInterface _sellAddr = zeroExData.sellToken;
|
||||
|
||||
uint256 avaxAmt;
|
||||
if (address(_sellAddr) == avaxAddr) {
|
||||
avaxAmt = zeroExData._sellAmt;
|
||||
} else {
|
||||
address transformWallet = address(
|
||||
zeroExInterface(zeroExAddr).getTransformWallet()
|
||||
);
|
||||
approve(
|
||||
TokenInterface(_sellAddr),
|
||||
transformWallet,
|
||||
zeroExData._sellAmt
|
||||
);
|
||||
}
|
||||
|
||||
zeroExData._buyAmt = _swapHelper(zeroExData, avaxAmt);
|
||||
setUint(setId, zeroExData._buyAmt);
|
||||
|
||||
return zeroExData;
|
||||
}
|
||||
}
|
46
contracts/avalanche/connectors/0x/interface.sol
Normal file
46
contracts/avalanche/connectors/0x/interface.sol
Normal file
|
@ -0,0 +1,46 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import {TokenInterface} from "../../common/interfaces.sol";
|
||||
|
||||
interface zeroExInterface {
|
||||
function getTransformWallet() external view returns (IFlashWallet wallet);
|
||||
}
|
||||
|
||||
interface IFlashWallet {
|
||||
/// @dev Execute an arbitrary call. Only an authority can call this.
|
||||
/// @param target The call target.
|
||||
/// @param callData The call data.
|
||||
/// @param value Ether to attach to the call.
|
||||
/// @return resultData The data returned by the call.
|
||||
function executeCall(
|
||||
address payable target,
|
||||
bytes calldata callData,
|
||||
uint256 value
|
||||
) external payable returns (bytes memory resultData);
|
||||
|
||||
/// @dev Execute an arbitrary delegatecall, in the context of this puppet.
|
||||
/// Only an authority can call this.
|
||||
/// @param target The call target.
|
||||
/// @param callData The call data.
|
||||
/// @return resultData The data returned by the call.
|
||||
function executeDelegateCall(
|
||||
address payable target,
|
||||
bytes calldata callData
|
||||
) external payable returns (bytes memory resultData);
|
||||
|
||||
/// @dev Allows the puppet to receive ETH.
|
||||
receive() external payable;
|
||||
|
||||
/// @dev Fetch the immutable owner/deployer of this contract.
|
||||
/// @return owner_ The immutable owner/deployer/
|
||||
function owner() external view returns (address owner_);
|
||||
}
|
||||
|
||||
struct ZeroExData {
|
||||
TokenInterface sellToken;
|
||||
TokenInterface buyToken;
|
||||
uint256 _sellAmt;
|
||||
uint256 _buyAmt;
|
||||
uint256 unitAmt;
|
||||
bytes callData;
|
||||
}
|
64
contracts/avalanche/connectors/0x/main.sol
Normal file
64
contracts/avalanche/connectors/0x/main.sol
Normal file
|
@ -0,0 +1,64 @@
|
|||
pragma solidity ^0.7.0;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
/**
|
||||
* @title 0x.
|
||||
* @dev On-chain DEX Aggregator.
|
||||
*/
|
||||
|
||||
import {TokenInterface, MemoryInterface} from "../../common/interfaces.sol";
|
||||
import {Stores} from "../../common/stores.sol";
|
||||
import {ZeroExData, zeroExInterface} from "./interface.sol";
|
||||
import {Helpers} from "./helpers.sol";
|
||||
import {Events} from "./events.sol";
|
||||
|
||||
abstract contract ZeroEx is Helpers {
|
||||
/**
|
||||
* @notice Swap tokens on 0x
|
||||
* @dev Sell Avax/ERC20_Token using 0x.
|
||||
* @param buyAddr The address of the token to buy.(For avax: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param sellAddr The address of the token to sell.(For avax: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param sellAmt The amount of the token to sell.
|
||||
* @param unitAmt The amount of buyAmt/sellAmt with slippage.
|
||||
* @param callData Data from 0x API.
|
||||
* @param setId ID stores the amount of token brought.
|
||||
*/
|
||||
|
||||
function swap(
|
||||
address buyAddr,
|
||||
address sellAddr,
|
||||
uint256 sellAmt,
|
||||
uint256 unitAmt,
|
||||
bytes calldata callData,
|
||||
uint256 setId
|
||||
)
|
||||
external
|
||||
payable
|
||||
returns (string memory _eventName, bytes memory _eventParam)
|
||||
{
|
||||
ZeroExData memory zeroExData = ZeroExData({
|
||||
buyToken: TokenInterface(buyAddr),
|
||||
sellToken: TokenInterface(sellAddr),
|
||||
unitAmt: unitAmt,
|
||||
callData: callData,
|
||||
_sellAmt: sellAmt,
|
||||
_buyAmt: 0
|
||||
});
|
||||
|
||||
zeroExData = _swap(zeroExData, setId);
|
||||
|
||||
_eventName = "LogSwap(address,address,uint256,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(
|
||||
buyAddr,
|
||||
sellAddr,
|
||||
zeroExData._buyAmt,
|
||||
zeroExData._sellAmt,
|
||||
0,
|
||||
setId
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
contract ConnectV2ZeroExAvalanche is ZeroEx {
|
||||
string public name = "0x-V4";
|
||||
}
|
|
@ -15,9 +15,9 @@ import {Events} from "./events.sol";
|
|||
abstract contract ZeroEx is Helpers {
|
||||
/**
|
||||
* @notice Swap tokens on 0x
|
||||
* @dev Sell ETH/ERC20_Token using 0x.
|
||||
* @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param sellAddr The address of the token to sell.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @dev Sell matic/ERC20_Token using 0x.
|
||||
* @param buyAddr The address of the token to buy.(For matic: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param sellAddr The address of the token to sell.(For matic: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param sellAmt The amount of the token to sell.
|
||||
* @param unitAmt The amount of buyAmt/sellAmt with slippage.
|
||||
* @param callData Data from 0x API.
|
||||
|
|
Loading…
Reference in New Issue
Block a user