-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tasks: Implement KyberSwap v2 swapper (#154)
Co-authored-by: Facu Spagnuolo <[email protected]>
- Loading branch information
1 parent
1976b99
commit 1265133
Showing
30 changed files
with
1,049 additions
and
38 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
packages/connectors/contracts/interfaces/kyberswap/IKyberSwapV2Connector.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
pragma solidity >=0.8.0; | ||
|
||
/** | ||
* @title KyberSwap V2 connector interface | ||
*/ | ||
interface IKyberSwapV2Connector { | ||
/** | ||
* @dev The token in is the same as the token out | ||
*/ | ||
error KyberSwapV2SwapSameToken(address token); | ||
|
||
/** | ||
* @dev The amount out is lower than the minimum amount out | ||
*/ | ||
error KyberSwapV2BadAmountOut(uint256 amountOut, uint256 minAmountOut); | ||
|
||
/** | ||
* @dev The post token in balance is lower than the previous token in balance minus the amount in | ||
*/ | ||
error KyberSwapV2BadPostTokenInBalance(uint256 postBalanceIn, uint256 preBalanceIn, uint256 amountIn); | ||
|
||
/** | ||
* @dev Tells the reference to KyberSwap aggregation router v2 | ||
*/ | ||
function kyberSwapV2Router() external view returns (address); | ||
|
||
/** | ||
* @dev Executes a token swap in KyberSwap V2 | ||
* @param tokenIn Token to be sent | ||
* @param tokenOut Token to be received | ||
* @param amountIn Amount of token in to be swapped | ||
* @param minAmountOut Minimum amount of token out willing to receive | ||
* @param data Calldata to be sent to the KyberSwap aggregation router | ||
*/ | ||
function execute(address tokenIn, address tokenOut, uint256 amountIn, uint256 minAmountOut, bytes memory data) | ||
external | ||
returns (uint256 amountOut); | ||
} |
70 changes: 70 additions & 0 deletions
70
packages/connectors/contracts/kyberswap/KyberSwapV2Connector.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import '@openzeppelin/contracts/token/ERC20/IERC20.sol'; | ||
import '@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol'; | ||
import '@openzeppelin/contracts/utils/Address.sol'; | ||
|
||
import '@mimic-fi/v3-helpers/contracts/utils/ERC20Helpers.sol'; | ||
|
||
import '../interfaces/kyberswap/IKyberSwapV2Connector.sol'; | ||
|
||
/** | ||
* @title KyberSwapV2Connector | ||
* @dev Interfaces with KyberSwap V2 to swap tokens | ||
*/ | ||
contract KyberSwapV2Connector is IKyberSwapV2Connector { | ||
// Reference to KyberSwap aggregation router v2 | ||
address public immutable override kyberSwapV2Router; | ||
|
||
/** | ||
* @dev Creates a new KyberSwapV2Connector contract | ||
* @param _kyberSwapV2Router KyberSwap aggregation router v2 reference | ||
*/ | ||
constructor(address _kyberSwapV2Router) { | ||
kyberSwapV2Router = _kyberSwapV2Router; | ||
} | ||
|
||
/** | ||
* @dev Executes a token swap in KyberSwap V2 | ||
* @param tokenIn Token to be sent | ||
* @param tokenOut Token to be received | ||
* @param amountIn Amount of token in to be swapped | ||
* @param minAmountOut Minimum amount of token out willing to receive | ||
* @param data Calldata to be sent to the KyberSwap aggregation router | ||
*/ | ||
function execute(address tokenIn, address tokenOut, uint256 amountIn, uint256 minAmountOut, bytes memory data) | ||
external | ||
override | ||
returns (uint256 amountOut) | ||
{ | ||
if (tokenIn == tokenOut) revert KyberSwapV2SwapSameToken(tokenIn); | ||
|
||
uint256 preBalanceIn = IERC20(tokenIn).balanceOf(address(this)); | ||
uint256 preBalanceOut = IERC20(tokenOut).balanceOf(address(this)); | ||
|
||
ERC20Helpers.approve(tokenIn, kyberSwapV2Router, amountIn); | ||
Address.functionCall(kyberSwapV2Router, data, 'KYBER_SWAP_V2_SWAP_FAILED'); | ||
|
||
uint256 postBalanceIn = IERC20(tokenIn).balanceOf(address(this)); | ||
bool isPostBalanceInUnexpected = postBalanceIn < preBalanceIn - amountIn; | ||
if (isPostBalanceInUnexpected) revert KyberSwapV2BadPostTokenInBalance(postBalanceIn, preBalanceIn, amountIn); | ||
|
||
uint256 postBalanceOut = IERC20(tokenOut).balanceOf(address(this)); | ||
amountOut = postBalanceOut - preBalanceOut; | ||
if (amountOut < minAmountOut) revert KyberSwapV2BadAmountOut(amountOut, minAmountOut); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import axios, { AxiosError } from 'axios' | ||
import { BigNumber, Contract } from 'ethers' | ||
|
||
const KYBER_SWAP_URL = 'https://aggregator-api.kyberswap.com' | ||
export type SwapResponse = { data: { data: { data: string } } } | ||
|
||
const CHAINS: { [key: number]: string } = { | ||
1: 'ethereum', | ||
8453: 'base', | ||
} | ||
|
||
export async function getKyberSwapSwapData( | ||
chainId: number, | ||
sender: Contract, | ||
tokenIn: Contract, | ||
tokenOut: Contract, | ||
amountIn: BigNumber, | ||
slippage: number | ||
): Promise<string> { | ||
try { | ||
const response = await getSwap(chainId, sender, tokenIn, tokenOut, amountIn, slippage) | ||
return response.data.data.data | ||
} catch (error) { | ||
if (error instanceof AxiosError) throw Error(error.toString() + ' - ' + error.response?.data?.description) | ||
else throw error | ||
} | ||
} | ||
|
||
async function getSwap( | ||
chainId: number, | ||
sender: Contract, | ||
tokenIn: Contract, | ||
tokenOut: Contract, | ||
amountIn: BigNumber, | ||
slippage: number | ||
): Promise<SwapResponse> { | ||
const chain = CHAINS[chainId] | ||
const response = await axios.get(`${KYBER_SWAP_URL}/${chain}/api/v1/routes`, { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json', | ||
}, | ||
params: { | ||
tokenIn: tokenIn.address, | ||
tokenOut: tokenOut.address, | ||
amountIn: amountIn.toString(), | ||
saveGas: true, | ||
}, | ||
}) | ||
|
||
// The value is in ranges [0, 2000], 10 means 0.1% | ||
const slippageTolerance = Math.floor(slippage < 1 ? slippage * 10000 : slippage) | ||
return await axios.post( | ||
`${KYBER_SWAP_URL}/${chain}/api/v1/route/build`, | ||
{ | ||
routeSummary: response.data.data.routeSummary, | ||
slippageTolerance, | ||
sender: sender.address, | ||
recipient: sender.address, | ||
}, | ||
{ | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json', | ||
}, | ||
} | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
packages/connectors/test/helpers/1inch-v5/fixtures/1/17525323/USDC-WBTC.json
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
packages/connectors/test/helpers/1inch-v5/fixtures/1/19932950/USDC-WBTC.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"tokenIn": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", | ||
"tokenOut": "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599", | ||
"amountIn": "10000000000", | ||
"slippage": 0.015, | ||
"data": "0x12aa3caf000000000000000000000000e37e799d5077682fa0a244d46e5649f71457bd09000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000002260fac5e5542a773aa44fbcfedf7c193bc2c599000000000000000000000000e37e799d5077682fa0a244d46e5649f71457bd09000000000000000000000000f42ec71a4440f5e9871c643696dd6dc9a38911f800000000000000000000000000000000000000000000000000000002540be4000000000000000000000000000000000000000000000000000000000000ddb859000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002540000000000000000000000000000000000000000000002360002080001be00a007e5c0d200000000000000000000000000000000000000000000019a0000ca0000b051204dece678ceceb27446b35c672dc7d61f30bad69ea0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800443df02124000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000217507ae7cc9d125d3b0020d6bdbf78f939e0a03fb07f59a73314e73794be0e57ac1b4e5122e0438eb3703bf871e31ce639bd351109c88666eaf939e0a03fb07f59a73314e73794be0e57ac1b4e0044a64833a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e37e799d5077682fa0a244d46e5649f71457bd0900a0f2fa6b662260fac5e5542a773aa44fbcfedf7c193bc2c5990000000000000000000000000000000000000000000000000000000000e118b800000000000000000000000000001cac80a06c4eca272260fac5e5542a773aa44fbcfedf7c193bc2c5991111111254eeb25477b68fb85ed929f73a9605820000000000000000000000008bb21a3d" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.