From f437e196a2c45410925b88799384f3f8277dbc1e Mon Sep 17 00:00:00 2001 From: bhavik Date: Mon, 6 Dec 2021 17:47:55 +0530 Subject: [PATCH 01/54] added 0x on mainnet --- contracts/mainnet/connectors/0x/events.sol | 12 ++ contracts/mainnet/connectors/0x/helpers.sol | 13 ++ contracts/mainnet/connectors/0x/interface.sol | 54 ++++++++ contracts/mainnet/connectors/0x/main.sol | 119 ++++++++++++++++++ 4 files changed, 198 insertions(+) create mode 100644 contracts/mainnet/connectors/0x/events.sol create mode 100644 contracts/mainnet/connectors/0x/helpers.sol create mode 100644 contracts/mainnet/connectors/0x/interface.sol create mode 100644 contracts/mainnet/connectors/0x/main.sol diff --git a/contracts/mainnet/connectors/0x/events.sol b/contracts/mainnet/connectors/0x/events.sol new file mode 100644 index 00000000..66f9943a --- /dev/null +++ b/contracts/mainnet/connectors/0x/events.sol @@ -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 + ); +} diff --git a/contracts/mainnet/connectors/0x/helpers.sol b/contracts/mainnet/connectors/0x/helpers.sol new file mode 100644 index 00000000..7919436d --- /dev/null +++ b/contracts/mainnet/connectors/0x/helpers.sol @@ -0,0 +1,13 @@ +pragma solidity ^0.7.0; + +import {TokenInterface} from "../../common/interfaces.sol"; +import {DSMath} from "../../common/math.sol"; +import {Basic} from "../../common/basic.sol"; + +abstract contract Helpers is DSMath, Basic { + /** + * @dev 0x Address + */ + address internal constant zeroExAddr = + 0xDef1C0ded9bec7F1a1670819833240f027b25EfF; +} diff --git a/contracts/mainnet/connectors/0x/interface.sol b/contracts/mainnet/connectors/0x/interface.sol new file mode 100644 index 00000000..a261afce --- /dev/null +++ b/contracts/mainnet/connectors/0x/interface.sol @@ -0,0 +1,54 @@ +pragma solidity ^0.7.0; + +import {TokenInterface} from "../../common/interfaces.sol"; + +interface zeroExInterface { + // function transformERC20( + // IERC20TokenV06 inputToken, + // IERC20TokenV06 outputToken, + // uint256 inputTokenAmount, + // uint256 minOutputTokenAmount, + // Transformation[] calldata transformations + // ) external payable returns (uint256 outputTokenAmount); + + 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; +} diff --git a/contracts/mainnet/connectors/0x/main.sol b/contracts/mainnet/connectors/0x/main.sol new file mode 100644 index 00000000..f55c496b --- /dev/null +++ b/contracts/mainnet/connectors/0x/main.sol @@ -0,0 +1,119 @@ +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"; + +contract zeroExHelper is Helpers { + function zeroExSwap(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 { + address transformWallet = address( + zeroExInterface(zeroExAddr).getTransformWallet() + ); + approve( + TokenInterface(_sellAddr), + transformWallet, + zeroExData._sellAmt + ); + } + + zeroExData._buyAmt = zeroExSwap(zeroExData, ethAmt); + setUint(setId, zeroExData._buyAmt); + + return zeroExData; + } +} + +abstract contract ZeroEx is zeroExHelper { + /** + * @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 = ""; +} From bc3fa5bf906de1dd1b5b4f162d4f6d36f2cad631 Mon Sep 17 00:00:00 2001 From: bhavik Date: Mon, 6 Dec 2021 22:14:25 +0530 Subject: [PATCH 02/54] added support on polygon --- contracts/mainnet/connectors/0x/helpers.sol | 57 ++++++++++++++- contracts/mainnet/connectors/0x/interface.sol | 8 --- contracts/mainnet/connectors/0x/main.sol | 60 +--------------- contracts/polygon/connectors/0x/events.sol | 12 ++++ contracts/polygon/connectors/0x/helpers.sol | 70 +++++++++++++++++++ contracts/polygon/connectors/0x/interface.sol | 49 +++++++++++++ contracts/polygon/connectors/0x/main.sol | 63 +++++++++++++++++ 7 files changed, 252 insertions(+), 67 deletions(-) create mode 100644 contracts/polygon/connectors/0x/events.sol create mode 100644 contracts/polygon/connectors/0x/helpers.sol create mode 100644 contracts/polygon/connectors/0x/interface.sol create mode 100644 contracts/polygon/connectors/0x/main.sol diff --git a/contracts/mainnet/connectors/0x/helpers.sol b/contracts/mainnet/connectors/0x/helpers.sol index 7919436d..459d3abd 100644 --- a/contracts/mainnet/connectors/0x/helpers.sol +++ b/contracts/mainnet/connectors/0x/helpers.sol @@ -3,11 +3,66 @@ 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"; -abstract contract Helpers is DSMath, Basic { +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 { + address transformWallet = address( + zeroExInterface(zeroExAddr).getTransformWallet() + ); + approve( + TokenInterface(_sellAddr), + transformWallet, + zeroExData._sellAmt + ); + } + + zeroExData._buyAmt = _swapHelper(zeroExData, ethAmt); + setUint(setId, zeroExData._buyAmt); + + return zeroExData; + } } diff --git a/contracts/mainnet/connectors/0x/interface.sol b/contracts/mainnet/connectors/0x/interface.sol index a261afce..d6c7c57e 100644 --- a/contracts/mainnet/connectors/0x/interface.sol +++ b/contracts/mainnet/connectors/0x/interface.sol @@ -3,14 +3,6 @@ pragma solidity ^0.7.0; import {TokenInterface} from "../../common/interfaces.sol"; interface zeroExInterface { - // function transformERC20( - // IERC20TokenV06 inputToken, - // IERC20TokenV06 outputToken, - // uint256 inputTokenAmount, - // uint256 minOutputTokenAmount, - // Transformation[] calldata transformations - // ) external payable returns (uint256 outputTokenAmount); - function getTransformWallet() external view returns (IFlashWallet wallet); } diff --git a/contracts/mainnet/connectors/0x/main.sol b/contracts/mainnet/connectors/0x/main.sol index f55c496b..bd0e27b9 100644 --- a/contracts/mainnet/connectors/0x/main.sol +++ b/contracts/mainnet/connectors/0x/main.sol @@ -12,63 +12,7 @@ import {ZeroExData, zeroExInterface} from "./interface.sol"; import {Helpers} from "./helpers.sol"; import {Events} from "./events.sol"; -contract zeroExHelper is Helpers { - function zeroExSwap(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 { - address transformWallet = address( - zeroExInterface(zeroExAddr).getTransformWallet() - ); - approve( - TokenInterface(_sellAddr), - transformWallet, - zeroExData._sellAmt - ); - } - - zeroExData._buyAmt = zeroExSwap(zeroExData, ethAmt); - setUint(setId, zeroExData._buyAmt); - - return zeroExData; - } -} - -abstract contract ZeroEx is zeroExHelper { +abstract contract ZeroEx is Helpers { /** * @dev Sell ETH/ERC20_Token using 0x. * @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) @@ -115,5 +59,5 @@ abstract contract ZeroEx is zeroExHelper { } contract ConnectV2ZeroEx is ZeroEx { - string public name = ""; + string public name = "0x-V4"; } diff --git a/contracts/polygon/connectors/0x/events.sol b/contracts/polygon/connectors/0x/events.sol new file mode 100644 index 00000000..66f9943a --- /dev/null +++ b/contracts/polygon/connectors/0x/events.sol @@ -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 + ); +} diff --git a/contracts/polygon/connectors/0x/helpers.sol b/contracts/polygon/connectors/0x/helpers.sol new file mode 100644 index 00000000..835b8d7d --- /dev/null +++ b/contracts/polygon/connectors/0x/helpers.sol @@ -0,0 +1,70 @@ +pragma solidity ^0.7.0; + +import {TokenInterface} from "../../common/interfaces.sol"; +import {DSMath} from "../../common/math.sol"; +import {ZeroExData, zeroExInterface} 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 { + address transformWallet = address( + zeroExInterface(zeroExAddr).getTransformWallet() + ); + approve( + TokenInterface(_sellAddr), + transformWallet, + zeroExData._sellAmt + ); + } + + zeroExData._buyAmt = _swapHelper(zeroExData, maticAmt); + setUint(setId, zeroExData._buyAmt); + + return zeroExData; + } +} diff --git a/contracts/polygon/connectors/0x/interface.sol b/contracts/polygon/connectors/0x/interface.sol new file mode 100644 index 00000000..79952c90 --- /dev/null +++ b/contracts/polygon/connectors/0x/interface.sol @@ -0,0 +1,49 @@ +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; +} diff --git a/contracts/polygon/connectors/0x/main.sol b/contracts/polygon/connectors/0x/main.sol new file mode 100644 index 00000000..bd0e27b9 --- /dev/null +++ b/contracts/polygon/connectors/0x/main.sol @@ -0,0 +1,63 @@ +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 { + /** + * @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"; +} From 19d74394b627f51e221b82882beabe1f74c50eec Mon Sep 17 00:00:00 2001 From: bhavik Date: Mon, 6 Dec 2021 23:21:03 +0530 Subject: [PATCH 03/54] added @notice comment --- contracts/mainnet/connectors/0x/main.sol | 1 + contracts/polygon/connectors/0x/main.sol | 1 + 2 files changed, 2 insertions(+) diff --git a/contracts/mainnet/connectors/0x/main.sol b/contracts/mainnet/connectors/0x/main.sol index bd0e27b9..72eb2220 100644 --- a/contracts/mainnet/connectors/0x/main.sol +++ b/contracts/mainnet/connectors/0x/main.sol @@ -14,6 +14,7 @@ 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) diff --git a/contracts/polygon/connectors/0x/main.sol b/contracts/polygon/connectors/0x/main.sol index bd0e27b9..72eb2220 100644 --- a/contracts/polygon/connectors/0x/main.sol +++ b/contracts/polygon/connectors/0x/main.sol @@ -14,6 +14,7 @@ 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) From 0486a25c7cf2701b057fee826b7f34c75b4f75c7 Mon Sep 17 00:00:00 2001 From: bhavik Date: Fri, 10 Dec 2021 23:18:57 +0530 Subject: [PATCH 04/54] added test for 0x --- test/0x/api.js | 59 ++++++++++++++ test/0x/zeroEx.test.js | 173 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 232 insertions(+) create mode 100644 test/0x/api.js create mode 100644 test/0x/zeroEx.test.js diff --git a/test/0x/api.js b/test/0x/api.js new file mode 100644 index 00000000..30201ac5 --- /dev/null +++ b/test/0x/api.js @@ -0,0 +1,59 @@ +import axios from "axios"; +import { childLogger } from "./logger"; + +const logger = childLogger("utils/api"); + +function getApi() { + const api = axios.create({}); + + api.interceptors.request.use( + (request) => { + logger.info({ + msg: "Request to external API", + req: { + method: request.method, + url: request.url, + params: request.params, + headers: request.headers, + }, + }); + + return request; + }, + (error) => { + logger.error({ msg: "Error while requesting external API", error }); + + return Promise.reject(error); + } + ); + + api.interceptors.response.use( + (response) => { + logger.info({ + msg: "Response from external API", + req: { + method: response.config.method, + url: response.config.url, + params: response.config.params, + }, + res: { + statusCode: response.status, + statusText: response.statusText, + headers: response.config.headers, + data: response.data, + }, + }); + + return response; + }, + (error) => { + logger.error({ msg: "Error response from external API", error }); + + return Promise.reject(error); + } + ); + + return api; +} + +export default getApi(); diff --git a/test/0x/zeroEx.test.js b/test/0x/zeroEx.test.js new file mode 100644 index 00000000..fe1ffa03 --- /dev/null +++ b/test/0x/zeroEx.test.js @@ -0,0 +1,173 @@ +const { expect } = require("chai"); +const hre = require("hardhat"); +const axios = require("axios"); +const { web3, deployments, waffle, ethers } = hre; +const { provider, deployContract } = waffle; +const BigNumber = require("bignumber.js"); + +// import axpi from "./api"; + +const deployAndEnableConnector = require("../../scripts/deployAndEnableConnector.js"); +const buildDSAv2 = require("../../scripts/buildDSAv2"); +const encodeSpells = require("../../scripts/encodeSpells.js"); +const getMasterSigner = require("../../scripts/getMasterSigner"); +const addLiquidity = require("../../scripts/addLiquidity"); + +const addresses = require("../../scripts/constant/addresses"); +const abis = require("../../scripts/constant/abis"); +const constants = require("../../scripts/constant/constant"); +const tokens = require("../../scripts/constant/tokens"); + +const connectV2CompoundArtifacts = require("../../artifacts/contracts/mainnet/connectors/0x/main.sol/ConnectV2ZeroEx.json"); + +describe("ZeroEx", function() { + const connectorName = "zeroEx-test"; + + let dsaWallet0; + let masterSigner; + let instaConnectorsV2; + let connector; + + const wallets = provider.getWallets(); + const [wallet0, wallet1, wallet2, wallet3] = wallets; + + before(async () => { + // await hre.network.provider.request({ + // method: "hardhat_reset", + // params: [ + // { + // forking: { + // jsonRpcUrl: hre.config.networks.hardhat.forking.url, + // blockNumber: 13300000, + // }, + // }, + // ], + // }); + masterSigner = await getMasterSigner(wallet3); + instaConnectorsV2 = await ethers.getContractAt( + abis.core.connectorsV2, + addresses.core.connectorsV2 + ); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: connectV2CompoundArtifacts, + 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 and DAI 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") + ); + + // await addLiquidity( + // "dai", + // dsaWallet0.address, + // ethers.utils.parseUnits("100000", 18) + // ); + // expect(await ether s.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseUnits("100000", 18)); + }); + }); + + describe("Main", function() { + it("should swap", async function() { + async function getArg() { + const slippage = 0.5; + + /* MATIC -> USDT */ + const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // Matic, decimals 18 + const sellTokenDecimals = 18; + const buyTokenAddress = "0xc2132D05D31c914a87C6611C10748AEb04B58e8F"; // USDT, decimals 6 + const buyTokenDecimals = 6; + const amount = 1; + + const srcAmount = new BigNumber(amount) + .times(new BigNumber(10).pow(sellTokenDecimals)) + .toFixed(0); + console.log(srcAmount); + + 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 buyTokenAmount = await axios + .get(url, { params: params }) + .then((data) => data.data.buyAmount); + + const calldata = await axios + .get(url, { params: params }) + .then((data) => data.data.data); + + console.log(calldata); + + let caculateUnitAmt = () => { + const buyTokenAmountRes = new BigNumber(buyTokenAmount) + .dividedBy(new BigNumber(10).pow(buyTokenDecimals)) + .toFixed(8); + + let unitAmt = 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(); + + console.log("unitAmt - " + unitAmt); + + 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(); + console.log(receipt); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( + ethers.utils.parseEther("9") + ); + }); + }); +}); From 5c9eb5c09ba8335162fd25f553f1bd46a0e450ae Mon Sep 17 00:00:00 2001 From: bhavik Date: Fri, 10 Dec 2021 23:20:43 +0530 Subject: [PATCH 05/54] added test --- test/0x/api.js | 59 ------------------------------------------ test/0x/zeroEx.test.js | 2 -- 2 files changed, 61 deletions(-) delete mode 100644 test/0x/api.js diff --git a/test/0x/api.js b/test/0x/api.js deleted file mode 100644 index 30201ac5..00000000 --- a/test/0x/api.js +++ /dev/null @@ -1,59 +0,0 @@ -import axios from "axios"; -import { childLogger } from "./logger"; - -const logger = childLogger("utils/api"); - -function getApi() { - const api = axios.create({}); - - api.interceptors.request.use( - (request) => { - logger.info({ - msg: "Request to external API", - req: { - method: request.method, - url: request.url, - params: request.params, - headers: request.headers, - }, - }); - - return request; - }, - (error) => { - logger.error({ msg: "Error while requesting external API", error }); - - return Promise.reject(error); - } - ); - - api.interceptors.response.use( - (response) => { - logger.info({ - msg: "Response from external API", - req: { - method: response.config.method, - url: response.config.url, - params: response.config.params, - }, - res: { - statusCode: response.status, - statusText: response.statusText, - headers: response.config.headers, - data: response.data, - }, - }); - - return response; - }, - (error) => { - logger.error({ msg: "Error response from external API", error }); - - return Promise.reject(error); - } - ); - - return api; -} - -export default getApi(); diff --git a/test/0x/zeroEx.test.js b/test/0x/zeroEx.test.js index fe1ffa03..804cde5b 100644 --- a/test/0x/zeroEx.test.js +++ b/test/0x/zeroEx.test.js @@ -5,8 +5,6 @@ const { web3, deployments, waffle, ethers } = hre; const { provider, deployContract } = waffle; const BigNumber = require("bignumber.js"); -// import axpi from "./api"; - const deployAndEnableConnector = require("../../scripts/deployAndEnableConnector.js"); const buildDSAv2 = require("../../scripts/buildDSAv2"); const encodeSpells = require("../../scripts/encodeSpells.js"); From f9d365ca7c15845729694ddd6405b9efd004c368 Mon Sep 17 00:00:00 2001 From: bhavik Date: Sat, 11 Dec 2021 00:14:32 +0530 Subject: [PATCH 06/54] fixed typo --- test/0x/zeroEx.test.js | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/test/0x/zeroEx.test.js b/test/0x/zeroEx.test.js index 804cde5b..820e6f74 100644 --- a/test/0x/zeroEx.test.js +++ b/test/0x/zeroEx.test.js @@ -30,17 +30,17 @@ describe("ZeroEx", function() { const [wallet0, wallet1, wallet2, wallet3] = wallets; before(async () => { - // await hre.network.provider.request({ - // method: "hardhat_reset", - // params: [ - // { - // forking: { - // jsonRpcUrl: hre.config.networks.hardhat.forking.url, - // blockNumber: 13300000, - // }, - // }, - // ], - // }); + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 13300000, + }, + }, + ], + }); masterSigner = await getMasterSigner(wallet3); instaConnectorsV2 = await ethers.getContractAt( abis.core.connectorsV2, @@ -67,7 +67,7 @@ describe("ZeroEx", function() { expect(!!dsaWallet0.address).to.be.true; }); - it("Deposit ETH and DAI into DSA wallet", async function() { + it("Deposit ETH into DSA wallet", async function() { await wallet0.sendTransaction({ to: dsaWallet0.address, value: ethers.utils.parseEther("10"), @@ -76,13 +76,6 @@ describe("ZeroEx", function() { expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( ethers.utils.parseEther("10") ); - - // await addLiquidity( - // "dai", - // dsaWallet0.address, - // ethers.utils.parseUnits("100000", 18) - // ); - // expect(await ether s.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseUnits("100000", 18)); }); }); @@ -91,11 +84,11 @@ describe("ZeroEx", function() { async function getArg() { const slippage = 0.5; - /* MATIC -> USDT */ - const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // Matic, decimals 18 + /* Eth -> dai */ + const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // eth, decimals 18 const sellTokenDecimals = 18; - const buyTokenAddress = "0xc2132D05D31c914a87C6611C10748AEb04B58e8F"; // USDT, decimals 6 - const buyTokenDecimals = 6; + const buyTokenAddress = "0x6b175474e89094c44da98b954eedeac495271d0f"; // dai, decimals 18 + const buyTokenDecimals = 18; const amount = 1; const srcAmount = new BigNumber(amount) From 8a23337efddc9f5541762e594cfa57b0e03d714f Mon Sep 17 00:00:00 2001 From: bhavik Date: Mon, 6 Dec 2021 17:47:55 +0530 Subject: [PATCH 07/54] added 0x on mainnet --- contracts/mainnet/connectors/0x/events.sol | 12 ++ contracts/mainnet/connectors/0x/helpers.sol | 13 ++ contracts/mainnet/connectors/0x/interface.sol | 54 ++++++++ contracts/mainnet/connectors/0x/main.sol | 119 ++++++++++++++++++ 4 files changed, 198 insertions(+) create mode 100644 contracts/mainnet/connectors/0x/events.sol create mode 100644 contracts/mainnet/connectors/0x/helpers.sol create mode 100644 contracts/mainnet/connectors/0x/interface.sol create mode 100644 contracts/mainnet/connectors/0x/main.sol diff --git a/contracts/mainnet/connectors/0x/events.sol b/contracts/mainnet/connectors/0x/events.sol new file mode 100644 index 00000000..66f9943a --- /dev/null +++ b/contracts/mainnet/connectors/0x/events.sol @@ -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 + ); +} diff --git a/contracts/mainnet/connectors/0x/helpers.sol b/contracts/mainnet/connectors/0x/helpers.sol new file mode 100644 index 00000000..7919436d --- /dev/null +++ b/contracts/mainnet/connectors/0x/helpers.sol @@ -0,0 +1,13 @@ +pragma solidity ^0.7.0; + +import {TokenInterface} from "../../common/interfaces.sol"; +import {DSMath} from "../../common/math.sol"; +import {Basic} from "../../common/basic.sol"; + +abstract contract Helpers is DSMath, Basic { + /** + * @dev 0x Address + */ + address internal constant zeroExAddr = + 0xDef1C0ded9bec7F1a1670819833240f027b25EfF; +} diff --git a/contracts/mainnet/connectors/0x/interface.sol b/contracts/mainnet/connectors/0x/interface.sol new file mode 100644 index 00000000..a261afce --- /dev/null +++ b/contracts/mainnet/connectors/0x/interface.sol @@ -0,0 +1,54 @@ +pragma solidity ^0.7.0; + +import {TokenInterface} from "../../common/interfaces.sol"; + +interface zeroExInterface { + // function transformERC20( + // IERC20TokenV06 inputToken, + // IERC20TokenV06 outputToken, + // uint256 inputTokenAmount, + // uint256 minOutputTokenAmount, + // Transformation[] calldata transformations + // ) external payable returns (uint256 outputTokenAmount); + + 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; +} diff --git a/contracts/mainnet/connectors/0x/main.sol b/contracts/mainnet/connectors/0x/main.sol new file mode 100644 index 00000000..f55c496b --- /dev/null +++ b/contracts/mainnet/connectors/0x/main.sol @@ -0,0 +1,119 @@ +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"; + +contract zeroExHelper is Helpers { + function zeroExSwap(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 { + address transformWallet = address( + zeroExInterface(zeroExAddr).getTransformWallet() + ); + approve( + TokenInterface(_sellAddr), + transformWallet, + zeroExData._sellAmt + ); + } + + zeroExData._buyAmt = zeroExSwap(zeroExData, ethAmt); + setUint(setId, zeroExData._buyAmt); + + return zeroExData; + } +} + +abstract contract ZeroEx is zeroExHelper { + /** + * @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 = ""; +} From 5afed9f288f2ee8543d6ccd4cb9c5e41b97011e8 Mon Sep 17 00:00:00 2001 From: bhavik Date: Mon, 6 Dec 2021 22:14:25 +0530 Subject: [PATCH 08/54] added support on polygon --- contracts/mainnet/connectors/0x/helpers.sol | 57 ++++++++++++++- contracts/mainnet/connectors/0x/interface.sol | 8 --- contracts/mainnet/connectors/0x/main.sol | 60 +--------------- contracts/polygon/connectors/0x/events.sol | 12 ++++ contracts/polygon/connectors/0x/helpers.sol | 70 +++++++++++++++++++ contracts/polygon/connectors/0x/interface.sol | 49 +++++++++++++ contracts/polygon/connectors/0x/main.sol | 63 +++++++++++++++++ 7 files changed, 252 insertions(+), 67 deletions(-) create mode 100644 contracts/polygon/connectors/0x/events.sol create mode 100644 contracts/polygon/connectors/0x/helpers.sol create mode 100644 contracts/polygon/connectors/0x/interface.sol create mode 100644 contracts/polygon/connectors/0x/main.sol diff --git a/contracts/mainnet/connectors/0x/helpers.sol b/contracts/mainnet/connectors/0x/helpers.sol index 7919436d..459d3abd 100644 --- a/contracts/mainnet/connectors/0x/helpers.sol +++ b/contracts/mainnet/connectors/0x/helpers.sol @@ -3,11 +3,66 @@ 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"; -abstract contract Helpers is DSMath, Basic { +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 { + address transformWallet = address( + zeroExInterface(zeroExAddr).getTransformWallet() + ); + approve( + TokenInterface(_sellAddr), + transformWallet, + zeroExData._sellAmt + ); + } + + zeroExData._buyAmt = _swapHelper(zeroExData, ethAmt); + setUint(setId, zeroExData._buyAmt); + + return zeroExData; + } } diff --git a/contracts/mainnet/connectors/0x/interface.sol b/contracts/mainnet/connectors/0x/interface.sol index a261afce..d6c7c57e 100644 --- a/contracts/mainnet/connectors/0x/interface.sol +++ b/contracts/mainnet/connectors/0x/interface.sol @@ -3,14 +3,6 @@ pragma solidity ^0.7.0; import {TokenInterface} from "../../common/interfaces.sol"; interface zeroExInterface { - // function transformERC20( - // IERC20TokenV06 inputToken, - // IERC20TokenV06 outputToken, - // uint256 inputTokenAmount, - // uint256 minOutputTokenAmount, - // Transformation[] calldata transformations - // ) external payable returns (uint256 outputTokenAmount); - function getTransformWallet() external view returns (IFlashWallet wallet); } diff --git a/contracts/mainnet/connectors/0x/main.sol b/contracts/mainnet/connectors/0x/main.sol index f55c496b..bd0e27b9 100644 --- a/contracts/mainnet/connectors/0x/main.sol +++ b/contracts/mainnet/connectors/0x/main.sol @@ -12,63 +12,7 @@ import {ZeroExData, zeroExInterface} from "./interface.sol"; import {Helpers} from "./helpers.sol"; import {Events} from "./events.sol"; -contract zeroExHelper is Helpers { - function zeroExSwap(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 { - address transformWallet = address( - zeroExInterface(zeroExAddr).getTransformWallet() - ); - approve( - TokenInterface(_sellAddr), - transformWallet, - zeroExData._sellAmt - ); - } - - zeroExData._buyAmt = zeroExSwap(zeroExData, ethAmt); - setUint(setId, zeroExData._buyAmt); - - return zeroExData; - } -} - -abstract contract ZeroEx is zeroExHelper { +abstract contract ZeroEx is Helpers { /** * @dev Sell ETH/ERC20_Token using 0x. * @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) @@ -115,5 +59,5 @@ abstract contract ZeroEx is zeroExHelper { } contract ConnectV2ZeroEx is ZeroEx { - string public name = ""; + string public name = "0x-V4"; } diff --git a/contracts/polygon/connectors/0x/events.sol b/contracts/polygon/connectors/0x/events.sol new file mode 100644 index 00000000..66f9943a --- /dev/null +++ b/contracts/polygon/connectors/0x/events.sol @@ -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 + ); +} diff --git a/contracts/polygon/connectors/0x/helpers.sol b/contracts/polygon/connectors/0x/helpers.sol new file mode 100644 index 00000000..835b8d7d --- /dev/null +++ b/contracts/polygon/connectors/0x/helpers.sol @@ -0,0 +1,70 @@ +pragma solidity ^0.7.0; + +import {TokenInterface} from "../../common/interfaces.sol"; +import {DSMath} from "../../common/math.sol"; +import {ZeroExData, zeroExInterface} 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 { + address transformWallet = address( + zeroExInterface(zeroExAddr).getTransformWallet() + ); + approve( + TokenInterface(_sellAddr), + transformWallet, + zeroExData._sellAmt + ); + } + + zeroExData._buyAmt = _swapHelper(zeroExData, maticAmt); + setUint(setId, zeroExData._buyAmt); + + return zeroExData; + } +} diff --git a/contracts/polygon/connectors/0x/interface.sol b/contracts/polygon/connectors/0x/interface.sol new file mode 100644 index 00000000..79952c90 --- /dev/null +++ b/contracts/polygon/connectors/0x/interface.sol @@ -0,0 +1,49 @@ +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; +} diff --git a/contracts/polygon/connectors/0x/main.sol b/contracts/polygon/connectors/0x/main.sol new file mode 100644 index 00000000..bd0e27b9 --- /dev/null +++ b/contracts/polygon/connectors/0x/main.sol @@ -0,0 +1,63 @@ +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 { + /** + * @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"; +} From 714a4d91b147ffee29a8cc5bce5de9c91536d445 Mon Sep 17 00:00:00 2001 From: bhavik Date: Mon, 6 Dec 2021 23:21:03 +0530 Subject: [PATCH 09/54] added @notice comment --- contracts/mainnet/connectors/0x/main.sol | 1 + contracts/polygon/connectors/0x/main.sol | 1 + 2 files changed, 2 insertions(+) diff --git a/contracts/mainnet/connectors/0x/main.sol b/contracts/mainnet/connectors/0x/main.sol index bd0e27b9..72eb2220 100644 --- a/contracts/mainnet/connectors/0x/main.sol +++ b/contracts/mainnet/connectors/0x/main.sol @@ -14,6 +14,7 @@ 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) diff --git a/contracts/polygon/connectors/0x/main.sol b/contracts/polygon/connectors/0x/main.sol index bd0e27b9..72eb2220 100644 --- a/contracts/polygon/connectors/0x/main.sol +++ b/contracts/polygon/connectors/0x/main.sol @@ -14,6 +14,7 @@ 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) From 19d8d85eaf12d86c63d1553cae6a6a40920224e9 Mon Sep 17 00:00:00 2001 From: bhavik Date: Fri, 10 Dec 2021 23:18:57 +0530 Subject: [PATCH 10/54] added test for 0x --- test/0x/api.js | 59 ++++++++++++++ test/0x/zeroEx.test.js | 173 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 232 insertions(+) create mode 100644 test/0x/api.js create mode 100644 test/0x/zeroEx.test.js diff --git a/test/0x/api.js b/test/0x/api.js new file mode 100644 index 00000000..30201ac5 --- /dev/null +++ b/test/0x/api.js @@ -0,0 +1,59 @@ +import axios from "axios"; +import { childLogger } from "./logger"; + +const logger = childLogger("utils/api"); + +function getApi() { + const api = axios.create({}); + + api.interceptors.request.use( + (request) => { + logger.info({ + msg: "Request to external API", + req: { + method: request.method, + url: request.url, + params: request.params, + headers: request.headers, + }, + }); + + return request; + }, + (error) => { + logger.error({ msg: "Error while requesting external API", error }); + + return Promise.reject(error); + } + ); + + api.interceptors.response.use( + (response) => { + logger.info({ + msg: "Response from external API", + req: { + method: response.config.method, + url: response.config.url, + params: response.config.params, + }, + res: { + statusCode: response.status, + statusText: response.statusText, + headers: response.config.headers, + data: response.data, + }, + }); + + return response; + }, + (error) => { + logger.error({ msg: "Error response from external API", error }); + + return Promise.reject(error); + } + ); + + return api; +} + +export default getApi(); diff --git a/test/0x/zeroEx.test.js b/test/0x/zeroEx.test.js new file mode 100644 index 00000000..fe1ffa03 --- /dev/null +++ b/test/0x/zeroEx.test.js @@ -0,0 +1,173 @@ +const { expect } = require("chai"); +const hre = require("hardhat"); +const axios = require("axios"); +const { web3, deployments, waffle, ethers } = hre; +const { provider, deployContract } = waffle; +const BigNumber = require("bignumber.js"); + +// import axpi from "./api"; + +const deployAndEnableConnector = require("../../scripts/deployAndEnableConnector.js"); +const buildDSAv2 = require("../../scripts/buildDSAv2"); +const encodeSpells = require("../../scripts/encodeSpells.js"); +const getMasterSigner = require("../../scripts/getMasterSigner"); +const addLiquidity = require("../../scripts/addLiquidity"); + +const addresses = require("../../scripts/constant/addresses"); +const abis = require("../../scripts/constant/abis"); +const constants = require("../../scripts/constant/constant"); +const tokens = require("../../scripts/constant/tokens"); + +const connectV2CompoundArtifacts = require("../../artifacts/contracts/mainnet/connectors/0x/main.sol/ConnectV2ZeroEx.json"); + +describe("ZeroEx", function() { + const connectorName = "zeroEx-test"; + + let dsaWallet0; + let masterSigner; + let instaConnectorsV2; + let connector; + + const wallets = provider.getWallets(); + const [wallet0, wallet1, wallet2, wallet3] = wallets; + + before(async () => { + // await hre.network.provider.request({ + // method: "hardhat_reset", + // params: [ + // { + // forking: { + // jsonRpcUrl: hre.config.networks.hardhat.forking.url, + // blockNumber: 13300000, + // }, + // }, + // ], + // }); + masterSigner = await getMasterSigner(wallet3); + instaConnectorsV2 = await ethers.getContractAt( + abis.core.connectorsV2, + addresses.core.connectorsV2 + ); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: connectV2CompoundArtifacts, + 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 and DAI 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") + ); + + // await addLiquidity( + // "dai", + // dsaWallet0.address, + // ethers.utils.parseUnits("100000", 18) + // ); + // expect(await ether s.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseUnits("100000", 18)); + }); + }); + + describe("Main", function() { + it("should swap", async function() { + async function getArg() { + const slippage = 0.5; + + /* MATIC -> USDT */ + const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // Matic, decimals 18 + const sellTokenDecimals = 18; + const buyTokenAddress = "0xc2132D05D31c914a87C6611C10748AEb04B58e8F"; // USDT, decimals 6 + const buyTokenDecimals = 6; + const amount = 1; + + const srcAmount = new BigNumber(amount) + .times(new BigNumber(10).pow(sellTokenDecimals)) + .toFixed(0); + console.log(srcAmount); + + 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 buyTokenAmount = await axios + .get(url, { params: params }) + .then((data) => data.data.buyAmount); + + const calldata = await axios + .get(url, { params: params }) + .then((data) => data.data.data); + + console.log(calldata); + + let caculateUnitAmt = () => { + const buyTokenAmountRes = new BigNumber(buyTokenAmount) + .dividedBy(new BigNumber(10).pow(buyTokenDecimals)) + .toFixed(8); + + let unitAmt = 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(); + + console.log("unitAmt - " + unitAmt); + + 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(); + console.log(receipt); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( + ethers.utils.parseEther("9") + ); + }); + }); +}); From a4c236ebeb54cc319980f6691c989da4edcea025 Mon Sep 17 00:00:00 2001 From: bhavik Date: Fri, 10 Dec 2021 23:20:43 +0530 Subject: [PATCH 11/54] added test --- test/0x/api.js | 59 ------------------------------------------ test/0x/zeroEx.test.js | 2 -- 2 files changed, 61 deletions(-) delete mode 100644 test/0x/api.js diff --git a/test/0x/api.js b/test/0x/api.js deleted file mode 100644 index 30201ac5..00000000 --- a/test/0x/api.js +++ /dev/null @@ -1,59 +0,0 @@ -import axios from "axios"; -import { childLogger } from "./logger"; - -const logger = childLogger("utils/api"); - -function getApi() { - const api = axios.create({}); - - api.interceptors.request.use( - (request) => { - logger.info({ - msg: "Request to external API", - req: { - method: request.method, - url: request.url, - params: request.params, - headers: request.headers, - }, - }); - - return request; - }, - (error) => { - logger.error({ msg: "Error while requesting external API", error }); - - return Promise.reject(error); - } - ); - - api.interceptors.response.use( - (response) => { - logger.info({ - msg: "Response from external API", - req: { - method: response.config.method, - url: response.config.url, - params: response.config.params, - }, - res: { - statusCode: response.status, - statusText: response.statusText, - headers: response.config.headers, - data: response.data, - }, - }); - - return response; - }, - (error) => { - logger.error({ msg: "Error response from external API", error }); - - return Promise.reject(error); - } - ); - - return api; -} - -export default getApi(); diff --git a/test/0x/zeroEx.test.js b/test/0x/zeroEx.test.js index fe1ffa03..804cde5b 100644 --- a/test/0x/zeroEx.test.js +++ b/test/0x/zeroEx.test.js @@ -5,8 +5,6 @@ const { web3, deployments, waffle, ethers } = hre; const { provider, deployContract } = waffle; const BigNumber = require("bignumber.js"); -// import axpi from "./api"; - const deployAndEnableConnector = require("../../scripts/deployAndEnableConnector.js"); const buildDSAv2 = require("../../scripts/buildDSAv2"); const encodeSpells = require("../../scripts/encodeSpells.js"); From 0d44bd896a291cdbb4c9b4878d3f6f4a44d43995 Mon Sep 17 00:00:00 2001 From: bhavik Date: Sat, 11 Dec 2021 00:14:32 +0530 Subject: [PATCH 12/54] fixed typo --- test/0x/zeroEx.test.js | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/test/0x/zeroEx.test.js b/test/0x/zeroEx.test.js index 804cde5b..820e6f74 100644 --- a/test/0x/zeroEx.test.js +++ b/test/0x/zeroEx.test.js @@ -30,17 +30,17 @@ describe("ZeroEx", function() { const [wallet0, wallet1, wallet2, wallet3] = wallets; before(async () => { - // await hre.network.provider.request({ - // method: "hardhat_reset", - // params: [ - // { - // forking: { - // jsonRpcUrl: hre.config.networks.hardhat.forking.url, - // blockNumber: 13300000, - // }, - // }, - // ], - // }); + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 13300000, + }, + }, + ], + }); masterSigner = await getMasterSigner(wallet3); instaConnectorsV2 = await ethers.getContractAt( abis.core.connectorsV2, @@ -67,7 +67,7 @@ describe("ZeroEx", function() { expect(!!dsaWallet0.address).to.be.true; }); - it("Deposit ETH and DAI into DSA wallet", async function() { + it("Deposit ETH into DSA wallet", async function() { await wallet0.sendTransaction({ to: dsaWallet0.address, value: ethers.utils.parseEther("10"), @@ -76,13 +76,6 @@ describe("ZeroEx", function() { expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( ethers.utils.parseEther("10") ); - - // await addLiquidity( - // "dai", - // dsaWallet0.address, - // ethers.utils.parseUnits("100000", 18) - // ); - // expect(await ether s.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseUnits("100000", 18)); }); }); @@ -91,11 +84,11 @@ describe("ZeroEx", function() { async function getArg() { const slippage = 0.5; - /* MATIC -> USDT */ - const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // Matic, decimals 18 + /* Eth -> dai */ + const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // eth, decimals 18 const sellTokenDecimals = 18; - const buyTokenAddress = "0xc2132D05D31c914a87C6611C10748AEb04B58e8F"; // USDT, decimals 6 - const buyTokenDecimals = 6; + const buyTokenAddress = "0x6b175474e89094c44da98b954eedeac495271d0f"; // dai, decimals 18 + const buyTokenDecimals = 18; const amount = 1; const srcAmount = new BigNumber(amount) From 29724c2fc895bc9ccd1387f3e7503f3a61a1b8de Mon Sep 17 00:00:00 2001 From: bhavik Date: Sat, 11 Dec 2021 19:48:50 +0530 Subject: [PATCH 13/54] updated-test --- test/mainnet/0x/zeroEx.test.ts | 165 +++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 test/mainnet/0x/zeroEx.test.ts diff --git a/test/mainnet/0x/zeroEx.test.ts b/test/mainnet/0x/zeroEx.test.ts new file mode 100644 index 00000000..557fe460 --- /dev/null +++ b/test/mainnet/0x/zeroEx.test.ts @@ -0,0 +1,165 @@ +import { expect } from "chai"; +import hre from "hardhat"; +const { web3, deployments, waffle, ethers } = hre; //check +const { provider, deployContract } = waffle +import axios from "axios"; +import { BigNumber } from "bignumber.js"; +import { ConnectV2ZeroEx, ConnectV2ZeroEx__factory } from "../../../typechain"; + +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 { addLiquidity } from "../../../scripts/tests/addLiquidity"; + +import { addresses } from "../../../scripts/tests/mainnet/addresses"; +import { abis } from "../../../scripts/constant/abis"; +import { tokens } from "../../../scripts/tests/mainnet/tokens"; +import { constants } from "../../../scripts/constant/constant"; + + +describe("ZeroEx", function() { + const connectorName = "zeroEx-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.forking.url, + blockNumber: 13300000, + }, + }, + ], + }); + 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(!!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"), + }); + console.log(dsaWallet0.address); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( + ethers.utils.parseEther("10") + ); + }); + }); + + describe("Main", function() { + it("should swap", async function() { + 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); + console.log(srcAmount); + + 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 buyTokenAmount = await axios + .get(url, { params: params }) + .then((data:any) => data.data.buyAmount); + + const calldata = await axios + .get(url, { params: params }) + .then((data:any) => data.data.data); + + console.log(calldata); + + 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(); + + console.log("unitAmt - " + unitAmt); + + 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(); + console.log(receipt); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( + ethers.utils.parseEther("9") + ); + }); + }); +}); From a5bb2e6a5a59e2348719e1378641ae7b755ed81d Mon Sep 17 00:00:00 2001 From: bhavik Date: Sun, 12 Dec 2021 00:20:22 +0530 Subject: [PATCH 14/54] added-test --- test/mainnet/0x/zeroEx.test.ts | 71 ++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 30 deletions(-) diff --git a/test/mainnet/0x/zeroEx.test.ts b/test/mainnet/0x/zeroEx.test.ts index 557fe460..8a04f890 100644 --- a/test/mainnet/0x/zeroEx.test.ts +++ b/test/mainnet/0x/zeroEx.test.ts @@ -1,7 +1,7 @@ import { expect } from "chai"; import hre from "hardhat"; const { web3, deployments, waffle, ethers } = hre; //check -const { provider, deployContract } = waffle +const { provider, deployContract } = waffle; import axios from "axios"; import { BigNumber } from "bignumber.js"; import { ConnectV2ZeroEx, ConnectV2ZeroEx__factory } from "../../../typechain"; @@ -17,31 +17,32 @@ import { abis } from "../../../scripts/constant/abis"; import { tokens } from "../../../scripts/tests/mainnet/tokens"; import { constants } from "../../../scripts/constant/constant"; +import er20abi from "../../../scripts/constant/abi/basics/erc20.json"; describe("ZeroEx", function() { const connectorName = "zeroEx-test"; - let dsaWallet0 : any; - let masterSigner:any; - let instaConnectorsV2 : any; - let connector:any; + 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.forking.url, - blockNumber: 13300000, - }, - }, - ], - }); + // await hre.network.provider.request({ + // method: "hardhat_reset", + // params: [ + // { + // forking: { + // // @ts-ignore + // jsonRpcUrl: hre.config.networks.forking.url, + // blockNumber: 13300000, + // }, + // }, + // ], + // }); masterSigner = await getMasterSigner(); instaConnectorsV2 = await ethers.getContractAt( abis.core.connectorsV2, @@ -53,7 +54,7 @@ describe("ZeroEx", function() { signer: masterSigner, connectors: instaConnectorsV2, }); - console.log("Connector address", connector.address); + // console.log("Connector address", connector.address); }); it("Should have contracts deployed.", async function() { @@ -73,7 +74,7 @@ describe("ZeroEx", function() { to: dsaWallet0.address, value: ethers.utils.parseEther("10"), }); - console.log(dsaWallet0.address); + // console.log(dsaWallet0.address); expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( ethers.utils.parseEther("10") ); @@ -81,9 +82,10 @@ describe("ZeroEx", function() { }); describe("Main", function() { - it("should swap", async function() { + it("should swap the tokens", async function() { + let buyTokenAmount: any; async function getArg() { - const slippage = 0.5; + // const slippage = 0.5; /* Eth -> dai */ const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // eth, decimals 18 @@ -95,7 +97,7 @@ describe("ZeroEx", function() { const srcAmount = new BigNumber(amount) .times(new BigNumber(10).pow(sellTokenDecimals)) .toFixed(0); - console.log(srcAmount); + // console.log(srcAmount); const fromAddress = dsaWallet0.address; @@ -107,15 +109,15 @@ describe("ZeroEx", function() { sellAmount: "1000000000000000000", // Always denominated in wei }; - const buyTokenAmount = await axios + const response = await axios .get(url, { params: params }) - .then((data:any) => data.data.buyAmount); + .then((data: any) => data); - const calldata = await axios - .get(url, { params: params }) - .then((data:any) => data.data.data); + buyTokenAmount = response.data.buyAmount; + const calldata = response.data.data; - console.log(calldata); + // console.log("calldata ", calldata); + // console.log("buyTokenAmount ", buyTokenAmount); let caculateUnitAmt = () => { const buyTokenAmountRes = new BigNumber(buyTokenAmount) @@ -132,7 +134,7 @@ describe("ZeroEx", function() { }; let unitAmt = caculateUnitAmt(); - console.log("unitAmt - " + unitAmt); + // console.log("unitAmt - " + unitAmt); return [ buyTokenAddress, @@ -156,7 +158,16 @@ describe("ZeroEx", function() { .connect(wallet0) .cast(...encodeSpells(spells), wallet1.address); const receipt = await tx.wait(); - console.log(receipt); + // console.log(receipt); + + const idai = await ethers.getContractAt( + er20abi, + "0x6b175474e89094c44da98b954eedeac495271d0f" // dai address + ); + + expect(await idai.balanceOf(dsaWallet0.address)).to.be.gte( + buyTokenAmount + ); expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( ethers.utils.parseEther("9") ); From 128b076c94f389828cc9658a17a713c8faede53c Mon Sep 17 00:00:00 2001 From: bhavik Date: Sun, 12 Dec 2021 15:45:25 +0530 Subject: [PATCH 15/54] updated-test --- test/mainnet/0x/zeroEx.test.ts | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/test/mainnet/0x/zeroEx.test.ts b/test/mainnet/0x/zeroEx.test.ts index 8a04f890..891e8e15 100644 --- a/test/mainnet/0x/zeroEx.test.ts +++ b/test/mainnet/0x/zeroEx.test.ts @@ -31,17 +31,17 @@ describe("ZeroEx", function() { const [wallet0, wallet1, wallet2, wallet3] = wallets; before(async () => { - // await hre.network.provider.request({ - // method: "hardhat_reset", - // params: [ - // { - // forking: { - // // @ts-ignore - // jsonRpcUrl: hre.config.networks.forking.url, - // blockNumber: 13300000, + // await hre.network.provider.request({ + // method: "hardhat_reset", + // params: [ + // { + // forking: { + // // @ts-ignore + // jsonRpcUrl: hre.config.networks.forking.url, + // blockNumber: 13300000, + // }, // }, - // }, - // ], + // ], // }); masterSigner = await getMasterSigner(); instaConnectorsV2 = await ethers.getContractAt( @@ -78,6 +78,10 @@ describe("ZeroEx", function() { expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( ethers.utils.parseEther("10") ); + const idai = await ethers.getContractAt( + er20abi, + "0x6b175474e89094c44da98b954eedeac495271d0f" // dai address + ); }); }); @@ -165,9 +169,6 @@ describe("ZeroEx", function() { "0x6b175474e89094c44da98b954eedeac495271d0f" // dai address ); - expect(await idai.balanceOf(dsaWallet0.address)).to.be.gte( - buyTokenAmount - ); expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( ethers.utils.parseEther("9") ); From 8798e36d1d7e87abc7521b464dd27f12eb4187d6 Mon Sep 17 00:00:00 2001 From: bhavik Date: Sun, 12 Dec 2021 15:52:09 +0530 Subject: [PATCH 16/54] fixed minor issue --- test/0x/zeroEx.test.js | 164 --------------------------------- test/mainnet/0x/zeroEx.test.ts | 3 + 2 files changed, 3 insertions(+), 164 deletions(-) delete mode 100644 test/0x/zeroEx.test.js diff --git a/test/0x/zeroEx.test.js b/test/0x/zeroEx.test.js deleted file mode 100644 index 820e6f74..00000000 --- a/test/0x/zeroEx.test.js +++ /dev/null @@ -1,164 +0,0 @@ -const { expect } = require("chai"); -const hre = require("hardhat"); -const axios = require("axios"); -const { web3, deployments, waffle, ethers } = hre; -const { provider, deployContract } = waffle; -const BigNumber = require("bignumber.js"); - -const deployAndEnableConnector = require("../../scripts/deployAndEnableConnector.js"); -const buildDSAv2 = require("../../scripts/buildDSAv2"); -const encodeSpells = require("../../scripts/encodeSpells.js"); -const getMasterSigner = require("../../scripts/getMasterSigner"); -const addLiquidity = require("../../scripts/addLiquidity"); - -const addresses = require("../../scripts/constant/addresses"); -const abis = require("../../scripts/constant/abis"); -const constants = require("../../scripts/constant/constant"); -const tokens = require("../../scripts/constant/tokens"); - -const connectV2CompoundArtifacts = require("../../artifacts/contracts/mainnet/connectors/0x/main.sol/ConnectV2ZeroEx.json"); - -describe("ZeroEx", function() { - const connectorName = "zeroEx-test"; - - let dsaWallet0; - let masterSigner; - let instaConnectorsV2; - let connector; - - const wallets = provider.getWallets(); - const [wallet0, wallet1, wallet2, wallet3] = wallets; - - before(async () => { - await hre.network.provider.request({ - method: "hardhat_reset", - params: [ - { - forking: { - jsonRpcUrl: hre.config.networks.hardhat.forking.url, - blockNumber: 13300000, - }, - }, - ], - }); - masterSigner = await getMasterSigner(wallet3); - instaConnectorsV2 = await ethers.getContractAt( - abis.core.connectorsV2, - addresses.core.connectorsV2 - ); - connector = await deployAndEnableConnector({ - connectorName, - contractArtifact: connectV2CompoundArtifacts, - 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"), - }); - console.log(dsaWallet0.address); - expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( - ethers.utils.parseEther("10") - ); - }); - }); - - describe("Main", function() { - it("should swap", async function() { - 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); - console.log(srcAmount); - - 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 buyTokenAmount = await axios - .get(url, { params: params }) - .then((data) => data.data.buyAmount); - - const calldata = await axios - .get(url, { params: params }) - .then((data) => data.data.data); - - console.log(calldata); - - let caculateUnitAmt = () => { - const buyTokenAmountRes = new BigNumber(buyTokenAmount) - .dividedBy(new BigNumber(10).pow(buyTokenDecimals)) - .toFixed(8); - - let unitAmt = 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(); - - console.log("unitAmt - " + unitAmt); - - 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(); - console.log(receipt); - expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( - ethers.utils.parseEther("9") - ); - }); - }); -}); diff --git a/test/mainnet/0x/zeroEx.test.ts b/test/mainnet/0x/zeroEx.test.ts index 891e8e15..6700524b 100644 --- a/test/mainnet/0x/zeroEx.test.ts +++ b/test/mainnet/0x/zeroEx.test.ts @@ -169,6 +169,9 @@ describe("ZeroEx", function() { "0x6b175474e89094c44da98b954eedeac495271d0f" // dai address ); + expect(await idai.balanceOf(dsaWallet0.address)).to.be.gte( + buyTokenAmount + ); expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( ethers.utils.parseEther("9") ); From f6e147290bbc31f082972f1f4310a4f4940e462b Mon Sep 17 00:00:00 2001 From: bhavik Date: Sun, 12 Dec 2021 18:26:27 +0530 Subject: [PATCH 17/54] added test on polygon --- contracts/polygon/connectors/0x/main.sol | 2 +- test/polygon/0x/zeroEx.test.ts | 177 +++++++++++++++++++++++ 2 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 test/polygon/0x/zeroEx.test.ts diff --git a/contracts/polygon/connectors/0x/main.sol b/contracts/polygon/connectors/0x/main.sol index 72eb2220..93d484df 100644 --- a/contracts/polygon/connectors/0x/main.sol +++ b/contracts/polygon/connectors/0x/main.sol @@ -59,6 +59,6 @@ abstract contract ZeroEx is Helpers { } } -contract ConnectV2ZeroEx is ZeroEx { +contract ConnectV2ZeroExPolygon is ZeroEx { string public name = "0x-V4"; } diff --git a/test/polygon/0x/zeroEx.test.ts b/test/polygon/0x/zeroEx.test.ts new file mode 100644 index 00000000..8159b3be --- /dev/null +++ b/test/polygon/0x/zeroEx.test.ts @@ -0,0 +1,177 @@ +import { expect } from "chai"; +import hre from "hardhat"; +const { web3, deployments, waffle, ethers } = hre; //check +const { provider, deployContract } = waffle; +import axios from "axios"; +import { BigNumber } from "bignumber.js"; +import { + ConnectV2ZeroExPolygon, + ConnectV2ZeroExPolygon__factory, +} from "../../../typechain"; + +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 { addLiquidity } from "../../../scripts/tests/addLiquidity"; + +import { addresses } from "../../../scripts/tests/polygon/addresses"; +import { abis } from "../../../scripts/constant/abis"; + +import er20abi from "../../../scripts/constant/abi/basics/erc20.json"; + +describe("ZeroEx", function() { + const connectorName = "zeroEx-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.forking.url, + // blockNumber: 13300000, + // }, + // }, + // ], + // }); + 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(!!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 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); + // console.log(srcAmount); + + const fromAddress = dsaWallet0.address; + + 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; + + // console.log("calldata ", calldata); + // console.log("buyTokenAmount ", buyTokenAmount); + + 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(); + + // console.log("unitAmt - " + unitAmt); + + 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(); + // console.log(receipt); + + const idai = await ethers.getContractAt( + er20abi, + "0x6b175474e89094c44da98b954eedeac495271d0f" // dai address + ); + + // expect(await idai.balanceOf(dsaWallet0.address)).to.be.gte( + // buyTokenAmount + // ); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( + ethers.utils.parseEther("9") + ); + }); + }); +}); From 2f6f66f640e93bd0c4ea649130641092856b5066 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Sun, 12 Dec 2021 18:51:58 +0530 Subject: [PATCH 18/54] fixed mainnet 0x test --- test/mainnet/0x/zeroEx.test.ts | 56 +++++++++++++++------------------- 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/test/mainnet/0x/zeroEx.test.ts b/test/mainnet/0x/zeroEx.test.ts index 6700524b..ec498bf6 100644 --- a/test/mainnet/0x/zeroEx.test.ts +++ b/test/mainnet/0x/zeroEx.test.ts @@ -1,48 +1,42 @@ -import { expect } from "chai"; import hre from "hardhat"; -const { web3, deployments, waffle, ethers } = hre; //check -const { provider, deployContract } = waffle; import axios from "axios"; +import { expect } from "chai"; +const { ethers } = hre; //check import { BigNumber } from "bignumber.js"; -import { ConnectV2ZeroEx, ConnectV2ZeroEx__factory } from "../../../typechain"; - 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 { addLiquidity } from "../../../scripts/tests/addLiquidity"; - import { addresses } from "../../../scripts/tests/mainnet/addresses"; import { abis } from "../../../scripts/constant/abis"; -import { tokens } from "../../../scripts/tests/mainnet/tokens"; -import { constants } from "../../../scripts/constant/constant"; - +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: any; - let masterSigner: any; + let dsaWallet0: Contract; + let wallet0: Signer, wallet1: Signer; + let masterSigner: Signer; 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.forking.url, - // blockNumber: 13300000, - // }, - // }, - // ], - // }); + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + // @ts-ignore + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 12796965, + }, + }, + ], + }); + [wallet0, wallet1] = await ethers.getSigners(); + masterSigner = await getMasterSigner(); instaConnectorsV2 = await ethers.getContractAt( abis.core.connectorsV2, @@ -54,18 +48,18 @@ describe("ZeroEx", function() { signer: masterSigner, connectors: instaConnectorsV2, }); - // console.log("Connector address", connector.address); + 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; + expect(!!(await masterSigner.getAddress())).to.be.true; }); describe("DSA wallet setup", function() { it("Should build DSA v2", async function() { - dsaWallet0 = await buildDSAv2(wallet0.address); + dsaWallet0 = await buildDSAv2(wallet0.getAddress()); expect(!!dsaWallet0.address).to.be.true; }); @@ -160,7 +154,7 @@ describe("ZeroEx", function() { ]; const tx = await dsaWallet0 .connect(wallet0) - .cast(...encodeSpells(spells), wallet1.address); + .cast(...encodeSpells(spells), wallet1.getAddress()); const receipt = await tx.wait(); // console.log(receipt); From 1457ae2509eeb09f42a728a6eee8ad2f8a737476 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Sun, 12 Dec 2021 19:01:49 +0530 Subject: [PATCH 19/54] fixes --- test/mainnet/0x/zeroEx.test.ts | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/test/mainnet/0x/zeroEx.test.ts b/test/mainnet/0x/zeroEx.test.ts index ec498bf6..f933a789 100644 --- a/test/mainnet/0x/zeroEx.test.ts +++ b/test/mainnet/0x/zeroEx.test.ts @@ -19,8 +19,8 @@ describe("ZeroEx", function() { let dsaWallet0: Contract; let wallet0: Signer, wallet1: Signer; let masterSigner: Signer; - let instaConnectorsV2: any; - let connector: any; + let instaConnectorsV2: Contract; + let connector: Contract; before(async () => { await hre.network.provider.request({ @@ -30,7 +30,6 @@ describe("ZeroEx", function() { forking: { // @ts-ignore jsonRpcUrl: hre.config.networks.hardhat.forking.url, - blockNumber: 12796965, }, }, ], @@ -84,7 +83,6 @@ describe("ZeroEx", function() { let buyTokenAmount: any; async function getArg() { // const slippage = 0.5; - /* Eth -> dai */ const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // eth, decimals 18 const sellTokenDecimals = 18; @@ -95,7 +93,6 @@ describe("ZeroEx", function() { const srcAmount = new BigNumber(amount) .times(new BigNumber(10).pow(sellTokenDecimals)) .toFixed(0); - // console.log(srcAmount); const fromAddress = dsaWallet0.address; @@ -114,9 +111,6 @@ describe("ZeroEx", function() { buyTokenAmount = response.data.buyAmount; const calldata = response.data.data; - // console.log("calldata ", calldata); - // console.log("buyTokenAmount ", buyTokenAmount); - let caculateUnitAmt = () => { const buyTokenAmountRes = new BigNumber(buyTokenAmount) .dividedBy(new BigNumber(10).pow(buyTokenDecimals)) @@ -132,8 +126,6 @@ describe("ZeroEx", function() { }; let unitAmt = caculateUnitAmt(); - // console.log("unitAmt - " + unitAmt); - return [ buyTokenAddress, sellTokenAddress, @@ -156,14 +148,13 @@ describe("ZeroEx", function() { .connect(wallet0) .cast(...encodeSpells(spells), wallet1.getAddress()); const receipt = await tx.wait(); - // console.log(receipt); - const idai = await ethers.getContractAt( + const daiToken = await ethers.getContractAt( er20abi, "0x6b175474e89094c44da98b954eedeac495271d0f" // dai address ); - expect(await idai.balanceOf(dsaWallet0.address)).to.be.gte( + expect(await daiToken.balanceOf(dsaWallet0.address)).to.be.gte( buyTokenAmount ); expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( From d2cb02aef3fa5f50c6667b0277713fe1831851cb Mon Sep 17 00:00:00 2001 From: bhavik Date: Sun, 12 Dec 2021 19:15:11 +0530 Subject: [PATCH 20/54] updated polygon test --- test/mainnet/0x/zeroEx.test.ts | 2 +- test/polygon/0x/zeroEx.test.ts | 48 ++++++++++++---------------------- 2 files changed, 18 insertions(+), 32 deletions(-) diff --git a/test/mainnet/0x/zeroEx.test.ts b/test/mainnet/0x/zeroEx.test.ts index ec498bf6..67f95006 100644 --- a/test/mainnet/0x/zeroEx.test.ts +++ b/test/mainnet/0x/zeroEx.test.ts @@ -30,7 +30,7 @@ describe("ZeroEx", function() { forking: { // @ts-ignore jsonRpcUrl: hre.config.networks.hardhat.forking.url, - blockNumber: 12796965, + // blockNumber: 12796965, }, }, ], diff --git a/test/polygon/0x/zeroEx.test.ts b/test/polygon/0x/zeroEx.test.ts index 8159b3be..58683855 100644 --- a/test/polygon/0x/zeroEx.test.ts +++ b/test/polygon/0x/zeroEx.test.ts @@ -1,36 +1,27 @@ -import { expect } from "chai"; import hre from "hardhat"; -const { web3, deployments, waffle, ethers } = hre; //check -const { provider, deployContract } = waffle; import axios from "axios"; +import { expect } from "chai"; +const { ethers } = hre; //check import { BigNumber } from "bignumber.js"; -import { - ConnectV2ZeroExPolygon, - ConnectV2ZeroExPolygon__factory, -} from "../../../typechain"; - 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 { addLiquidity } from "../../../scripts/tests/addLiquidity"; - -import { addresses } from "../../../scripts/tests/polygon/addresses"; +import { addresses } from "../../../scripts/tests/mainnet/addresses"; import { abis } from "../../../scripts/constant/abis"; - +import { ConnectV2ZeroExPolygon, 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: any; - let masterSigner: any; + let dsaWallet0: Contract; + let wallet0: Signer, wallet1: Signer; + let masterSigner: Signer; 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", @@ -55,18 +46,18 @@ describe("ZeroEx", function() { signer: masterSigner, connectors: instaConnectorsV2, }); - // console.log("Connector address", connector.address); + 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; + expect(!!masterSigner.getAddress()).to.be.true; }); describe("DSA wallet setup", function() { it("Should build DSA v2", async function() { - dsaWallet0 = await buildDSAv2(wallet0.address); + dsaWallet0 = await buildDSAv2(wallet0.getAddress()); expect(!!dsaWallet0.address).to.be.true; }); @@ -98,7 +89,6 @@ describe("ZeroEx", function() { const srcAmount = new BigNumber(amount) .times(new BigNumber(10).pow(sellTokenDecimals)) .toFixed(0); - // console.log(srcAmount); const fromAddress = dsaWallet0.address; @@ -117,9 +107,6 @@ describe("ZeroEx", function() { buyTokenAmount = response.data.buyAmount; const calldata = response.data.data; - // console.log("calldata ", calldata); - // console.log("buyTokenAmount ", buyTokenAmount); - let caculateUnitAmt = () => { const buyTokenAmountRes = new BigNumber(buyTokenAmount) .dividedBy(new BigNumber(10).pow(buyTokenDecimals)) @@ -135,7 +122,6 @@ describe("ZeroEx", function() { }; let unitAmt = caculateUnitAmt(); - // console.log("unitAmt - " + unitAmt); return [ buyTokenAddress, @@ -157,18 +143,18 @@ describe("ZeroEx", function() { ]; const tx = await dsaWallet0 .connect(wallet0) - .cast(...encodeSpells(spells), wallet1.address); + .cast(...encodeSpells(spells), wallet1.getAddress()); const receipt = await tx.wait(); - // console.log(receipt); + const idai = await ethers.getContractAt( er20abi, - "0x6b175474e89094c44da98b954eedeac495271d0f" // dai address + "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063" // dai address ); - // expect(await idai.balanceOf(dsaWallet0.address)).to.be.gte( - // buyTokenAmount - // ); + expect(await idai.balanceOf(dsaWallet0.address)).to.be.gte( + buyTokenAmount + ); expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( ethers.utils.parseEther("9") ); From fe4d30aee8a5134d316f81d33d1f1a978609fa4c Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Sun, 12 Dec 2021 19:41:43 +0530 Subject: [PATCH 21/54] updated tests --- test/mainnet/0x/zeroEx.test.ts | 4 +-- test/polygon/0x/zeroEx.test.ts | 50 ++++++++++++++++------------------ 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/test/mainnet/0x/zeroEx.test.ts b/test/mainnet/0x/zeroEx.test.ts index f933a789..c363ef7e 100644 --- a/test/mainnet/0x/zeroEx.test.ts +++ b/test/mainnet/0x/zeroEx.test.ts @@ -58,7 +58,7 @@ describe("ZeroEx", function() { describe("DSA wallet setup", function() { it("Should build DSA v2", async function() { - dsaWallet0 = await buildDSAv2(wallet0.getAddress()); + dsaWallet0 = await buildDSAv2(await wallet0.getAddress()); expect(!!dsaWallet0.address).to.be.true; }); @@ -146,7 +146,7 @@ describe("ZeroEx", function() { ]; const tx = await dsaWallet0 .connect(wallet0) - .cast(...encodeSpells(spells), wallet1.getAddress()); + .cast(...encodeSpells(spells), await wallet1.getAddress()); const receipt = await tx.wait(); const daiToken = await ethers.getContractAt( diff --git a/test/polygon/0x/zeroEx.test.ts b/test/polygon/0x/zeroEx.test.ts index 58683855..2ef4d5bb 100644 --- a/test/polygon/0x/zeroEx.test.ts +++ b/test/polygon/0x/zeroEx.test.ts @@ -7,7 +7,7 @@ import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnable 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 { addresses } from "../../../scripts/tests/polygon/addresses"; import { abis } from "../../../scripts/constant/abis"; import { ConnectV2ZeroExPolygon, ConnectV2ZeroExPolygon__factory } from "../../../typechain"; import er20abi from "../../../scripts/constant/abi/basics/erc20.json"; @@ -19,22 +19,23 @@ describe("ZeroEx", function() { let dsaWallet0: Contract; let wallet0: Signer, wallet1: Signer; let masterSigner: Signer; - let instaConnectorsV2: any; - let connector: any; + let instaConnectorsV2: Contract; + let connector: Contract; before(async () => { - // await hre.network.provider.request({ - // method: "hardhat_reset", - // params: [ - // { - // forking: { - // // @ts-ignore - // jsonRpcUrl: hre.config.networks.forking.url, - // blockNumber: 13300000, - // }, - // }, - // ], - // }); + 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, @@ -52,12 +53,12 @@ describe("ZeroEx", function() { it("Should have contracts deployed.", async function() { expect(!!instaConnectorsV2.address).to.be.true; expect(!!connector.address).to.be.true; - expect(!!masterSigner.getAddress()).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(wallet0.getAddress()); + dsaWallet0 = await buildDSAv2(await wallet0.getAddress()); expect(!!dsaWallet0.address).to.be.true; }); @@ -78,7 +79,6 @@ describe("ZeroEx", function() { let buyTokenAmount: any; async function getArg() { // const slippage = 0.5; - /* matic -> dai */ const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // matic, decimals 18 const sellTokenDecimals = 18; @@ -90,8 +90,6 @@ describe("ZeroEx", function() { .times(new BigNumber(10).pow(sellTokenDecimals)) .toFixed(0); - const fromAddress = dsaWallet0.address; - let url = `https://polygon.api.0x.org/swap/v1/quote`; const params = { @@ -122,7 +120,6 @@ describe("ZeroEx", function() { }; let unitAmt = caculateUnitAmt(); - return [ buyTokenAddress, sellTokenAddress, @@ -143,18 +140,17 @@ describe("ZeroEx", function() { ]; const tx = await dsaWallet0 .connect(wallet0) - .cast(...encodeSpells(spells), wallet1.getAddress()); + .cast(...encodeSpells(spells), await wallet1.getAddress()); const receipt = await tx.wait(); - - const idai = await ethers.getContractAt( + const daiToken = await ethers.getContractAt( er20abi, "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063" // dai address ); - expect(await idai.balanceOf(dsaWallet0.address)).to.be.gte( - buyTokenAmount - ); + expect(await daiToken.balanceOf(dsaWallet0.address)).to.be.gte( + buyTokenAmount + ); expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( ethers.utils.parseEther("9") ); From 054f95a92d713a038bc3b521a655d48d18b2174a Mon Sep 17 00:00:00 2001 From: bhavik Date: Sun, 12 Dec 2021 20:12:31 +0530 Subject: [PATCH 22/54] updated instaindex address on avalanche --- scripts/tests/avalanche/addresses.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/tests/avalanche/addresses.ts b/scripts/tests/avalanche/addresses.ts index d2da378f..d92a5bf7 100644 --- a/scripts/tests/avalanche/addresses.ts +++ b/scripts/tests/avalanche/addresses.ts @@ -5,7 +5,7 @@ export const addresses: Record = { // "INSTAPOOL-A": "0x8f1e38c53af7bD2b2bE01b9580911b7Cca504F1b", }, core: { - connectorsV2: "0x67fCE99Dd6d8d659eea2a1ac1b8881c57eb6592B", - instaIndex: "0x1eE00C305C51Ff3bE60162456A9B533C07cD9288", + connectorsV2: "0x127d8cD0E2b2E0366D522DeA53A787bfE9002C14", + instaIndex: " 0x6CE3e607C808b4f4C26B7F6aDAeB619e49CAbb25", }, }; From 7edaa872e72bb79e18618b745166dba22f022e05 Mon Sep 17 00:00:00 2001 From: bhavik Date: Sun, 12 Dec 2021 20:20:43 +0530 Subject: [PATCH 23/54] added support on avalache --- contracts/avalanche/connectors/0x/events.sol | 12 ++++ contracts/avalanche/connectors/0x/helpers.sol | 68 +++++++++++++++++++ .../avalanche/connectors/0x/interface.sol | 46 +++++++++++++ contracts/avalanche/connectors/0x/main.sol | 64 +++++++++++++++++ contracts/polygon/connectors/0x/main.sol | 6 +- 5 files changed, 193 insertions(+), 3 deletions(-) create mode 100644 contracts/avalanche/connectors/0x/events.sol create mode 100644 contracts/avalanche/connectors/0x/helpers.sol create mode 100644 contracts/avalanche/connectors/0x/interface.sol create mode 100644 contracts/avalanche/connectors/0x/main.sol diff --git a/contracts/avalanche/connectors/0x/events.sol b/contracts/avalanche/connectors/0x/events.sol new file mode 100644 index 00000000..66f9943a --- /dev/null +++ b/contracts/avalanche/connectors/0x/events.sol @@ -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 + ); +} diff --git a/contracts/avalanche/connectors/0x/helpers.sol b/contracts/avalanche/connectors/0x/helpers.sol new file mode 100644 index 00000000..dea216d4 --- /dev/null +++ b/contracts/avalanche/connectors/0x/helpers.sol @@ -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; + } +} diff --git a/contracts/avalanche/connectors/0x/interface.sol b/contracts/avalanche/connectors/0x/interface.sol new file mode 100644 index 00000000..d6c7c57e --- /dev/null +++ b/contracts/avalanche/connectors/0x/interface.sol @@ -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; +} diff --git a/contracts/avalanche/connectors/0x/main.sol b/contracts/avalanche/connectors/0x/main.sol new file mode 100644 index 00000000..fefe0566 --- /dev/null +++ b/contracts/avalanche/connectors/0x/main.sol @@ -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"; +} diff --git a/contracts/polygon/connectors/0x/main.sol b/contracts/polygon/connectors/0x/main.sol index 93d484df..cc63d57e 100644 --- a/contracts/polygon/connectors/0x/main.sol +++ b/contracts/polygon/connectors/0x/main.sol @@ -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. From d416e342ea88ff45af7d293703b4022c99c04f92 Mon Sep 17 00:00:00 2001 From: bhavik Date: Sun, 12 Dec 2021 21:23:30 +0530 Subject: [PATCH 24/54] fixed typo --- test/mainnet/0x/zeroEx.test.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/mainnet/0x/zeroEx.test.ts b/test/mainnet/0x/zeroEx.test.ts index c363ef7e..2cddb01f 100644 --- a/test/mainnet/0x/zeroEx.test.ts +++ b/test/mainnet/0x/zeroEx.test.ts @@ -71,10 +71,6 @@ describe("ZeroEx", function() { expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( ethers.utils.parseEther("10") ); - const idai = await ethers.getContractAt( - er20abi, - "0x6b175474e89094c44da98b954eedeac495271d0f" // dai address - ); }); }); From e821a7aa9ddf046b2214686865b543784b403f97 Mon Sep 17 00:00:00 2001 From: bhavik Date: Mon, 6 Dec 2021 17:47:55 +0530 Subject: [PATCH 25/54] added 0x on mainnet --- contracts/mainnet/connectors/0x/events.sol | 12 ++ contracts/mainnet/connectors/0x/helpers.sol | 13 ++ contracts/mainnet/connectors/0x/interface.sol | 54 ++++++++ contracts/mainnet/connectors/0x/main.sol | 119 ++++++++++++++++++ 4 files changed, 198 insertions(+) create mode 100644 contracts/mainnet/connectors/0x/events.sol create mode 100644 contracts/mainnet/connectors/0x/helpers.sol create mode 100644 contracts/mainnet/connectors/0x/interface.sol create mode 100644 contracts/mainnet/connectors/0x/main.sol diff --git a/contracts/mainnet/connectors/0x/events.sol b/contracts/mainnet/connectors/0x/events.sol new file mode 100644 index 00000000..66f9943a --- /dev/null +++ b/contracts/mainnet/connectors/0x/events.sol @@ -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 + ); +} diff --git a/contracts/mainnet/connectors/0x/helpers.sol b/contracts/mainnet/connectors/0x/helpers.sol new file mode 100644 index 00000000..7919436d --- /dev/null +++ b/contracts/mainnet/connectors/0x/helpers.sol @@ -0,0 +1,13 @@ +pragma solidity ^0.7.0; + +import {TokenInterface} from "../../common/interfaces.sol"; +import {DSMath} from "../../common/math.sol"; +import {Basic} from "../../common/basic.sol"; + +abstract contract Helpers is DSMath, Basic { + /** + * @dev 0x Address + */ + address internal constant zeroExAddr = + 0xDef1C0ded9bec7F1a1670819833240f027b25EfF; +} diff --git a/contracts/mainnet/connectors/0x/interface.sol b/contracts/mainnet/connectors/0x/interface.sol new file mode 100644 index 00000000..a261afce --- /dev/null +++ b/contracts/mainnet/connectors/0x/interface.sol @@ -0,0 +1,54 @@ +pragma solidity ^0.7.0; + +import {TokenInterface} from "../../common/interfaces.sol"; + +interface zeroExInterface { + // function transformERC20( + // IERC20TokenV06 inputToken, + // IERC20TokenV06 outputToken, + // uint256 inputTokenAmount, + // uint256 minOutputTokenAmount, + // Transformation[] calldata transformations + // ) external payable returns (uint256 outputTokenAmount); + + 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; +} diff --git a/contracts/mainnet/connectors/0x/main.sol b/contracts/mainnet/connectors/0x/main.sol new file mode 100644 index 00000000..f55c496b --- /dev/null +++ b/contracts/mainnet/connectors/0x/main.sol @@ -0,0 +1,119 @@ +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"; + +contract zeroExHelper is Helpers { + function zeroExSwap(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 { + address transformWallet = address( + zeroExInterface(zeroExAddr).getTransformWallet() + ); + approve( + TokenInterface(_sellAddr), + transformWallet, + zeroExData._sellAmt + ); + } + + zeroExData._buyAmt = zeroExSwap(zeroExData, ethAmt); + setUint(setId, zeroExData._buyAmt); + + return zeroExData; + } +} + +abstract contract ZeroEx is zeroExHelper { + /** + * @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 = ""; +} From d7cb32816f4c66742596b73995911d4db43e0cc9 Mon Sep 17 00:00:00 2001 From: bhavik Date: Mon, 6 Dec 2021 22:14:25 +0530 Subject: [PATCH 26/54] added support on polygon --- contracts/mainnet/connectors/0x/helpers.sol | 57 ++++++++++++++- contracts/mainnet/connectors/0x/interface.sol | 8 --- contracts/mainnet/connectors/0x/main.sol | 60 +--------------- contracts/polygon/connectors/0x/events.sol | 12 ++++ contracts/polygon/connectors/0x/helpers.sol | 70 +++++++++++++++++++ contracts/polygon/connectors/0x/interface.sol | 49 +++++++++++++ contracts/polygon/connectors/0x/main.sol | 63 +++++++++++++++++ 7 files changed, 252 insertions(+), 67 deletions(-) create mode 100644 contracts/polygon/connectors/0x/events.sol create mode 100644 contracts/polygon/connectors/0x/helpers.sol create mode 100644 contracts/polygon/connectors/0x/interface.sol create mode 100644 contracts/polygon/connectors/0x/main.sol diff --git a/contracts/mainnet/connectors/0x/helpers.sol b/contracts/mainnet/connectors/0x/helpers.sol index 7919436d..459d3abd 100644 --- a/contracts/mainnet/connectors/0x/helpers.sol +++ b/contracts/mainnet/connectors/0x/helpers.sol @@ -3,11 +3,66 @@ 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"; -abstract contract Helpers is DSMath, Basic { +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 { + address transformWallet = address( + zeroExInterface(zeroExAddr).getTransformWallet() + ); + approve( + TokenInterface(_sellAddr), + transformWallet, + zeroExData._sellAmt + ); + } + + zeroExData._buyAmt = _swapHelper(zeroExData, ethAmt); + setUint(setId, zeroExData._buyAmt); + + return zeroExData; + } } diff --git a/contracts/mainnet/connectors/0x/interface.sol b/contracts/mainnet/connectors/0x/interface.sol index a261afce..d6c7c57e 100644 --- a/contracts/mainnet/connectors/0x/interface.sol +++ b/contracts/mainnet/connectors/0x/interface.sol @@ -3,14 +3,6 @@ pragma solidity ^0.7.0; import {TokenInterface} from "../../common/interfaces.sol"; interface zeroExInterface { - // function transformERC20( - // IERC20TokenV06 inputToken, - // IERC20TokenV06 outputToken, - // uint256 inputTokenAmount, - // uint256 minOutputTokenAmount, - // Transformation[] calldata transformations - // ) external payable returns (uint256 outputTokenAmount); - function getTransformWallet() external view returns (IFlashWallet wallet); } diff --git a/contracts/mainnet/connectors/0x/main.sol b/contracts/mainnet/connectors/0x/main.sol index f55c496b..bd0e27b9 100644 --- a/contracts/mainnet/connectors/0x/main.sol +++ b/contracts/mainnet/connectors/0x/main.sol @@ -12,63 +12,7 @@ import {ZeroExData, zeroExInterface} from "./interface.sol"; import {Helpers} from "./helpers.sol"; import {Events} from "./events.sol"; -contract zeroExHelper is Helpers { - function zeroExSwap(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 { - address transformWallet = address( - zeroExInterface(zeroExAddr).getTransformWallet() - ); - approve( - TokenInterface(_sellAddr), - transformWallet, - zeroExData._sellAmt - ); - } - - zeroExData._buyAmt = zeroExSwap(zeroExData, ethAmt); - setUint(setId, zeroExData._buyAmt); - - return zeroExData; - } -} - -abstract contract ZeroEx is zeroExHelper { +abstract contract ZeroEx is Helpers { /** * @dev Sell ETH/ERC20_Token using 0x. * @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) @@ -115,5 +59,5 @@ abstract contract ZeroEx is zeroExHelper { } contract ConnectV2ZeroEx is ZeroEx { - string public name = ""; + string public name = "0x-V4"; } diff --git a/contracts/polygon/connectors/0x/events.sol b/contracts/polygon/connectors/0x/events.sol new file mode 100644 index 00000000..66f9943a --- /dev/null +++ b/contracts/polygon/connectors/0x/events.sol @@ -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 + ); +} diff --git a/contracts/polygon/connectors/0x/helpers.sol b/contracts/polygon/connectors/0x/helpers.sol new file mode 100644 index 00000000..835b8d7d --- /dev/null +++ b/contracts/polygon/connectors/0x/helpers.sol @@ -0,0 +1,70 @@ +pragma solidity ^0.7.0; + +import {TokenInterface} from "../../common/interfaces.sol"; +import {DSMath} from "../../common/math.sol"; +import {ZeroExData, zeroExInterface} 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 { + address transformWallet = address( + zeroExInterface(zeroExAddr).getTransformWallet() + ); + approve( + TokenInterface(_sellAddr), + transformWallet, + zeroExData._sellAmt + ); + } + + zeroExData._buyAmt = _swapHelper(zeroExData, maticAmt); + setUint(setId, zeroExData._buyAmt); + + return zeroExData; + } +} diff --git a/contracts/polygon/connectors/0x/interface.sol b/contracts/polygon/connectors/0x/interface.sol new file mode 100644 index 00000000..79952c90 --- /dev/null +++ b/contracts/polygon/connectors/0x/interface.sol @@ -0,0 +1,49 @@ +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; +} diff --git a/contracts/polygon/connectors/0x/main.sol b/contracts/polygon/connectors/0x/main.sol new file mode 100644 index 00000000..bd0e27b9 --- /dev/null +++ b/contracts/polygon/connectors/0x/main.sol @@ -0,0 +1,63 @@ +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 { + /** + * @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"; +} From 3d9deca4da2d296c4f95d6875292590d3d441e2e Mon Sep 17 00:00:00 2001 From: bhavik Date: Mon, 6 Dec 2021 23:21:03 +0530 Subject: [PATCH 27/54] added @notice comment --- contracts/mainnet/connectors/0x/main.sol | 1 + contracts/polygon/connectors/0x/main.sol | 1 + 2 files changed, 2 insertions(+) diff --git a/contracts/mainnet/connectors/0x/main.sol b/contracts/mainnet/connectors/0x/main.sol index bd0e27b9..72eb2220 100644 --- a/contracts/mainnet/connectors/0x/main.sol +++ b/contracts/mainnet/connectors/0x/main.sol @@ -14,6 +14,7 @@ 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) diff --git a/contracts/polygon/connectors/0x/main.sol b/contracts/polygon/connectors/0x/main.sol index bd0e27b9..72eb2220 100644 --- a/contracts/polygon/connectors/0x/main.sol +++ b/contracts/polygon/connectors/0x/main.sol @@ -14,6 +14,7 @@ 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) From 6dce9ff07ae79a7a13eb85c44931e3687b14dc16 Mon Sep 17 00:00:00 2001 From: bhavik Date: Fri, 10 Dec 2021 23:18:57 +0530 Subject: [PATCH 28/54] added test for 0x --- test/0x/api.js | 59 ++++++++++++++ test/0x/zeroEx.test.js | 173 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 232 insertions(+) create mode 100644 test/0x/api.js create mode 100644 test/0x/zeroEx.test.js diff --git a/test/0x/api.js b/test/0x/api.js new file mode 100644 index 00000000..30201ac5 --- /dev/null +++ b/test/0x/api.js @@ -0,0 +1,59 @@ +import axios from "axios"; +import { childLogger } from "./logger"; + +const logger = childLogger("utils/api"); + +function getApi() { + const api = axios.create({}); + + api.interceptors.request.use( + (request) => { + logger.info({ + msg: "Request to external API", + req: { + method: request.method, + url: request.url, + params: request.params, + headers: request.headers, + }, + }); + + return request; + }, + (error) => { + logger.error({ msg: "Error while requesting external API", error }); + + return Promise.reject(error); + } + ); + + api.interceptors.response.use( + (response) => { + logger.info({ + msg: "Response from external API", + req: { + method: response.config.method, + url: response.config.url, + params: response.config.params, + }, + res: { + statusCode: response.status, + statusText: response.statusText, + headers: response.config.headers, + data: response.data, + }, + }); + + return response; + }, + (error) => { + logger.error({ msg: "Error response from external API", error }); + + return Promise.reject(error); + } + ); + + return api; +} + +export default getApi(); diff --git a/test/0x/zeroEx.test.js b/test/0x/zeroEx.test.js new file mode 100644 index 00000000..fe1ffa03 --- /dev/null +++ b/test/0x/zeroEx.test.js @@ -0,0 +1,173 @@ +const { expect } = require("chai"); +const hre = require("hardhat"); +const axios = require("axios"); +const { web3, deployments, waffle, ethers } = hre; +const { provider, deployContract } = waffle; +const BigNumber = require("bignumber.js"); + +// import axpi from "./api"; + +const deployAndEnableConnector = require("../../scripts/deployAndEnableConnector.js"); +const buildDSAv2 = require("../../scripts/buildDSAv2"); +const encodeSpells = require("../../scripts/encodeSpells.js"); +const getMasterSigner = require("../../scripts/getMasterSigner"); +const addLiquidity = require("../../scripts/addLiquidity"); + +const addresses = require("../../scripts/constant/addresses"); +const abis = require("../../scripts/constant/abis"); +const constants = require("../../scripts/constant/constant"); +const tokens = require("../../scripts/constant/tokens"); + +const connectV2CompoundArtifacts = require("../../artifacts/contracts/mainnet/connectors/0x/main.sol/ConnectV2ZeroEx.json"); + +describe("ZeroEx", function() { + const connectorName = "zeroEx-test"; + + let dsaWallet0; + let masterSigner; + let instaConnectorsV2; + let connector; + + const wallets = provider.getWallets(); + const [wallet0, wallet1, wallet2, wallet3] = wallets; + + before(async () => { + // await hre.network.provider.request({ + // method: "hardhat_reset", + // params: [ + // { + // forking: { + // jsonRpcUrl: hre.config.networks.hardhat.forking.url, + // blockNumber: 13300000, + // }, + // }, + // ], + // }); + masterSigner = await getMasterSigner(wallet3); + instaConnectorsV2 = await ethers.getContractAt( + abis.core.connectorsV2, + addresses.core.connectorsV2 + ); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: connectV2CompoundArtifacts, + 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 and DAI 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") + ); + + // await addLiquidity( + // "dai", + // dsaWallet0.address, + // ethers.utils.parseUnits("100000", 18) + // ); + // expect(await ether s.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseUnits("100000", 18)); + }); + }); + + describe("Main", function() { + it("should swap", async function() { + async function getArg() { + const slippage = 0.5; + + /* MATIC -> USDT */ + const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // Matic, decimals 18 + const sellTokenDecimals = 18; + const buyTokenAddress = "0xc2132D05D31c914a87C6611C10748AEb04B58e8F"; // USDT, decimals 6 + const buyTokenDecimals = 6; + const amount = 1; + + const srcAmount = new BigNumber(amount) + .times(new BigNumber(10).pow(sellTokenDecimals)) + .toFixed(0); + console.log(srcAmount); + + 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 buyTokenAmount = await axios + .get(url, { params: params }) + .then((data) => data.data.buyAmount); + + const calldata = await axios + .get(url, { params: params }) + .then((data) => data.data.data); + + console.log(calldata); + + let caculateUnitAmt = () => { + const buyTokenAmountRes = new BigNumber(buyTokenAmount) + .dividedBy(new BigNumber(10).pow(buyTokenDecimals)) + .toFixed(8); + + let unitAmt = 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(); + + console.log("unitAmt - " + unitAmt); + + 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(); + console.log(receipt); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( + ethers.utils.parseEther("9") + ); + }); + }); +}); From ec48612d22e3029b13c4f44c9f95649da3122bba Mon Sep 17 00:00:00 2001 From: bhavik Date: Fri, 10 Dec 2021 23:20:43 +0530 Subject: [PATCH 29/54] added test --- test/0x/api.js | 59 ------------------------------------------ test/0x/zeroEx.test.js | 2 -- 2 files changed, 61 deletions(-) delete mode 100644 test/0x/api.js diff --git a/test/0x/api.js b/test/0x/api.js deleted file mode 100644 index 30201ac5..00000000 --- a/test/0x/api.js +++ /dev/null @@ -1,59 +0,0 @@ -import axios from "axios"; -import { childLogger } from "./logger"; - -const logger = childLogger("utils/api"); - -function getApi() { - const api = axios.create({}); - - api.interceptors.request.use( - (request) => { - logger.info({ - msg: "Request to external API", - req: { - method: request.method, - url: request.url, - params: request.params, - headers: request.headers, - }, - }); - - return request; - }, - (error) => { - logger.error({ msg: "Error while requesting external API", error }); - - return Promise.reject(error); - } - ); - - api.interceptors.response.use( - (response) => { - logger.info({ - msg: "Response from external API", - req: { - method: response.config.method, - url: response.config.url, - params: response.config.params, - }, - res: { - statusCode: response.status, - statusText: response.statusText, - headers: response.config.headers, - data: response.data, - }, - }); - - return response; - }, - (error) => { - logger.error({ msg: "Error response from external API", error }); - - return Promise.reject(error); - } - ); - - return api; -} - -export default getApi(); diff --git a/test/0x/zeroEx.test.js b/test/0x/zeroEx.test.js index fe1ffa03..804cde5b 100644 --- a/test/0x/zeroEx.test.js +++ b/test/0x/zeroEx.test.js @@ -5,8 +5,6 @@ const { web3, deployments, waffle, ethers } = hre; const { provider, deployContract } = waffle; const BigNumber = require("bignumber.js"); -// import axpi from "./api"; - const deployAndEnableConnector = require("../../scripts/deployAndEnableConnector.js"); const buildDSAv2 = require("../../scripts/buildDSAv2"); const encodeSpells = require("../../scripts/encodeSpells.js"); From a26235672048b92f032df773fdfb6a05a6da9774 Mon Sep 17 00:00:00 2001 From: bhavik Date: Sat, 11 Dec 2021 00:14:32 +0530 Subject: [PATCH 30/54] fixed typo --- test/0x/zeroEx.test.js | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/test/0x/zeroEx.test.js b/test/0x/zeroEx.test.js index 804cde5b..820e6f74 100644 --- a/test/0x/zeroEx.test.js +++ b/test/0x/zeroEx.test.js @@ -30,17 +30,17 @@ describe("ZeroEx", function() { const [wallet0, wallet1, wallet2, wallet3] = wallets; before(async () => { - // await hre.network.provider.request({ - // method: "hardhat_reset", - // params: [ - // { - // forking: { - // jsonRpcUrl: hre.config.networks.hardhat.forking.url, - // blockNumber: 13300000, - // }, - // }, - // ], - // }); + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 13300000, + }, + }, + ], + }); masterSigner = await getMasterSigner(wallet3); instaConnectorsV2 = await ethers.getContractAt( abis.core.connectorsV2, @@ -67,7 +67,7 @@ describe("ZeroEx", function() { expect(!!dsaWallet0.address).to.be.true; }); - it("Deposit ETH and DAI into DSA wallet", async function() { + it("Deposit ETH into DSA wallet", async function() { await wallet0.sendTransaction({ to: dsaWallet0.address, value: ethers.utils.parseEther("10"), @@ -76,13 +76,6 @@ describe("ZeroEx", function() { expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( ethers.utils.parseEther("10") ); - - // await addLiquidity( - // "dai", - // dsaWallet0.address, - // ethers.utils.parseUnits("100000", 18) - // ); - // expect(await ether s.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseUnits("100000", 18)); }); }); @@ -91,11 +84,11 @@ describe("ZeroEx", function() { async function getArg() { const slippage = 0.5; - /* MATIC -> USDT */ - const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // Matic, decimals 18 + /* Eth -> dai */ + const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // eth, decimals 18 const sellTokenDecimals = 18; - const buyTokenAddress = "0xc2132D05D31c914a87C6611C10748AEb04B58e8F"; // USDT, decimals 6 - const buyTokenDecimals = 6; + const buyTokenAddress = "0x6b175474e89094c44da98b954eedeac495271d0f"; // dai, decimals 18 + const buyTokenDecimals = 18; const amount = 1; const srcAmount = new BigNumber(amount) From 34a7ebe4ae2fd08ca4605d374ae0c8a368e2cbc0 Mon Sep 17 00:00:00 2001 From: bhavik Date: Sat, 11 Dec 2021 19:48:50 +0530 Subject: [PATCH 31/54] updated-test --- test/mainnet/0x/zeroEx.test.ts | 165 +++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 test/mainnet/0x/zeroEx.test.ts diff --git a/test/mainnet/0x/zeroEx.test.ts b/test/mainnet/0x/zeroEx.test.ts new file mode 100644 index 00000000..557fe460 --- /dev/null +++ b/test/mainnet/0x/zeroEx.test.ts @@ -0,0 +1,165 @@ +import { expect } from "chai"; +import hre from "hardhat"; +const { web3, deployments, waffle, ethers } = hre; //check +const { provider, deployContract } = waffle +import axios from "axios"; +import { BigNumber } from "bignumber.js"; +import { ConnectV2ZeroEx, ConnectV2ZeroEx__factory } from "../../../typechain"; + +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 { addLiquidity } from "../../../scripts/tests/addLiquidity"; + +import { addresses } from "../../../scripts/tests/mainnet/addresses"; +import { abis } from "../../../scripts/constant/abis"; +import { tokens } from "../../../scripts/tests/mainnet/tokens"; +import { constants } from "../../../scripts/constant/constant"; + + +describe("ZeroEx", function() { + const connectorName = "zeroEx-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.forking.url, + blockNumber: 13300000, + }, + }, + ], + }); + 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(!!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"), + }); + console.log(dsaWallet0.address); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( + ethers.utils.parseEther("10") + ); + }); + }); + + describe("Main", function() { + it("should swap", async function() { + 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); + console.log(srcAmount); + + 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 buyTokenAmount = await axios + .get(url, { params: params }) + .then((data:any) => data.data.buyAmount); + + const calldata = await axios + .get(url, { params: params }) + .then((data:any) => data.data.data); + + console.log(calldata); + + 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(); + + console.log("unitAmt - " + unitAmt); + + 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(); + console.log(receipt); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( + ethers.utils.parseEther("9") + ); + }); + }); +}); From f178550bff8ac9dd60c0e64a0530c47de078708f Mon Sep 17 00:00:00 2001 From: bhavik Date: Sun, 12 Dec 2021 00:20:22 +0530 Subject: [PATCH 33/54] added-test --- test/mainnet/0x/zeroEx.test.ts | 71 ++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 30 deletions(-) diff --git a/test/mainnet/0x/zeroEx.test.ts b/test/mainnet/0x/zeroEx.test.ts index 557fe460..8a04f890 100644 --- a/test/mainnet/0x/zeroEx.test.ts +++ b/test/mainnet/0x/zeroEx.test.ts @@ -1,7 +1,7 @@ import { expect } from "chai"; import hre from "hardhat"; const { web3, deployments, waffle, ethers } = hre; //check -const { provider, deployContract } = waffle +const { provider, deployContract } = waffle; import axios from "axios"; import { BigNumber } from "bignumber.js"; import { ConnectV2ZeroEx, ConnectV2ZeroEx__factory } from "../../../typechain"; @@ -17,31 +17,32 @@ import { abis } from "../../../scripts/constant/abis"; import { tokens } from "../../../scripts/tests/mainnet/tokens"; import { constants } from "../../../scripts/constant/constant"; +import er20abi from "../../../scripts/constant/abi/basics/erc20.json"; describe("ZeroEx", function() { const connectorName = "zeroEx-test"; - let dsaWallet0 : any; - let masterSigner:any; - let instaConnectorsV2 : any; - let connector:any; + 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.forking.url, - blockNumber: 13300000, - }, - }, - ], - }); + // await hre.network.provider.request({ + // method: "hardhat_reset", + // params: [ + // { + // forking: { + // // @ts-ignore + // jsonRpcUrl: hre.config.networks.forking.url, + // blockNumber: 13300000, + // }, + // }, + // ], + // }); masterSigner = await getMasterSigner(); instaConnectorsV2 = await ethers.getContractAt( abis.core.connectorsV2, @@ -53,7 +54,7 @@ describe("ZeroEx", function() { signer: masterSigner, connectors: instaConnectorsV2, }); - console.log("Connector address", connector.address); + // console.log("Connector address", connector.address); }); it("Should have contracts deployed.", async function() { @@ -73,7 +74,7 @@ describe("ZeroEx", function() { to: dsaWallet0.address, value: ethers.utils.parseEther("10"), }); - console.log(dsaWallet0.address); + // console.log(dsaWallet0.address); expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( ethers.utils.parseEther("10") ); @@ -81,9 +82,10 @@ describe("ZeroEx", function() { }); describe("Main", function() { - it("should swap", async function() { + it("should swap the tokens", async function() { + let buyTokenAmount: any; async function getArg() { - const slippage = 0.5; + // const slippage = 0.5; /* Eth -> dai */ const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // eth, decimals 18 @@ -95,7 +97,7 @@ describe("ZeroEx", function() { const srcAmount = new BigNumber(amount) .times(new BigNumber(10).pow(sellTokenDecimals)) .toFixed(0); - console.log(srcAmount); + // console.log(srcAmount); const fromAddress = dsaWallet0.address; @@ -107,15 +109,15 @@ describe("ZeroEx", function() { sellAmount: "1000000000000000000", // Always denominated in wei }; - const buyTokenAmount = await axios + const response = await axios .get(url, { params: params }) - .then((data:any) => data.data.buyAmount); + .then((data: any) => data); - const calldata = await axios - .get(url, { params: params }) - .then((data:any) => data.data.data); + buyTokenAmount = response.data.buyAmount; + const calldata = response.data.data; - console.log(calldata); + // console.log("calldata ", calldata); + // console.log("buyTokenAmount ", buyTokenAmount); let caculateUnitAmt = () => { const buyTokenAmountRes = new BigNumber(buyTokenAmount) @@ -132,7 +134,7 @@ describe("ZeroEx", function() { }; let unitAmt = caculateUnitAmt(); - console.log("unitAmt - " + unitAmt); + // console.log("unitAmt - " + unitAmt); return [ buyTokenAddress, @@ -156,7 +158,16 @@ describe("ZeroEx", function() { .connect(wallet0) .cast(...encodeSpells(spells), wallet1.address); const receipt = await tx.wait(); - console.log(receipt); + // console.log(receipt); + + const idai = await ethers.getContractAt( + er20abi, + "0x6b175474e89094c44da98b954eedeac495271d0f" // dai address + ); + + expect(await idai.balanceOf(dsaWallet0.address)).to.be.gte( + buyTokenAmount + ); expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( ethers.utils.parseEther("9") ); From 718bd8a830b1fa4785627f42b7ad3e305de9008e Mon Sep 17 00:00:00 2001 From: bhavik Date: Sun, 12 Dec 2021 15:45:25 +0530 Subject: [PATCH 34/54] updated-test --- test/mainnet/0x/zeroEx.test.ts | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/test/mainnet/0x/zeroEx.test.ts b/test/mainnet/0x/zeroEx.test.ts index 8a04f890..891e8e15 100644 --- a/test/mainnet/0x/zeroEx.test.ts +++ b/test/mainnet/0x/zeroEx.test.ts @@ -31,17 +31,17 @@ describe("ZeroEx", function() { const [wallet0, wallet1, wallet2, wallet3] = wallets; before(async () => { - // await hre.network.provider.request({ - // method: "hardhat_reset", - // params: [ - // { - // forking: { - // // @ts-ignore - // jsonRpcUrl: hre.config.networks.forking.url, - // blockNumber: 13300000, + // await hre.network.provider.request({ + // method: "hardhat_reset", + // params: [ + // { + // forking: { + // // @ts-ignore + // jsonRpcUrl: hre.config.networks.forking.url, + // blockNumber: 13300000, + // }, // }, - // }, - // ], + // ], // }); masterSigner = await getMasterSigner(); instaConnectorsV2 = await ethers.getContractAt( @@ -78,6 +78,10 @@ describe("ZeroEx", function() { expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( ethers.utils.parseEther("10") ); + const idai = await ethers.getContractAt( + er20abi, + "0x6b175474e89094c44da98b954eedeac495271d0f" // dai address + ); }); }); @@ -165,9 +169,6 @@ describe("ZeroEx", function() { "0x6b175474e89094c44da98b954eedeac495271d0f" // dai address ); - expect(await idai.balanceOf(dsaWallet0.address)).to.be.gte( - buyTokenAmount - ); expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( ethers.utils.parseEther("9") ); From 46b4bf81343de411c7ac8aa5bc4f0b1ef819283f Mon Sep 17 00:00:00 2001 From: bhavik Date: Sun, 12 Dec 2021 15:52:09 +0530 Subject: [PATCH 35/54] fixed minor issue --- test/0x/zeroEx.test.js | 164 --------------------------------- test/mainnet/0x/zeroEx.test.ts | 3 + 2 files changed, 3 insertions(+), 164 deletions(-) delete mode 100644 test/0x/zeroEx.test.js diff --git a/test/0x/zeroEx.test.js b/test/0x/zeroEx.test.js deleted file mode 100644 index 820e6f74..00000000 --- a/test/0x/zeroEx.test.js +++ /dev/null @@ -1,164 +0,0 @@ -const { expect } = require("chai"); -const hre = require("hardhat"); -const axios = require("axios"); -const { web3, deployments, waffle, ethers } = hre; -const { provider, deployContract } = waffle; -const BigNumber = require("bignumber.js"); - -const deployAndEnableConnector = require("../../scripts/deployAndEnableConnector.js"); -const buildDSAv2 = require("../../scripts/buildDSAv2"); -const encodeSpells = require("../../scripts/encodeSpells.js"); -const getMasterSigner = require("../../scripts/getMasterSigner"); -const addLiquidity = require("../../scripts/addLiquidity"); - -const addresses = require("../../scripts/constant/addresses"); -const abis = require("../../scripts/constant/abis"); -const constants = require("../../scripts/constant/constant"); -const tokens = require("../../scripts/constant/tokens"); - -const connectV2CompoundArtifacts = require("../../artifacts/contracts/mainnet/connectors/0x/main.sol/ConnectV2ZeroEx.json"); - -describe("ZeroEx", function() { - const connectorName = "zeroEx-test"; - - let dsaWallet0; - let masterSigner; - let instaConnectorsV2; - let connector; - - const wallets = provider.getWallets(); - const [wallet0, wallet1, wallet2, wallet3] = wallets; - - before(async () => { - await hre.network.provider.request({ - method: "hardhat_reset", - params: [ - { - forking: { - jsonRpcUrl: hre.config.networks.hardhat.forking.url, - blockNumber: 13300000, - }, - }, - ], - }); - masterSigner = await getMasterSigner(wallet3); - instaConnectorsV2 = await ethers.getContractAt( - abis.core.connectorsV2, - addresses.core.connectorsV2 - ); - connector = await deployAndEnableConnector({ - connectorName, - contractArtifact: connectV2CompoundArtifacts, - 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"), - }); - console.log(dsaWallet0.address); - expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( - ethers.utils.parseEther("10") - ); - }); - }); - - describe("Main", function() { - it("should swap", async function() { - 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); - console.log(srcAmount); - - 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 buyTokenAmount = await axios - .get(url, { params: params }) - .then((data) => data.data.buyAmount); - - const calldata = await axios - .get(url, { params: params }) - .then((data) => data.data.data); - - console.log(calldata); - - let caculateUnitAmt = () => { - const buyTokenAmountRes = new BigNumber(buyTokenAmount) - .dividedBy(new BigNumber(10).pow(buyTokenDecimals)) - .toFixed(8); - - let unitAmt = 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(); - - console.log("unitAmt - " + unitAmt); - - 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(); - console.log(receipt); - expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( - ethers.utils.parseEther("9") - ); - }); - }); -}); diff --git a/test/mainnet/0x/zeroEx.test.ts b/test/mainnet/0x/zeroEx.test.ts index 891e8e15..6700524b 100644 --- a/test/mainnet/0x/zeroEx.test.ts +++ b/test/mainnet/0x/zeroEx.test.ts @@ -169,6 +169,9 @@ describe("ZeroEx", function() { "0x6b175474e89094c44da98b954eedeac495271d0f" // dai address ); + expect(await idai.balanceOf(dsaWallet0.address)).to.be.gte( + buyTokenAmount + ); expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( ethers.utils.parseEther("9") ); From 306b3e25d39261e13f04f72a67d9795a71970ec8 Mon Sep 17 00:00:00 2001 From: bhavik Date: Sun, 12 Dec 2021 18:26:27 +0530 Subject: [PATCH 36/54] added test on polygon --- contracts/polygon/connectors/0x/main.sol | 2 +- test/polygon/0x/zeroEx.test.ts | 177 +++++++++++++++++++++++ 2 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 test/polygon/0x/zeroEx.test.ts diff --git a/contracts/polygon/connectors/0x/main.sol b/contracts/polygon/connectors/0x/main.sol index 72eb2220..93d484df 100644 --- a/contracts/polygon/connectors/0x/main.sol +++ b/contracts/polygon/connectors/0x/main.sol @@ -59,6 +59,6 @@ abstract contract ZeroEx is Helpers { } } -contract ConnectV2ZeroEx is ZeroEx { +contract ConnectV2ZeroExPolygon is ZeroEx { string public name = "0x-V4"; } diff --git a/test/polygon/0x/zeroEx.test.ts b/test/polygon/0x/zeroEx.test.ts new file mode 100644 index 00000000..8159b3be --- /dev/null +++ b/test/polygon/0x/zeroEx.test.ts @@ -0,0 +1,177 @@ +import { expect } from "chai"; +import hre from "hardhat"; +const { web3, deployments, waffle, ethers } = hre; //check +const { provider, deployContract } = waffle; +import axios from "axios"; +import { BigNumber } from "bignumber.js"; +import { + ConnectV2ZeroExPolygon, + ConnectV2ZeroExPolygon__factory, +} from "../../../typechain"; + +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 { addLiquidity } from "../../../scripts/tests/addLiquidity"; + +import { addresses } from "../../../scripts/tests/polygon/addresses"; +import { abis } from "../../../scripts/constant/abis"; + +import er20abi from "../../../scripts/constant/abi/basics/erc20.json"; + +describe("ZeroEx", function() { + const connectorName = "zeroEx-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.forking.url, + // blockNumber: 13300000, + // }, + // }, + // ], + // }); + 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(!!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 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); + // console.log(srcAmount); + + const fromAddress = dsaWallet0.address; + + 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; + + // console.log("calldata ", calldata); + // console.log("buyTokenAmount ", buyTokenAmount); + + 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(); + + // console.log("unitAmt - " + unitAmt); + + 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(); + // console.log(receipt); + + const idai = await ethers.getContractAt( + er20abi, + "0x6b175474e89094c44da98b954eedeac495271d0f" // dai address + ); + + // expect(await idai.balanceOf(dsaWallet0.address)).to.be.gte( + // buyTokenAmount + // ); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( + ethers.utils.parseEther("9") + ); + }); + }); +}); From fcb7893f66e7ddfd4f2969cd4cd7d57903563fee Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Sun, 12 Dec 2021 18:51:58 +0530 Subject: [PATCH 37/54] fixed mainnet 0x test --- test/mainnet/0x/zeroEx.test.ts | 56 +++++++++++++++------------------- 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/test/mainnet/0x/zeroEx.test.ts b/test/mainnet/0x/zeroEx.test.ts index 6700524b..ec498bf6 100644 --- a/test/mainnet/0x/zeroEx.test.ts +++ b/test/mainnet/0x/zeroEx.test.ts @@ -1,48 +1,42 @@ -import { expect } from "chai"; import hre from "hardhat"; -const { web3, deployments, waffle, ethers } = hre; //check -const { provider, deployContract } = waffle; import axios from "axios"; +import { expect } from "chai"; +const { ethers } = hre; //check import { BigNumber } from "bignumber.js"; -import { ConnectV2ZeroEx, ConnectV2ZeroEx__factory } from "../../../typechain"; - 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 { addLiquidity } from "../../../scripts/tests/addLiquidity"; - import { addresses } from "../../../scripts/tests/mainnet/addresses"; import { abis } from "../../../scripts/constant/abis"; -import { tokens } from "../../../scripts/tests/mainnet/tokens"; -import { constants } from "../../../scripts/constant/constant"; - +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: any; - let masterSigner: any; + let dsaWallet0: Contract; + let wallet0: Signer, wallet1: Signer; + let masterSigner: Signer; 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.forking.url, - // blockNumber: 13300000, - // }, - // }, - // ], - // }); + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + // @ts-ignore + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 12796965, + }, + }, + ], + }); + [wallet0, wallet1] = await ethers.getSigners(); + masterSigner = await getMasterSigner(); instaConnectorsV2 = await ethers.getContractAt( abis.core.connectorsV2, @@ -54,18 +48,18 @@ describe("ZeroEx", function() { signer: masterSigner, connectors: instaConnectorsV2, }); - // console.log("Connector address", connector.address); + 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; + expect(!!(await masterSigner.getAddress())).to.be.true; }); describe("DSA wallet setup", function() { it("Should build DSA v2", async function() { - dsaWallet0 = await buildDSAv2(wallet0.address); + dsaWallet0 = await buildDSAv2(wallet0.getAddress()); expect(!!dsaWallet0.address).to.be.true; }); @@ -160,7 +154,7 @@ describe("ZeroEx", function() { ]; const tx = await dsaWallet0 .connect(wallet0) - .cast(...encodeSpells(spells), wallet1.address); + .cast(...encodeSpells(spells), wallet1.getAddress()); const receipt = await tx.wait(); // console.log(receipt); From c2b7132ab34c8c5bfa865d40971d5f5d9250559b Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Sun, 12 Dec 2021 19:01:49 +0530 Subject: [PATCH 38/54] fixes --- test/mainnet/0x/zeroEx.test.ts | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/test/mainnet/0x/zeroEx.test.ts b/test/mainnet/0x/zeroEx.test.ts index ec498bf6..f933a789 100644 --- a/test/mainnet/0x/zeroEx.test.ts +++ b/test/mainnet/0x/zeroEx.test.ts @@ -19,8 +19,8 @@ describe("ZeroEx", function() { let dsaWallet0: Contract; let wallet0: Signer, wallet1: Signer; let masterSigner: Signer; - let instaConnectorsV2: any; - let connector: any; + let instaConnectorsV2: Contract; + let connector: Contract; before(async () => { await hre.network.provider.request({ @@ -30,7 +30,6 @@ describe("ZeroEx", function() { forking: { // @ts-ignore jsonRpcUrl: hre.config.networks.hardhat.forking.url, - blockNumber: 12796965, }, }, ], @@ -84,7 +83,6 @@ describe("ZeroEx", function() { let buyTokenAmount: any; async function getArg() { // const slippage = 0.5; - /* Eth -> dai */ const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // eth, decimals 18 const sellTokenDecimals = 18; @@ -95,7 +93,6 @@ describe("ZeroEx", function() { const srcAmount = new BigNumber(amount) .times(new BigNumber(10).pow(sellTokenDecimals)) .toFixed(0); - // console.log(srcAmount); const fromAddress = dsaWallet0.address; @@ -114,9 +111,6 @@ describe("ZeroEx", function() { buyTokenAmount = response.data.buyAmount; const calldata = response.data.data; - // console.log("calldata ", calldata); - // console.log("buyTokenAmount ", buyTokenAmount); - let caculateUnitAmt = () => { const buyTokenAmountRes = new BigNumber(buyTokenAmount) .dividedBy(new BigNumber(10).pow(buyTokenDecimals)) @@ -132,8 +126,6 @@ describe("ZeroEx", function() { }; let unitAmt = caculateUnitAmt(); - // console.log("unitAmt - " + unitAmt); - return [ buyTokenAddress, sellTokenAddress, @@ -156,14 +148,13 @@ describe("ZeroEx", function() { .connect(wallet0) .cast(...encodeSpells(spells), wallet1.getAddress()); const receipt = await tx.wait(); - // console.log(receipt); - const idai = await ethers.getContractAt( + const daiToken = await ethers.getContractAt( er20abi, "0x6b175474e89094c44da98b954eedeac495271d0f" // dai address ); - expect(await idai.balanceOf(dsaWallet0.address)).to.be.gte( + expect(await daiToken.balanceOf(dsaWallet0.address)).to.be.gte( buyTokenAmount ); expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( From 17aea053f6d137ad3a31114ab73957c4bc18e89d Mon Sep 17 00:00:00 2001 From: bhavik Date: Sun, 12 Dec 2021 19:15:11 +0530 Subject: [PATCH 39/54] updated polygon test --- test/polygon/0x/zeroEx.test.ts | 48 ++++++++++++---------------------- 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/test/polygon/0x/zeroEx.test.ts b/test/polygon/0x/zeroEx.test.ts index 8159b3be..58683855 100644 --- a/test/polygon/0x/zeroEx.test.ts +++ b/test/polygon/0x/zeroEx.test.ts @@ -1,36 +1,27 @@ -import { expect } from "chai"; import hre from "hardhat"; -const { web3, deployments, waffle, ethers } = hre; //check -const { provider, deployContract } = waffle; import axios from "axios"; +import { expect } from "chai"; +const { ethers } = hre; //check import { BigNumber } from "bignumber.js"; -import { - ConnectV2ZeroExPolygon, - ConnectV2ZeroExPolygon__factory, -} from "../../../typechain"; - 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 { addLiquidity } from "../../../scripts/tests/addLiquidity"; - -import { addresses } from "../../../scripts/tests/polygon/addresses"; +import { addresses } from "../../../scripts/tests/mainnet/addresses"; import { abis } from "../../../scripts/constant/abis"; - +import { ConnectV2ZeroExPolygon, 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: any; - let masterSigner: any; + let dsaWallet0: Contract; + let wallet0: Signer, wallet1: Signer; + let masterSigner: Signer; 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", @@ -55,18 +46,18 @@ describe("ZeroEx", function() { signer: masterSigner, connectors: instaConnectorsV2, }); - // console.log("Connector address", connector.address); + 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; + expect(!!masterSigner.getAddress()).to.be.true; }); describe("DSA wallet setup", function() { it("Should build DSA v2", async function() { - dsaWallet0 = await buildDSAv2(wallet0.address); + dsaWallet0 = await buildDSAv2(wallet0.getAddress()); expect(!!dsaWallet0.address).to.be.true; }); @@ -98,7 +89,6 @@ describe("ZeroEx", function() { const srcAmount = new BigNumber(amount) .times(new BigNumber(10).pow(sellTokenDecimals)) .toFixed(0); - // console.log(srcAmount); const fromAddress = dsaWallet0.address; @@ -117,9 +107,6 @@ describe("ZeroEx", function() { buyTokenAmount = response.data.buyAmount; const calldata = response.data.data; - // console.log("calldata ", calldata); - // console.log("buyTokenAmount ", buyTokenAmount); - let caculateUnitAmt = () => { const buyTokenAmountRes = new BigNumber(buyTokenAmount) .dividedBy(new BigNumber(10).pow(buyTokenDecimals)) @@ -135,7 +122,6 @@ describe("ZeroEx", function() { }; let unitAmt = caculateUnitAmt(); - // console.log("unitAmt - " + unitAmt); return [ buyTokenAddress, @@ -157,18 +143,18 @@ describe("ZeroEx", function() { ]; const tx = await dsaWallet0 .connect(wallet0) - .cast(...encodeSpells(spells), wallet1.address); + .cast(...encodeSpells(spells), wallet1.getAddress()); const receipt = await tx.wait(); - // console.log(receipt); + const idai = await ethers.getContractAt( er20abi, - "0x6b175474e89094c44da98b954eedeac495271d0f" // dai address + "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063" // dai address ); - // expect(await idai.balanceOf(dsaWallet0.address)).to.be.gte( - // buyTokenAmount - // ); + expect(await idai.balanceOf(dsaWallet0.address)).to.be.gte( + buyTokenAmount + ); expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( ethers.utils.parseEther("9") ); From 724307243d35ca33915fc0faa2e2608b8183534f Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Sun, 12 Dec 2021 19:41:43 +0530 Subject: [PATCH 41/54] updated tests --- test/mainnet/0x/zeroEx.test.ts | 4 +-- test/polygon/0x/zeroEx.test.ts | 50 ++++++++++++++++------------------ 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/test/mainnet/0x/zeroEx.test.ts b/test/mainnet/0x/zeroEx.test.ts index f933a789..c363ef7e 100644 --- a/test/mainnet/0x/zeroEx.test.ts +++ b/test/mainnet/0x/zeroEx.test.ts @@ -58,7 +58,7 @@ describe("ZeroEx", function() { describe("DSA wallet setup", function() { it("Should build DSA v2", async function() { - dsaWallet0 = await buildDSAv2(wallet0.getAddress()); + dsaWallet0 = await buildDSAv2(await wallet0.getAddress()); expect(!!dsaWallet0.address).to.be.true; }); @@ -146,7 +146,7 @@ describe("ZeroEx", function() { ]; const tx = await dsaWallet0 .connect(wallet0) - .cast(...encodeSpells(spells), wallet1.getAddress()); + .cast(...encodeSpells(spells), await wallet1.getAddress()); const receipt = await tx.wait(); const daiToken = await ethers.getContractAt( diff --git a/test/polygon/0x/zeroEx.test.ts b/test/polygon/0x/zeroEx.test.ts index 58683855..2ef4d5bb 100644 --- a/test/polygon/0x/zeroEx.test.ts +++ b/test/polygon/0x/zeroEx.test.ts @@ -7,7 +7,7 @@ import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnable 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 { addresses } from "../../../scripts/tests/polygon/addresses"; import { abis } from "../../../scripts/constant/abis"; import { ConnectV2ZeroExPolygon, ConnectV2ZeroExPolygon__factory } from "../../../typechain"; import er20abi from "../../../scripts/constant/abi/basics/erc20.json"; @@ -19,22 +19,23 @@ describe("ZeroEx", function() { let dsaWallet0: Contract; let wallet0: Signer, wallet1: Signer; let masterSigner: Signer; - let instaConnectorsV2: any; - let connector: any; + let instaConnectorsV2: Contract; + let connector: Contract; before(async () => { - // await hre.network.provider.request({ - // method: "hardhat_reset", - // params: [ - // { - // forking: { - // // @ts-ignore - // jsonRpcUrl: hre.config.networks.forking.url, - // blockNumber: 13300000, - // }, - // }, - // ], - // }); + 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, @@ -52,12 +53,12 @@ describe("ZeroEx", function() { it("Should have contracts deployed.", async function() { expect(!!instaConnectorsV2.address).to.be.true; expect(!!connector.address).to.be.true; - expect(!!masterSigner.getAddress()).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(wallet0.getAddress()); + dsaWallet0 = await buildDSAv2(await wallet0.getAddress()); expect(!!dsaWallet0.address).to.be.true; }); @@ -78,7 +79,6 @@ describe("ZeroEx", function() { let buyTokenAmount: any; async function getArg() { // const slippage = 0.5; - /* matic -> dai */ const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // matic, decimals 18 const sellTokenDecimals = 18; @@ -90,8 +90,6 @@ describe("ZeroEx", function() { .times(new BigNumber(10).pow(sellTokenDecimals)) .toFixed(0); - const fromAddress = dsaWallet0.address; - let url = `https://polygon.api.0x.org/swap/v1/quote`; const params = { @@ -122,7 +120,6 @@ describe("ZeroEx", function() { }; let unitAmt = caculateUnitAmt(); - return [ buyTokenAddress, sellTokenAddress, @@ -143,18 +140,17 @@ describe("ZeroEx", function() { ]; const tx = await dsaWallet0 .connect(wallet0) - .cast(...encodeSpells(spells), wallet1.getAddress()); + .cast(...encodeSpells(spells), await wallet1.getAddress()); const receipt = await tx.wait(); - - const idai = await ethers.getContractAt( + const daiToken = await ethers.getContractAt( er20abi, "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063" // dai address ); - expect(await idai.balanceOf(dsaWallet0.address)).to.be.gte( - buyTokenAmount - ); + expect(await daiToken.balanceOf(dsaWallet0.address)).to.be.gte( + buyTokenAmount + ); expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( ethers.utils.parseEther("9") ); From a5a38165675dfefce9b508e3b5982a23b4c70946 Mon Sep 17 00:00:00 2001 From: bhavik Date: Sun, 12 Dec 2021 20:12:31 +0530 Subject: [PATCH 42/54] updated instaindex address on avalanche --- scripts/tests/avalanche/addresses.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/tests/avalanche/addresses.ts b/scripts/tests/avalanche/addresses.ts index d2da378f..d92a5bf7 100644 --- a/scripts/tests/avalanche/addresses.ts +++ b/scripts/tests/avalanche/addresses.ts @@ -5,7 +5,7 @@ export const addresses: Record = { // "INSTAPOOL-A": "0x8f1e38c53af7bD2b2bE01b9580911b7Cca504F1b", }, core: { - connectorsV2: "0x67fCE99Dd6d8d659eea2a1ac1b8881c57eb6592B", - instaIndex: "0x1eE00C305C51Ff3bE60162456A9B533C07cD9288", + connectorsV2: "0x127d8cD0E2b2E0366D522DeA53A787bfE9002C14", + instaIndex: " 0x6CE3e607C808b4f4C26B7F6aDAeB619e49CAbb25", }, }; From 0696118a510d3bcd70dbadc99e373503f17563e8 Mon Sep 17 00:00:00 2001 From: bhavik Date: Sun, 12 Dec 2021 20:20:43 +0530 Subject: [PATCH 43/54] added support on avalache --- contracts/avalanche/connectors/0x/events.sol | 12 ++++ contracts/avalanche/connectors/0x/helpers.sol | 68 +++++++++++++++++++ .../avalanche/connectors/0x/interface.sol | 46 +++++++++++++ contracts/avalanche/connectors/0x/main.sol | 64 +++++++++++++++++ contracts/polygon/connectors/0x/main.sol | 6 +- 5 files changed, 193 insertions(+), 3 deletions(-) create mode 100644 contracts/avalanche/connectors/0x/events.sol create mode 100644 contracts/avalanche/connectors/0x/helpers.sol create mode 100644 contracts/avalanche/connectors/0x/interface.sol create mode 100644 contracts/avalanche/connectors/0x/main.sol diff --git a/contracts/avalanche/connectors/0x/events.sol b/contracts/avalanche/connectors/0x/events.sol new file mode 100644 index 00000000..66f9943a --- /dev/null +++ b/contracts/avalanche/connectors/0x/events.sol @@ -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 + ); +} diff --git a/contracts/avalanche/connectors/0x/helpers.sol b/contracts/avalanche/connectors/0x/helpers.sol new file mode 100644 index 00000000..dea216d4 --- /dev/null +++ b/contracts/avalanche/connectors/0x/helpers.sol @@ -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; + } +} diff --git a/contracts/avalanche/connectors/0x/interface.sol b/contracts/avalanche/connectors/0x/interface.sol new file mode 100644 index 00000000..d6c7c57e --- /dev/null +++ b/contracts/avalanche/connectors/0x/interface.sol @@ -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; +} diff --git a/contracts/avalanche/connectors/0x/main.sol b/contracts/avalanche/connectors/0x/main.sol new file mode 100644 index 00000000..fefe0566 --- /dev/null +++ b/contracts/avalanche/connectors/0x/main.sol @@ -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"; +} diff --git a/contracts/polygon/connectors/0x/main.sol b/contracts/polygon/connectors/0x/main.sol index 93d484df..cc63d57e 100644 --- a/contracts/polygon/connectors/0x/main.sol +++ b/contracts/polygon/connectors/0x/main.sol @@ -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. From 2207a073ece6daa06a23e87afc1cb41063a35039 Mon Sep 17 00:00:00 2001 From: bhavik Date: Sun, 12 Dec 2021 21:23:30 +0530 Subject: [PATCH 45/54] fixed typo --- test/mainnet/0x/zeroEx.test.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/mainnet/0x/zeroEx.test.ts b/test/mainnet/0x/zeroEx.test.ts index c363ef7e..2cddb01f 100644 --- a/test/mainnet/0x/zeroEx.test.ts +++ b/test/mainnet/0x/zeroEx.test.ts @@ -71,10 +71,6 @@ describe("ZeroEx", function() { expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( ethers.utils.parseEther("10") ); - const idai = await ethers.getContractAt( - er20abi, - "0x6b175474e89094c44da98b954eedeac495271d0f" // dai address - ); }); }); From 7dcd5072bc40856e46d14a8c58daea372a8b049b Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Mon, 13 Dec 2021 02:07:54 +0530 Subject: [PATCH 46/54] Update scripts/tests/avalanche/addresses.ts --- scripts/tests/avalanche/addresses.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/tests/avalanche/addresses.ts b/scripts/tests/avalanche/addresses.ts index d92a5bf7..e3919fb2 100644 --- a/scripts/tests/avalanche/addresses.ts +++ b/scripts/tests/avalanche/addresses.ts @@ -6,6 +6,6 @@ export const addresses: Record = { }, core: { connectorsV2: "0x127d8cD0E2b2E0366D522DeA53A787bfE9002C14", - instaIndex: " 0x6CE3e607C808b4f4C26B7F6aDAeB619e49CAbb25", + instaIndex: "0x6CE3e607C808b4f4C26B7F6aDAeB619e49CAbb25", }, }; From bc3d9bc5ab5f42b33acc0d19de352e54081efdf9 Mon Sep 17 00:00:00 2001 From: bhavik Date: Fri, 3 Dec 2021 17:38:48 +0530 Subject: [PATCH 47/54] added paraswap on mainnet --- .../avalanche/connectors/paraswap/helpers.sol | 45 +++++++----- .../mainnet/connectors/paraswap/events.sol | 11 +++ .../mainnet/connectors/paraswap/helpers.sol | 70 +++++++++++++++++++ .../mainnet/connectors/paraswap/interface.sol | 5 ++ .../mainnet/connectors/paraswap/main.sol | 55 +++++++++++++++ 5 files changed, 170 insertions(+), 16 deletions(-) create mode 100644 contracts/mainnet/connectors/paraswap/events.sol create mode 100644 contracts/mainnet/connectors/paraswap/helpers.sol create mode 100644 contracts/mainnet/connectors/paraswap/interface.sol create mode 100644 contracts/mainnet/connectors/paraswap/main.sol diff --git a/contracts/avalanche/connectors/paraswap/helpers.sol b/contracts/avalanche/connectors/paraswap/helpers.sol index ccee429b..861f3760 100644 --- a/contracts/avalanche/connectors/paraswap/helpers.sol +++ b/contracts/avalanche/connectors/paraswap/helpers.sol @@ -1,12 +1,11 @@ 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"; +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; @@ -16,17 +15,27 @@ abstract contract Helpers is DSMath, Basic { bytes callData; } - address internal constant paraswap = 0xDEF171Fe48CF0115B1d80b88dc8eAB59176FEe57; + address internal constant paraswap = + 0xDEF171Fe48CF0115B1d80b88dc8eAB59176FEe57; - function _swapHelper(SwapData memory swapData, uint256 wmaticAmt) 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); - (bool success, ) = paraswap.call{value: wmaticAmt}(swapData.callData); + (bool success, ) = paraswap.call{value: wavaxAmt}(swapData.callData); if (!success) revert("paraswap-failed"); uint256 finalBal = getTokenBal(buyToken); @@ -36,22 +45,26 @@ 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 maticAmt; + uint256 avaxAmt; if (address(_sellAddr) == avaxAddr) { - maticAmt = swapData._sellAmt; + avaxAmt = swapData._sellAmt; } else { - address tokenProxy = AugustusSwapperInterface(paraswap).getTokenTransferProxy(); + address tokenProxy = AugustusSwapperInterface(paraswap) + .getTokenTransferProxy(); approve(TokenInterface(_sellAddr), tokenProxy, swapData._sellAmt); } - swapData._buyAmt = _swapHelper(swapData, maticAmt); + swapData._buyAmt = _swapHelper(swapData, avaxAmt); setUint(setId, swapData._buyAmt); return swapData; } -} \ No newline at end of file +} diff --git a/contracts/mainnet/connectors/paraswap/events.sol b/contracts/mainnet/connectors/paraswap/events.sol new file mode 100644 index 00000000..0f9470dc --- /dev/null +++ b/contracts/mainnet/connectors/paraswap/events.sol @@ -0,0 +1,11 @@ +pragma solidity ^0.7.0; + +contract Events { + event LogSwap( + address buyToken, + address sellToken, + uint256 buyAmt, + uint256 sellAmt, + uint256 setId + ); +} \ No newline at end of file diff --git a/contracts/mainnet/connectors/paraswap/helpers.sol b/contracts/mainnet/connectors/paraswap/helpers.sol new file mode 100644 index 00000000..8f8e8d74 --- /dev/null +++ b/contracts/mainnet/connectors/paraswap/helpers.sol @@ -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; + } +} diff --git a/contracts/mainnet/connectors/paraswap/interface.sol b/contracts/mainnet/connectors/paraswap/interface.sol new file mode 100644 index 00000000..a63b4c77 --- /dev/null +++ b/contracts/mainnet/connectors/paraswap/interface.sol @@ -0,0 +1,5 @@ +pragma solidity ^0.7.0; + +interface AugustusSwapperInterface { + function getTokenTransferProxy() external view returns (address); +} \ No newline at end of file diff --git a/contracts/mainnet/connectors/paraswap/main.sol b/contracts/mainnet/connectors/paraswap/main.sol new file mode 100644 index 00000000..2041c11c --- /dev/null +++ b/contracts/mainnet/connectors/paraswap/main.sol @@ -0,0 +1,55 @@ +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 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 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 ConnectV2Paraswap is ParaswapResolver { + string public name = "Paraswap-v1"; +} \ No newline at end of file From dfe3a9e056c31438721642e51808409f13cf8515 Mon Sep 17 00:00:00 2001 From: bhavik Date: Fri, 3 Dec 2021 23:13:41 +0530 Subject: [PATCH 48/54] updated-comments --- .../mainnet/connectors/paraswap/main.sol | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/contracts/mainnet/connectors/paraswap/main.sol b/contracts/mainnet/connectors/paraswap/main.sol index 2041c11c..ad152a66 100644 --- a/contracts/mainnet/connectors/paraswap/main.sol +++ b/contracts/mainnet/connectors/paraswap/main.sol @@ -5,21 +5,21 @@ pragma solidity ^0.7.0; * @dev DEX Aggregator. */ -import { TokenInterface } from "../../common/interfaces.sol"; -import { Stores } from "../../common/stores.sol"; -import { Helpers } from "./helpers.sol"; +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 MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) - * @param sellAddr The address of the token to sell.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @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, @@ -27,7 +27,11 @@ abstract contract ParaswapResolver is Helpers { uint256 unitAmt, bytes calldata callData, uint256 setId - ) external payable returns (string memory _eventName, bytes memory _eventParam) { + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { Helpers.SwapData memory swapData = Helpers.SwapData({ buyToken: TokenInterface(buyAddr), sellToken: TokenInterface(sellAddr), @@ -52,4 +56,4 @@ abstract contract ParaswapResolver is Helpers { contract ConnectV2Paraswap is ParaswapResolver { string public name = "Paraswap-v1"; -} \ No newline at end of file +} From aba4325ebe332308ebeef8a425500e43665f1ae3 Mon Sep 17 00:00:00 2001 From: bhavik Date: Sun, 12 Dec 2021 01:45:55 +0530 Subject: [PATCH 49/54] added-test --- test/mainnet/paraswap/paraswap.test.ts | 182 +++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 test/mainnet/paraswap/paraswap.test.ts diff --git a/test/mainnet/paraswap/paraswap.test.ts b/test/mainnet/paraswap/paraswap.test.ts new file mode 100644 index 00000000..39f56574 --- /dev/null +++ b/test/mainnet/paraswap/paraswap.test.ts @@ -0,0 +1,182 @@ +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 { + ConnectV2ParaswapMainnet, + ConnectV2ParaswapMainnet__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: { + // 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 + ); + const iusdc = await ethers.getContractAt( + erc20, + "0xc2132D05D31c914a87C6611C10748AEb04B58e8F" + ); + // console.log("instance = ", iusdc); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: ConnectV2ParaswapMainnet__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("it should swap tokens", 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); + // console.log(calldata); + + 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") + ); + }); + }); +}); From 0fb4826c5bf38ae919e58d8b64a6d720040e4138 Mon Sep 17 00:00:00 2001 From: bhavik Date: Sun, 12 Dec 2021 01:57:35 +0530 Subject: [PATCH 50/54] changed contract name --- contracts/mainnet/connectors/paraswap/main.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/paraswap/main.sol b/contracts/mainnet/connectors/paraswap/main.sol index ad152a66..91180ad4 100644 --- a/contracts/mainnet/connectors/paraswap/main.sol +++ b/contracts/mainnet/connectors/paraswap/main.sol @@ -54,6 +54,6 @@ abstract contract ParaswapResolver is Helpers { } } -contract ConnectV2Paraswap is ParaswapResolver { +contract ConnectV2ParaswapMainnet is ParaswapResolver { string public name = "Paraswap-v1"; } From 9b6c759ce858104351606a7a93028f14f75e5108 Mon Sep 17 00:00:00 2001 From: bhavik Date: Sun, 12 Dec 2021 01:58:36 +0530 Subject: [PATCH 51/54] updated-test --- test/mainnet/paraswap/paraswap.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mainnet/paraswap/paraswap.test.ts b/test/mainnet/paraswap/paraswap.test.ts index 39f56574..b64656b0 100644 --- a/test/mainnet/paraswap/paraswap.test.ts +++ b/test/mainnet/paraswap/paraswap.test.ts @@ -79,7 +79,7 @@ describe("Paraswap", function() { }); }); describe("Main", function() { - it("it should swap tokens", async function() { + it("should swap successfully", async function() { async function getArg() { const slippage = 1; /* eth -> USDT */ From 612980c52b5ba98705a3f3b3ea42abae5c08a5e8 Mon Sep 17 00:00:00 2001 From: bhavik Date: Sun, 26 Dec 2021 18:38:47 +0530 Subject: [PATCH 52/54] updated-contract --- contracts/avalanche/connectors/0x/helpers.sol | 7 +- .../avalanche/connectors/0x/interface.sol | 34 ---- contracts/avalanche/connectors/0x/main.sol | 2 +- contracts/mainnet/connectors/0x/helpers.sol | 7 +- contracts/mainnet/connectors/0x/interface.sol | 34 ---- contracts/mainnet/connectors/0x/main.sol | 2 +- contracts/polygon/connectors/0x/helpers.sol | 7 +- contracts/polygon/connectors/0x/interface.sol | 37 ---- contracts/polygon/connectors/0x/main.sol | 2 +- hardhat.config.ts | 3 +- scripts/deployment/deploy.ts | 27 +-- scripts/deployment/deployConnector.ts | 13 +- scripts/tests/avalanche/addresses.ts | 4 - test/avalanche/0x/zeroEx.test.ts | 159 ++++++++++++++++++ test/polygon/0x/zeroEx.test.ts | 2 +- 15 files changed, 197 insertions(+), 143 deletions(-) create mode 100644 test/avalanche/0x/zeroEx.test.ts diff --git a/contracts/avalanche/connectors/0x/helpers.sol b/contracts/avalanche/connectors/0x/helpers.sol index dea216d4..cefa1dee 100644 --- a/contracts/avalanche/connectors/0x/helpers.sol +++ b/contracts/avalanche/connectors/0x/helpers.sol @@ -3,7 +3,7 @@ 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"; +import {ZeroExData} from "./interface.sol"; contract Helpers is DSMath, Basic { /** @@ -50,12 +50,9 @@ contract Helpers is DSMath, Basic { if (address(_sellAddr) == avaxAddr) { avaxAmt = zeroExData._sellAmt; } else { - address transformWallet = address( - zeroExInterface(zeroExAddr).getTransformWallet() - ); approve( TokenInterface(_sellAddr), - transformWallet, + zeroExAddr, zeroExData._sellAmt ); } diff --git a/contracts/avalanche/connectors/0x/interface.sol b/contracts/avalanche/connectors/0x/interface.sol index d6c7c57e..ae6016f2 100644 --- a/contracts/avalanche/connectors/0x/interface.sol +++ b/contracts/avalanche/connectors/0x/interface.sol @@ -2,40 +2,6 @@ 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; diff --git a/contracts/avalanche/connectors/0x/main.sol b/contracts/avalanche/connectors/0x/main.sol index fefe0566..6c5693a5 100644 --- a/contracts/avalanche/connectors/0x/main.sol +++ b/contracts/avalanche/connectors/0x/main.sol @@ -8,7 +8,7 @@ pragma experimental ABIEncoderV2; import {TokenInterface, MemoryInterface} from "../../common/interfaces.sol"; import {Stores} from "../../common/stores.sol"; -import {ZeroExData, zeroExInterface} from "./interface.sol"; +import {ZeroExData} from "./interface.sol"; import {Helpers} from "./helpers.sol"; import {Events} from "./events.sol"; diff --git a/contracts/mainnet/connectors/0x/helpers.sol b/contracts/mainnet/connectors/0x/helpers.sol index 459d3abd..b3979d06 100644 --- a/contracts/mainnet/connectors/0x/helpers.sol +++ b/contracts/mainnet/connectors/0x/helpers.sol @@ -3,7 +3,7 @@ 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"; +import {ZeroExData} from "./interface.sol"; contract Helpers is DSMath, Basic { /** @@ -50,12 +50,9 @@ contract Helpers is DSMath, Basic { if (address(_sellAddr) == ethAddr) { ethAmt = zeroExData._sellAmt; } else { - address transformWallet = address( - zeroExInterface(zeroExAddr).getTransformWallet() - ); approve( TokenInterface(_sellAddr), - transformWallet, + zeroExAddr, zeroExData._sellAmt ); } diff --git a/contracts/mainnet/connectors/0x/interface.sol b/contracts/mainnet/connectors/0x/interface.sol index d6c7c57e..ae6016f2 100644 --- a/contracts/mainnet/connectors/0x/interface.sol +++ b/contracts/mainnet/connectors/0x/interface.sol @@ -2,40 +2,6 @@ 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; diff --git a/contracts/mainnet/connectors/0x/main.sol b/contracts/mainnet/connectors/0x/main.sol index 72eb2220..404bd844 100644 --- a/contracts/mainnet/connectors/0x/main.sol +++ b/contracts/mainnet/connectors/0x/main.sol @@ -8,7 +8,7 @@ pragma experimental ABIEncoderV2; import {TokenInterface, MemoryInterface} from "../../common/interfaces.sol"; import {Stores} from "../../common/stores.sol"; -import {ZeroExData, zeroExInterface} from "./interface.sol"; +import {ZeroExData} from "./interface.sol"; import {Helpers} from "./helpers.sol"; import {Events} from "./events.sol"; diff --git a/contracts/polygon/connectors/0x/helpers.sol b/contracts/polygon/connectors/0x/helpers.sol index 835b8d7d..b90a2d77 100644 --- a/contracts/polygon/connectors/0x/helpers.sol +++ b/contracts/polygon/connectors/0x/helpers.sol @@ -2,7 +2,7 @@ pragma solidity ^0.7.0; import {TokenInterface} from "../../common/interfaces.sol"; import {DSMath} from "../../common/math.sol"; -import {ZeroExData, zeroExInterface} from "./interface.sol"; +import {ZeroExData} from "./interface.sol"; import {Basic} from "../../common/basic.sol"; contract Helpers is DSMath, Basic { @@ -52,12 +52,9 @@ contract Helpers is DSMath, Basic { if (address(_sellAddr) == maticAddr) { maticAmt = zeroExData._sellAmt; } else { - address transformWallet = address( - zeroExInterface(zeroExAddr).getTransformWallet() - ); approve( TokenInterface(_sellAddr), - transformWallet, + zeroExAddr, zeroExData._sellAmt ); } diff --git a/contracts/polygon/connectors/0x/interface.sol b/contracts/polygon/connectors/0x/interface.sol index 79952c90..ae6016f2 100644 --- a/contracts/polygon/connectors/0x/interface.sol +++ b/contracts/polygon/connectors/0x/interface.sol @@ -2,43 +2,6 @@ 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; diff --git a/contracts/polygon/connectors/0x/main.sol b/contracts/polygon/connectors/0x/main.sol index cc63d57e..e47c6508 100644 --- a/contracts/polygon/connectors/0x/main.sol +++ b/contracts/polygon/connectors/0x/main.sol @@ -8,7 +8,7 @@ pragma experimental ABIEncoderV2; import {TokenInterface, MemoryInterface} from "../../common/interfaces.sol"; import {Stores} from "../../common/stores.sol"; -import {ZeroExData, zeroExInterface} from "./interface.sol"; +import {ZeroExData} from "./interface.sol"; import {Helpers} from "./helpers.sol"; import {Events} from "./events.sol"; diff --git a/hardhat.config.ts b/hardhat.config.ts index 1c102759..2d804e9c 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -99,6 +99,7 @@ const config: HardhatUserConfig = { forking: { url: String(getNetworkUrl(String(process.env.networkType))), }, + gasPrice: 25000000000, }, mainnet: createConfig("mainnet"), polygon: createConfig("polygon"), @@ -112,7 +113,7 @@ const config: HardhatUserConfig = { tests: "./test", }, etherscan: { - apiKey: getScanApiKey(getNetworkUrl(String(process.env.networkType))), + apiKey: getScanApiKey(String(process.env.networkType)), }, typechain: { outDir: "typechain", diff --git a/scripts/deployment/deploy.ts b/scripts/deployment/deploy.ts index 381fe091..f11ae66b 100644 --- a/scripts/deployment/deploy.ts +++ b/scripts/deployment/deploy.ts @@ -5,19 +5,20 @@ async function main() { const accounts = await ethers.getSigners(); const connectMapping: Record = { - "1INCH-A": "ConnectV2OneInch", - "1INCH-B": "ConnectV2OneProto", - "AAVE-V1-A": "ConnectV2AaveV1", - "AAVE-V2-A": "ConnectV2AaveV2", - "AUTHORITY-A": "ConnectV2Auth", - "BASIC-A": "ConnectV2Basic", - "COMP-A": "ConnectV2COMP", - "COMPOUND-A": "ConnectV2Compound", - "DYDX-A": "ConnectV2Dydx", - "FEE-A": "ConnectV2Fee", - "GELATO-A": "ConnectV2Gelato", - "MAKERDAO-A": "ConnectV2Maker", - "UNISWAP-A": "ConnectV2UniswapV2", + // "1INCH-A": "ConnectV2OneInch", + // "1INCH-B": "ConnectV2OneProto", + // "AAVE-V1-A": "ConnectV2AaveV1", + // "AAVE-V2-A": "ConnectV2AaveV2", + // "AUTHORITY-A": "ConnectV2Auth", + // "BASIC-A": "ConnectV2Basic", + // "COMP-A": "ConnectV2COMP", + // "COMPOUND-A": "ConnectV2Compound", + // "DYDX-A": "ConnectV2Dydx", + // "FEE-A": "ConnectV2Fee", + // "GELATO-A": "ConnectV2Gelato", + // "MAKERDAO-A": "ConnectV2Maker", + // "UNISWAP-A": "ConnectV2UniswapV2", + "ZEROEX-A": "ConnectV2ZeroExAvalanche", }; const addressMapping: Record = {}; diff --git a/scripts/deployment/deployConnector.ts b/scripts/deployment/deployConnector.ts index 44e4667d..8f3bcd28 100644 --- a/scripts/deployment/deployConnector.ts +++ b/scripts/deployment/deployConnector.ts @@ -1,4 +1,4 @@ -import { ethers } from "hardhat"; +import hre, { ethers } from "hardhat"; export const deployConnector = async (connectorName: string) => { const Connector = await ethers.getContractFactory(connectorName); @@ -6,5 +6,16 @@ export const deployConnector = async (connectorName: string) => { await connector.deployed(); console.log(`${connectorName} Deployed: ${connector.address}`); + + try { + await hre.run("verify:verify", { + address: connector.address, + constructorArguments: [], + }); + } catch (error) { + console.log(`Failed to verify: ${connectorName}@${connector.address}`); + console.log(error); + console.log(); + } return connector.address; }; diff --git a/scripts/tests/avalanche/addresses.ts b/scripts/tests/avalanche/addresses.ts index a7aabaa1..e3919fb2 100644 --- a/scripts/tests/avalanche/addresses.ts +++ b/scripts/tests/avalanche/addresses.ts @@ -6,10 +6,6 @@ export const addresses: Record = { }, core: { connectorsV2: "0x127d8cD0E2b2E0366D522DeA53A787bfE9002C14", -<<<<<<< HEAD - instaIndex: " 0x6CE3e607C808b4f4C26B7F6aDAeB619e49CAbb25", -======= instaIndex: "0x6CE3e607C808b4f4C26B7F6aDAeB619e49CAbb25", ->>>>>>> 7dcd5072bc40856e46d14a8c58daea372a8b049b }, }; diff --git a/test/avalanche/0x/zeroEx.test.ts b/test/avalanche/0x/zeroEx.test.ts new file mode 100644 index 00000000..dfb1c011 --- /dev/null +++ b/test/avalanche/0x/zeroEx.test.ts @@ -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") + ); + }); + }); +}); diff --git a/test/polygon/0x/zeroEx.test.ts b/test/polygon/0x/zeroEx.test.ts index 2ef4d5bb..54758ce5 100644 --- a/test/polygon/0x/zeroEx.test.ts +++ b/test/polygon/0x/zeroEx.test.ts @@ -9,7 +9,7 @@ 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, ConnectV2ZeroExPolygon__factory } from "../../../typechain"; +import {ConnectV2ZeroExPolygon__factory } from "../../../typechain"; import er20abi from "../../../scripts/constant/abi/basics/erc20.json"; import type { Signer, Contract } from "ethers"; From ac3ef0f0717c2b35c6515f282dd3d0db72fbbfe9 Mon Sep 17 00:00:00 2001 From: bhavik mehta Date: Sat, 1 Jan 2022 17:18:13 +0530 Subject: [PATCH 53/54] changed contract name --- .../mainnet/connectors/paraswap/main.sol | 4 +- test/mainnet/paraswap/paraswap.test.ts | 37 ++++++++----------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/contracts/mainnet/connectors/paraswap/main.sol b/contracts/mainnet/connectors/paraswap/main.sol index 91180ad4..0d32dd04 100644 --- a/contracts/mainnet/connectors/paraswap/main.sol +++ b/contracts/mainnet/connectors/paraswap/main.sol @@ -54,6 +54,6 @@ abstract contract ParaswapResolver is Helpers { } } -contract ConnectV2ParaswapMainnet is ParaswapResolver { - string public name = "Paraswap-v1"; +contract ConnectV2ParaswapV5 is ParaswapResolver { + string public name = "Paraswap-v5"; } diff --git a/test/mainnet/paraswap/paraswap.test.ts b/test/mainnet/paraswap/paraswap.test.ts index b64656b0..d225285c 100644 --- a/test/mainnet/paraswap/paraswap.test.ts +++ b/test/mainnet/paraswap/paraswap.test.ts @@ -6,9 +6,8 @@ import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnable import { getMasterSigner } from "../../../scripts/tests/getMasterSigner"; import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2"; import { - ConnectV2ParaswapMainnet, - ConnectV2ParaswapMainnet__factory, -} from "../../../typechain"; + ConnectV2ParaswapV5__factory, +} from "../../../typechain" import { parseEther } from "@ethersproject/units"; import { encodeSpells } from "../../../scripts/tests/encodeSpells"; import { tokens } from "../../../scripts/tests/mainnet/tokens"; @@ -27,31 +26,28 @@ describe("Paraswap", function() { const wallets = provider.getWallets(); const [wallet0, wallet1, wallet2, wallet3] = wallets; before(async () => { - // await hre.network.provider.request({ - // method: "hardhat_reset", - // params: [ - // { - // forking: { - // jsonRpcUrl: hre.config.networks.hardhat.forking.url, - // blockNumber: 13300000, - // }, - // }, - // ], - // }); + 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 ); - const iusdc = await ethers.getContractAt( - erc20, - "0xc2132D05D31c914a87C6611C10748AEb04B58e8F" - ); - // console.log("instance = ", iusdc); + connector = await deployAndEnableConnector({ connectorName, - contractArtifact: ConnectV2ParaswapMainnet__factory, + contractArtifact: ConnectV2ParaswapV5__factory, signer: masterSigner, connectors: instaConnectorsV2, }); @@ -126,7 +122,6 @@ describe("Paraswap", function() { const calldata = await axios .post(url2, txConfig) .then((data) => data.data.data); - // console.log(calldata); function caculateUnitAmt( buyAmount: any, From a6165f2c5d1fe1028aa80479f874ece65d1120dd Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Tue, 11 Jan 2022 01:28:49 +0530 Subject: [PATCH 54/54] added erc721 and 1155 contracts --- .../connectors/basic-ERC1155/events.sol | 21 +++++ .../polygon/connectors/basic-ERC1155/main.sol | 94 +++++++++++++++++++ .../connectors/basic-ERC721/events.sol | 19 ++++ .../polygon/connectors/basic-ERC721/main.sol | 77 +++++++++++++++ 4 files changed, 211 insertions(+) create mode 100644 contracts/polygon/connectors/basic-ERC1155/events.sol create mode 100644 contracts/polygon/connectors/basic-ERC1155/main.sol create mode 100644 contracts/polygon/connectors/basic-ERC721/events.sol create mode 100644 contracts/polygon/connectors/basic-ERC721/main.sol diff --git a/contracts/polygon/connectors/basic-ERC1155/events.sol b/contracts/polygon/connectors/basic-ERC1155/events.sol new file mode 100644 index 00000000..def17f4c --- /dev/null +++ b/contracts/polygon/connectors/basic-ERC1155/events.sol @@ -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 + ); +} diff --git a/contracts/polygon/connectors/basic-ERC1155/main.sol b/contracts/polygon/connectors/basic-ERC1155/main.sol new file mode 100644 index 00000000..2958151f --- /dev/null +++ b/contracts/polygon/connectors/basic-ERC1155/main.sol @@ -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"; +} diff --git a/contracts/polygon/connectors/basic-ERC721/events.sol b/contracts/polygon/connectors/basic-ERC721/events.sol new file mode 100644 index 00000000..b47e4b4d --- /dev/null +++ b/contracts/polygon/connectors/basic-ERC721/events.sol @@ -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 + ); +} diff --git a/contracts/polygon/connectors/basic-ERC721/main.sol b/contracts/polygon/connectors/basic-ERC721/main.sol new file mode 100644 index 00000000..465bf335 --- /dev/null +++ b/contracts/polygon/connectors/basic-ERC721/main.sol @@ -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"; +}