@paraswap/dex-lib 3.6.2 → 3.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/build/dex/aave-v1/aave-v1.d.ts +1 -2
  2. package/build/dex/aave-v1/aave-v1.js +0 -23
  3. package/build/dex/aave-v1/aave-v1.js.map +1 -1
  4. package/build/dex/compound.js +1 -1
  5. package/build/dex/compound.js.map +1 -1
  6. package/build/dex/etherfi/etherfi.d.ts +26 -0
  7. package/build/dex/etherfi/etherfi.js +96 -0
  8. package/build/dex/etherfi/etherfi.js.map +1 -0
  9. package/build/dex/index.js +2 -0
  10. package/build/dex/index.js.map +1 -1
  11. package/build/dex/maverick-v2/config.d.ts +2 -2
  12. package/build/dex/maverick-v2/config.js +16 -9
  13. package/build/dex/maverick-v2/config.js.map +1 -1
  14. package/build/dex/maverick-v2/maverick-math/maverick-basic-math.js +3 -3
  15. package/build/dex/maverick-v2/maverick-math/maverick-basic-math.js.map +1 -1
  16. package/build/dex/maverick-v2/maverick-math/maverick-bin-math.js +4 -4
  17. package/build/dex/maverick-v2/maverick-math/maverick-bin-math.js.map +1 -1
  18. package/build/dex/maverick-v2/maverick-math/maverick-math.js +3 -3
  19. package/build/dex/maverick-v2/maverick-math/maverick-math.js.map +1 -1
  20. package/build/dex/maverick-v2/maverick-math/maverick-pool-math.js +13 -13
  21. package/build/dex/maverick-v2/maverick-math/maverick-pool-math.js.map +1 -1
  22. package/build/dex/maverick-v2/maverick-math/maverick-swap-math.js +1 -1
  23. package/build/dex/maverick-v2/maverick-math/maverick-swap-math.js.map +1 -1
  24. package/build/dex/maverick-v2/maverick-math/maverick-tick-math.js +4 -4
  25. package/build/dex/maverick-v2/maverick-math/maverick-tick-math.js.map +1 -1
  26. package/build/dex/maverick-v2/maverick-math/maverick-twa-math.js +3 -3
  27. package/build/dex/maverick-v2/maverick-math/maverick-twa-math.js.map +1 -1
  28. package/build/dex/maverick-v2/maverick-v2-pool.d.ts +12 -9
  29. package/build/dex/maverick-v2/maverick-v2-pool.js +71 -16
  30. package/build/dex/maverick-v2/maverick-v2-pool.js.map +1 -1
  31. package/build/dex/maverick-v2/maverick-v2.d.ts +4 -8
  32. package/build/dex/maverick-v2/maverick-v2.js +62 -90
  33. package/build/dex/maverick-v2/maverick-v2.js.map +1 -1
  34. package/build/dex/maverick-v2/types.d.ts +1 -2
  35. package/build/dex/platypus/platypus.d.ts +1 -2
  36. package/build/dex/platypus/platypus.js +0 -45
  37. package/build/dex/platypus/platypus.js.map +1 -1
  38. package/build/dex/sdai/config.d.ts +11 -0
  39. package/build/dex/sdai/config.js +21 -0
  40. package/build/dex/sdai/config.js.map +1 -0
  41. package/build/dex/sdai/constants.d.ts +2 -0
  42. package/build/dex/sdai/constants.js +6 -0
  43. package/build/dex/sdai/constants.js.map +1 -0
  44. package/build/dex/sdai/scripts/validate-state.js +102 -0
  45. package/build/dex/sdai/scripts/validate-state.js.map +1 -0
  46. package/build/dex/sdai/sdai-pool.d.ts +16 -0
  47. package/build/dex/sdai/sdai-pool.js +85 -0
  48. package/build/dex/sdai/sdai-pool.js.map +1 -0
  49. package/build/dex/sdai/sdai.d.ts +51 -0
  50. package/build/dex/sdai/sdai.js +172 -0
  51. package/build/dex/sdai/sdai.js.map +1 -0
  52. package/build/dex/sdai/types.d.ts +21 -0
  53. package/build/dex/sdai/types.js +11 -0
  54. package/build/dex/sdai/types.js.map +1 -0
  55. package/build/dex/sdai/utils.d.ts +4 -0
  56. package/build/dex/sdai/utils.js +39 -0
  57. package/build/dex/sdai/utils.js.map +1 -0
  58. package/build/dex/stable-pool.js +8 -1
  59. package/build/dex/stable-pool.js.map +1 -1
  60. package/build/dex/swaap-v1/swaap-v1.d.ts +1 -2
  61. package/build/dex/swaap-v1/swaap-v1.js +0 -14
  62. package/build/dex/swaap-v1/swaap-v1.js.map +1 -1
  63. package/package.json +1 -1
  64. package/src/abi/maverick-v2/MaverickV2Pool.json +1234 -0
  65. package/src/abi/maverick-v2/MaverickV2PoolLens.json +1232 -0
  66. package/src/abi/maverick-v2/MaverickV2Quoter.json +278 -0
  67. package/src/dex/index.ts +2 -0
  68. package/src/dex/maverick-v2/config.ts +28 -0
  69. package/src/dex/maverick-v2/maverick-math/maverick-basic-math.ts +152 -0
  70. package/src/dex/maverick-v2/maverick-math/maverick-bin-math.ts +288 -0
  71. package/src/dex/maverick-v2/maverick-math/maverick-delta-math.ts +32 -0
  72. package/src/dex/maverick-v2/maverick-math/maverick-math.ts +30 -0
  73. package/src/dex/maverick-v2/maverick-math/maverick-pool-lib.ts +42 -0
  74. package/src/dex/maverick-v2/maverick-math/maverick-pool-math.ts +709 -0
  75. package/src/dex/maverick-v2/maverick-math/maverick-swap-math.ts +199 -0
  76. package/src/dex/maverick-v2/maverick-math/maverick-tick-math.ts +171 -0
  77. package/src/dex/maverick-v2/maverick-math/maverick-twa-math.ts +54 -0
  78. package/src/dex/maverick-v2/maverick-v2-e2e.test.ts +193 -0
  79. package/src/dex/maverick-v2/maverick-v2-events.test.ts +112 -0
  80. package/src/dex/maverick-v2/maverick-v2-integration.test.ts +283 -0
  81. package/src/dex/maverick-v2/maverick-v2-pool.ts +346 -0
  82. package/src/dex/maverick-v2/maverick-v2.ts +380 -0
  83. package/src/dex/maverick-v2/types.ts +151 -0
  84. package/tests/constants-e2e.ts +2 -2
  85. package/.vscode/launch.json +0 -53
  86. package/.vscode/settings.json +0 -2
  87. package/.vscode/tasks.json +0 -15
  88. package/build/abi/uniswap-v3/AlienBaseV3Router.abi.json +0 -52
  89. package/build/abi/wombat/pool.json +0 -1289
  90. package/build/dex/aave-v2/tokens-avalanche.json +0 -44
  91. package/build/dex/aave-v2/tokens-mainnet.json +0 -188
  92. package/build/dex/aave-v2/tokens-polygon.json +0 -44
  93. package/build/dex/maker-psm/scripts/gem.abi.json +0 -136
  94. package/build/dex/maker-psm/scripts/validate-state.d.ts +0 -0
  95. package/build/dex/maker-psm/scripts/validate-state.js +0 -185
  96. package/build/dex/maker-psm/scripts/validate-state.js.map +0 -1
  97. package/build/dex/maker-psm/scripts/vat.abi.json +0 -243
  98. package/build/dex/solidly-v3/scripts/check-event-pool-event.js +0 -53
  99. package/build/dex/solidly-v3/scripts/check-event-pool-event.js.map +0 -1
  100. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.d.ts +0 -17
  101. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js +0 -64
  102. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js.map +0 -1
  103. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.d.ts +0 -17
  104. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js +0 -79
  105. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js.map +0 -1
  106. package/src/dex/maker-psm/scripts/gem.abi.json +0 -136
  107. package/src/dex/maker-psm/scripts/validate-state.ts +0 -221
  108. package/src/dex/maker-psm/scripts/vat.abi.json +0 -243
  109. /package/build/dex/{solidly-v3/scripts/check-event-pool-event.d.ts → sdai/scripts/validate-state.d.ts} +0 -0
