forked from DefiLlama/yield-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathestimateFee.ts
More file actions
250 lines (211 loc) · 6.62 KB
/
Copy pathestimateFee.ts
File metadata and controls
250 lines (211 loc) · 6.62 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// forked from see https://github.com/chunza2542/uniswap.fish
// @ts-nocheck
const bn = require('bignumber.js');
interface Tick {
tickIdx: string;
liquidityNet: string;
price0: string;
price1: string;
}
bn.config({ EXPONENTIAL_AT: 999999, DECIMAL_PLACES: 40 });
const Q96 = new bn(2).pow(96);
const getTickFromPrice = (
price: number,
token0Decimal: string,
token1Decimal: string
): number => {
const token0 = expandDecimals(price, Number(token0Decimal));
const token1 = expandDecimals(1, Number(token1Decimal));
const sqrtPrice = encodeSqrtPriceX96(token1).div(encodeSqrtPriceX96(token0));
return Math.log(sqrtPrice.toNumber()) / Math.log(Math.sqrt(1.0001));
};
// for calculation detail, please visit README.md (Section: Calculation Breakdown, No. 1)
interface TokensAmount {
amount0: number;
amount1: number;
}
const getTokensAmountFromDepositAmountUSD = (
P: number,
Pl: number,
Pu: number,
priceUSDX: number,
priceUSDY: number,
depositAmountUSD: number
): TokensAmount => {
const deltaL =
depositAmountUSD /
((Math.sqrt(P) - Math.sqrt(Pl)) * priceUSDY +
(1 / Math.sqrt(P) - 1 / Math.sqrt(Pu)) * priceUSDX);
let deltaY = deltaL * (Math.sqrt(P) - Math.sqrt(Pl));
if (deltaY * priceUSDY < 0) deltaY = 0;
if (deltaY * priceUSDY > depositAmountUSD)
deltaY = depositAmountUSD / priceUSDY;
let deltaX = deltaL * (1 / Math.sqrt(P) - 1 / Math.sqrt(Pu));
if (deltaX * priceUSDX < 0) deltaX = 0;
if (deltaX * priceUSDX > depositAmountUSD)
deltaX = depositAmountUSD / priceUSDX;
return { amount0: deltaX, amount1: deltaY };
};
// for calculation detail, please visit README.md (Section: Calculation Breakdown, No. 2)
const getLiquidityForAmount0 = (
sqrtRatioAX96: bn,
sqrtRatioBX96: bn,
amount0: bn
): bn => {
// amount0 * (sqrt(upper) * sqrt(lower)) / (sqrt(upper) - sqrt(lower))
const intermediate = mulDiv(sqrtRatioBX96, sqrtRatioAX96, Q96);
return mulDiv(amount0, intermediate, sqrtRatioBX96.minus(sqrtRatioAX96));
};
const getLiquidityForAmount1 = (
sqrtRatioAX96: bn,
sqrtRatioBX96: bn,
amount1: bn
): bn => {
// amount1 / (sqrt(upper) - sqrt(lower))
return mulDiv(amount1, Q96, sqrtRatioBX96.minus(sqrtRatioAX96));
};
const getSqrtPriceX96 = (
price: number,
token0Decimal: number,
token1Decimal: number
): bn => {
const token0 = expandDecimals(price, token0Decimal);
const token1 = expandDecimals(1, token1Decimal);
return token0.div(token1).sqrt().multipliedBy(Q96);
};
const getLiquidityDelta = (
P: number,
lowerP: number,
upperP: number,
amount0: number,
amount1: number,
token0Decimal: number,
token1Decimal: number
): bn => {
const amt0 = expandDecimals(amount0, token1Decimal);
const amt1 = expandDecimals(amount1, token0Decimal);
const sqrtRatioX96 = getSqrtPriceX96(P, token0Decimal, token1Decimal);
const sqrtRatioAX96 = getSqrtPriceX96(lowerP, token0Decimal, token1Decimal);
const sqrtRatioBX96 = getSqrtPriceX96(upperP, token0Decimal, token1Decimal);
let liquidity: bn;
if (sqrtRatioX96.lte(sqrtRatioAX96)) {
liquidity = getLiquidityForAmount0(sqrtRatioAX96, sqrtRatioBX96, amt0);
} else if (sqrtRatioX96.lt(sqrtRatioBX96)) {
const liquidity0 = getLiquidityForAmount0(
sqrtRatioX96,
sqrtRatioBX96,
amt0
);
const liquidity1 = getLiquidityForAmount1(
sqrtRatioAX96,
sqrtRatioX96,
amt1
);
liquidity = bn.min(liquidity0, liquidity1);
} else {
liquidity = getLiquidityForAmount1(sqrtRatioAX96, sqrtRatioBX96, amt1);
}
return liquidity;
};
const estimateFee = (
liquidityDelta: bn,
liquidity: bn,
volume24H: number,
feeTier: string
): number => {
const feeTierPercentage = getFeeTierPercentage(feeTier);
const liquidityPercentage = liquidityDelta
.div(liquidity.plus(liquidityDelta))
.toNumber();
return feeTierPercentage * volume24H * liquidityPercentage;
};
const getLiquidityFromTick = (poolTicks: Tick[], tick: number): bn => {
// calculate a cumulative of liquidityNet from all ticks that poolTicks[i] <= tick
let liquidity: bn = new bn(0);
for (let i = 0; i < poolTicks.length - 1; ++i) {
liquidity = liquidity.plus(new bn(poolTicks[i].liquidityNet));
const lowerTick = Number(poolTicks[i].tickIdx);
const upperTick = Number(poolTicks[i + 1]?.tickIdx);
if (lowerTick <= tick && tick <= upperTick) {
break;
}
}
return liquidity;
};
// private helper functions
const encodeSqrtPriceX96 = (price: number | string | bn): bn => {
return new bn(price).sqrt().multipliedBy(Q96).integerValue(3);
};
const expandDecimals = (n: number | string | bn, exp: number): bn => {
return new bn(n).multipliedBy(new bn(10).pow(exp));
};
const mulDiv = (a: bn, b: bn, multiplier: bn) => {
return a.multipliedBy(b).div(multiplier);
};
const getFeeTierPercentage = (tier: string): number => {
if (tier === '100') return 0.01 / 100;
if (tier === '300') return 0.03 / 100;
if (tier === '500') return 0.05 / 100;
if (tier === '750') return 0.075 / 100;
if (tier === '2500') return 0.25 / 100;
if (tier === '10000') return 1 / 100;
return 0;
};
const FEE_BASE = 10_000;
function parseProtocolFees(feeProtocol) {
const packed = Number(feeProtocol);
if (Number.isNaN(packed)) {
throw new Error(`Invalid fee protocol ${feeProtocol}`);
}
const token0ProtocolFee = packed % 2 ** 16;
const token1ProtocolFee = packed >> 16;
return [token0ProtocolFee / FEE_BASE, token1ProtocolFee / FEE_BASE];
}
module.exports.EstimatedFees = (
priceAssumptionValue,
priceRangeValue,
currentPriceUSDToken1,
currentPriceUSDToken0,
depositAmountUSD,
decimalsToken0,
decimalsToken1,
feeTier,
volume,
feeProtocol,
poolTicks
) => {
const P = priceAssumptionValue;
let Pl = priceRangeValue[0];
let Pu = priceRangeValue[1];
const priceUSDX = currentPriceUSDToken1 || 1;
const priceUSDY = currentPriceUSDToken0 || 1;
// For now the protocol fee is the same on both tokens so here we just use the fee on token0
const [protocolFee] = parseProtocolFees(feeProtocol);
const { amount0, amount1 } = getTokensAmountFromDepositAmountUSD(
P,
Pl,
Pu,
priceUSDX,
priceUSDY,
depositAmountUSD
);
const deltaL = getLiquidityDelta(
P,
Pl,
Pu,
amount0,
amount1,
Number(decimalsToken0 || 18),
Number(decimalsToken1 || 18)
);
let currentTick = getTickFromPrice(
P,
decimalsToken0 || '18',
decimalsToken1 || '18'
);
const L = getLiquidityFromTick(poolTicks, currentTick);
const estimatedFee =
P >= Pl && P <= Pu ? estimateFee(deltaL, L, volume, feeTier) : 0;
const estimatedFeeAfterProtocolFee = estimatedFee * (1 - protocolFee);
return estimatedFeeAfterProtocolFee;
};