-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCurveLpTokenUniv3Callee.ts
73 lines (66 loc) · 2.76 KB
/
CurveLpTokenUniv3Callee.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import type { CalleeFunctions, CollateralConfig, GetCalleeDataParams, Pool } 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 { convertCollateralToDai, encodePools } from './helpers/uniswapV3';
import { routeToPool } from './helpers/pools';
export const CHARTER_MANAGER_ADDRESS = '0x8377CD01a5834a6EaD3b7efb482f678f2092b77e';
const getCalleeData = async function (
network: string,
collateral: CollateralConfig,
marketId: string,
profitAddress: string,
params?: GetCalleeDataParams
): Promise<string> {
const marketData = collateral.exchanges[marketId];
if (marketData?.callee !== 'CurveLpTokenUniv3Callee') {
throw new Error(`Can not encode route for the "${collateral.ilk}"`);
}
const preloadedPools = !!params && 'pools' in params ? params.pools : undefined;
if (!preloadedPools) {
throw new Error(`Can not encode route for the "${collateral.ilk}" without preloaded pools`);
}
const joinName = getJoinNameByCollateralType(collateral.ilk);
if (!joinName) {
throw new Error(`Collateral "${collateral.ilk}" does not have join contract`);
}
const joinAdapterAddress = await getContractAddressByName(network, joinName);
const route = await encodePools(network, preloadedPools);
const curveData = [CURVE_POOL_ADDRESS, CURVE_COIN_INDEX];
const minProfit = 1;
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,
marketId: string,
collateralAmount: BigNumber
): Promise<{ price: BigNumber; pools: Pool[] }> {
const marketData = collateral.exchanges[marketId];
if (marketData.callee !== 'CurveLpTokenUniv3Callee') {
throw new Error(`Can not get market price for the "${collateral.ilk}"`);
}
// 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 {
price: daiAmount.dividedBy(collateralAmount),
pools: await routeToPool(network, marketData.route, collateral.symbol),
};
};
const CurveLpTokenUniv3Callee: CalleeFunctions = {
getCalleeData,
getMarketPrice,
};
export default CurveLpTokenUniv3Callee;