-
Notifications
You must be signed in to change notification settings - Fork 136
/
elementPool.ts
271 lines (247 loc) · 9.31 KB
/
elementPool.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import { BigNumber, formatFixed, parseFixed } from '@ethersproject/bignumber';
import { WeiPerEther as ONE, Zero } from '@ethersproject/constants';
import { isSameAddress } from '../../utils';
import { BigNumber as OldBigNumber, bnum } from '../../utils/bignumber';
import {
PoolBase,
PoolTypes,
PoolPairBase,
SwapTypes,
SubgraphPoolBase,
SubgraphToken,
} from '../../types';
import { getAddress } from '@ethersproject/address';
import {
_exactTokenInForTokenOut,
_tokenInForExactTokenOut,
_spotPriceAfterSwapExactTokenInForTokenOut,
_spotPriceAfterSwapTokenInForExactTokenOut,
_derivativeSpotPriceAfterSwapExactTokenInForTokenOut,
_derivativeSpotPriceAfterSwapTokenInForExactTokenOut,
getTimeTillExpiry,
} from './elementMath';
type ElementPoolToken = Pick<SubgraphToken, 'address' | 'balance' | 'decimals'>;
export type ElementPoolPairData = PoolPairBase & {
totalShares: BigNumber;
expiryTime: number;
unitSeconds: number;
principalToken: string;
baseToken: string;
currentBlockTimestamp: number;
};
export class ElementPool implements PoolBase {
poolType: PoolTypes = PoolTypes.Element;
id: string;
address: string;
swapFee: BigNumber;
totalShares: BigNumber;
tokens: ElementPoolToken[];
tokensList: string[];
// Element specific
expiryTime: number;
unitSeconds: number;
principalToken: string;
baseToken: string;
currentBlockTimestamp: number;
static fromPool(pool: SubgraphPoolBase): ElementPool {
if (!pool.expiryTime) throw new Error('ElementPool missing expiryTime');
if (!pool.unitSeconds)
throw new Error('ElementPool missing unitSeconds');
if (!pool.principalToken)
throw new Error('ElementPool missing principalToken');
if (!pool.baseToken) throw new Error('ElementPool missing baseToken');
return new ElementPool(
pool.id,
pool.address,
pool.swapFee,
pool.totalShares,
pool.tokens,
pool.tokensList,
pool.expiryTime,
pool.unitSeconds,
pool.principalToken,
pool.baseToken
);
}
constructor(
id: string,
address: string,
swapFee: string,
totalShares: string,
tokens: ElementPoolToken[],
tokensList: string[],
expiryTime: number,
unitSeconds: number,
principalToken: string,
baseToken: string
) {
this.id = id;
this.address = address;
this.swapFee = parseFixed(swapFee, 18);
this.totalShares = parseFixed(totalShares, 18);
this.tokens = tokens;
this.tokensList = tokensList;
this.expiryTime = expiryTime;
this.unitSeconds = unitSeconds;
this.principalToken = principalToken;
this.baseToken = baseToken;
this.currentBlockTimestamp = 0;
}
setCurrentBlockTimestamp(timestamp: number): void {
this.currentBlockTimestamp = timestamp;
}
parsePoolPairData(tokenIn: string, tokenOut: string): ElementPoolPairData {
const tokenIndexIn = this.tokens.findIndex(
(t) => getAddress(t.address) === getAddress(tokenIn)
);
if (tokenIndexIn < 0) throw 'Pool does not contain tokenIn';
const tI = this.tokens[tokenIndexIn];
const decimalsIn = tI.decimals;
const tokenIndexOut = this.tokens.findIndex(
(t) => getAddress(t.address) === getAddress(tokenOut)
);
if (tokenIndexOut < 0) throw 'Pool does not contain tokenOut';
const tO = this.tokens[tokenIndexOut];
const decimalsOut = tO.decimals;
// We already add the virtual LP shares to the right balance
const realBalanceIn = parseFixed(tI.balance, decimalsIn);
const realBalanceOut = parseFixed(tO.balance, decimalsOut);
let balanceIn = realBalanceIn;
let balanceOut = realBalanceOut;
if (tokenIn == this.principalToken) {
balanceIn = realBalanceIn.add(this.totalShares);
} else if (tokenOut == this.principalToken) {
balanceOut = realBalanceOut.add(this.totalShares);
}
const poolPairData: ElementPoolPairData = {
id: this.id,
address: this.address,
poolType: this.poolType,
tokenIn: tokenIn,
tokenOut: tokenOut,
principalToken: this.principalToken,
baseToken: this.baseToken,
decimalsIn: Number(decimalsIn),
decimalsOut: Number(decimalsOut),
balanceIn,
balanceOut,
swapFee: this.swapFee,
totalShares: this.totalShares,
expiryTime: this.expiryTime,
unitSeconds: this.unitSeconds,
currentBlockTimestamp: this.currentBlockTimestamp,
};
return poolPairData;
}
// Normalized liquidity is an abstract term that can be thought of the
// inverse of the slippage. It is proportional to the token balances in the
// pool but also depends on the shape of the invariant curve.
// As a standard, we define normalized liquidity in tokenOut
getNormalizedLiquidity(poolPairData: ElementPoolPairData): OldBigNumber {
// This could be refined by using the inverse of the slippage, but
// in practice this won't have a big impact in path selection for
// multi-hops so not a big priority
return bnum(
formatFixed(poolPairData.balanceOut, poolPairData.decimalsOut)
);
}
getLimitAmountSwap(
poolPairData: ElementPoolPairData,
swapType: SwapTypes
): OldBigNumber {
const MAX_OUT_RATIO = parseFixed('0.3', 18);
if (swapType === SwapTypes.SwapExactIn) {
// "Ai < (Bi**(1-t)+Bo**(1-t))**(1/(1-t))-Bi" must hold in order for
// base of root to be non-negative
const Bi = parseFloat(
formatFixed(poolPairData.balanceIn, poolPairData.decimalsIn)
);
const Bo = parseFloat(
formatFixed(poolPairData.balanceOut, poolPairData.decimalsOut)
);
const t = getTimeTillExpiry(
this.expiryTime,
this.currentBlockTimestamp,
this.unitSeconds
);
return bnum((Bi ** (1 - t) + Bo ** (1 - t)) ** (1 / (1 - t)) - Bi);
} else {
return bnum(
formatFixed(
poolPairData.balanceOut.mul(MAX_OUT_RATIO).div(ONE),
poolPairData.decimalsOut
)
);
}
}
// Updates the balance of a given token for the pool
updateTokenBalanceForPool(token: string, newBalance: BigNumber): void {
// token is BPT
if (this.address == token) {
this.totalShares = newBalance;
} else {
// token is underlying in the pool
const T = this.tokens.find((t) => isSameAddress(t.address, token));
if (!T) throw Error('Pool does not contain this token');
T.balance = formatFixed(newBalance, T.decimals);
}
}
_exactTokenInForTokenOut(
poolPairData: ElementPoolPairData,
amount: OldBigNumber
): OldBigNumber {
poolPairData.currentBlockTimestamp = this.currentBlockTimestamp;
return _exactTokenInForTokenOut(amount, poolPairData);
}
_tokenInForExactTokenOut(
poolPairData: ElementPoolPairData,
amount: OldBigNumber
): OldBigNumber {
poolPairData.currentBlockTimestamp = this.currentBlockTimestamp;
return _tokenInForExactTokenOut(amount, poolPairData);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_calcTokensOutGivenExactBptIn(bptAmountIn: BigNumber): BigNumber[] {
// Missing maths for this
return new Array(this.tokens.length).fill(Zero);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_calcBptOutGivenExactTokensIn(amountsIn: BigNumber[]): BigNumber {
// Missing maths for this
return Zero;
}
_spotPriceAfterSwapExactTokenInForTokenOut(
poolPairData: ElementPoolPairData,
amount: OldBigNumber
): OldBigNumber {
poolPairData.currentBlockTimestamp = this.currentBlockTimestamp;
return _spotPriceAfterSwapExactTokenInForTokenOut(amount, poolPairData);
}
_spotPriceAfterSwapTokenInForExactTokenOut(
poolPairData: ElementPoolPairData,
amount: OldBigNumber
): OldBigNumber {
poolPairData.currentBlockTimestamp = this.currentBlockTimestamp;
return _spotPriceAfterSwapTokenInForExactTokenOut(amount, poolPairData);
}
_derivativeSpotPriceAfterSwapExactTokenInForTokenOut(
poolPairData: ElementPoolPairData,
amount: OldBigNumber
): OldBigNumber {
poolPairData.currentBlockTimestamp = this.currentBlockTimestamp;
return _derivativeSpotPriceAfterSwapExactTokenInForTokenOut(
amount,
poolPairData
);
}
_derivativeSpotPriceAfterSwapTokenInForExactTokenOut(
poolPairData: ElementPoolPairData,
amount: OldBigNumber
): OldBigNumber {
poolPairData.currentBlockTimestamp = this.currentBlockTimestamp;
return _derivativeSpotPriceAfterSwapTokenInForExactTokenOut(
amount,
poolPairData
);
}
}