From 397c49fe791b6b39626b5f5743d28aec67c390ef Mon Sep 17 00:00:00 2001 From: bhavik-m Date: Thu, 20 Jan 2022 22:48:25 +0530 Subject: [PATCH 1/3] Added Connector --- .../avalanche/connectors/1inch-v4/events.sol | 12 ++ .../avalanche/connectors/1inch-v4/helpers.sol | 13 ++ .../connectors/1inch-v4/interface.sol | 30 +++++ .../avalanche/connectors/1inch-v4/main.sol | 111 ++++++++++++++++++ 4 files changed, 166 insertions(+) create mode 100644 contracts/avalanche/connectors/1inch-v4/events.sol create mode 100644 contracts/avalanche/connectors/1inch-v4/helpers.sol create mode 100644 contracts/avalanche/connectors/1inch-v4/interface.sol create mode 100644 contracts/avalanche/connectors/1inch-v4/main.sol diff --git a/contracts/avalanche/connectors/1inch-v4/events.sol b/contracts/avalanche/connectors/1inch-v4/events.sol new file mode 100644 index 00000000..bec3b27a --- /dev/null +++ b/contracts/avalanche/connectors/1inch-v4/events.sol @@ -0,0 +1,12 @@ +pragma solidity ^0.7.0; + +contract Events { + event LogSell( + address indexed buyToken, + address indexed sellToken, + uint256 buyAmt, + uint256 sellAmt, + uint256 getId, + uint256 setId + ); +} \ No newline at end of file diff --git a/contracts/avalanche/connectors/1inch-v4/helpers.sol b/contracts/avalanche/connectors/1inch-v4/helpers.sol new file mode 100644 index 00000000..b569d2d6 --- /dev/null +++ b/contracts/avalanche/connectors/1inch-v4/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 1Inch Address + */ + address internal constant oneInchAddr = 0x1111111254fb6c44bAC0beD2854e76F90643097d; +} \ No newline at end of file diff --git a/contracts/avalanche/connectors/1inch-v4/interface.sol b/contracts/avalanche/connectors/1inch-v4/interface.sol new file mode 100644 index 00000000..d2d790b7 --- /dev/null +++ b/contracts/avalanche/connectors/1inch-v4/interface.sol @@ -0,0 +1,30 @@ +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../../common/interfaces.sol"; + +interface OneInchInterace { + function swap( + TokenInterface fromToken, + TokenInterface toToken, + uint256 fromTokenAmount, + uint256 minReturnAmount, + uint256 guaranteedAmount, + address payable referrer, + address[] calldata callAddresses, + bytes calldata callDataConcat, + uint256[] calldata starts, + uint256[] calldata gasLimitsAndValues + ) + external + payable + returns (uint256 returnAmount); +} + +struct OneInchData { + TokenInterface sellToken; + TokenInterface buyToken; + uint _sellAmt; + uint _buyAmt; + uint unitAmt; + bytes callData; +} \ No newline at end of file diff --git a/contracts/avalanche/connectors/1inch-v4/main.sol b/contracts/avalanche/connectors/1inch-v4/main.sol new file mode 100644 index 00000000..593cb737 --- /dev/null +++ b/contracts/avalanche/connectors/1inch-v4/main.sol @@ -0,0 +1,111 @@ +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +/** + * @title 1InchV4. + * @dev On-chain DEX Aggregator. + */ + +// import files from common directory +import { TokenInterface , MemoryInterface } from "../../../common/interfaces.sol"; +import { Stores } from "../../../common/stores.sol"; +import { OneInchInterace, OneInchData } from "./interface.sol"; +import { Helpers } from "./helpers.sol"; +import { Events } from "./events.sol"; + +abstract contract OneInchResolver is Helpers, Events { + /** + * @dev 1inch API swap handler + * @param oneInchData - contains data returned from 1inch API. Struct defined in interfaces.sol + * @param avaxAmt - Avax to swap for .value() + */ + function oneInchSwap( + OneInchData memory oneInchData, + uint avaxAmt + ) internal returns (uint buyAmt) { + TokenInterface buyToken = oneInchData.buyToken; + (uint _buyDec, uint _sellDec) = getTokensDec(buyToken, oneInchData.sellToken); + uint _sellAmt18 = convertTo18(_sellDec, oneInchData._sellAmt); + uint _slippageAmt = convert18ToDec(_buyDec, wmul(oneInchData.unitAmt, _sellAmt18)); + + uint initalBal = getTokenBal(buyToken); + + // solium-disable-next-line security/no-call-value + (bool success, ) = oneInchAddr.call{value: avaxAmt}(oneInchData.callData); + if (!success) revert("1Inch-swap-failed"); + + uint finalBal = getTokenBal(buyToken); + + buyAmt = sub(finalBal, initalBal); + + require(_slippageAmt <= buyAmt, "Too much slippage"); + } + +} + +abstract contract OneInchResolverHelpers is OneInchResolver { + + /** + * @dev Gets the swapping data from 1inch's API. + * @param oneInchData Struct with multiple swap data defined in interfaces.sol + * @param setId Set token amount at this ID in `InstaMemory` Contract. + */ + function _sell( + OneInchData memory oneInchData, + uint setId + ) internal returns (OneInchData memory) { + TokenInterface _sellAddr = oneInchData.sellToken; + + uint avaxAmt; + if (address(_sellAddr) == avaxAddr) { + avaxAmt = oneInchData._sellAmt; + } else { + approve(TokenInterface(_sellAddr), oneInchAddr, oneInchData._sellAmt); + } + + oneInchData._buyAmt = oneInchSwap(oneInchData, avaxAmt); + setUint(setId, oneInchData._buyAmt); + + return oneInchData; + + } +} + +abstract contract OneInch is OneInchResolverHelpers { + /** + * @dev Sell Avax/ERC20_Token using 1Inch. + * @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain. + * @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 1inch API. + * @param setId ID stores the amount of token brought. + */ + function sell( + address buyAddr, + address sellAddr, + uint sellAmt, + uint unitAmt, + bytes calldata callData, + uint setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + OneInchData memory oneInchData = OneInchData({ + buyToken: TokenInterface(buyAddr), + sellToken: TokenInterface(sellAddr), + unitAmt: unitAmt, + callData: callData, + _sellAmt: sellAmt, + _buyAmt: 0 + }); + + oneInchData = _sell(oneInchData, setId); + + _eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode(buyAddr, sellAddr, oneInchData._buyAmt, oneInchData._sellAmt, 0, setId); + } +} + +contract ConnectV2OneInchV4 is OneInch { + string public name = "1Inch-v4"; +} From 8694406d75cfa8eb3f865a8b562b1a8d444e2da3 Mon Sep 17 00:00:00 2001 From: bhavik-m Date: Thu, 20 Jan 2022 23:10:44 +0530 Subject: [PATCH 2/3] minor correction --- contracts/avalanche/connectors/1inch-v4/helpers.sol | 6 +++--- contracts/avalanche/connectors/1inch-v4/interface.sol | 2 +- contracts/avalanche/connectors/1inch-v4/main.sol | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/contracts/avalanche/connectors/1inch-v4/helpers.sol b/contracts/avalanche/connectors/1inch-v4/helpers.sol index b569d2d6..8582731c 100644 --- a/contracts/avalanche/connectors/1inch-v4/helpers.sol +++ b/contracts/avalanche/connectors/1inch-v4/helpers.sol @@ -1,8 +1,8 @@ pragma solidity ^0.7.0; -import { TokenInterface } from "../../../common/interfaces.sol"; -import { DSMath } from "../../../common/math.sol"; -import { Basic } from "../../../common/basic.sol"; +import { TokenInterface } from "../../common/interfaces.sol"; +import { DSMath } from "../../common/math.sol"; +import { Basic } from "../../common/basic.sol"; abstract contract Helpers is DSMath, Basic { diff --git a/contracts/avalanche/connectors/1inch-v4/interface.sol b/contracts/avalanche/connectors/1inch-v4/interface.sol index d2d790b7..f35b9277 100644 --- a/contracts/avalanche/connectors/1inch-v4/interface.sol +++ b/contracts/avalanche/connectors/1inch-v4/interface.sol @@ -1,6 +1,6 @@ pragma solidity ^0.7.0; -import { TokenInterface } from "../../../common/interfaces.sol"; +import { TokenInterface } from "../../common/interfaces.sol"; interface OneInchInterace { function swap( diff --git a/contracts/avalanche/connectors/1inch-v4/main.sol b/contracts/avalanche/connectors/1inch-v4/main.sol index 593cb737..f6689381 100644 --- a/contracts/avalanche/connectors/1inch-v4/main.sol +++ b/contracts/avalanche/connectors/1inch-v4/main.sol @@ -7,8 +7,8 @@ pragma experimental ABIEncoderV2; */ // import files from common directory -import { TokenInterface , MemoryInterface } from "../../../common/interfaces.sol"; -import { Stores } from "../../../common/stores.sol"; +import { TokenInterface , MemoryInterface } from "../../common/interfaces.sol"; +import { Stores } from "../../common/stores.sol"; import { OneInchInterace, OneInchData } from "./interface.sol"; import { Helpers } from "./helpers.sol"; import { Events } from "./events.sol"; @@ -106,6 +106,6 @@ abstract contract OneInch is OneInchResolverHelpers { } } -contract ConnectV2OneInchV4 is OneInch { +contract ConnectV2OneInchV4Avalanche is OneInch { string public name = "1Inch-v4"; } From 1c741889d1263f87d7f097f27f3c35e2e87c2d63 Mon Sep 17 00:00:00 2001 From: bhavik-m Date: Thu, 20 Jan 2022 23:15:20 +0530 Subject: [PATCH 3/3] testcases --- hardhat.config.ts | 1 - test/avalanche/1inch/oneInch.test.ts | 160 +++++++++++++++++++++++++++ 2 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 test/avalanche/1inch/oneInch.test.ts diff --git a/hardhat.config.ts b/hardhat.config.ts index 94542cf8..d0d1000c 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -109,7 +109,6 @@ const config: HardhatUserConfig = { forking: { url: String(getNetworkUrl(String(process.env.networkType))), }, - gasPrice: 25000000000, }, mainnet: createConfig("mainnet"), polygon: createConfig("polygon"), diff --git a/test/avalanche/1inch/oneInch.test.ts b/test/avalanche/1inch/oneInch.test.ts new file mode 100644 index 00000000..9ac0ba4d --- /dev/null +++ b/test/avalanche/1inch/oneInch.test.ts @@ -0,0 +1,160 @@ +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 {ConnectV2OneInchV4Avalanche__factory } from "../../../typechain"; +import er20abi from "../../../scripts/constant/abi/basics/erc20.json"; +import type { Signer, Contract } from "ethers"; + +describe("1Inch", function() { + const connectorName = "1inch-connector-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: ConnectV2OneInchV4Avalanche__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 avax 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; + /* avax -> dai */ + const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // avax, decimals 18 + const sellTokenDecimals = 18; + const buyTokenAddress = "0xd586e7f844cea2f87f50152665bcbc2c279d8d70"; // 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://api.1inch.exchange/v4.0/43114/swap`; + + const params = { + toTokenAddress: buyTokenAddress, + fromTokenAddress: sellTokenAddress, + amount: "1000000000000000000", // Always denominated in wei + fromAddress: dsaWallet0.address, + slippage:1 + }; + const response = await axios + .get(url, { params: params }) + .then((data: any) => data); + + buyTokenAmount = response.data.toTokenAmount; + const calldata = response.data.tx.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: "sell", + 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") + ); + }); + }); +});