From 612980c52b5ba98705a3f3b3ea42abae5c08a5e8 Mon Sep 17 00:00:00 2001 From: bhavik Date: Sun, 26 Dec 2021 18:38:47 +0530 Subject: [PATCH] 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";