Skip to content

Commit

Permalink
Merge branch 'master' of github.com-facuspagnuolo:mimic-fi/v3-core in…
Browse files Browse the repository at this point in the history
…to connectors/implement_hop_swap_connector
  • Loading branch information
facuspagnuolo committed Jun 26, 2023
2 parents 20f5a5c + 597b23c commit 48ecc9d
Show file tree
Hide file tree
Showing 21 changed files with 86 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-connectors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
- name: Set up environment
uses: ./.github/actions/setup
- name: Set up hardhat config
run: .github/scripts/setup-hardhat-config.sh ${{secrets.GOERLI_RPC}} ${{secrets.MUMBAI_RPC}} ${{secrets.MAINNET_RPC}} ${{secrets.POLYGON_RPC}} ${{secrets.OPTIMISM_RPC}} ${{secrets.ARBITRUM_RPC}} ${{secrets.GNOSIS_RPC}} ${{secrets.AVALANCHE_RPC}} ${{secrets.BSC_RPC}} ${{secrets.FANTOM_RPC}}
run: .github/scripts/setup-hardhat-config.sh ${{secrets.MAINNET_RPC}} ${{secrets.POLYGON_RPC}} ${{secrets.OPTIMISM_RPC}} ${{secrets.ARBITRUM_RPC}} ${{secrets.GNOSIS_RPC}} ${{secrets.AVALANCHE_RPC}} ${{secrets.BSC_RPC}} ${{secrets.FANTOM_RPC}}
- name: Build
run: yarn build
- name: Test mainnet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ contract UniswapV2Connector {
* @param tokenOut Token being received
* @param amountIn Amount of tokenIn being swapped
* @param minAmountOut Minimum amount of tokenOut willing to receive
* @param hopTokens List of hop-tokens between tokenIn and tokenOut
* @param hopTokens Optional list of hop-tokens between tokenIn and tokenOut, only used for multi-hops
*/
function execute(
address tokenIn,
Expand All @@ -64,8 +64,8 @@ contract UniswapV2Connector {

ERC20Helpers.approve(tokenIn, address(uniswapV2Router), amountIn);
hopTokens.length == 0
? _singleSwapUniswapV2(tokenIn, tokenOut, amountIn, minAmountOut)
: _batchSwapUniswapV2(tokenIn, tokenOut, amountIn, minAmountOut, hopTokens);
? _singleSwap(tokenIn, tokenOut, amountIn, minAmountOut)
: _batchSwap(tokenIn, tokenOut, amountIn, minAmountOut, hopTokens);

uint256 postBalanceIn = IERC20(tokenIn).balanceOf(address(this));
require(postBalanceIn >= preBalanceIn - amountIn, 'UNI_V2_BAD_TOKEN_IN_BALANCE');
Expand All @@ -82,7 +82,7 @@ contract UniswapV2Connector {
* @param amountIn Amount of tokenIn being swapped
* @param minAmountOut Minimum amount of tokenOut willing to receive
*/
function _singleSwapUniswapV2(address tokenIn, address tokenOut, uint256 amountIn, uint256 minAmountOut)
function _singleSwap(address tokenIn, address tokenOut, uint256 amountIn, uint256 minAmountOut)
internal
returns (uint256[] memory)
{
Expand All @@ -100,7 +100,7 @@ contract UniswapV2Connector {
* @param minAmountOut Minimum amount of the last token in the path willing to receive
* @param hopTokens List of hop-tokens between tokenIn and tokenOut
*/
function _batchSwapUniswapV2(
function _batchSwap(
address tokenIn,
address tokenOut,
uint256 amountIn,
Expand Down
2 changes: 1 addition & 1 deletion packages/connectors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"test:mainnet": "yarn test --fork mainnet --block-number 17525323",
"test:polygon": "yarn test --fork polygon --block-number 44153231",
"test:optimism": "yarn test --fork optimism --block-number 105914596",
"test:arbitrum": "yarn test --fork arbitrum --block-number 103739165",
"test:arbitrum": "yarn test --fork arbitrum --block-number 105116582",
"test:gnosis": "yarn test --fork gnosis --block-number 28580764",
"prepare": "yarn build"
},
Expand Down
47 changes: 47 additions & 0 deletions packages/connectors/src/1inch-v5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import axios, { AxiosError } from 'axios'
import { BigNumber, Contract } from 'ethers'

const ONE_INCH_URL = 'https://api.1inch.io/v5.0'

export type SwapResponse = { data: { tx: { data: string } } }

export async function get1inchSwapData(
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.tx.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> {
return axios.get(`${ONE_INCH_URL}/${chainId}/swap`, {
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
params: {
disableEstimate: true,
fromAddress: sender.address,
fromTokenAddress: tokenIn.address,
toTokenAddress: tokenOut.address,
amount: amountIn.toString(),
slippage: slippage < 1 ? slippage * 100 : slippage,
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import hre from 'hardhat'
import { HardhatNetworkConfig } from 'hardhat/types'
import path from 'path'

import { get1inchSwapData } from '../../../src/1inch'
import { get1inchSwapData } from '../../../src/1inch-v5'

type Fixture = {
tokenIn: string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"data": "0xa6886da90000000000000000000000000000000000000000000000000000000000000020000000000000000000000000ff970a61a04b1ca14834a43f5de4533ebddb5cc800000000000000000000000082af49447d8a07e3bd95bd0d56f35241523fbab1000000000000000000000000e592427a0aece92de3edee1f18e0157c0586156400000000000000000000000000000000000000000000000000000002540be4000000000000000000000000000000000000000000000000004891e1cf3a2e1aeb000000000000000000000000000000000000000000000000494d8995d881517e010000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000649995f30000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007973573d1113c256fee02ccf1b1e1be0eb93aad00000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000220690000a4f9c94f57be0c88dde4dca79e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002bff970a61a04b1ca14834a43f5de4533ebddb5cc80001f482af49447d8a07e3bd95bd0d56f35241523fbab10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"sig": "0x7bdfe0635d4a2a21ff15455ae33b35e6a8cacbe14d3fee51e0bf9b1e148954ce5cfe94c8ae6e11763a1d53462a0a55b4a8300a578ee57848ffd2b2897eb4f4e21b",
"signer": "0x6278c27CF5534F07fA8f1Ab6188a155cb8750FFA",
"tokenIn": "0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8",
"tokenOut": "0x82af49447d8a07e3bd95bd0d56f35241523fbab1",
"amountIn": "10000000000",
"minAmountOut": "5229208922504436459",
"expectedAmountOut": "5282029214650945918"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"data": "0x46c67b6d00000000000000000000000000000000000000000000000000000000000000200000000000000000000000002f2a2543b76a4166549f7aab2e75bef0aefc5b0f0000000000000000000000000000000000000000000000000000000005f5e10000000000000000000000000000000000000000000000000000000007051905d3000000000000000000000000000000000000000000000000000000071740103900000000000000000000000007973573d1113c256fee02ccf1b1e1be0eb93aad0000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000d400000000000000000000000000000000000000000000000000000000064999602960a7daba2c545bfabe2437bd7af9292000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000005600000000000000000000000000000000000000000000000000000000000002328000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000ff970a61a04b1ca14834a43f5de4533ebddb5cc800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000280000000000000000000000000745ec73855cec7249e5ff4c9dd81cc65b4d297a9000000000000000000000000000000000000000000000000000000000000037900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000e592427a0aece92de3edee1f18e0157c05861564000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000064a2d060000000000000000000000000000000000000000000000000000000000000002b2f2a2543b76a4166549f7aab2e75bef0aefc5b0f0001f4ff970a61a04b1ca14834a43f5de4533ebddb5cc80000000000000000000000000000000000000000000000000000000000000000003ad7f275e27ac579ca88e0b4765828242a9e8c49000000000000000000000000000000000000000000000000000000000000239700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000eff23b4be1091b53205e35f3afcd9c7182bf3062000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000d5b927956057075377263aab7f8afc12f85100db00000000000000000000000000000000000000000000000000000000000003e80000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000320000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000745ec73855cec7249e5ff4c9dd81cc65b4d297a9000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000e592427a0aece92de3edee1f18e0157c05861564000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000064a2d060000000000000000000000000000000000000000000000000000000000000002b2f2a2543b76a4166549f7aab2e75bef0aefc5b0f0001f482af49447d8a07e3bd95bd0d56f35241523fbab1000000000000000000000000000000000000000000000000000000000000000000ff970a61a04b1ca14834a43f5de4533ebddb5cc80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000745ec73855cec7249e5ff4c9dd81cc65b4d297a9000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000fa58b8024b49836772180f2df902f231ba712f72000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000064a2d060000000000000000000000000000000000000000000000000000000000000002882af49447d8a07e3bd95bd0d56f35241523fbab1ff970a61a04b1ca14834a43f5de4533ebddb5cc80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"sig": "0xa044506f1764c9f1152b797c7be49ffcf2b5b01e8bbdc458efb74898676e67be1d23f87072e2f3376d37254dab32ffc83b9d53a7290f29019c429f4b6a579a7e1b",
"signer": "0x6278c27CF5534F07fA8f1Ab6188a155cb8750FFA",
"tokenIn": "0x2f2a2543b76a4166549f7aab2e75bef0aefc5b0f",
"tokenOut": "0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8",
"amountIn": "100000000",
"minAmountOut": "30150297043",
"expectedAmountOut": "30454845497"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"data": "0x54e3f31b000000000000000000000000000000000000000000000000000000000000002000000000000000000000000082af49447d8a07e3bd95bd0d56f35241523fbab1000000000000000000000000ff970a61a04b1ca14834a43f5de4533ebddb5cc80000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000000000000000000000000000000000006f9f5bfb0000000000000000000000000000000000000000000000000000000070bfffd100000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000220000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000003e000000000000000000000000007973573d1113c256fee02ccf1b1e1be0eb93aad00000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000042000000000000000000000000000000000000000000000000000000000649995f83da4e0439a4a4a59bf7c961a70a911f4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000fa58b8024b49836772180f2df902f231ba712f720000000000000000000000000000000000000000000000000000000000000124c04b8d59000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000def171fe48cf0115b1d80b88dc8eab59176fee570000000000000000000000000000000000000000000000000000000064a2d04c0000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002882af49447d8a07e3bd95bd0d56f35241523fbab1ff970a61a04b1ca14834a43f5de4533ebddb5cc800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000124000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"sig": "0xf7846923136f7899cef3ca36cfc36420d4c98c8c48c1b6409b9aa20631bcbe4d4c6880e1539a24b4227ea31ed7536b14e537c6e74983e243f7a44118fb4ed6371b",
"signer": "0x6278c27CF5534F07fA8f1Ab6188a155cb8750FFA",
"tokenIn": "0x82af49447d8a07e3bd95bd0d56f35241523fbab1",
"tokenOut": "0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8",
"amountIn": "1000000000000000000",
"minAmountOut": "1872714747",
"expectedAmountOut": "1891631057"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'
import { expect } from 'chai'
import { BigNumber, Contract } from 'ethers'

import { loadOrGet1inchSwapData } from '../../helpers/1inch'
import { loadOrGet1inchSwapData } from '../../helpers/1inch-v5'

export function itBehavesLikeOneInchV5Connector(
CHAIN: number,
Expand Down

0 comments on commit 48ecc9d

Please sign in to comment.