@@ -0,0 +1,346 @@
1
+ import { Interface, defaultAbiCoder } from '@ethersproject/abi';
2
+ import { DeepReadonly } from 'ts-essentials';
3
+ import { Address, BlockHeader, Log, Logger, Token } from '../../types';
4
+ import { catchParseLogError } from '../../utils';
5
+ import { StatefulEventSubscriber } from '../../stateful-event-subscriber';
6
+ import { IDexHelper } from '../../dex-helper/idex-helper';
7
+ import { PoolState } from './types';
8
+ import MaverickV2PoolABI from '../../abi/maverick-v2/MaverickV2Pool.json';
9
+ import MaverickV2PoolLensABI from '../../abi/maverick-v2/MaverickV2PoolLens.json';
10
+ import { AbiItem } from 'web3-utils';
11
+ import { Contract } from 'web3-eth-contract';
12
+ import { MaverickPoolMath } from './maverick-math/maverick-pool-math';
13
+ import _ from 'lodash';
14
+ import { MultiResult } from '../../lib/multi-wrapper';
15
+ import { BytesLike } from 'ethers';
16
+ import { extractSuccessAndValue } from '../../lib/decoders';
17
+
18
+ export const decodeMaverickFullState = (
19
+ result: MultiResult<BytesLike> | BytesLike,
20
+ ) => {
21
+ const [isSuccess, toDecode] = extractSuccessAndValue(result);
22
+
23
+ if (!isSuccess || toDecode === '0x') {
24
+ throw new Error('Could not extract value from struct TODO.');
25
+ }
26
+
27
+ const decoded = defaultAbiCoder.decode(
28
+ [
29
+ `
30
+ tuple(
31
+ tuple(
32
+ uint128 reserveA,
33
+ uint128 reserveB,
34
+ uint128 totalSupply,
35
+ uint32[4] binIdsByTick
36
+ )[] tickStateMapping,
37
+ tuple(
38
+ uint128 mergeBinBalance,
39
+ uint128 tickBalance,
40
+ uint128 totalSupply,
41
+ uint8 kind,
42
+ int32 tick,
43
+ uint32 mergeId
44
+ )[] binStateMapping,
45
+ tuple(
46
+ uint128[4] values
47
+ )[] binIdByTickKindMapping,
48
+ tuple(
49
+ uint128 reserveA,
50
+ uint128 reserveB,
51
+ int64 lastTwaD8,
52
+ int64 lastLogPriceD8,
53
+ uint40 lastTimestamp,
54
+ int32 activeTick,
55
+ bool isLocked,
56
+ uint32 binCounter,
57
+ uint8 protocolFeeRatioD3
58
+ ) state,
59
+ tuple(uint256 amountA, uint256 amountB) protocolFees
60
+ ) poolState
61
+ `,
62
+ ],
63
+ toDecode,
64
+ );
65
+ return decoded[0];
66
+ };
67
+
68
+ export class MaverickV2EventPool extends StatefulEventSubscriber<PoolState> {
69
+ handlers: {
70
+ [event: string]: (
71
+ event: any,
72
+ state: DeepReadonly<PoolState>,
73
+ log: Readonly<Log>,
74
+ blockHeader: BlockHeader,
75
+ ) => DeepReadonly<PoolState> | null;
76
+ } = {};
77
+
78
+ poolContract: Contract;
79
+ logDecoder: (log: Log) => any;
80
+
81
+ addressesSubscribed: string[];
82
+
83
+ poolMath: MaverickPoolMath;
84
+
85
+ constructor(
86
+ readonly parentName: string,
87
+ protected network: number,
88
+ protected dexHelper: IDexHelper,
89
+ logger: Logger,
90
+ public tokenA: Token,
91
+ public tokenB: Token,
92
+ public feeAIn: bigint,
93
+ public feeBIn: bigint,
94
+ public tickSpacing: bigint,
95
+ public protocolFeeRatio: bigint,
96
+ public lookback: bigint,
97
+ public activeTick: bigint,
98
+ public address: Address,
99
+ public poolLensAddress: Address,
100
+ protected maverickV2Iface = new Interface(MaverickV2PoolABI),
101
+ protected maverickV2LensIface = new Interface(MaverickV2PoolLensABI),
102
+ ) {
103
+ const name = `${parentName.toLowerCase()}-${tokenA.symbol}-${
104
+ tokenB.symbol
105
+ }-${address.toLowerCase()}`;
106
+
107
+ super(parentName, name, dexHelper, logger);
108
+
109
+ // TODO: make logDecoder decode logs that
110
+ this.logDecoder = (log: Log) => this.maverickV2Iface.parseLog(log);
111
+ this.addressesSubscribed = [address];
112
+
113
+ // Add handlers
114
+ this.handlers['PoolAddLiquidity'] = this.handleAddLiquidityEvent.bind(this);
115
+ this.handlers['PoolRemoveLiquidity'] =
116
+ this.handleRemoveLiquidityEvent.bind(this);
117
+ this.handlers['PoolSwap'] = this.handleSwapEvent.bind(this);
118
+
119
+ this.poolMath = new MaverickPoolMath(
120
+ BigInt(feeAIn),
121
+ BigInt(feeBIn),
122
+ BigInt(lookback),
123
+ BigInt(tickSpacing),
124
+ BigInt(protocolFeeRatio),
125
+ BigInt(activeTick),
126
+ BigInt(tokenA.decimals),
127
+ BigInt(tokenB.decimals),
128
+ );
129
+
130
+ this.poolContract = new this.dexHelper.web3Provider.eth.Contract(
131
+ MaverickV2PoolABI as AbiItem[],
132
+ this.address,
133
+ );
134
+ }
135
+
136
+ /**
137
+ * The function is called every time any of the subscribed
138
+ * addresses release log. The function accepts the current
139
+ * state, updates the state according to the log, and returns
140
+ * the updated state.
141
+ * @param state - Current state of event subscriber
142
+ * @param log - Log released by one of the subscribed addresses
143
+ * @returns Updates state of the event subscriber after the log
144
+ */
145
+ protected processLog(
146
+ state: DeepReadonly<PoolState>,
147
+ log: Readonly<Log>,
148
+ blockHeader: Readonly<BlockHeader>,
149
+ ): DeepReadonly<PoolState> | null {
150
+ try {
151
+ const event = this.logDecoder(log);
152
+ if (event.name in this.handlers) {
153
+ return this.handlers[event.name](event, state, log, blockHeader);
154
+ }
155
+ } catch (e) {
156
+ catchParseLogError(e, this.logger);
157
+ }
158
+
159
+ return null;
160
+ }
161
+
162
+ /**
163
+ * The function generates state using on-chain calls. This
164
+ * function is called to regenerate state if the event based
165
+ * system fails to fetch events and the local state is no
166
+ * more correct.
167
+ * @param blockNumber - Blocknumber for which the state should
168
+ * should be generated
169
+ * @returns state of the event subscriber at blocknumber
170
+ */
171
+
172
+ async generateState(blockNumber: number): Promise<DeepReadonly<PoolState>> {
173
+ try {
174
+ const poolContractState = await this.poolContract.methods
175
+ .getState()
176
+ .call({}, blockNumber);
177
+
178
+ const poolState: PoolState = {
179
+ activeTick: BigInt(poolContractState.activeTick),
180
+ binCounter: BigInt(poolContractState.binCounter),
181
+ reserveA: BigInt(poolContractState.reserveA),
182
+ reserveB: BigInt(poolContractState.reserveB),
183
+ lastTwaD8: BigInt(poolContractState.lastTwaD8),
184
+ lastLogPriceD8: BigInt(poolContractState.lastLogPriceD8),
185
+ lastTimestamp: BigInt(poolContractState.lastTimestamp),
186
+ bins: {},
187
+ ticks: {},
188
+ };
189
+
190
+ const calls = [];
191
+
192
+ for (let i = 0n; i < poolState.binCounter / 5000n + 1n; i++) {
193
+ calls.push({
194
+ target: this.poolLensAddress,
195
+ callData: this.maverickV2LensIface.encodeFunctionData(
196
+ 'getFullPoolState',
197
+ [this.address, i * 5000n, (i + 1n) * 5000n],
198
+ ),
199
+ decodeFunction: (data: any) => data.toString(),
200
+ });
201
+ }
202
+
203
+ const poolLensStates = await this.dexHelper.multiWrapper
204
+ .aggregate<string>(calls, blockNumber)
205
+ .then(data => {
206
+ return data.map(item => decodeMaverickFullState(item));
207
+ });
208
+
209
+ poolLensStates.forEach(poolLensState => {
210
+ poolLensState.binStateMapping.forEach((bin: any, i: number) => {
211
+ if (i === 0) return;
212
+
213
+ const tick = poolLensState.tickStateMapping[i];
214
+
215
+ poolState.bins[i.toString()] = {
216
+ mergeBinBalance: BigInt(bin.mergeBinBalance),
217
+ mergeId: BigInt(bin.mergeId),
218
+ totalSupply: BigInt(bin.totalSupply),
219
+ kind: BigInt(bin.kind),
220
+ tick: BigInt(bin.tick),
221
+ tickBalance: BigInt(bin.tickBalance),
222
+ };
223
+
224
+ poolState.ticks[bin.tick.toString()] = {
225
+ reserveA: BigInt(tick.reserveA),
226
+ reserveB: BigInt(tick.reserveB),
227
+ totalSupply: BigInt(tick.totalSupply),
228
+ binIdsByTick: {},
229
+ };
230
+
231
+ tick.binIdsByTick.forEach((id: any, i: number) => {
232
+ if (id != 0n) {
233
+ poolState.ticks[bin.tick.toString()].binIdsByTick[i.toString()] =
234
+ BigInt(id);
235
+ }
236
+ });
237
+ });
238
+ });
239
+
240
+ return poolState;
241
+ } catch {
242
+ return {
243
+ activeTick: BigInt(0),
244
+ binCounter: BigInt(0),
245
+ reserveA: BigInt(0),
246
+ reserveB: BigInt(0),
247
+ lastTwaD8: BigInt(0),
248
+ lastLogPriceD8: BigInt(0),
249
+ lastTimestamp: BigInt(0),
250
+ bins: {},
251
+ ticks: {},
252
+ };
253
+ }
254
+ }
255
+
256
+ handleRemoveLiquidityEvent(
257
+ event: any,
258
+ state: PoolState,
259
+ log: Log,
260
+ blockHeader: BlockHeader,
261
+ ) {
262
+ const blockTimestamp = BigInt(blockHeader.timestamp);
263
+ this.poolMath.removeLiquidity(state, blockTimestamp, {
264
+ binIds: event.args.params.binIds.map((id: any) => BigInt(id)),
265
+ amounts: event.args.params.amounts.map((amount: any) => BigInt(amount)),
266
+ });
267
+ return state;
268
+ }
269
+
270
+ handleAddLiquidityEvent(
271
+ event: any,
272
+ state: PoolState,
273
+ log: Log,
274
+ blockHeader: BlockHeader,
275
+ ) {
276
+ const blockTimestamp = BigInt(blockHeader.timestamp);
277
+ this.poolMath.addLiquidity(state, blockTimestamp, {
278
+ ticks: event.args.params.ticks.map((amount: any) => BigInt(amount)),
279
+ amounts: event.args.params.amounts.map((amount: any) => BigInt(amount)),
280
+ kind: BigInt(event.args.params.kind),
281
+ });
282
+ return state;
283
+ }
284
+
285
+ handleSwapEvent(
286
+ event: any,
287
+ state: PoolState,
288
+ log: Log,
289
+ blockHeader: BlockHeader,
290
+ ) {
291
+ const blockTimestamp = BigInt(blockHeader.timestamp);
292
+ this.poolMath.swap(
293
+ state,
294
+ blockTimestamp,
295
+ BigInt(event.args.params.amount),
296
+ event.args.params.tokenAIn,
297
+ event.args.params.exactOutput,
298
+ BigInt(event.args.params.tickLimit),
299
+ );
300
+ return state;
301
+ }
302
+
303
+ swap(
304
+ amount: bigint,
305
+ from: Token,
306
+ to: Token,
307
+ side: boolean,
308
+ ): [bigint, bigint] {
309
+ try {
310
+ const tempState = _.cloneDeep(this.state!);
311
+
312
+ const preActiveTick = tempState.activeTick;
313
+
314
+ const output = this.poolMath.estimateSwap(
315
+ tempState,
316
+ amount,
317
+ from.address.toLowerCase() === this.tokenA.address.toLowerCase(),
318
+ side,
319
+ from.address.toLowerCase() === this.tokenA.address.toLowerCase()
320
+ ? tempState.activeTick + 100n
321
+ : tempState.activeTick - 100n,
322
+ );
323
+
324
+ if (output[0] === 0n && output[1] === 0n) {
325
+ this.logger.trace(
326
+ `Reached max swap iteration calculation for address=${this.address} amount=${amount}, from=${from.address}, to=${to.address}, side=${side}`,
327
+ );
328
+ return [0n, 0n];
329
+ }
330
+
331
+ const postActiveTick = tempState.activeTick;
332
+ const tickDiff = Math.abs(Number(postActiveTick) - Number(preActiveTick));
333
+
334
+ return [
335
+ side ? output[0] : output[1],
336
+ // Tick calculation must be started from 1 to account at least one tick
337
+ BigInt(tickDiff + 1),
338
+ ];
339
+ } catch (e) {
340
+ this.logger.debug(
341
+ `Failed to calculate swap for address=${this.address} amount=${amount}, from=${from.address}, to=${to.address}, side=${side} math: ${e}`,
342
+ );
343
+ return [0n, 0n];
344
+ }
345
+ }
346
+ }