From 72ba571ce3a00b4d1223e19e9a03924a284073c0 Mon Sep 17 00:00:00 2001 From: q1q0 Date: Tue, 13 Jun 2023 13:24:06 -0400 Subject: [PATCH 1/5] add kyber swap --- .../kyber/{ => kyber_v2}/events.sol | 0 .../kyber/{ => kyber_v2}/helpers.sol | 4 +- .../kyber/{ => kyber_v2}/interface.sol | 0 .../connectors/kyber/{ => kyber_v2}/main.sol | 2 +- .../connectors/kyber/kyber_v3/events.sol | 12 ++ .../connectors/kyber/kyber_v3/helpers.sol | 71 ++++++++ .../connectors/kyber/kyber_v3/interface.sol | 6 + .../connectors/kyber/kyber_v3/main.sol | 60 ++++++ test/mainnet/kyberswap/kyberswap.test.ts | 171 ++++++++++++++++++ 9 files changed, 323 insertions(+), 3 deletions(-) rename contracts/mainnet/connectors/kyber/{ => kyber_v2}/events.sol (100%) rename contracts/mainnet/connectors/kyber/{ => kyber_v2}/helpers.sol (79%) rename contracts/mainnet/connectors/kyber/{ => kyber_v2}/interface.sol (100%) rename contracts/mainnet/connectors/kyber/{ => kyber_v2}/main.sol (96%) create mode 100644 contracts/mainnet/connectors/kyber/kyber_v3/events.sol create mode 100644 contracts/mainnet/connectors/kyber/kyber_v3/helpers.sol create mode 100644 contracts/mainnet/connectors/kyber/kyber_v3/interface.sol create mode 100644 contracts/mainnet/connectors/kyber/kyber_v3/main.sol create mode 100644 test/mainnet/kyberswap/kyberswap.test.ts diff --git a/contracts/mainnet/connectors/kyber/events.sol b/contracts/mainnet/connectors/kyber/kyber_v2/events.sol similarity index 100% rename from contracts/mainnet/connectors/kyber/events.sol rename to contracts/mainnet/connectors/kyber/kyber_v2/events.sol diff --git a/contracts/mainnet/connectors/kyber/helpers.sol b/contracts/mainnet/connectors/kyber/kyber_v2/helpers.sol similarity index 79% rename from contracts/mainnet/connectors/kyber/helpers.sol rename to contracts/mainnet/connectors/kyber/kyber_v2/helpers.sol index 7c0e6f17..721228cd 100644 --- a/contracts/mainnet/connectors/kyber/helpers.sol +++ b/contracts/mainnet/connectors/kyber/kyber_v2/helpers.sol @@ -1,8 +1,8 @@ //SPDX-License-Identifier: MIT pragma solidity ^0.7.0; -import { DSMath } from "../../common/math.sol"; -import { Basic } from "../../common/basic.sol"; +import { DSMath } from "../../../common/math.sol"; +import { Basic } from "../../../common/basic.sol"; import { KyberInterface } from "./interface.sol"; abstract contract Helpers is DSMath, Basic { diff --git a/contracts/mainnet/connectors/kyber/interface.sol b/contracts/mainnet/connectors/kyber/kyber_v2/interface.sol similarity index 100% rename from contracts/mainnet/connectors/kyber/interface.sol rename to contracts/mainnet/connectors/kyber/kyber_v2/interface.sol diff --git a/contracts/mainnet/connectors/kyber/main.sol b/contracts/mainnet/connectors/kyber/kyber_v2/main.sol similarity index 96% rename from contracts/mainnet/connectors/kyber/main.sol rename to contracts/mainnet/connectors/kyber/kyber_v2/main.sol index 8b106565..ac81389f 100644 --- a/contracts/mainnet/connectors/kyber/main.sol +++ b/contracts/mainnet/connectors/kyber/kyber_v2/main.sol @@ -6,7 +6,7 @@ pragma solidity ^0.7.0; * @dev Decentralised Swapping. */ -import { TokenInterface } from "../../common/interfaces.sol"; +import { TokenInterface } from "../../../common/interfaces.sol"; import { Helpers } from "./helpers.sol"; import { Events } from "./events.sol"; diff --git a/contracts/mainnet/connectors/kyber/kyber_v3/events.sol b/contracts/mainnet/connectors/kyber/kyber_v3/events.sol new file mode 100644 index 00000000..dcba616e --- /dev/null +++ b/contracts/mainnet/connectors/kyber/kyber_v3/events.sol @@ -0,0 +1,12 @@ +//SPDX-License-Identifier: MIT +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/kyber/kyber_v3/helpers.sol b/contracts/mainnet/connectors/kyber/kyber_v3/helpers.sol new file mode 100644 index 00000000..a278476e --- /dev/null +++ b/contracts/mainnet/connectors/kyber/kyber_v3/helpers.sol @@ -0,0 +1,71 @@ +//SPDX-License-Identifier: MIT +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 kyberswap = + 0x6131B5fae19EA4f9D964eAc0408E4408b66337b5; + + 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, ) = kyberswap.call{value: wethAmt}(swapData.callData); + if (!success) revert("kyberswap-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(kyberswap) + .getTokenTransferProxy(); + approve(TokenInterface(_sellAddr), tokenProxy, swapData._sellAmt); + } + + swapData._buyAmt = _swapHelper(swapData, ethAmt); + + setUint(setId, swapData._buyAmt); + + return swapData; + } +} diff --git a/contracts/mainnet/connectors/kyber/kyber_v3/interface.sol b/contracts/mainnet/connectors/kyber/kyber_v3/interface.sol new file mode 100644 index 00000000..7de1fbbf --- /dev/null +++ b/contracts/mainnet/connectors/kyber/kyber_v3/interface.sol @@ -0,0 +1,6 @@ +//SPDX-License-Identifier: MIT +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/kyber/kyber_v3/main.sol b/contracts/mainnet/connectors/kyber/kyber_v3/main.sol new file mode 100644 index 00000000..4b2b96dc --- /dev/null +++ b/contracts/mainnet/connectors/kyber/kyber_v3/main.sol @@ -0,0 +1,60 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +/** + * @title Kyber. + * @dev DEX Aggregator. + */ + +import {TokenInterface} from "../../../common/interfaces.sol"; +import {Stores} from "../../../common/stores.sol"; +import {Helpers} from "./helpers.sol"; + +abstract contract KyberResolver is Helpers { + /** + * @dev Sell ETH/ERC20_Token using KyberSwap. + * @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain. + * @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAddr The address of the token to sell.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAmt The amount of the token to sell. + * @param unitAmt The amount of buyAmt/sellAmt with slippage. + * @param callData Data from kyberswap 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 ConnectV2KyberV3 is KyberResolver { + string public name = "Kyber-v3"; +} diff --git a/test/mainnet/kyberswap/kyberswap.test.ts b/test/mainnet/kyberswap/kyberswap.test.ts new file mode 100644 index 00000000..93831934 --- /dev/null +++ b/test/mainnet/kyberswap/kyberswap.test.ts @@ -0,0 +1,171 @@ +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 { + ConnectV2KyberV3__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("Kyberswap", function() { + const connectorName = "kyberswap-test"; + let dsaWallet0: any; + let masterSigner: any; + let instaConnectorsV2: any; + let connector: any; + const wallets = provider.getWallets(); + const [wallet0, wallet1, wallet2, wallet3] = wallets; + before(async () => { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + // @ts-ignore + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + // blockNumber: 13300000, + }, + }, + ], + }); + masterSigner = await getMasterSigner(); + const erc20 = abis.basic.erc20; + instaConnectorsV2 = await ethers.getContractAt( + abis.core.connectorsV2, + addresses.core.connectorsV2 + ); + + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: ConnectV2KyberV3__factory, + signer: masterSigner, + connectors: instaConnectorsV2, + }); + // console.log("Connector address", connector.address); + }); + it("Should have contracts deployed.", async function() { + expect(!!instaConnectorsV2.address).to.be.true; + expect(!!connector.address).to.be.true; + expect(!!masterSigner.address).to.be.true; + }); + describe("DSA wallet setup", function() { + it("Should build DSA v2", async function() { + dsaWallet0 = await buildDSAv2(wallet0.address); + expect(!!dsaWallet0.address).to.be.true; + }); + it("Deposit ETH into DSA wallet", async function() { + await wallet0.sendTransaction({ + to: dsaWallet0.address, + value: ethers.utils.parseEther("10"), + }); + + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( + ethers.utils.parseEther("10") + ); + }); + }); + describe("Main", function() { + it("should swap successfully", async function() { + async function getArg() { + const slippage = 1; + /* eth -> USDT */ + const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // eth, decimals 18 + const sellTokenDecimals = 18; + const buyTokenAddress = "0xdac17f958d2ee523a2206206994597c13d831ec7"; // USDT, decimals 6 + const buyTokenDecimals = 6; + const amount = 2; + const srcAmount = new BigNumber(amount) + .times(new BigNumber(10).pow(sellTokenDecimals)) + .toFixed(0); + const fromAddress = dsaWallet0.address; + let url = `https://api.kyber.network/quote_amount`; + let params = { + base: sellTokenAddress, + quote: buyTokenAddress, + amount: srcAmount, + type: "sell", + }; + + const buyTokenAmount = await axios + .get(url, { params: params }) + .then((data) => data.data); + console.log("--------buy amount--------",buyTokenAmount) + let minAmount = new BigNumber(buyTokenAmount) + .times(1 - slippage / 100) + .toFixed(0); + + let txConfig = { + user_address: fromAddress, + src_id: sellTokenAddress, + dst_id: buyTokenAddress, + src_qty: srcAmount, + min_dst_qty: minAmount, + gas_price: 'high' + }; + let url2 = "https://api.kyber.network/trade_data"; + const calldata = await axios + .post(url2, txConfig) + .then((data) => data.data.data); + + function caculateUnitAmt( + buyAmount: any, + sellAmount: any, + buyDecimal: any, + sellDecimal: any, + maxSlippage: any + ) { + let unitAmt: any; + unitAmt = new BigNumber(buyAmount) + .dividedBy(10 ** buyDecimal) + .dividedBy(new BigNumber(sellAmount).dividedBy(10 ** sellDecimal)); + unitAmt = unitAmt.multipliedBy((100 - maxSlippage) / 100); + unitAmt = unitAmt.multipliedBy(1e18).toFixed(0); + return unitAmt; + } + + let unitAmt = caculateUnitAmt( + buyTokenAmount, + srcAmount, + buyTokenDecimals, + sellTokenDecimals, + 1 + ); + + return [ + buyTokenAddress, + sellTokenAddress, + srcAmount, + unitAmt, + calldata, + 0, + ]; + } + let arg = await getArg(); + const spells = [ + { + connector: connectorName, + method: "swap", + args: arg, + }, + ]; + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( + ethers.utils.parseEther("8") + ); + }); + }); +}); From 52ecac01a302c0f3a3e1c544d3c6a49eed2db849 Mon Sep 17 00:00:00 2001 From: q1q0 Date: Wed, 14 Jun 2023 08:29:41 -0400 Subject: [PATCH 2/5] update test script --- .../connectors/kyber/kyber_v3/helpers.sol | 7 +--- .../connectors/kyber/kyber_v3/main.sol | 1 - test/mainnet/kyberswap/kyberswap.test.ts | 35 ++++++++----------- 3 files changed, 15 insertions(+), 28 deletions(-) diff --git a/contracts/mainnet/connectors/kyber/kyber_v3/helpers.sol b/contracts/mainnet/connectors/kyber/kyber_v3/helpers.sol index a278476e..5b38d196 100644 --- a/contracts/mainnet/connectors/kyber/kyber_v3/helpers.sol +++ b/contracts/mainnet/connectors/kyber/kyber_v3/helpers.sol @@ -4,7 +4,6 @@ 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 { @@ -35,7 +34,6 @@ abstract contract Helpers is DSMath, Basic { ); uint256 initalBal = getTokenBal(buyToken); - (bool success, ) = kyberswap.call{value: wethAmt}(swapData.callData); if (!success) revert("kyberswap-failed"); @@ -57,11 +55,8 @@ abstract contract Helpers is DSMath, Basic { if (address(_sellAddr) == ethAddr) { ethAmt = swapData._sellAmt; } else { - address tokenProxy = AugustusSwapperInterface(kyberswap) - .getTokenTransferProxy(); - approve(TokenInterface(_sellAddr), tokenProxy, swapData._sellAmt); + approve(TokenInterface(_sellAddr), kyberswap, swapData._sellAmt); } - swapData._buyAmt = _swapHelper(swapData, ethAmt); setUint(setId, swapData._buyAmt); diff --git a/contracts/mainnet/connectors/kyber/kyber_v3/main.sol b/contracts/mainnet/connectors/kyber/kyber_v3/main.sol index 4b2b96dc..582176df 100644 --- a/contracts/mainnet/connectors/kyber/kyber_v3/main.sol +++ b/contracts/mainnet/connectors/kyber/kyber_v3/main.sol @@ -41,7 +41,6 @@ abstract contract KyberResolver is Helpers { _sellAmt: sellAmt, _buyAmt: 0 }); - swapData = _swap(swapData, setId); _eventName = "LogSwap(address,address,uint256,uint256,uint256)"; diff --git a/test/mainnet/kyberswap/kyberswap.test.ts b/test/mainnet/kyberswap/kyberswap.test.ts index 93831934..25be2b95 100644 --- a/test/mainnet/kyberswap/kyberswap.test.ts +++ b/test/mainnet/kyberswap/kyberswap.test.ts @@ -88,31 +88,25 @@ describe("Kyberswap", function() { .times(new BigNumber(10).pow(sellTokenDecimals)) .toFixed(0); const fromAddress = dsaWallet0.address; - let url = `https://api.kyber.network/quote_amount`; + let url = `https://aggregator-api.kyberswap.com/ethereum/api/v1/routes`; let params = { - base: sellTokenAddress, - quote: buyTokenAddress, - amount: srcAmount, - type: "sell", + tokenIn: sellTokenAddress, + tokenOut: buyTokenAddress, + amountIn: srcAmount, }; - const buyTokenAmount = await axios + const routeSummary = await axios .get(url, { params: params }) - .then((data) => data.data); - console.log("--------buy amount--------",buyTokenAmount) - let minAmount = new BigNumber(buyTokenAmount) - .times(1 - slippage / 100) - .toFixed(0); + .then((data) => data.data.routeSummary); + + let txConfig = { - user_address: fromAddress, - src_id: sellTokenAddress, - dst_id: buyTokenAddress, - src_qty: srcAmount, - min_dst_qty: minAmount, - gas_price: 'high' + routeSummary: routeSummary, + sender: fromAddress, + recipient: fromAddress, }; - let url2 = "https://api.kyber.network/trade_data"; + let url2 = "https://aggregator-api.kyberswap.com/ethereum/api/v1/route/build"; const calldata = await axios .post(url2, txConfig) .then((data) => data.data.data); @@ -134,13 +128,13 @@ describe("Kyberswap", function() { } let unitAmt = caculateUnitAmt( - buyTokenAmount, + routeSummary.amountOut, srcAmount, buyTokenDecimals, sellTokenDecimals, 1 ); - + console.log('----------------unitAmt-------------', unitAmt.toString()) return [ buyTokenAddress, sellTokenAddress, @@ -162,7 +156,6 @@ describe("Kyberswap", function() { .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 cb6563b816741d6d04beb071802b498e237f7e36 Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Wed, 14 Jun 2023 20:06:35 +0530 Subject: [PATCH 3/5] Fix KyberSwap testcases --- .../connectors/kyber/kyber_v3/main.sol | 4 ++-- test/mainnet/kyberswap/kyberswap.test.ts | 21 +++++++++++-------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/contracts/mainnet/connectors/kyber/kyber_v3/main.sol b/contracts/mainnet/connectors/kyber/kyber_v3/main.sol index 582176df..4385016b 100644 --- a/contracts/mainnet/connectors/kyber/kyber_v3/main.sol +++ b/contracts/mainnet/connectors/kyber/kyber_v3/main.sol @@ -54,6 +54,6 @@ abstract contract KyberResolver is Helpers { } } -contract ConnectV2KyberV3 is KyberResolver { - string public name = "Kyber-v3"; +contract ConnectV2KyberAggregator is KyberResolver { + string public name = "Kyber-aggregator-v1.0"; } diff --git a/test/mainnet/kyberswap/kyberswap.test.ts b/test/mainnet/kyberswap/kyberswap.test.ts index 25be2b95..62e1bb04 100644 --- a/test/mainnet/kyberswap/kyberswap.test.ts +++ b/test/mainnet/kyberswap/kyberswap.test.ts @@ -6,7 +6,7 @@ import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnable import { getMasterSigner } from "../../../scripts/tests/getMasterSigner"; import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2"; import { - ConnectV2KyberV3__factory, + ConnectV2KyberAggregator__factory, } from "../../../typechain" import { parseEther } from "@ethersproject/units"; import { encodeSpells } from "../../../scripts/tests/encodeSpells"; @@ -47,7 +47,7 @@ describe("Kyberswap", function() { connector = await deployAndEnableConnector({ connectorName, - contractArtifact: ConnectV2KyberV3__factory, + contractArtifact: ConnectV2KyberAggregator__factory, signer: masterSigner, connectors: instaConnectorsV2, }); @@ -74,8 +74,10 @@ describe("Kyberswap", function() { ); }); }); - describe("Main", function() { + describe("Main", async function() { it("should swap successfully", async function() { + const latestBlock = await hre.ethers.provider.getBlock("latest"); + console.log('latestBlock: ', latestBlock.number) async function getArg() { const slippage = 1; /* eth -> USDT */ @@ -97,19 +99,18 @@ describe("Kyberswap", function() { const routeSummary = await axios .get(url, { params: params }) - .then((data) => data.data.routeSummary); - - + .then((response) => response.data.data.routeSummary); let txConfig = { routeSummary: routeSummary, sender: fromAddress, recipient: fromAddress, }; + let url2 = "https://aggregator-api.kyberswap.com/ethereum/api/v1/route/build"; const calldata = await axios .post(url2, txConfig) - .then((data) => data.data.data); + .then((response) => response.data.data); function caculateUnitAmt( buyAmount: any, @@ -134,17 +135,19 @@ describe("Kyberswap", function() { sellTokenDecimals, 1 ); - console.log('----------------unitAmt-------------', unitAmt.toString()) + console.log('unitAmt: ', unitAmt.toString()) return [ buyTokenAddress, sellTokenAddress, srcAmount, unitAmt, - calldata, + calldata.data, 0, ]; } + let arg = await getArg(); + const spells = [ { connector: connectorName, From 59aa71214f1ce73b71cde890e9a0e12dcc617450 Mon Sep 17 00:00:00 2001 From: q1q0 Date: Wed, 14 Jun 2023 11:27:17 -0400 Subject: [PATCH 4/5] add to multichain --- .../arbitrum/connectors/kyber_v3/events.sol | 12 ++++ .../arbitrum/connectors/kyber_v3/helpers.sol | 66 +++++++++++++++++++ .../connectors/kyber_v3/interface.sol | 6 ++ .../arbitrum/connectors/kyber_v3/main.sol | 59 +++++++++++++++++ .../avalanche/connectors/kyber_v3/events.sol | 12 ++++ .../avalanche/connectors/kyber_v3/helpers.sol | 66 +++++++++++++++++++ .../connectors/kyber_v3/interface.sol | 6 ++ .../avalanche/connectors/kyber_v3/main.sol | 59 +++++++++++++++++ .../fantom/connectors/kyber_v3/events.sol | 12 ++++ .../fantom/connectors/kyber_v3/helpers.sol | 66 +++++++++++++++++++ .../fantom/connectors/kyber_v3/interface.sol | 6 ++ contracts/fantom/connectors/kyber_v3/main.sol | 59 +++++++++++++++++ .../optimism/connectors/kyber_v3/events.sol | 12 ++++ .../optimism/connectors/kyber_v3/helpers.sol | 66 +++++++++++++++++++ .../connectors/kyber_v3/interface.sol | 6 ++ .../optimism/connectors/kyber_v3/main.sol | 59 +++++++++++++++++ .../polygon/connectors/kyber_v3/events.sol | 12 ++++ .../polygon/connectors/kyber_v3/helpers.sol | 66 +++++++++++++++++++ .../polygon/connectors/kyber_v3/interface.sol | 6 ++ .../polygon/connectors/kyber_v3/main.sol | 59 +++++++++++++++++ 20 files changed, 715 insertions(+) create mode 100644 contracts/arbitrum/connectors/kyber_v3/events.sol create mode 100644 contracts/arbitrum/connectors/kyber_v3/helpers.sol create mode 100644 contracts/arbitrum/connectors/kyber_v3/interface.sol create mode 100644 contracts/arbitrum/connectors/kyber_v3/main.sol create mode 100644 contracts/avalanche/connectors/kyber_v3/events.sol create mode 100644 contracts/avalanche/connectors/kyber_v3/helpers.sol create mode 100644 contracts/avalanche/connectors/kyber_v3/interface.sol create mode 100644 contracts/avalanche/connectors/kyber_v3/main.sol create mode 100644 contracts/fantom/connectors/kyber_v3/events.sol create mode 100644 contracts/fantom/connectors/kyber_v3/helpers.sol create mode 100644 contracts/fantom/connectors/kyber_v3/interface.sol create mode 100644 contracts/fantom/connectors/kyber_v3/main.sol create mode 100644 contracts/optimism/connectors/kyber_v3/events.sol create mode 100644 contracts/optimism/connectors/kyber_v3/helpers.sol create mode 100644 contracts/optimism/connectors/kyber_v3/interface.sol create mode 100644 contracts/optimism/connectors/kyber_v3/main.sol create mode 100644 contracts/polygon/connectors/kyber_v3/events.sol create mode 100644 contracts/polygon/connectors/kyber_v3/helpers.sol create mode 100644 contracts/polygon/connectors/kyber_v3/interface.sol create mode 100644 contracts/polygon/connectors/kyber_v3/main.sol diff --git a/contracts/arbitrum/connectors/kyber_v3/events.sol b/contracts/arbitrum/connectors/kyber_v3/events.sol new file mode 100644 index 00000000..dcba616e --- /dev/null +++ b/contracts/arbitrum/connectors/kyber_v3/events.sol @@ -0,0 +1,12 @@ +//SPDX-License-Identifier: MIT +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/arbitrum/connectors/kyber_v3/helpers.sol b/contracts/arbitrum/connectors/kyber_v3/helpers.sol new file mode 100644 index 00000000..e4ea6d8b --- /dev/null +++ b/contracts/arbitrum/connectors/kyber_v3/helpers.sol @@ -0,0 +1,66 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +import {DSMath} from "../../common/math.sol"; +import {Basic} from "../../common/basic.sol"; +import {TokenInterface} from "../../common/interfaces.sol"; + +abstract contract Helpers is DSMath, Basic { + struct SwapData { + TokenInterface sellToken; + TokenInterface buyToken; + uint256 _sellAmt; + uint256 _buyAmt; + uint256 unitAmt; + bytes callData; + } + + address internal constant kyberswap = + 0x6131B5fae19EA4f9D964eAc0408E4408b66337b5; + + 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, ) = kyberswap.call{value: wethAmt}(swapData.callData); + if (!success) revert("kyberswap-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 { + approve(TokenInterface(_sellAddr), kyberswap, swapData._sellAmt); + } + swapData._buyAmt = _swapHelper(swapData, ethAmt); + + setUint(setId, swapData._buyAmt); + + return swapData; + } +} diff --git a/contracts/arbitrum/connectors/kyber_v3/interface.sol b/contracts/arbitrum/connectors/kyber_v3/interface.sol new file mode 100644 index 00000000..7de1fbbf --- /dev/null +++ b/contracts/arbitrum/connectors/kyber_v3/interface.sol @@ -0,0 +1,6 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +interface AugustusSwapperInterface { + function getTokenTransferProxy() external view returns (address); +} \ No newline at end of file diff --git a/contracts/arbitrum/connectors/kyber_v3/main.sol b/contracts/arbitrum/connectors/kyber_v3/main.sol new file mode 100644 index 00000000..e0f1675d --- /dev/null +++ b/contracts/arbitrum/connectors/kyber_v3/main.sol @@ -0,0 +1,59 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +/** + * @title Kyber. + * @dev DEX Aggregator. + */ + +import {TokenInterface} from "../../common/interfaces.sol"; +import {Stores} from "../../common/stores.sol"; +import {Helpers} from "./helpers.sol"; + +abstract contract KyberArbitrumResolver is Helpers { + /** + * @dev Sell ETH/ERC20_Token using KyberSwap. + * @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain. + * @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAddr The address of the token to sell.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAmt The amount of the token to sell. + * @param unitAmt The amount of buyAmt/sellAmt with slippage. + * @param callData Data from kyberswap 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 ConnectV2KyberV3Arbitrum is KyberArbitrumResolver { + string public name = "Kyber-v3"; +} diff --git a/contracts/avalanche/connectors/kyber_v3/events.sol b/contracts/avalanche/connectors/kyber_v3/events.sol new file mode 100644 index 00000000..dcba616e --- /dev/null +++ b/contracts/avalanche/connectors/kyber_v3/events.sol @@ -0,0 +1,12 @@ +//SPDX-License-Identifier: MIT +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/avalanche/connectors/kyber_v3/helpers.sol b/contracts/avalanche/connectors/kyber_v3/helpers.sol new file mode 100644 index 00000000..2cd1c299 --- /dev/null +++ b/contracts/avalanche/connectors/kyber_v3/helpers.sol @@ -0,0 +1,66 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +import {DSMath} from "../../common/math.sol"; +import {Basic} from "../../common/basic.sol"; +import {TokenInterface} from "../../common/interfaces.sol"; + +abstract contract Helpers is DSMath, Basic { + struct SwapData { + TokenInterface sellToken; + TokenInterface buyToken; + uint256 _sellAmt; + uint256 _buyAmt; + uint256 unitAmt; + bytes callData; + } + + address internal constant kyberswap = + 0x6131B5fae19EA4f9D964eAc0408E4408b66337b5; + + 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, ) = kyberswap.call{value: wethAmt}(swapData.callData); + if (!success) revert("kyberswap-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) == avaxAddr) { + ethAmt = swapData._sellAmt; + } else { + approve(TokenInterface(_sellAddr), kyberswap, swapData._sellAmt); + } + swapData._buyAmt = _swapHelper(swapData, ethAmt); + + setUint(setId, swapData._buyAmt); + + return swapData; + } +} diff --git a/contracts/avalanche/connectors/kyber_v3/interface.sol b/contracts/avalanche/connectors/kyber_v3/interface.sol new file mode 100644 index 00000000..7de1fbbf --- /dev/null +++ b/contracts/avalanche/connectors/kyber_v3/interface.sol @@ -0,0 +1,6 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +interface AugustusSwapperInterface { + function getTokenTransferProxy() external view returns (address); +} \ No newline at end of file diff --git a/contracts/avalanche/connectors/kyber_v3/main.sol b/contracts/avalanche/connectors/kyber_v3/main.sol new file mode 100644 index 00000000..50e98c20 --- /dev/null +++ b/contracts/avalanche/connectors/kyber_v3/main.sol @@ -0,0 +1,59 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +/** + * @title Kyber. + * @dev DEX Aggregator. + */ + +import {TokenInterface} from "../../common/interfaces.sol"; +import {Stores} from "../../common/stores.sol"; +import {Helpers} from "./helpers.sol"; + +abstract contract KyberAvalancheResolver is Helpers { + /** + * @dev Sell ETH/ERC20_Token using KyberSwap. + * @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain. + * @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAddr The address of the token to sell.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAmt The amount of the token to sell. + * @param unitAmt The amount of buyAmt/sellAmt with slippage. + * @param callData Data from kyberswap 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 ConnectV2KyberV3Avalanche is KyberAvalancheResolver { + string public name = "Kyber-v3"; +} diff --git a/contracts/fantom/connectors/kyber_v3/events.sol b/contracts/fantom/connectors/kyber_v3/events.sol new file mode 100644 index 00000000..dcba616e --- /dev/null +++ b/contracts/fantom/connectors/kyber_v3/events.sol @@ -0,0 +1,12 @@ +//SPDX-License-Identifier: MIT +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/fantom/connectors/kyber_v3/helpers.sol b/contracts/fantom/connectors/kyber_v3/helpers.sol new file mode 100644 index 00000000..cb5dae6d --- /dev/null +++ b/contracts/fantom/connectors/kyber_v3/helpers.sol @@ -0,0 +1,66 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +import {DSMath} from "../../common/math.sol"; +import {Basic} from "../../common/basic.sol"; +import {TokenInterface} from "../../common/interfaces.sol"; + +abstract contract Helpers is DSMath, Basic { + struct SwapData { + TokenInterface sellToken; + TokenInterface buyToken; + uint256 _sellAmt; + uint256 _buyAmt; + uint256 unitAmt; + bytes callData; + } + + address internal constant kyberswap = + 0x6131B5fae19EA4f9D964eAc0408E4408b66337b5; + + 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, ) = kyberswap.call{value: wethAmt}(swapData.callData); + if (!success) revert("kyberswap-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) == ftmAddr) { + ethAmt = swapData._sellAmt; + } else { + approve(TokenInterface(_sellAddr), kyberswap, swapData._sellAmt); + } + swapData._buyAmt = _swapHelper(swapData, ethAmt); + + setUint(setId, swapData._buyAmt); + + return swapData; + } +} diff --git a/contracts/fantom/connectors/kyber_v3/interface.sol b/contracts/fantom/connectors/kyber_v3/interface.sol new file mode 100644 index 00000000..7de1fbbf --- /dev/null +++ b/contracts/fantom/connectors/kyber_v3/interface.sol @@ -0,0 +1,6 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +interface AugustusSwapperInterface { + function getTokenTransferProxy() external view returns (address); +} \ No newline at end of file diff --git a/contracts/fantom/connectors/kyber_v3/main.sol b/contracts/fantom/connectors/kyber_v3/main.sol new file mode 100644 index 00000000..e468a8df --- /dev/null +++ b/contracts/fantom/connectors/kyber_v3/main.sol @@ -0,0 +1,59 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +/** + * @title Kyber. + * @dev DEX Aggregator. + */ + +import {TokenInterface} from "../../common/interfaces.sol"; +import {Stores} from "../../common/stores.sol"; +import {Helpers} from "./helpers.sol"; + +abstract contract KyberFantomResolver is Helpers { + /** + * @dev Sell ETH/ERC20_Token using KyberSwap. + * @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain. + * @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAddr The address of the token to sell.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAmt The amount of the token to sell. + * @param unitAmt The amount of buyAmt/sellAmt with slippage. + * @param callData Data from kyberswap 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 ConnectV2KyberV3Fantom is KyberFantomResolver { + string public name = "Kyber-v3"; +} diff --git a/contracts/optimism/connectors/kyber_v3/events.sol b/contracts/optimism/connectors/kyber_v3/events.sol new file mode 100644 index 00000000..dcba616e --- /dev/null +++ b/contracts/optimism/connectors/kyber_v3/events.sol @@ -0,0 +1,12 @@ +//SPDX-License-Identifier: MIT +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/optimism/connectors/kyber_v3/helpers.sol b/contracts/optimism/connectors/kyber_v3/helpers.sol new file mode 100644 index 00000000..e4ea6d8b --- /dev/null +++ b/contracts/optimism/connectors/kyber_v3/helpers.sol @@ -0,0 +1,66 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +import {DSMath} from "../../common/math.sol"; +import {Basic} from "../../common/basic.sol"; +import {TokenInterface} from "../../common/interfaces.sol"; + +abstract contract Helpers is DSMath, Basic { + struct SwapData { + TokenInterface sellToken; + TokenInterface buyToken; + uint256 _sellAmt; + uint256 _buyAmt; + uint256 unitAmt; + bytes callData; + } + + address internal constant kyberswap = + 0x6131B5fae19EA4f9D964eAc0408E4408b66337b5; + + 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, ) = kyberswap.call{value: wethAmt}(swapData.callData); + if (!success) revert("kyberswap-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 { + approve(TokenInterface(_sellAddr), kyberswap, swapData._sellAmt); + } + swapData._buyAmt = _swapHelper(swapData, ethAmt); + + setUint(setId, swapData._buyAmt); + + return swapData; + } +} diff --git a/contracts/optimism/connectors/kyber_v3/interface.sol b/contracts/optimism/connectors/kyber_v3/interface.sol new file mode 100644 index 00000000..7de1fbbf --- /dev/null +++ b/contracts/optimism/connectors/kyber_v3/interface.sol @@ -0,0 +1,6 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +interface AugustusSwapperInterface { + function getTokenTransferProxy() external view returns (address); +} \ No newline at end of file diff --git a/contracts/optimism/connectors/kyber_v3/main.sol b/contracts/optimism/connectors/kyber_v3/main.sol new file mode 100644 index 00000000..4c5979a9 --- /dev/null +++ b/contracts/optimism/connectors/kyber_v3/main.sol @@ -0,0 +1,59 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +/** + * @title Kyber. + * @dev DEX Aggregator. + */ + +import {TokenInterface} from "../../common/interfaces.sol"; +import {Stores} from "../../common/stores.sol"; +import {Helpers} from "./helpers.sol"; + +abstract contract KyberOptimismResolver is Helpers { + /** + * @dev Sell ETH/ERC20_Token using KyberSwap. + * @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain. + * @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAddr The address of the token to sell.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAmt The amount of the token to sell. + * @param unitAmt The amount of buyAmt/sellAmt with slippage. + * @param callData Data from kyberswap 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 ConnectV2KyberV3Optimism is KyberOptimismResolver { + string public name = "Kyber-v3"; +} diff --git a/contracts/polygon/connectors/kyber_v3/events.sol b/contracts/polygon/connectors/kyber_v3/events.sol new file mode 100644 index 00000000..dcba616e --- /dev/null +++ b/contracts/polygon/connectors/kyber_v3/events.sol @@ -0,0 +1,12 @@ +//SPDX-License-Identifier: MIT +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/polygon/connectors/kyber_v3/helpers.sol b/contracts/polygon/connectors/kyber_v3/helpers.sol new file mode 100644 index 00000000..926f00cc --- /dev/null +++ b/contracts/polygon/connectors/kyber_v3/helpers.sol @@ -0,0 +1,66 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +import {DSMath} from "../../common/math.sol"; +import {Basic} from "../../common/basic.sol"; +import {TokenInterface} from "../../common/interfaces.sol"; + +abstract contract Helpers is DSMath, Basic { + struct SwapData { + TokenInterface sellToken; + TokenInterface buyToken; + uint256 _sellAmt; + uint256 _buyAmt; + uint256 unitAmt; + bytes callData; + } + + address internal constant kyberswap = + 0x6131B5fae19EA4f9D964eAc0408E4408b66337b5; + + 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, ) = kyberswap.call{value: wethAmt}(swapData.callData); + if (!success) revert("kyberswap-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) == maticAddr) { + ethAmt = swapData._sellAmt; + } else { + approve(TokenInterface(_sellAddr), kyberswap, swapData._sellAmt); + } + swapData._buyAmt = _swapHelper(swapData, ethAmt); + + setUint(setId, swapData._buyAmt); + + return swapData; + } +} diff --git a/contracts/polygon/connectors/kyber_v3/interface.sol b/contracts/polygon/connectors/kyber_v3/interface.sol new file mode 100644 index 00000000..7de1fbbf --- /dev/null +++ b/contracts/polygon/connectors/kyber_v3/interface.sol @@ -0,0 +1,6 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +interface AugustusSwapperInterface { + function getTokenTransferProxy() external view returns (address); +} \ No newline at end of file diff --git a/contracts/polygon/connectors/kyber_v3/main.sol b/contracts/polygon/connectors/kyber_v3/main.sol new file mode 100644 index 00000000..55d35dbd --- /dev/null +++ b/contracts/polygon/connectors/kyber_v3/main.sol @@ -0,0 +1,59 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +/** + * @title Kyber. + * @dev DEX Aggregator. + */ + +import {TokenInterface} from "../../common/interfaces.sol"; +import {Stores} from "../../common/stores.sol"; +import {Helpers} from "./helpers.sol"; + +abstract contract KyberPolygonResolver is Helpers { + /** + * @dev Sell ETH/ERC20_Token using KyberSwap. + * @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain. + * @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAddr The address of the token to sell.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAmt The amount of the token to sell. + * @param unitAmt The amount of buyAmt/sellAmt with slippage. + * @param callData Data from kyberswap 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 ConnectV2KyberV3Polygon is KyberPolygonResolver { + string public name = "Kyber-v3"; +} From 3450ab0ffc47bc3a3a6d351bc251de9cad86a34e Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Thu, 15 Jun 2023 01:04:18 +0530 Subject: [PATCH 5/5] Apply changes to Kyber contracts --- .../{kyber_v3 => kyber_aggregator}/events.sol | 0 .../{kyber_v3 => kyber_aggregator}/helpers.sol | 4 ++-- .../connectors/{kyber_v3 => kyber_aggregator}/main.sol | 4 ++-- contracts/arbitrum/connectors/kyber_v3/interface.sol | 6 ------ .../{kyber_v3 => kyber_aggregator}/events.sol | 0 .../{kyber_v3 => kyber_aggregator}/helpers.sol | 10 +++++----- .../connectors/{kyber_v3 => kyber_aggregator}/main.sol | 10 +++++----- contracts/avalanche/connectors/kyber_v3/interface.sol | 6 ------ .../{kyber_v3 => kyber_aggregator}/events.sol | 0 .../{kyber_v3 => kyber_aggregator}/helpers.sol | 10 +++++----- .../connectors/{kyber_v3 => kyber_aggregator}/main.sol | 10 +++++----- contracts/fantom/connectors/kyber_v3/interface.sol | 6 ------ .../kyber/{kyber_v3 => kyber_aggregator}/events.sol | 0 .../kyber/{kyber_v3 => kyber_aggregator}/helpers.sol | 4 ++-- .../kyber/{kyber_v3 => kyber_aggregator}/main.sol | 0 .../mainnet/connectors/kyber/kyber_v3/interface.sol | 6 ------ .../{kyber_v3 => kyber_aggregator}/events.sol | 0 .../{kyber_v3 => kyber_aggregator}/helpers.sol | 4 ++-- .../connectors/{kyber_v3 => kyber_aggregator}/main.sol | 4 ++-- contracts/optimism/connectors/kyber_v3/interface.sol | 6 ------ .../{kyber_v3 => kyber_aggregator}/events.sol | 0 .../{kyber_v3 => kyber_aggregator}/helpers.sol | 10 +++++----- .../connectors/{kyber_v3 => kyber_aggregator}/main.sol | 10 +++++----- contracts/polygon/connectors/kyber_v3/interface.sol | 6 ------ 24 files changed, 40 insertions(+), 76 deletions(-) rename contracts/arbitrum/connectors/{kyber_v3 => kyber_aggregator}/events.sol (100%) rename contracts/arbitrum/connectors/{kyber_v3 => kyber_aggregator}/helpers.sol (92%) rename contracts/arbitrum/connectors/{kyber_v3 => kyber_aggregator}/main.sol (93%) delete mode 100644 contracts/arbitrum/connectors/kyber_v3/interface.sol rename contracts/avalanche/connectors/{kyber_v3 => kyber_aggregator}/events.sol (100%) rename contracts/avalanche/connectors/{kyber_v3 => kyber_aggregator}/helpers.sol (86%) rename contracts/avalanche/connectors/{kyber_v3 => kyber_aggregator}/main.sol (79%) delete mode 100644 contracts/avalanche/connectors/kyber_v3/interface.sol rename contracts/fantom/connectors/{kyber_v3 => kyber_aggregator}/events.sol (100%) rename contracts/fantom/connectors/{kyber_v3 => kyber_aggregator}/helpers.sol (85%) rename contracts/fantom/connectors/{kyber_v3 => kyber_aggregator}/main.sol (84%) delete mode 100644 contracts/fantom/connectors/kyber_v3/interface.sol rename contracts/mainnet/connectors/kyber/{kyber_v3 => kyber_aggregator}/events.sol (100%) rename contracts/mainnet/connectors/kyber/{kyber_v3 => kyber_aggregator}/helpers.sol (92%) rename contracts/mainnet/connectors/kyber/{kyber_v3 => kyber_aggregator}/main.sol (100%) delete mode 100644 contracts/mainnet/connectors/kyber/kyber_v3/interface.sol rename contracts/optimism/connectors/{kyber_v3 => kyber_aggregator}/events.sol (100%) rename contracts/optimism/connectors/{kyber_v3 => kyber_aggregator}/helpers.sol (92%) rename contracts/optimism/connectors/{kyber_v3 => kyber_aggregator}/main.sol (93%) delete mode 100644 contracts/optimism/connectors/kyber_v3/interface.sol rename contracts/polygon/connectors/{kyber_v3 => kyber_aggregator}/events.sol (100%) rename contracts/polygon/connectors/{kyber_v3 => kyber_aggregator}/helpers.sol (85%) rename contracts/polygon/connectors/{kyber_v3 => kyber_aggregator}/main.sol (82%) delete mode 100644 contracts/polygon/connectors/kyber_v3/interface.sol diff --git a/contracts/arbitrum/connectors/kyber_v3/events.sol b/contracts/arbitrum/connectors/kyber_aggregator/events.sol similarity index 100% rename from contracts/arbitrum/connectors/kyber_v3/events.sol rename to contracts/arbitrum/connectors/kyber_aggregator/events.sol diff --git a/contracts/arbitrum/connectors/kyber_v3/helpers.sol b/contracts/arbitrum/connectors/kyber_aggregator/helpers.sol similarity index 92% rename from contracts/arbitrum/connectors/kyber_v3/helpers.sol rename to contracts/arbitrum/connectors/kyber_aggregator/helpers.sol index e4ea6d8b..5d2d297a 100644 --- a/contracts/arbitrum/connectors/kyber_v3/helpers.sol +++ b/contracts/arbitrum/connectors/kyber_aggregator/helpers.sol @@ -18,7 +18,7 @@ abstract contract Helpers is DSMath, Basic { address internal constant kyberswap = 0x6131B5fae19EA4f9D964eAc0408E4408b66337b5; - function _swapHelper(SwapData memory swapData, uint256 wethAmt) + function _swapHelper(SwapData memory swapData, uint256 ethAmt) internal returns (uint256 buyAmt) { @@ -34,7 +34,7 @@ abstract contract Helpers is DSMath, Basic { ); uint256 initalBal = getTokenBal(buyToken); - (bool success, ) = kyberswap.call{value: wethAmt}(swapData.callData); + (bool success, ) = kyberswap.call{value: ethAmt}(swapData.callData); if (!success) revert("kyberswap-failed"); uint256 finalBal = getTokenBal(buyToken); diff --git a/contracts/arbitrum/connectors/kyber_v3/main.sol b/contracts/arbitrum/connectors/kyber_aggregator/main.sol similarity index 93% rename from contracts/arbitrum/connectors/kyber_v3/main.sol rename to contracts/arbitrum/connectors/kyber_aggregator/main.sol index e0f1675d..6d151700 100644 --- a/contracts/arbitrum/connectors/kyber_v3/main.sol +++ b/contracts/arbitrum/connectors/kyber_aggregator/main.sol @@ -54,6 +54,6 @@ abstract contract KyberArbitrumResolver is Helpers { } } -contract ConnectV2KyberV3Arbitrum is KyberArbitrumResolver { - string public name = "Kyber-v3"; +contract ConnectV2KyberAggregatorArbitrum is KyberArbitrumResolver { + string public name = "Kyber-aggregator-v1.0"; } diff --git a/contracts/arbitrum/connectors/kyber_v3/interface.sol b/contracts/arbitrum/connectors/kyber_v3/interface.sol deleted file mode 100644 index 7de1fbbf..00000000 --- a/contracts/arbitrum/connectors/kyber_v3/interface.sol +++ /dev/null @@ -1,6 +0,0 @@ -//SPDX-License-Identifier: MIT -pragma solidity ^0.7.0; - -interface AugustusSwapperInterface { - function getTokenTransferProxy() external view returns (address); -} \ No newline at end of file diff --git a/contracts/avalanche/connectors/kyber_v3/events.sol b/contracts/avalanche/connectors/kyber_aggregator/events.sol similarity index 100% rename from contracts/avalanche/connectors/kyber_v3/events.sol rename to contracts/avalanche/connectors/kyber_aggregator/events.sol diff --git a/contracts/avalanche/connectors/kyber_v3/helpers.sol b/contracts/avalanche/connectors/kyber_aggregator/helpers.sol similarity index 86% rename from contracts/avalanche/connectors/kyber_v3/helpers.sol rename to contracts/avalanche/connectors/kyber_aggregator/helpers.sol index 2cd1c299..ac5c7651 100644 --- a/contracts/avalanche/connectors/kyber_v3/helpers.sol +++ b/contracts/avalanche/connectors/kyber_aggregator/helpers.sol @@ -18,7 +18,7 @@ abstract contract Helpers is DSMath, Basic { address internal constant kyberswap = 0x6131B5fae19EA4f9D964eAc0408E4408b66337b5; - function _swapHelper(SwapData memory swapData, uint256 wethAmt) + function _swapHelper(SwapData memory swapData, uint256 avaxAmt) internal returns (uint256 buyAmt) { @@ -34,7 +34,7 @@ abstract contract Helpers is DSMath, Basic { ); uint256 initalBal = getTokenBal(buyToken); - (bool success, ) = kyberswap.call{value: wethAmt}(swapData.callData); + (bool success, ) = kyberswap.call{value: avaxAmt}(swapData.callData); if (!success) revert("kyberswap-failed"); uint256 finalBal = getTokenBal(buyToken); @@ -50,14 +50,14 @@ abstract contract Helpers is DSMath, Basic { { TokenInterface _sellAddr = swapData.sellToken; - uint256 ethAmt; + uint256 avaxAmt; if (address(_sellAddr) == avaxAddr) { - ethAmt = swapData._sellAmt; + avaxAmt = swapData._sellAmt; } else { approve(TokenInterface(_sellAddr), kyberswap, swapData._sellAmt); } - swapData._buyAmt = _swapHelper(swapData, ethAmt); + swapData._buyAmt = _swapHelper(swapData, avaxAmt); setUint(setId, swapData._buyAmt); diff --git a/contracts/avalanche/connectors/kyber_v3/main.sol b/contracts/avalanche/connectors/kyber_aggregator/main.sol similarity index 79% rename from contracts/avalanche/connectors/kyber_v3/main.sol rename to contracts/avalanche/connectors/kyber_aggregator/main.sol index 50e98c20..5bd2f25c 100644 --- a/contracts/avalanche/connectors/kyber_v3/main.sol +++ b/contracts/avalanche/connectors/kyber_aggregator/main.sol @@ -12,10 +12,10 @@ import {Helpers} from "./helpers.sol"; abstract contract KyberAvalancheResolver is Helpers { /** - * @dev Sell ETH/ERC20_Token using KyberSwap. + * @dev Sell AVAX/ERC20_Token using KyberSwap. * @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain. - * @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) - * @param sellAddr The address of the token to sell.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param 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 kyberswap API. @@ -54,6 +54,6 @@ abstract contract KyberAvalancheResolver is Helpers { } } -contract ConnectV2KyberV3Avalanche is KyberAvalancheResolver { - string public name = "Kyber-v3"; +contract ConnectV2KyberAggregatorAvalanche is KyberAvalancheResolver { + string public name = "Kyber-aggregator-v1.0"; } diff --git a/contracts/avalanche/connectors/kyber_v3/interface.sol b/contracts/avalanche/connectors/kyber_v3/interface.sol deleted file mode 100644 index 7de1fbbf..00000000 --- a/contracts/avalanche/connectors/kyber_v3/interface.sol +++ /dev/null @@ -1,6 +0,0 @@ -//SPDX-License-Identifier: MIT -pragma solidity ^0.7.0; - -interface AugustusSwapperInterface { - function getTokenTransferProxy() external view returns (address); -} \ No newline at end of file diff --git a/contracts/fantom/connectors/kyber_v3/events.sol b/contracts/fantom/connectors/kyber_aggregator/events.sol similarity index 100% rename from contracts/fantom/connectors/kyber_v3/events.sol rename to contracts/fantom/connectors/kyber_aggregator/events.sol diff --git a/contracts/fantom/connectors/kyber_v3/helpers.sol b/contracts/fantom/connectors/kyber_aggregator/helpers.sol similarity index 85% rename from contracts/fantom/connectors/kyber_v3/helpers.sol rename to contracts/fantom/connectors/kyber_aggregator/helpers.sol index cb5dae6d..188f7da1 100644 --- a/contracts/fantom/connectors/kyber_v3/helpers.sol +++ b/contracts/fantom/connectors/kyber_aggregator/helpers.sol @@ -18,7 +18,7 @@ abstract contract Helpers is DSMath, Basic { address internal constant kyberswap = 0x6131B5fae19EA4f9D964eAc0408E4408b66337b5; - function _swapHelper(SwapData memory swapData, uint256 wethAmt) + function _swapHelper(SwapData memory swapData, uint256 ftmAmt) internal returns (uint256 buyAmt) { @@ -34,7 +34,7 @@ abstract contract Helpers is DSMath, Basic { ); uint256 initalBal = getTokenBal(buyToken); - (bool success, ) = kyberswap.call{value: wethAmt}(swapData.callData); + (bool success, ) = kyberswap.call{value: ftmAmt}(swapData.callData); if (!success) revert("kyberswap-failed"); uint256 finalBal = getTokenBal(buyToken); @@ -50,14 +50,14 @@ abstract contract Helpers is DSMath, Basic { { TokenInterface _sellAddr = swapData.sellToken; - uint256 ethAmt; + uint256 ftmAmt; if (address(_sellAddr) == ftmAddr) { - ethAmt = swapData._sellAmt; + ftmAmt = swapData._sellAmt; } else { approve(TokenInterface(_sellAddr), kyberswap, swapData._sellAmt); } - swapData._buyAmt = _swapHelper(swapData, ethAmt); + swapData._buyAmt = _swapHelper(swapData, ftmAmt); setUint(setId, swapData._buyAmt); diff --git a/contracts/fantom/connectors/kyber_v3/main.sol b/contracts/fantom/connectors/kyber_aggregator/main.sol similarity index 84% rename from contracts/fantom/connectors/kyber_v3/main.sol rename to contracts/fantom/connectors/kyber_aggregator/main.sol index e468a8df..fa5603a6 100644 --- a/contracts/fantom/connectors/kyber_v3/main.sol +++ b/contracts/fantom/connectors/kyber_aggregator/main.sol @@ -12,10 +12,10 @@ import {Helpers} from "./helpers.sol"; abstract contract KyberFantomResolver is Helpers { /** - * @dev Sell ETH/ERC20_Token using KyberSwap. + * @dev Sell ftm/ERC20_Token using KyberSwap. * @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain. - * @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) - * @param sellAddr The address of the token to sell.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param buyAddr The address of the token to buy.(For FTM: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAddr The address of the token to sell.(For FTM: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) * @param sellAmt The amount of the token to sell. * @param unitAmt The amount of buyAmt/sellAmt with slippage. * @param callData Data from kyberswap API. @@ -54,6 +54,6 @@ abstract contract KyberFantomResolver is Helpers { } } -contract ConnectV2KyberV3Fantom is KyberFantomResolver { - string public name = "Kyber-v3"; +contract ConnectV2KyberAggregatorFantom is KyberFantomResolver { + string public name = "Kyber-aggregator-v1.0"; } diff --git a/contracts/fantom/connectors/kyber_v3/interface.sol b/contracts/fantom/connectors/kyber_v3/interface.sol deleted file mode 100644 index 7de1fbbf..00000000 --- a/contracts/fantom/connectors/kyber_v3/interface.sol +++ /dev/null @@ -1,6 +0,0 @@ -//SPDX-License-Identifier: MIT -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/kyber/kyber_v3/events.sol b/contracts/mainnet/connectors/kyber/kyber_aggregator/events.sol similarity index 100% rename from contracts/mainnet/connectors/kyber/kyber_v3/events.sol rename to contracts/mainnet/connectors/kyber/kyber_aggregator/events.sol diff --git a/contracts/mainnet/connectors/kyber/kyber_v3/helpers.sol b/contracts/mainnet/connectors/kyber/kyber_aggregator/helpers.sol similarity index 92% rename from contracts/mainnet/connectors/kyber/kyber_v3/helpers.sol rename to contracts/mainnet/connectors/kyber/kyber_aggregator/helpers.sol index 5b38d196..1b429f2d 100644 --- a/contracts/mainnet/connectors/kyber/kyber_v3/helpers.sol +++ b/contracts/mainnet/connectors/kyber/kyber_aggregator/helpers.sol @@ -18,7 +18,7 @@ abstract contract Helpers is DSMath, Basic { address internal constant kyberswap = 0x6131B5fae19EA4f9D964eAc0408E4408b66337b5; - function _swapHelper(SwapData memory swapData, uint256 wethAmt) + function _swapHelper(SwapData memory swapData, uint256 ethAmt) internal returns (uint256 buyAmt) { @@ -34,7 +34,7 @@ abstract contract Helpers is DSMath, Basic { ); uint256 initalBal = getTokenBal(buyToken); - (bool success, ) = kyberswap.call{value: wethAmt}(swapData.callData); + (bool success, ) = kyberswap.call{value: ethAmt}(swapData.callData); if (!success) revert("kyberswap-failed"); uint256 finalBal = getTokenBal(buyToken); diff --git a/contracts/mainnet/connectors/kyber/kyber_v3/main.sol b/contracts/mainnet/connectors/kyber/kyber_aggregator/main.sol similarity index 100% rename from contracts/mainnet/connectors/kyber/kyber_v3/main.sol rename to contracts/mainnet/connectors/kyber/kyber_aggregator/main.sol diff --git a/contracts/mainnet/connectors/kyber/kyber_v3/interface.sol b/contracts/mainnet/connectors/kyber/kyber_v3/interface.sol deleted file mode 100644 index 7de1fbbf..00000000 --- a/contracts/mainnet/connectors/kyber/kyber_v3/interface.sol +++ /dev/null @@ -1,6 +0,0 @@ -//SPDX-License-Identifier: MIT -pragma solidity ^0.7.0; - -interface AugustusSwapperInterface { - function getTokenTransferProxy() external view returns (address); -} \ No newline at end of file diff --git a/contracts/optimism/connectors/kyber_v3/events.sol b/contracts/optimism/connectors/kyber_aggregator/events.sol similarity index 100% rename from contracts/optimism/connectors/kyber_v3/events.sol rename to contracts/optimism/connectors/kyber_aggregator/events.sol diff --git a/contracts/optimism/connectors/kyber_v3/helpers.sol b/contracts/optimism/connectors/kyber_aggregator/helpers.sol similarity index 92% rename from contracts/optimism/connectors/kyber_v3/helpers.sol rename to contracts/optimism/connectors/kyber_aggregator/helpers.sol index e4ea6d8b..5d2d297a 100644 --- a/contracts/optimism/connectors/kyber_v3/helpers.sol +++ b/contracts/optimism/connectors/kyber_aggregator/helpers.sol @@ -18,7 +18,7 @@ abstract contract Helpers is DSMath, Basic { address internal constant kyberswap = 0x6131B5fae19EA4f9D964eAc0408E4408b66337b5; - function _swapHelper(SwapData memory swapData, uint256 wethAmt) + function _swapHelper(SwapData memory swapData, uint256 ethAmt) internal returns (uint256 buyAmt) { @@ -34,7 +34,7 @@ abstract contract Helpers is DSMath, Basic { ); uint256 initalBal = getTokenBal(buyToken); - (bool success, ) = kyberswap.call{value: wethAmt}(swapData.callData); + (bool success, ) = kyberswap.call{value: ethAmt}(swapData.callData); if (!success) revert("kyberswap-failed"); uint256 finalBal = getTokenBal(buyToken); diff --git a/contracts/optimism/connectors/kyber_v3/main.sol b/contracts/optimism/connectors/kyber_aggregator/main.sol similarity index 93% rename from contracts/optimism/connectors/kyber_v3/main.sol rename to contracts/optimism/connectors/kyber_aggregator/main.sol index 4c5979a9..4e8acb9c 100644 --- a/contracts/optimism/connectors/kyber_v3/main.sol +++ b/contracts/optimism/connectors/kyber_aggregator/main.sol @@ -54,6 +54,6 @@ abstract contract KyberOptimismResolver is Helpers { } } -contract ConnectV2KyberV3Optimism is KyberOptimismResolver { - string public name = "Kyber-v3"; +contract ConnectV2KyberAggregatorOptimism is KyberOptimismResolver { + string public name = "Kyber-aggregator-v1.0"; } diff --git a/contracts/optimism/connectors/kyber_v3/interface.sol b/contracts/optimism/connectors/kyber_v3/interface.sol deleted file mode 100644 index 7de1fbbf..00000000 --- a/contracts/optimism/connectors/kyber_v3/interface.sol +++ /dev/null @@ -1,6 +0,0 @@ -//SPDX-License-Identifier: MIT -pragma solidity ^0.7.0; - -interface AugustusSwapperInterface { - function getTokenTransferProxy() external view returns (address); -} \ No newline at end of file diff --git a/contracts/polygon/connectors/kyber_v3/events.sol b/contracts/polygon/connectors/kyber_aggregator/events.sol similarity index 100% rename from contracts/polygon/connectors/kyber_v3/events.sol rename to contracts/polygon/connectors/kyber_aggregator/events.sol diff --git a/contracts/polygon/connectors/kyber_v3/helpers.sol b/contracts/polygon/connectors/kyber_aggregator/helpers.sol similarity index 85% rename from contracts/polygon/connectors/kyber_v3/helpers.sol rename to contracts/polygon/connectors/kyber_aggregator/helpers.sol index 926f00cc..1cc47870 100644 --- a/contracts/polygon/connectors/kyber_v3/helpers.sol +++ b/contracts/polygon/connectors/kyber_aggregator/helpers.sol @@ -18,7 +18,7 @@ abstract contract Helpers is DSMath, Basic { address internal constant kyberswap = 0x6131B5fae19EA4f9D964eAc0408E4408b66337b5; - function _swapHelper(SwapData memory swapData, uint256 wethAmt) + function _swapHelper(SwapData memory swapData, uint256 maticAmt) internal returns (uint256 buyAmt) { @@ -34,7 +34,7 @@ abstract contract Helpers is DSMath, Basic { ); uint256 initalBal = getTokenBal(buyToken); - (bool success, ) = kyberswap.call{value: wethAmt}(swapData.callData); + (bool success, ) = kyberswap.call{value: maticAmt}(swapData.callData); if (!success) revert("kyberswap-failed"); uint256 finalBal = getTokenBal(buyToken); @@ -50,14 +50,14 @@ abstract contract Helpers is DSMath, Basic { { TokenInterface _sellAddr = swapData.sellToken; - uint256 ethAmt; + uint256 maticAmt; if (address(_sellAddr) == maticAddr) { - ethAmt = swapData._sellAmt; + maticAmt = swapData._sellAmt; } else { approve(TokenInterface(_sellAddr), kyberswap, swapData._sellAmt); } - swapData._buyAmt = _swapHelper(swapData, ethAmt); + swapData._buyAmt = _swapHelper(swapData, maticAmt); setUint(setId, swapData._buyAmt); diff --git a/contracts/polygon/connectors/kyber_v3/main.sol b/contracts/polygon/connectors/kyber_aggregator/main.sol similarity index 82% rename from contracts/polygon/connectors/kyber_v3/main.sol rename to contracts/polygon/connectors/kyber_aggregator/main.sol index 55d35dbd..134ab153 100644 --- a/contracts/polygon/connectors/kyber_v3/main.sol +++ b/contracts/polygon/connectors/kyber_aggregator/main.sol @@ -12,10 +12,10 @@ import {Helpers} from "./helpers.sol"; abstract contract KyberPolygonResolver is Helpers { /** - * @dev Sell ETH/ERC20_Token using KyberSwap. + * @dev Sell MATIC/ERC20_Token using KyberSwap. * @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain. - * @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) - * @param sellAddr The address of the token to sell.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param 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 kyberswap API. @@ -54,6 +54,6 @@ abstract contract KyberPolygonResolver is Helpers { } } -contract ConnectV2KyberV3Polygon is KyberPolygonResolver { - string public name = "Kyber-v3"; +contract ConnectV2KyberAggregatorPolygon is KyberPolygonResolver { + string public name = "Kyber-aggregator-v1.0"; } diff --git a/contracts/polygon/connectors/kyber_v3/interface.sol b/contracts/polygon/connectors/kyber_v3/interface.sol deleted file mode 100644 index 7de1fbbf..00000000 --- a/contracts/polygon/connectors/kyber_v3/interface.sol +++ /dev/null @@ -1,6 +0,0 @@ -//SPDX-License-Identifier: MIT -pragma solidity ^0.7.0; - -interface AugustusSwapperInterface { - function getTokenTransferProxy() external view returns (address); -} \ No newline at end of file