-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: CRVV1ETHSTETH callee support (#156)
- Loading branch information
1 parent
c61eccb
commit 7feb3f3
Showing
15 changed files
with
219 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { CalleeFunctions, CollateralConfig } from '../types'; | ||
import { ethers } from 'ethers'; | ||
import BigNumber from '../bignumber'; | ||
import { getContractAddressByName, getJoinNameByCollateralType } from '../contracts'; | ||
import { convertCrvethToEth, CURVE_COIN_INDEX, CURVE_POOL_ADDRESS } from './helpers/curve'; | ||
import { encodeRoute, convertCollateralToDai } from './helpers/uniswapV3'; | ||
|
||
export const CHARTER_MANAGER_ADDRESS = '0x8377CD01a5834a6EaD3b7efb482f678f2092b77e'; | ||
|
||
const getCalleeData = async function ( | ||
network: string, | ||
collateral: CollateralConfig, | ||
profitAddress: string | ||
): Promise<string> { | ||
if (collateral.exchange.callee !== 'CurveLpTokenUniv3Callee') { | ||
throw new Error(`Can not encode route for the "${collateral.ilk}"`); | ||
} | ||
const route = await encodeRoute(network, collateral.exchange.route); | ||
const curveData = [CURVE_POOL_ADDRESS, CURVE_COIN_INDEX]; | ||
const joinAdapterAddress = await getContractAddressByName(network, getJoinNameByCollateralType(collateral.ilk)); | ||
const minProfit = 0; | ||
const typesArray = ['address', 'address', 'uint256', 'bytes', 'address', 'tuple(address,uint256)']; | ||
return ethers.utils.defaultAbiCoder.encode(typesArray, [ | ||
profitAddress, | ||
joinAdapterAddress, | ||
minProfit, | ||
route, | ||
CHARTER_MANAGER_ADDRESS, | ||
curveData, | ||
]); | ||
}; | ||
|
||
const getMarketPrice = async function ( | ||
network: string, | ||
_collateral: CollateralConfig, | ||
collateralAmount: BigNumber | ||
): Promise<BigNumber> { | ||
// convert stETH into ETH | ||
const wethAmount = await convertCrvethToEth(network, collateralAmount); | ||
|
||
// convert ETH into DAI | ||
const daiAmount = await convertCollateralToDai(network, 'ETH', wethAmount); | ||
|
||
// return price per unit | ||
return daiAmount.dividedBy(collateralAmount); | ||
}; | ||
|
||
const UniswapV2CalleeDai: CalleeFunctions = { | ||
getCalleeData, | ||
getMarketPrice, | ||
}; | ||
|
||
export default UniswapV2CalleeDai; |
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,29 @@ | ||
import { ethers } from 'ethers'; | ||
import BigNumber from '../../bignumber'; | ||
import getProvider from '../../provider'; | ||
import CURVE_POOL_ABI from '../../abis/CURVE_POOL.json'; | ||
import { ETH_NUMBER_OF_DIGITS } from '../../constants/UNITS'; | ||
|
||
export const CURVE_POOL_ADDRESS = '0xDC24316b9AE028F1497c275EB9192a3Ea0f67022'; | ||
export const CURVE_COIN_INDEX = 0; | ||
|
||
const getCurvePollContract = async function (network: string): Promise<ethers.Contract> { | ||
const provider = await getProvider(network); | ||
return new ethers.Contract(CURVE_POOL_ADDRESS, CURVE_POOL_ABI as ethers.ContractInterface, provider); | ||
}; | ||
|
||
export const convertStethToEth = async function (network: string, stethAmount: BigNumber): Promise<BigNumber> { | ||
const curvePoolContract = await getCurvePollContract(network); | ||
const stethIntegerAmount = stethAmount.shiftedBy(ETH_NUMBER_OF_DIGITS).toFixed(0); | ||
const wethIntegerAmount = await curvePoolContract.get_dy(1, CURVE_COIN_INDEX, stethIntegerAmount); | ||
return new BigNumber(wethIntegerAmount._hex).shiftedBy(-ETH_NUMBER_OF_DIGITS); | ||
}; | ||
|
||
export const convertCrvethToEth = async function (network: string, stethAmount: BigNumber) { | ||
const curvePoolContract = await getCurvePollContract(network); | ||
const stethIntegerAmount = stethAmount.shiftedBy(ETH_NUMBER_OF_DIGITS).toFixed(0); | ||
const wethIntegerAmount = await curvePoolContract.calc_withdraw_one_coin(stethIntegerAmount, CURVE_COIN_INDEX, { | ||
gasLimit: 1000000, | ||
}); | ||
return new BigNumber(wethIntegerAmount._hex).shiftedBy(-ETH_NUMBER_OF_DIGITS); | ||
}; |
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,51 @@ | ||
import { ethers } from 'ethers'; | ||
import { abi as UNISWAP_V3_QUOTER_ABI } from '@uniswap/v3-periphery/artifacts/contracts/lens/Quoter.sol/Quoter.json'; | ||
import BigNumber from '../../bignumber'; | ||
import getProvider from '../../provider'; | ||
import { getContractAddressByName } from '../../contracts'; | ||
import { DAI_NUMBER_OF_DIGITS } from '../../constants/UNITS'; | ||
import { getCollateralConfigBySymbol } from '../../constants/COLLATERALS'; | ||
|
||
const UNISWAP_V3_QUOTER_ADDRESS = '0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6'; | ||
export const UNISWAP_FEE = 3000; // denominated in hundredths of a bip | ||
|
||
export const encodeRoute = async function (network: string, collateralSymbols: string[]): Promise<string> { | ||
const types = [] as string[]; | ||
const values = [] as Array<string | number>; | ||
|
||
for (const collateralSymbol of collateralSymbols) { | ||
types.push('address'); | ||
values.push(await getContractAddressByName(network, collateralSymbol)); | ||
|
||
types.push('uint24'); | ||
values.push(UNISWAP_FEE); | ||
} | ||
|
||
types.push('address'); | ||
values.push(await getContractAddressByName(network, 'MCD_DAI')); | ||
return ethers.utils.solidityPack(types, values); | ||
}; | ||
|
||
export const convertCollateralToDai = async function ( | ||
network: string, | ||
collateralSymbol: string, | ||
collateralAmount: BigNumber | ||
): Promise<BigNumber> { | ||
const provider = await getProvider(network); | ||
const uniswapV3quoterContract = await new ethers.Contract( | ||
UNISWAP_V3_QUOTER_ADDRESS, | ||
UNISWAP_V3_QUOTER_ABI, | ||
provider | ||
); | ||
const collateral = await getCollateralConfigBySymbol(collateralSymbol); | ||
const collateralIntegerAmount = collateralAmount.shiftedBy(collateral.decimals).toFixed(0); | ||
const daiIntegerAmount = await uniswapV3quoterContract.callStatic.quoteExactInputSingle( | ||
await getContractAddressByName(network, collateralSymbol), | ||
await getContractAddressByName(network, 'MCD_DAI'), | ||
UNISWAP_FEE, | ||
collateralIntegerAmount, | ||
0 | ||
); | ||
const daiAmount = new BigNumber(daiIntegerAmount._hex).shiftedBy(-DAI_NUMBER_OF_DIGITS); | ||
return daiAmount; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import truncateText from './truncateText'; | ||
|
||
const parseMetamaskError = function (errorMessage = ''): string { | ||
const jsonFirstIndex = errorMessage.indexOf('{'); | ||
const jsonLastIndex = errorMessage.lastIndexOf('}'); | ||
try { | ||
const jsonString = errorMessage.substring(jsonFirstIndex, jsonLastIndex + 1); | ||
const metamaskError = JSON.parse(jsonString); | ||
return truncateText(metamaskError?.value?.data?.message || 'unknown'); | ||
} catch { | ||
return truncateText(errorMessage || 'unknown'); | ||
} | ||
}; | ||
|
||
export default parseMetamaskError; |
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.