mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
Merge branch 'main' into feat/sushiswap
This commit is contained in:
commit
f712c84528
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
|
||||
);
|
||||
}
|
65
contracts/avalanche/connectors/0x/helpers.sol
Normal file
65
contracts/avalanche/connectors/0x/helpers.sol
Normal file
|
@ -0,0 +1,65 @@
|
|||
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} 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 {
|
||||
approve(
|
||||
TokenInterface(_sellAddr),
|
||||
zeroExAddr,
|
||||
zeroExData._sellAmt
|
||||
);
|
||||
}
|
||||
|
||||
zeroExData._buyAmt = _swapHelper(zeroExData, avaxAmt);
|
||||
setUint(setId, zeroExData._buyAmt);
|
||||
|
||||
return zeroExData;
|
||||
}
|
||||
}
|
12
contracts/avalanche/connectors/0x/interface.sol
Normal file
12
contracts/avalanche/connectors/0x/interface.sol
Normal file
|
@ -0,0 +1,12 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import {TokenInterface} from "../../common/interfaces.sol";
|
||||
|
||||
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} 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";
|
||||
}
|
|
@ -6,7 +6,6 @@ import { TokenInterface } from "../../common/interfaces.sol";
|
|||
import {AugustusSwapperInterface} from "./interface.sol";
|
||||
|
||||
abstract contract Helpers is DSMath, Basic {
|
||||
|
||||
struct SwapData {
|
||||
TokenInterface sellToken;
|
||||
TokenInterface buyToken;
|
||||
|
@ -16,13 +15,23 @@ abstract contract Helpers is DSMath, Basic {
|
|||
bytes callData;
|
||||
}
|
||||
|
||||
address internal constant paraswap = 0xDEF171Fe48CF0115B1d80b88dc8eAB59176FEe57;
|
||||
address internal constant paraswap =
|
||||
0xDEF171Fe48CF0115B1d80b88dc8eAB59176FEe57;
|
||||
|
||||
function _swapHelper(SwapData memory swapData, uint256 wavaxAmt) internal returns (uint256 buyAmt) {
|
||||
function _swapHelper(SwapData memory swapData, uint256 wavaxAmt)
|
||||
internal
|
||||
returns (uint256 buyAmt)
|
||||
{
|
||||
TokenInterface buyToken = swapData.buyToken;
|
||||
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(buyToken, swapData.sellToken);
|
||||
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(
|
||||
buyToken,
|
||||
swapData.sellToken
|
||||
);
|
||||
uint256 _sellAmt18 = convertTo18(_sellDec, swapData._sellAmt);
|
||||
uint256 _slippageAmt = convert18ToDec(_buyDec, wmul(swapData.unitAmt, _sellAmt18));
|
||||
uint256 _slippageAmt = convert18ToDec(
|
||||
_buyDec,
|
||||
wmul(swapData.unitAmt, _sellAmt18)
|
||||
);
|
||||
|
||||
uint256 initalBal = getTokenBal(buyToken);
|
||||
|
||||
|
@ -36,7 +45,10 @@ abstract contract Helpers is DSMath, Basic {
|
|||
require(_slippageAmt <= buyAmt, "Too much slippage");
|
||||
}
|
||||
|
||||
function _swap(SwapData memory swapData, uint256 setId) internal returns (SwapData memory) {
|
||||
function _swap(SwapData memory swapData, uint256 setId)
|
||||
internal
|
||||
returns (SwapData memory)
|
||||
{
|
||||
TokenInterface _sellAddr = swapData.sellToken;
|
||||
|
||||
uint256 avaxAmt;
|
||||
|
@ -44,7 +56,8 @@ abstract contract Helpers is DSMath, Basic {
|
|||
if (address(_sellAddr) == avaxAddr) {
|
||||
avaxAmt = swapData._sellAmt;
|
||||
} else {
|
||||
address tokenProxy = AugustusSwapperInterface(paraswap).getTokenTransferProxy();
|
||||
address tokenProxy = AugustusSwapperInterface(paraswap)
|
||||
.getTokenTransferProxy();
|
||||
approve(TokenInterface(_sellAddr), tokenProxy, swapData._sellAmt);
|
||||
}
|
||||
|
||||
|
|
12
contracts/mainnet/connectors/0x/events.sol
Normal file
12
contracts/mainnet/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
|
||||
);
|
||||
}
|
65
contracts/mainnet/connectors/0x/helpers.sol
Normal file
65
contracts/mainnet/connectors/0x/helpers.sol
Normal file
|
@ -0,0 +1,65 @@
|
|||
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} from "./interface.sol";
|
||||
|
||||
contract Helpers is DSMath, Basic {
|
||||
/**
|
||||
* @dev 0x Address
|
||||
*/
|
||||
address internal constant zeroExAddr =
|
||||
0xDef1C0ded9bec7F1a1670819833240f027b25EfF;
|
||||
|
||||
function _swapHelper(ZeroExData memory zeroExData, uint256 ethAmt)
|
||||
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: ethAmt}(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 ethAmt;
|
||||
if (address(_sellAddr) == ethAddr) {
|
||||
ethAmt = zeroExData._sellAmt;
|
||||
} else {
|
||||
approve(
|
||||
TokenInterface(_sellAddr),
|
||||
zeroExAddr,
|
||||
zeroExData._sellAmt
|
||||
);
|
||||
}
|
||||
|
||||
zeroExData._buyAmt = _swapHelper(zeroExData, ethAmt);
|
||||
setUint(setId, zeroExData._buyAmt);
|
||||
|
||||
return zeroExData;
|
||||
}
|
||||
}
|
12
contracts/mainnet/connectors/0x/interface.sol
Normal file
12
contracts/mainnet/connectors/0x/interface.sol
Normal file
|
@ -0,0 +1,12 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import {TokenInterface} from "../../common/interfaces.sol";
|
||||
|
||||
struct ZeroExData {
|
||||
TokenInterface sellToken;
|
||||
TokenInterface buyToken;
|
||||
uint256 _sellAmt;
|
||||
uint256 _buyAmt;
|
||||
uint256 unitAmt;
|
||||
bytes callData;
|
||||
}
|
64
contracts/mainnet/connectors/0x/main.sol
Normal file
64
contracts/mainnet/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} 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 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)
|
||||
* @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 ConnectV2ZeroEx is ZeroEx {
|
||||
string public name = "0x-V4";
|
||||
}
|
11
contracts/mainnet/connectors/paraswap/events.sol
Normal file
11
contracts/mainnet/connectors/paraswap/events.sol
Normal file
|
@ -0,0 +1,11 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
contract Events {
|
||||
event LogSwap(
|
||||
address buyToken,
|
||||
address sellToken,
|
||||
uint256 buyAmt,
|
||||
uint256 sellAmt,
|
||||
uint256 setId
|
||||
);
|
||||
}
|
70
contracts/mainnet/connectors/paraswap/helpers.sol
Normal file
70
contracts/mainnet/connectors/paraswap/helpers.sol
Normal file
|
@ -0,0 +1,70 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import {DSMath} from "../../common/math.sol";
|
||||
import {Basic} from "../../common/basic.sol";
|
||||
import {TokenInterface} from "../../common/interfaces.sol";
|
||||
import {AugustusSwapperInterface} from "./interface.sol";
|
||||
|
||||
abstract contract Helpers is DSMath, Basic {
|
||||
struct SwapData {
|
||||
TokenInterface sellToken;
|
||||
TokenInterface buyToken;
|
||||
uint256 _sellAmt;
|
||||
uint256 _buyAmt;
|
||||
uint256 unitAmt;
|
||||
bytes callData;
|
||||
}
|
||||
|
||||
address internal constant paraswap =
|
||||
0xDEF171Fe48CF0115B1d80b88dc8eAB59176FEe57;
|
||||
|
||||
function _swapHelper(SwapData memory swapData, uint256 wethAmt)
|
||||
internal
|
||||
returns (uint256 buyAmt)
|
||||
{
|
||||
TokenInterface buyToken = swapData.buyToken;
|
||||
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(
|
||||
buyToken,
|
||||
swapData.sellToken
|
||||
);
|
||||
uint256 _sellAmt18 = convertTo18(_sellDec, swapData._sellAmt);
|
||||
uint256 _slippageAmt = convert18ToDec(
|
||||
_buyDec,
|
||||
wmul(swapData.unitAmt, _sellAmt18)
|
||||
);
|
||||
|
||||
uint256 initalBal = getTokenBal(buyToken);
|
||||
|
||||
(bool success, ) = paraswap.call{value: wethAmt}(swapData.callData);
|
||||
if (!success) revert("paraswap-failed");
|
||||
|
||||
uint256 finalBal = getTokenBal(buyToken);
|
||||
|
||||
buyAmt = sub(finalBal, initalBal);
|
||||
|
||||
require(_slippageAmt <= buyAmt, "Too much slippage");
|
||||
}
|
||||
|
||||
function _swap(SwapData memory swapData, uint256 setId)
|
||||
internal
|
||||
returns (SwapData memory)
|
||||
{
|
||||
TokenInterface _sellAddr = swapData.sellToken;
|
||||
|
||||
uint256 ethAmt;
|
||||
|
||||
if (address(_sellAddr) == ethAddr) {
|
||||
ethAmt = swapData._sellAmt;
|
||||
} else {
|
||||
address tokenProxy = AugustusSwapperInterface(paraswap)
|
||||
.getTokenTransferProxy();
|
||||
approve(TokenInterface(_sellAddr), tokenProxy, swapData._sellAmt);
|
||||
}
|
||||
|
||||
swapData._buyAmt = _swapHelper(swapData, ethAmt);
|
||||
|
||||
setUint(setId, swapData._buyAmt);
|
||||
|
||||
return swapData;
|
||||
}
|
||||
}
|
5
contracts/mainnet/connectors/paraswap/interface.sol
Normal file
5
contracts/mainnet/connectors/paraswap/interface.sol
Normal file
|
@ -0,0 +1,5 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
interface AugustusSwapperInterface {
|
||||
function getTokenTransferProxy() external view returns (address);
|
||||
}
|
59
contracts/mainnet/connectors/paraswap/main.sol
Normal file
59
contracts/mainnet/connectors/paraswap/main.sol
Normal file
|
@ -0,0 +1,59 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
/**
|
||||
* @title Paraswap.
|
||||
* @dev DEX Aggregator.
|
||||
*/
|
||||
|
||||
import {TokenInterface} from "../../common/interfaces.sol";
|
||||
import {Stores} from "../../common/stores.sol";
|
||||
import {Helpers} from "./helpers.sol";
|
||||
|
||||
abstract contract ParaswapResolver is Helpers {
|
||||
/**
|
||||
* @dev Sell ETH/ERC20_Token using ParaSwap.
|
||||
* @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain.
|
||||
* @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param sellAddr The address of the token to sell.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param sellAmt The amount of the token to sell.
|
||||
* @param unitAmt The amount of buyAmt/sellAmt with slippage.
|
||||
* @param callData Data from paraswap 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)
|
||||
{
|
||||
Helpers.SwapData memory swapData = Helpers.SwapData({
|
||||
buyToken: TokenInterface(buyAddr),
|
||||
sellToken: TokenInterface(sellAddr),
|
||||
unitAmt: unitAmt,
|
||||
callData: callData,
|
||||
_sellAmt: sellAmt,
|
||||
_buyAmt: 0
|
||||
});
|
||||
|
||||
swapData = _swap(swapData, setId);
|
||||
|
||||
_eventName = "LogSwap(address,address,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(
|
||||
address(swapData.buyToken),
|
||||
address(swapData.sellToken),
|
||||
swapData._buyAmt,
|
||||
swapData._sellAmt,
|
||||
setId
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
contract ConnectV2ParaswapV5 is ParaswapResolver {
|
||||
string public name = "Paraswap-v5";
|
||||
}
|
12
contracts/polygon/connectors/0x/events.sol
Normal file
12
contracts/polygon/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
|
||||
);
|
||||
}
|
67
contracts/polygon/connectors/0x/helpers.sol
Normal file
67
contracts/polygon/connectors/0x/helpers.sol
Normal file
|
@ -0,0 +1,67 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import {TokenInterface} from "../../common/interfaces.sol";
|
||||
import {DSMath} from "../../common/math.sol";
|
||||
import {ZeroExData} from "./interface.sol";
|
||||
import {Basic} from "../../common/basic.sol";
|
||||
|
||||
contract Helpers is DSMath, Basic {
|
||||
/**
|
||||
* @dev 0x Address
|
||||
*/
|
||||
address internal constant zeroExAddr =
|
||||
0xDef1C0ded9bec7F1a1670819833240f027b25EfF;
|
||||
|
||||
function _swapHelper(ZeroExData memory zeroExData, uint256 maticAmt)
|
||||
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: maticAmt}(
|
||||
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 maticAmt;
|
||||
if (address(_sellAddr) == maticAddr) {
|
||||
maticAmt = zeroExData._sellAmt;
|
||||
} else {
|
||||
approve(
|
||||
TokenInterface(_sellAddr),
|
||||
zeroExAddr,
|
||||
zeroExData._sellAmt
|
||||
);
|
||||
}
|
||||
|
||||
zeroExData._buyAmt = _swapHelper(zeroExData, maticAmt);
|
||||
setUint(setId, zeroExData._buyAmt);
|
||||
|
||||
return zeroExData;
|
||||
}
|
||||
}
|
12
contracts/polygon/connectors/0x/interface.sol
Normal file
12
contracts/polygon/connectors/0x/interface.sol
Normal file
|
@ -0,0 +1,12 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import {TokenInterface} from "../../common/interfaces.sol";
|
||||
|
||||
struct ZeroExData {
|
||||
TokenInterface sellToken;
|
||||
TokenInterface buyToken;
|
||||
uint256 _sellAmt;
|
||||
uint256 _buyAmt;
|
||||
uint256 unitAmt;
|
||||
bytes callData;
|
||||
}
|
64
contracts/polygon/connectors/0x/main.sol
Normal file
64
contracts/polygon/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} 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 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.
|
||||
* @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 ConnectV2ZeroExPolygon is ZeroEx {
|
||||
string public name = "0x-V4";
|
||||
}
|
21
contracts/polygon/connectors/basic-ERC1155/events.sol
Normal file
21
contracts/polygon/connectors/basic-ERC1155/events.sol
Normal file
|
@ -0,0 +1,21 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.7.0;
|
||||
|
||||
contract Events {
|
||||
event LogDepositERC1155(
|
||||
address indexed erc1155,
|
||||
address from,
|
||||
uint256 tokenId,
|
||||
uint256 amount,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
event LogWithdrawERC1155(
|
||||
address indexed erc1155,
|
||||
uint256 tokenId,
|
||||
address indexed to,
|
||||
uint256 amount,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
}
|
94
contracts/polygon/connectors/basic-ERC1155/main.sol
Normal file
94
contracts/polygon/connectors/basic-ERC1155/main.sol
Normal file
|
@ -0,0 +1,94 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.7.0;
|
||||
|
||||
/**
|
||||
* @title Basic.
|
||||
* @dev Deposit & Withdraw from ERC1155 DSA.
|
||||
*/
|
||||
import {IERC1155} from "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
|
||||
|
||||
import {DSMath} from "../../common/math.sol";
|
||||
import {Basic} from "../../common/basic.sol";
|
||||
import {Events} from "./events.sol";
|
||||
|
||||
abstract contract BasicResolver is Events, DSMath, Basic {
|
||||
/**
|
||||
* @dev Deposit Assets To Smart Account.
|
||||
* @notice Deposit a ERC1155 token to DSA
|
||||
* @param token Address of token.
|
||||
* @param tokenId ID of token.
|
||||
* @param amount Amount to deposit.
|
||||
* @param getId ID to retrieve amount.
|
||||
* @param setId ID stores the amount.
|
||||
*/
|
||||
function depositERC1155(
|
||||
address token,
|
||||
uint256 tokenId,
|
||||
uint256 amount,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
)
|
||||
public
|
||||
payable
|
||||
returns (string memory _eventName, bytes memory _eventParam)
|
||||
{
|
||||
uint256 _amount = getUint(getId, amount);
|
||||
|
||||
IERC1155 tokenContract = IERC1155(token);
|
||||
tokenContract.safeTransferFrom(
|
||||
msg.sender,
|
||||
address(this),
|
||||
tokenId,
|
||||
_amount,
|
||||
""
|
||||
);
|
||||
|
||||
setUint(setId, _amount);
|
||||
|
||||
_eventName = "LogDepositERC1155(address,address,uint256,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(
|
||||
token,
|
||||
msg.sender,
|
||||
tokenId,
|
||||
_amount,
|
||||
getId,
|
||||
setId
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Withdraw Assets To Smart Account.
|
||||
* @notice Withdraw a ERC1155 token from DSA
|
||||
* @param token Address of the token.
|
||||
* @param tokenId ID of token.
|
||||
* @param to The address to receive the token upon withdrawal
|
||||
* @param amount Amount to withdraw.
|
||||
* @param getId ID to retrieve amount.
|
||||
* @param setId ID stores the amount.
|
||||
*/
|
||||
function withdrawERC1155(
|
||||
address token,
|
||||
uint256 tokenId,
|
||||
address payable to,
|
||||
uint256 amount,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
)
|
||||
public
|
||||
payable
|
||||
returns (string memory _eventName, bytes memory _eventParam)
|
||||
{
|
||||
uint256 _amount = getUint(getId, amount);
|
||||
IERC1155 tokenContract = IERC1155(token);
|
||||
tokenContract.safeTransferFrom(address(this), to, tokenId, _amount, "");
|
||||
|
||||
setUint(setId, _amount);
|
||||
|
||||
_eventName = "LogWithdrawERC1155(address,uint256,address,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(token, tokenId, to, _amount, getId, setId);
|
||||
}
|
||||
}
|
||||
|
||||
contract ConnectV2BasicERC1155Polygon is BasicResolver {
|
||||
string public constant name = "BASIC-ERC1155-v1.0";
|
||||
}
|
19
contracts/polygon/connectors/basic-ERC721/events.sol
Normal file
19
contracts/polygon/connectors/basic-ERC721/events.sol
Normal file
|
@ -0,0 +1,19 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.7.0;
|
||||
|
||||
contract Events {
|
||||
event LogDepositERC721(
|
||||
address indexed erc721,
|
||||
address from,
|
||||
uint256 tokenId,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
event LogWithdrawERC721(
|
||||
address indexed erc721,
|
||||
uint256 tokenId,
|
||||
address indexed to,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
}
|
77
contracts/polygon/connectors/basic-ERC721/main.sol
Normal file
77
contracts/polygon/connectors/basic-ERC721/main.sol
Normal file
|
@ -0,0 +1,77 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.7.0;
|
||||
|
||||
/**
|
||||
* @title Basic.
|
||||
* @dev Deposit & Withdraw ERC721 from DSA.
|
||||
*/
|
||||
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
|
||||
|
||||
import {DSMath} from "../../common/math.sol";
|
||||
import {Basic} from "../../common/basic.sol";
|
||||
import {Events} from "./events.sol";
|
||||
|
||||
abstract contract BasicResolver is Events, DSMath, Basic {
|
||||
/**
|
||||
* @dev Deposit Assets To Smart Account.
|
||||
* @notice Deposit a ERC721 token to DSA
|
||||
* @param token Address of token.
|
||||
* @param tokenId ID of token.
|
||||
* @param getId ID to retrieve tokenId.
|
||||
* @param setId ID stores the tokenId.
|
||||
*/
|
||||
function depositERC721(
|
||||
address token,
|
||||
uint256 tokenId,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
)
|
||||
public
|
||||
payable
|
||||
returns (string memory _eventName, bytes memory _eventParam)
|
||||
{
|
||||
uint256 _tokenId = getUint(getId, tokenId);
|
||||
|
||||
IERC721 tokenContract = IERC721(token);
|
||||
tokenContract.safeTransferFrom(msg.sender, address(this), _tokenId);
|
||||
|
||||
setUint(setId, _tokenId);
|
||||
|
||||
_eventName = "LogDepositERC721(address,address,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(token, msg.sender, _tokenId, getId, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Withdraw Assets To Smart Account.
|
||||
* @notice Withdraw a ERC721 token from DSA
|
||||
* @param token Address of the token.
|
||||
* @param tokenId ID of token.
|
||||
* @param to The address to receive the token upon withdrawal
|
||||
* @param getId ID to retrieve tokenId.
|
||||
* @param setId ID stores the tokenId.
|
||||
*/
|
||||
function withdrawERC721(
|
||||
address token,
|
||||
uint256 tokenId,
|
||||
address payable to,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
)
|
||||
public
|
||||
payable
|
||||
returns (string memory _eventName, bytes memory _eventParam)
|
||||
{
|
||||
uint256 _tokenId = getUint(getId, tokenId);
|
||||
IERC721 tokenContract = IERC721(token);
|
||||
tokenContract.safeTransferFrom(address(this), to, _tokenId);
|
||||
|
||||
setUint(setId, _tokenId);
|
||||
|
||||
_eventName = "LogWithdrawERC721(address,uint256,address,uint256,uint256)";
|
||||
_eventParam = abi.encode(token, _tokenId, to, getId, setId);
|
||||
}
|
||||
}
|
||||
|
||||
contract ConnectV2BasicERC721Polygon is BasicResolver {
|
||||
string public constant name = "BASIC-ERC721-v1.0";
|
||||
}
|
|
@ -105,6 +105,7 @@ const config: HardhatUserConfig = {
|
|||
forking: {
|
||||
url: String(getNetworkUrl(String(process.env.networkType))),
|
||||
},
|
||||
gasPrice: 25000000000,
|
||||
},
|
||||
mainnet: createConfig("mainnet"),
|
||||
polygon: createConfig("polygon"),
|
||||
|
|
|
@ -23,6 +23,7 @@ async function main() {
|
|||
"Uniswap-V3-Staker-v1.1" : "ConnectV2UniswapV3StakerPolygon",
|
||||
"Paraswap-v5" : "ConnectV2ParaswapV5Polygon"
|
||||
"1INCH-V4" : "ConnectV2OneInchV4Polygon",
|
||||
"ZEROEX-A": "ConnectV2ZeroExAvalanche",
|
||||
};
|
||||
|
||||
const addressMapping: Record<string, string> = {};
|
||||
|
|
|
@ -10,13 +10,12 @@ export const deployConnector = async (connectorName: string) => {
|
|||
try {
|
||||
await hre.run("verify:verify", {
|
||||
address: connector.address,
|
||||
constructorArguments: []
|
||||
}
|
||||
)
|
||||
constructorArguments: [],
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(`Failed to verify: ${connectorName}@${connector.address}`)
|
||||
console.log(error)
|
||||
console.log()
|
||||
console.log(`Failed to verify: ${connectorName}@${connector.address}`);
|
||||
console.log(error);
|
||||
console.log();
|
||||
}
|
||||
return connector.address;
|
||||
};
|
||||
|
|
|
@ -5,7 +5,7 @@ export const addresses: Record<string, any> = {
|
|||
// "INSTAPOOL-A": "0x8f1e38c53af7bD2b2bE01b9580911b7Cca504F1b",
|
||||
},
|
||||
core: {
|
||||
connectorsV2: "0x67fCE99Dd6d8d659eea2a1ac1b8881c57eb6592B",
|
||||
instaIndex: "0x1eE00C305C51Ff3bE60162456A9B533C07cD9288",
|
||||
connectorsV2: "0x127d8cD0E2b2E0366D522DeA53A787bfE9002C14",
|
||||
instaIndex: "0x6CE3e607C808b4f4C26B7F6aDAeB619e49CAbb25",
|
||||
},
|
||||
};
|
||||
|
|
159
test/avalanche/0x/zeroEx.test.ts
Normal file
159
test/avalanche/0x/zeroEx.test.ts
Normal file
|
@ -0,0 +1,159 @@
|
|||
import hre from "hardhat";
|
||||
import axios from "axios";
|
||||
import { expect } from "chai";
|
||||
const { ethers } = hre; //check
|
||||
import { BigNumber } from "bignumber.js";
|
||||
import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector";
|
||||
import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2";
|
||||
import { encodeSpells } from "../../../scripts/tests/encodeSpells";
|
||||
import { getMasterSigner } from "../../../scripts/tests/getMasterSigner";
|
||||
import { addresses } from "../../../scripts/tests/avalanche/addresses";
|
||||
import { abis } from "../../../scripts/constant/abis";
|
||||
import {ConnectV2ZeroExAvalanche__factory } from "../../../typechain";
|
||||
import er20abi from "../../../scripts/constant/abi/basics/erc20.json";
|
||||
import type { Signer, Contract } from "ethers";
|
||||
|
||||
describe("ZeroEx", function() {
|
||||
const connectorName = "zeroEx-test";
|
||||
|
||||
let dsaWallet0: Contract;
|
||||
let wallet0: Signer, wallet1: Signer;
|
||||
let masterSigner: Signer;
|
||||
let instaConnectorsV2: Contract;
|
||||
let connector: Contract;
|
||||
|
||||
before(async () => {
|
||||
await hre.network.provider.request({
|
||||
method: "hardhat_reset",
|
||||
params: [
|
||||
{
|
||||
forking: {
|
||||
// @ts-ignore
|
||||
jsonRpcUrl: hre.config.networks.hardhat.forking.url,
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
[wallet0, wallet1] = await ethers.getSigners();
|
||||
|
||||
masterSigner = await getMasterSigner();
|
||||
instaConnectorsV2 = await ethers.getContractAt(
|
||||
abis.core.connectorsV2,
|
||||
addresses.core.connectorsV2
|
||||
);
|
||||
connector = await deployAndEnableConnector({
|
||||
connectorName,
|
||||
contractArtifact: ConnectV2ZeroExAvalanche__factory,
|
||||
signer: masterSigner,
|
||||
connectors: instaConnectorsV2,
|
||||
});
|
||||
console.log("Connector address", connector.address);
|
||||
});
|
||||
|
||||
it("Should have contracts deployed.", async function() {
|
||||
expect(!!instaConnectorsV2.address).to.be.true;
|
||||
expect(!!connector.address).to.be.true;
|
||||
expect(!!(await masterSigner.getAddress())).to.be.true;
|
||||
});
|
||||
|
||||
describe("DSA wallet setup", function() {
|
||||
it("Should build DSA v2", async function() {
|
||||
dsaWallet0 = await buildDSAv2(await wallet0.getAddress());
|
||||
expect(!!dsaWallet0.address).to.be.true;
|
||||
});
|
||||
|
||||
it("Deposit matic into DSA wallet", async function() {
|
||||
await wallet0.sendTransaction({
|
||||
to: dsaWallet0.address,
|
||||
value: ethers.utils.parseEther("10"),
|
||||
});
|
||||
|
||||
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(
|
||||
ethers.utils.parseEther("10")
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Main", function() {
|
||||
it("should swap the tokens", async function() {
|
||||
let buyTokenAmount: any;
|
||||
async function getArg() {
|
||||
// const slippage = 0.5;
|
||||
/* matic -> dai */
|
||||
const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // matic, decimals 18
|
||||
const sellTokenDecimals = 18;
|
||||
const buyTokenAddress = "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063"; // dai, decimals 18
|
||||
const buyTokenDecimals = 18;
|
||||
const amount = 1;
|
||||
|
||||
const srcAmount = new BigNumber(amount)
|
||||
.times(new BigNumber(10).pow(sellTokenDecimals))
|
||||
.toFixed(0);
|
||||
|
||||
let url = `https://avalanche.api.0x.org/swap/v1/quote`;
|
||||
|
||||
const params = {
|
||||
buyToken: "DAI",
|
||||
sellToken: "AVAX",
|
||||
sellAmount: "1000000000000000000", // Always denominated in wei
|
||||
};
|
||||
const response = await axios
|
||||
.get(url, { params: params })
|
||||
.then((data: any) => data);
|
||||
|
||||
console.log(response);
|
||||
buyTokenAmount = response.data.buyAmount;
|
||||
const calldata = response.data.data;
|
||||
|
||||
let caculateUnitAmt = () => {
|
||||
const buyTokenAmountRes = new BigNumber(buyTokenAmount)
|
||||
.dividedBy(new BigNumber(10).pow(buyTokenDecimals))
|
||||
.toFixed(8);
|
||||
|
||||
let unitAmt: any = new BigNumber(buyTokenAmountRes).dividedBy(
|
||||
new BigNumber(amount)
|
||||
);
|
||||
|
||||
unitAmt = unitAmt.multipliedBy((100 - 0.3) / 100);
|
||||
unitAmt = unitAmt.multipliedBy(1e18).toFixed(0);
|
||||
return unitAmt;
|
||||
};
|
||||
let unitAmt = caculateUnitAmt();
|
||||
|
||||
return [
|
||||
buyTokenAddress,
|
||||
sellTokenAddress,
|
||||
srcAmount,
|
||||
unitAmt,
|
||||
calldata,
|
||||
0,
|
||||
];
|
||||
}
|
||||
|
||||
let arg = await getArg();
|
||||
const spells = [
|
||||
{
|
||||
connector: connectorName,
|
||||
method: "swap",
|
||||
args: arg,
|
||||
},
|
||||
];
|
||||
const tx = await dsaWallet0
|
||||
.connect(wallet0)
|
||||
.cast(...encodeSpells(spells), await wallet1.getAddress());
|
||||
const receipt = await tx.wait();
|
||||
|
||||
const daiToken = await ethers.getContractAt(
|
||||
er20abi,
|
||||
"0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063" // dai address
|
||||
);
|
||||
|
||||
expect(await daiToken.balanceOf(dsaWallet0.address)).to.be.gte(
|
||||
buyTokenAmount
|
||||
);
|
||||
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(
|
||||
ethers.utils.parseEther("9")
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
161
test/mainnet/0x/zeroEx.test.ts
Normal file
161
test/mainnet/0x/zeroEx.test.ts
Normal file
|
@ -0,0 +1,161 @@
|
|||
import hre from "hardhat";
|
||||
import axios from "axios";
|
||||
import { expect } from "chai";
|
||||
const { ethers } = hre; //check
|
||||
import { BigNumber } from "bignumber.js";
|
||||
import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector";
|
||||
import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2";
|
||||
import { encodeSpells } from "../../../scripts/tests/encodeSpells";
|
||||
import { getMasterSigner } from "../../../scripts/tests/getMasterSigner";
|
||||
import { addresses } from "../../../scripts/tests/mainnet/addresses";
|
||||
import { abis } from "../../../scripts/constant/abis";
|
||||
import { ConnectV2ZeroEx, ConnectV2ZeroEx__factory } from "../../../typechain";
|
||||
import er20abi from "../../../scripts/constant/abi/basics/erc20.json";
|
||||
import type { Signer, Contract } from "ethers";
|
||||
|
||||
describe("ZeroEx", function() {
|
||||
const connectorName = "zeroEx-test";
|
||||
|
||||
let dsaWallet0: Contract;
|
||||
let wallet0: Signer, wallet1: Signer;
|
||||
let masterSigner: Signer;
|
||||
let instaConnectorsV2: Contract;
|
||||
let connector: Contract;
|
||||
|
||||
before(async () => {
|
||||
await hre.network.provider.request({
|
||||
method: "hardhat_reset",
|
||||
params: [
|
||||
{
|
||||
forking: {
|
||||
// @ts-ignore
|
||||
jsonRpcUrl: hre.config.networks.hardhat.forking.url,
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
[wallet0, wallet1] = await ethers.getSigners();
|
||||
|
||||
masterSigner = await getMasterSigner();
|
||||
instaConnectorsV2 = await ethers.getContractAt(
|
||||
abis.core.connectorsV2,
|
||||
addresses.core.connectorsV2
|
||||
);
|
||||
connector = await deployAndEnableConnector({
|
||||
connectorName,
|
||||
contractArtifact: ConnectV2ZeroEx__factory,
|
||||
signer: masterSigner,
|
||||
connectors: instaConnectorsV2,
|
||||
});
|
||||
console.log("Connector address", connector.address);
|
||||
});
|
||||
|
||||
it("Should have contracts deployed.", async function() {
|
||||
expect(!!instaConnectorsV2.address).to.be.true;
|
||||
expect(!!connector.address).to.be.true;
|
||||
expect(!!(await masterSigner.getAddress())).to.be.true;
|
||||
});
|
||||
|
||||
describe("DSA wallet setup", function() {
|
||||
it("Should build DSA v2", async function() {
|
||||
dsaWallet0 = await buildDSAv2(await wallet0.getAddress());
|
||||
expect(!!dsaWallet0.address).to.be.true;
|
||||
});
|
||||
|
||||
it("Deposit ETH into DSA wallet", async function() {
|
||||
await wallet0.sendTransaction({
|
||||
to: dsaWallet0.address,
|
||||
value: ethers.utils.parseEther("10"),
|
||||
});
|
||||
// console.log(dsaWallet0.address);
|
||||
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(
|
||||
ethers.utils.parseEther("10")
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Main", function() {
|
||||
it("should swap the tokens", async function() {
|
||||
let buyTokenAmount: any;
|
||||
async function getArg() {
|
||||
// const slippage = 0.5;
|
||||
/* Eth -> dai */
|
||||
const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // eth, decimals 18
|
||||
const sellTokenDecimals = 18;
|
||||
const buyTokenAddress = "0x6b175474e89094c44da98b954eedeac495271d0f"; // dai, decimals 18
|
||||
const buyTokenDecimals = 18;
|
||||
const amount = 1;
|
||||
|
||||
const srcAmount = new BigNumber(amount)
|
||||
.times(new BigNumber(10).pow(sellTokenDecimals))
|
||||
.toFixed(0);
|
||||
|
||||
const fromAddress = dsaWallet0.address;
|
||||
|
||||
let url = `https://api.0x.org/swap/v1/quote`;
|
||||
|
||||
const params = {
|
||||
buyToken: "DAI",
|
||||
sellToken: "ETH",
|
||||
sellAmount: "1000000000000000000", // Always denominated in wei
|
||||
};
|
||||
|
||||
const response = await axios
|
||||
.get(url, { params: params })
|
||||
.then((data: any) => data);
|
||||
|
||||
buyTokenAmount = response.data.buyAmount;
|
||||
const calldata = response.data.data;
|
||||
|
||||
let caculateUnitAmt = () => {
|
||||
const buyTokenAmountRes = new BigNumber(buyTokenAmount)
|
||||
.dividedBy(new BigNumber(10).pow(buyTokenDecimals))
|
||||
.toFixed(8);
|
||||
|
||||
let unitAmt: any = new BigNumber(buyTokenAmountRes).dividedBy(
|
||||
new BigNumber(amount)
|
||||
);
|
||||
|
||||
unitAmt = unitAmt.multipliedBy((100 - 0.3) / 100);
|
||||
unitAmt = unitAmt.multipliedBy(1e18).toFixed(0);
|
||||
return unitAmt;
|
||||
};
|
||||
let unitAmt = caculateUnitAmt();
|
||||
|
||||
return [
|
||||
buyTokenAddress,
|
||||
sellTokenAddress,
|
||||
srcAmount,
|
||||
unitAmt,
|
||||
calldata,
|
||||
0,
|
||||
];
|
||||
}
|
||||
|
||||
let arg = await getArg();
|
||||
const spells = [
|
||||
{
|
||||
connector: connectorName,
|
||||
method: "swap",
|
||||
args: arg,
|
||||
},
|
||||
];
|
||||
const tx = await dsaWallet0
|
||||
.connect(wallet0)
|
||||
.cast(...encodeSpells(spells), await wallet1.getAddress());
|
||||
const receipt = await tx.wait();
|
||||
|
||||
const daiToken = await ethers.getContractAt(
|
||||
er20abi,
|
||||
"0x6b175474e89094c44da98b954eedeac495271d0f" // dai address
|
||||
);
|
||||
|
||||
expect(await daiToken.balanceOf(dsaWallet0.address)).to.be.gte(
|
||||
buyTokenAmount
|
||||
);
|
||||
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(
|
||||
ethers.utils.parseEther("9")
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
177
test/mainnet/paraswap/paraswap.test.ts
Normal file
177
test/mainnet/paraswap/paraswap.test.ts
Normal file
|
@ -0,0 +1,177 @@
|
|||
import hre from "hardhat";
|
||||
import { expect } from "chai";
|
||||
import { abis } from "../../../scripts/constant/abis";
|
||||
import { addresses } from "../../../scripts/tests/mainnet/addresses";
|
||||
import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector";
|
||||
import { getMasterSigner } from "../../../scripts/tests/getMasterSigner";
|
||||
import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2";
|
||||
import {
|
||||
ConnectV2ParaswapV5__factory,
|
||||
} from "../../../typechain"
|
||||
import { parseEther } from "@ethersproject/units";
|
||||
import { encodeSpells } from "../../../scripts/tests/encodeSpells";
|
||||
import { tokens } from "../../../scripts/tests/mainnet/tokens";
|
||||
import { constants } from "../../../scripts/constant/constant";
|
||||
import { addLiquidity } from "../../../scripts/tests/addLiquidity";
|
||||
import BigNumber from "bignumber.js";
|
||||
import axios from "axios";
|
||||
const { web3, deployments, waffle, ethers } = hre;
|
||||
const { provider, deployContract } = waffle;
|
||||
describe("Paraswap", function() {
|
||||
const connectorName = "paraswap-test";
|
||||
let dsaWallet0: any;
|
||||
let masterSigner: any;
|
||||
let instaConnectorsV2: any;
|
||||
let connector: any;
|
||||
const wallets = provider.getWallets();
|
||||
const [wallet0, wallet1, wallet2, wallet3] = wallets;
|
||||
before(async () => {
|
||||
await hre.network.provider.request({
|
||||
method: "hardhat_reset",
|
||||
params: [
|
||||
{
|
||||
forking: {
|
||||
// @ts-ignore
|
||||
jsonRpcUrl: hre.config.networks.hardhat.forking.url,
|
||||
// blockNumber: 13300000,
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
masterSigner = await getMasterSigner();
|
||||
const erc20 = abis.basic.erc20;
|
||||
instaConnectorsV2 = await ethers.getContractAt(
|
||||
abis.core.connectorsV2,
|
||||
addresses.core.connectorsV2
|
||||
);
|
||||
|
||||
connector = await deployAndEnableConnector({
|
||||
connectorName,
|
||||
contractArtifact: ConnectV2ParaswapV5__factory,
|
||||
signer: masterSigner,
|
||||
connectors: instaConnectorsV2,
|
||||
});
|
||||
// console.log("Connector address", connector.address);
|
||||
});
|
||||
it("Should have contracts deployed.", async function() {
|
||||
expect(!!instaConnectorsV2.address).to.be.true;
|
||||
expect(!!connector.address).to.be.true;
|
||||
expect(!!masterSigner.address).to.be.true;
|
||||
});
|
||||
describe("DSA wallet setup", function() {
|
||||
it("Should build DSA v2", async function() {
|
||||
dsaWallet0 = await buildDSAv2(wallet0.address);
|
||||
expect(!!dsaWallet0.address).to.be.true;
|
||||
});
|
||||
it("Deposit ETH into DSA wallet", async function() {
|
||||
await wallet0.sendTransaction({
|
||||
to: dsaWallet0.address,
|
||||
value: ethers.utils.parseEther("10"),
|
||||
});
|
||||
|
||||
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(
|
||||
ethers.utils.parseEther("10")
|
||||
);
|
||||
});
|
||||
});
|
||||
describe("Main", function() {
|
||||
it("should swap successfully", async function() {
|
||||
async function getArg() {
|
||||
const slippage = 1;
|
||||
/* eth -> USDT */
|
||||
const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // eth, decimals 18
|
||||
const sellTokenDecimals = 18;
|
||||
const buyTokenAddress = "0xdac17f958d2ee523a2206206994597c13d831ec7"; // USDT, decimals 6
|
||||
const buyTokenDecimals = 6;
|
||||
const amount = 2;
|
||||
const srcAmount = new BigNumber(amount)
|
||||
.times(new BigNumber(10).pow(sellTokenDecimals))
|
||||
.toFixed(0);
|
||||
const fromAddress = dsaWallet0.address;
|
||||
let url = `https://apiv5.paraswap.io/prices/`;
|
||||
let params = {
|
||||
srcToken: sellTokenAddress,
|
||||
destToken: buyTokenAddress,
|
||||
srcDecimals: sellTokenDecimals,
|
||||
destDecimals: buyTokenDecimals,
|
||||
amount: srcAmount,
|
||||
side: "SELL",
|
||||
network: 1,
|
||||
};
|
||||
|
||||
const priceRoute = await axios
|
||||
.get(url, { params: params })
|
||||
.then((data) => data.data.priceRoute);
|
||||
|
||||
let buyTokenAmount = priceRoute.destAmount;
|
||||
let minAmount = new BigNumber(priceRoute.destAmount)
|
||||
.times(1 - slippage / 100)
|
||||
.toFixed(0);
|
||||
|
||||
let txConfig = {
|
||||
priceRoute: priceRoute,
|
||||
srcToken: sellTokenAddress,
|
||||
destToken: buyTokenAddress,
|
||||
srcDecimals: sellTokenDecimals,
|
||||
destDecimals: buyTokenDecimals,
|
||||
srcAmount: srcAmount,
|
||||
destAmount: minAmount,
|
||||
userAddress: fromAddress,
|
||||
};
|
||||
let url2 = "https://apiv5.paraswap.io/transactions/1?ignoreChecks=true";
|
||||
const calldata = await axios
|
||||
.post(url2, txConfig)
|
||||
.then((data) => data.data.data);
|
||||
|
||||
function caculateUnitAmt(
|
||||
buyAmount: any,
|
||||
sellAmount: any,
|
||||
buyDecimal: any,
|
||||
sellDecimal: any,
|
||||
maxSlippage: any
|
||||
) {
|
||||
let unitAmt: any;
|
||||
unitAmt = new BigNumber(buyAmount)
|
||||
.dividedBy(10 ** buyDecimal)
|
||||
.dividedBy(new BigNumber(sellAmount).dividedBy(10 ** sellDecimal));
|
||||
unitAmt = unitAmt.multipliedBy((100 - maxSlippage) / 100);
|
||||
unitAmt = unitAmt.multipliedBy(1e18).toFixed(0);
|
||||
return unitAmt;
|
||||
}
|
||||
|
||||
let unitAmt = caculateUnitAmt(
|
||||
buyTokenAmount,
|
||||
srcAmount,
|
||||
buyTokenDecimals,
|
||||
sellTokenDecimals,
|
||||
1
|
||||
);
|
||||
|
||||
return [
|
||||
buyTokenAddress,
|
||||
sellTokenAddress,
|
||||
srcAmount,
|
||||
unitAmt,
|
||||
calldata,
|
||||
0,
|
||||
];
|
||||
}
|
||||
let arg = await getArg();
|
||||
const spells = [
|
||||
{
|
||||
connector: connectorName,
|
||||
method: "swap",
|
||||
args: arg,
|
||||
},
|
||||
];
|
||||
const tx = await dsaWallet0
|
||||
.connect(wallet0)
|
||||
.cast(...encodeSpells(spells), wallet1.address);
|
||||
const receipt = await tx.wait();
|
||||
|
||||
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(
|
||||
ethers.utils.parseEther("8")
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
159
test/polygon/0x/zeroEx.test.ts
Normal file
159
test/polygon/0x/zeroEx.test.ts
Normal file
|
@ -0,0 +1,159 @@
|
|||
import hre from "hardhat";
|
||||
import axios from "axios";
|
||||
import { expect } from "chai";
|
||||
const { ethers } = hre; //check
|
||||
import { BigNumber } from "bignumber.js";
|
||||
import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector";
|
||||
import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2";
|
||||
import { encodeSpells } from "../../../scripts/tests/encodeSpells";
|
||||
import { getMasterSigner } from "../../../scripts/tests/getMasterSigner";
|
||||
import { addresses } from "../../../scripts/tests/polygon/addresses";
|
||||
import { abis } from "../../../scripts/constant/abis";
|
||||
import {ConnectV2ZeroExPolygon__factory } from "../../../typechain";
|
||||
import er20abi from "../../../scripts/constant/abi/basics/erc20.json";
|
||||
import type { Signer, Contract } from "ethers";
|
||||
|
||||
describe("ZeroEx", function() {
|
||||
const connectorName = "zeroEx-test";
|
||||
|
||||
let dsaWallet0: Contract;
|
||||
let wallet0: Signer, wallet1: Signer;
|
||||
let masterSigner: Signer;
|
||||
let instaConnectorsV2: Contract;
|
||||
let connector: Contract;
|
||||
|
||||
before(async () => {
|
||||
await hre.network.provider.request({
|
||||
method: "hardhat_reset",
|
||||
params: [
|
||||
{
|
||||
forking: {
|
||||
// @ts-ignore
|
||||
jsonRpcUrl: hre.config.networks.hardhat.forking.url,
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
[wallet0, wallet1] = await ethers.getSigners();
|
||||
|
||||
masterSigner = await getMasterSigner();
|
||||
instaConnectorsV2 = await ethers.getContractAt(
|
||||
abis.core.connectorsV2,
|
||||
addresses.core.connectorsV2
|
||||
);
|
||||
connector = await deployAndEnableConnector({
|
||||
connectorName,
|
||||
contractArtifact: ConnectV2ZeroExPolygon__factory,
|
||||
signer: masterSigner,
|
||||
connectors: instaConnectorsV2,
|
||||
});
|
||||
console.log("Connector address", connector.address);
|
||||
});
|
||||
|
||||
it("Should have contracts deployed.", async function() {
|
||||
expect(!!instaConnectorsV2.address).to.be.true;
|
||||
expect(!!connector.address).to.be.true;
|
||||
expect(!!(await masterSigner.getAddress())).to.be.true;
|
||||
});
|
||||
|
||||
describe("DSA wallet setup", function() {
|
||||
it("Should build DSA v2", async function() {
|
||||
dsaWallet0 = await buildDSAv2(await wallet0.getAddress());
|
||||
expect(!!dsaWallet0.address).to.be.true;
|
||||
});
|
||||
|
||||
it("Deposit matic into DSA wallet", async function() {
|
||||
await wallet0.sendTransaction({
|
||||
to: dsaWallet0.address,
|
||||
value: ethers.utils.parseEther("10"),
|
||||
});
|
||||
|
||||
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(
|
||||
ethers.utils.parseEther("10")
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Main", function() {
|
||||
it("should swap the tokens", async function() {
|
||||
let buyTokenAmount: any;
|
||||
async function getArg() {
|
||||
// const slippage = 0.5;
|
||||
/* matic -> dai */
|
||||
const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // matic, decimals 18
|
||||
const sellTokenDecimals = 18;
|
||||
const buyTokenAddress = "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063"; // dai, decimals 18
|
||||
const buyTokenDecimals = 18;
|
||||
const amount = 1;
|
||||
|
||||
const srcAmount = new BigNumber(amount)
|
||||
.times(new BigNumber(10).pow(sellTokenDecimals))
|
||||
.toFixed(0);
|
||||
|
||||
let url = `https://polygon.api.0x.org/swap/v1/quote`;
|
||||
|
||||
const params = {
|
||||
buyToken: "DAI",
|
||||
sellToken: "MATIC",
|
||||
sellAmount: "1000000000000000000", // Always denominated in wei
|
||||
};
|
||||
|
||||
const response = await axios
|
||||
.get(url, { params: params })
|
||||
.then((data: any) => data);
|
||||
|
||||
buyTokenAmount = response.data.buyAmount;
|
||||
const calldata = response.data.data;
|
||||
|
||||
let caculateUnitAmt = () => {
|
||||
const buyTokenAmountRes = new BigNumber(buyTokenAmount)
|
||||
.dividedBy(new BigNumber(10).pow(buyTokenDecimals))
|
||||
.toFixed(8);
|
||||
|
||||
let unitAmt: any = new BigNumber(buyTokenAmountRes).dividedBy(
|
||||
new BigNumber(amount)
|
||||
);
|
||||
|
||||
unitAmt = unitAmt.multipliedBy((100 - 0.3) / 100);
|
||||
unitAmt = unitAmt.multipliedBy(1e18).toFixed(0);
|
||||
return unitAmt;
|
||||
};
|
||||
let unitAmt = caculateUnitAmt();
|
||||
|
||||
return [
|
||||
buyTokenAddress,
|
||||
sellTokenAddress,
|
||||
srcAmount,
|
||||
unitAmt,
|
||||
calldata,
|
||||
0,
|
||||
];
|
||||
}
|
||||
|
||||
let arg = await getArg();
|
||||
const spells = [
|
||||
{
|
||||
connector: connectorName,
|
||||
method: "swap",
|
||||
args: arg,
|
||||
},
|
||||
];
|
||||
const tx = await dsaWallet0
|
||||
.connect(wallet0)
|
||||
.cast(...encodeSpells(spells), await wallet1.getAddress());
|
||||
const receipt = await tx.wait();
|
||||
|
||||
const daiToken = await ethers.getContractAt(
|
||||
er20abi,
|
||||
"0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063" // dai address
|
||||
);
|
||||
|
||||
expect(await daiToken.balanceOf(dsaWallet0.address)).to.be.gte(
|
||||
buyTokenAmount
|
||||
);
|
||||
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(
|
||||
ethers.utils.parseEther("9")
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user