-
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.
Merge branch 'master' of github.com-facuspagnuolo:mimic-fi/v3-core in…
…to connectors/implement_wormhole_bridge_connector
- Loading branch information
Showing
25 changed files
with
456 additions
and
75 deletions.
There are no files selected for viewing
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
19 changes: 19 additions & 0 deletions
19
packages/connectors/contracts/swap/paraswap-v5/IParaswapV5Augustus.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,19 @@ | ||
// 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; | ||
|
||
interface IParaswapV5Augustus { | ||
function getTokenTransferProxy() external view returns (address); | ||
} |
68 changes: 68 additions & 0 deletions
68
packages/connectors/contracts/swap/paraswap-v5/ParaswapV5Connector.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,68 @@ | ||
// 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/utils/Address.sol'; | ||
|
||
import '@mimic-fi/v3-helpers/contracts/utils/ERC20Helpers.sol'; | ||
|
||
import './IParaswapV5Augustus.sol'; | ||
|
||
/** | ||
* @title ParaswapV5Connector | ||
* @dev Interfaces with Paraswap V5 to swap tokens | ||
*/ | ||
contract ParaswapV5Connector { | ||
// Reference to Paraswap V5 Augustus swapper | ||
IParaswapV5Augustus public immutable paraswapV5Augustus; | ||
|
||
/** | ||
* @dev Creates a new ParaswapV5Connector contract | ||
* @param _paraswapV5Augustus Paraswap V5 augusts reference | ||
*/ | ||
constructor(address _paraswapV5Augustus) { | ||
paraswapV5Augustus = IParaswapV5Augustus(_paraswapV5Augustus); | ||
} | ||
|
||
/** | ||
* @dev Executes a token swap in Paraswap V5 | ||
* @param tokenIn Token being sent | ||
* @param tokenOut Token being received | ||
* @param amountIn Amount of tokenIn being swapped | ||
* @param minAmountOut Minimum amount of tokenOut willing to receive | ||
* @param data Calldata to be sent to the Augusuts swapper | ||
*/ | ||
function execute(address tokenIn, address tokenOut, uint256 amountIn, uint256 minAmountOut, bytes memory data) | ||
external | ||
returns (uint256 amountOut) | ||
{ | ||
require(tokenIn != tokenOut, 'PARASWAP_V5_SWAP_SAME_TOKEN'); | ||
|
||
uint256 preBalanceIn = IERC20(tokenIn).balanceOf(address(this)); | ||
uint256 preBalanceOut = IERC20(tokenOut).balanceOf(address(this)); | ||
|
||
address tokenTransferProxy = paraswapV5Augustus.getTokenTransferProxy(); | ||
ERC20Helpers.approve(tokenIn, tokenTransferProxy, amountIn); | ||
Address.functionCall(address(paraswapV5Augustus), data, 'PARASWAP_V5_SWAP_FAILED'); | ||
|
||
uint256 postBalanceIn = IERC20(tokenIn).balanceOf(address(this)); | ||
require(postBalanceIn >= preBalanceIn - amountIn, 'PARASWAP_V5_BAD_TOKEN_IN_BALANCE'); | ||
|
||
uint256 postBalanceOut = IERC20(tokenOut).balanceOf(address(this)); | ||
amountOut = postBalanceOut - preBalanceOut; | ||
require(amountOut >= minAmountOut, 'PARASWAP_V5_MIN_AMOUNT_OUT'); | ||
} | ||
} |
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,103 @@ | ||
import { bn, currentTimestamp, MINUTE, pct } from '@mimic-fi/v3-helpers' | ||
import axios, { AxiosError } from 'axios' | ||
import { BigNumber, Contract } from 'ethers' | ||
|
||
const PARASWAP_URL = 'https://apiv5.paraswap.io' | ||
|
||
export type PricesResponse = { data: { priceRoute: { [key: string]: string } } } | ||
|
||
export type TransactionsResponse = { data: { data: string; sig: string; signer: string } } | ||
|
||
export type SwapData = { | ||
data: string | ||
sig: string | ||
signer: string | ||
minAmountOut: BigNumber | ||
expectedAmountOut: BigNumber | ||
} | ||
|
||
export async function getParaswapSwapData( | ||
chainId: number, | ||
sender: Contract, | ||
tokenIn: Contract, | ||
tokenOut: Contract, | ||
amountIn: BigNumber, | ||
slippage: number | ||
): Promise<SwapData> { | ||
const prices = await getPrices(chainId, sender, tokenIn, tokenOut, amountIn) | ||
const priceRoute = prices.data.priceRoute | ||
|
||
try { | ||
const { destAmount } = priceRoute | ||
const minAmountOut = bn(destAmount).sub(pct(bn(destAmount), slippage)) | ||
const transactions = await postTransactions(chainId, sender, tokenIn, tokenOut, amountIn, minAmountOut, priceRoute) | ||
const { data, sig, signer } = transactions.data | ||
return { data, sig, signer, minAmountOut, expectedAmountOut: bn(destAmount) } | ||
} catch (error) { | ||
if (error instanceof AxiosError) throw Error(error.toString() + ' - ' + error.response?.data?.error) | ||
else throw error | ||
} | ||
} | ||
|
||
export async function getPrices( | ||
chainId: number, | ||
sender: Contract, | ||
tokenIn: Contract, | ||
tokenOut: Contract, | ||
amountIn: BigNumber | ||
): Promise<PricesResponse> { | ||
return axios.get(`${PARASWAP_URL}/prices`, { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json', | ||
}, | ||
params: { | ||
srcToken: tokenIn.address, | ||
srcDecimals: await tokenIn.decimals(), | ||
destToken: tokenOut.address, | ||
destDecimals: await tokenOut.decimals(), | ||
amount: amountIn.toString(), | ||
side: 'SELL', | ||
network: chainId, | ||
userAddress: sender.address, | ||
}, | ||
}) | ||
} | ||
|
||
export async function postTransactions( | ||
chainId: number, | ||
sender: Contract, | ||
tokenIn: Contract, | ||
tokenOut: Contract, | ||
amountIn: BigNumber, | ||
minAmountOut: BigNumber, | ||
priceRoute: { [key: string]: string } | ||
): Promise<TransactionsResponse> { | ||
const { ethers } = await import('hardhat') | ||
return axios.post( | ||
`${PARASWAP_URL}/transactions/${chainId}`, | ||
{ | ||
srcToken: tokenIn.address, | ||
destToken: tokenOut.address, | ||
srcAmount: amountIn.toString(), | ||
srcDecimals: await tokenIn.decimals(), | ||
destAmount: minAmountOut.toString(), | ||
destDecimals: await tokenOut.decimals(), | ||
userAddress: sender.address, | ||
receiver: sender.address, | ||
deadline: (await currentTimestamp()).add(MINUTE).toString(), | ||
priceRoute, | ||
}, | ||
{ | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json', | ||
}, | ||
params: { | ||
gasPrice: (await ethers.provider.getGasPrice()).toString(), | ||
signCalldata: true, | ||
ignoreChecks: true, | ||
}, | ||
} | ||
) | ||
} |
7 changes: 0 additions & 7 deletions
7
packages/connectors/test/helpers/1inch/fixtures/1/17525323/USDC-WBTC.json
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
packages/connectors/test/helpers/1inch/fixtures/1/17525323/USDC-WETH.json
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
packages/connectors/test/helpers/1inch/fixtures/1/17525323/WBTC-USDC.json
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
packages/connectors/test/helpers/1inch/fixtures/1/17525323/WETH-USDC.json
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
packages/connectors/test/helpers/1inch/fixtures/137/44153231/USDC-WBTC.json
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
packages/connectors/test/helpers/1inch/fixtures/137/44153231/USDC-WETH.json
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
packages/connectors/test/helpers/1inch/fixtures/137/44153231/WBTC-USDC.json
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.