@paraswap/dex-lib 2.14.0 → 2.14.2

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 (107) hide show
  1. package/.idea/codeStyles/codeStyleConfig.xml +5 -0
  2. package/.idea/misc.xml +6 -0
  3. package/.idea/modules.xml +8 -0
  4. package/.idea/vcs.xml +6 -0
  5. package/.vscode/launch.json +21 -0
  6. package/build/config.js +23 -5
  7. package/build/config.js.map +1 -1
  8. package/build/dex/aave-v3/tokens/arbitrum.json +50 -0
  9. package/build/dex/aave-v3/tokens/avalanche.json +50 -0
  10. package/build/dex/aave-v3/tokens/fantom.json +62 -0
  11. package/build/dex/aave-v3/tokens/mainnet.json +44 -0
  12. package/build/dex/aave-v3/tokens/optimism.json +50 -0
  13. package/build/dex/aave-v3/tokens/polygon.json +98 -0
  14. package/build/dex/balancer-v2/VirtualBoostedPool.d.ts +79 -0
  15. package/build/dex/balancer-v2/VirtualBoostedPool.js +382 -0
  16. package/build/dex/balancer-v2/VirtualBoostedPool.js.map +1 -0
  17. package/build/dex/generic-rfq/e2e-test-config.d.ts +9 -0
  18. package/build/dex/generic-rfq/e2e-test-config.js +33 -0
  19. package/build/dex/generic-rfq/e2e-test-config.js.map +1 -0
  20. package/build/dex/generic-rfq/rate-fetcher.js +2 -0
  21. package/build/dex/generic-rfq/rate-fetcher.js.map +1 -1
  22. package/build/dex/generic-rfq/validators.d.ts +1 -1
  23. package/build/dex/generic-rfq/validators.js +36 -26
  24. package/build/dex/generic-rfq/validators.js.map +1 -1
  25. package/build/dex/index.js +2 -2
  26. package/build/dex/index.js.map +1 -1
  27. package/build/dex/simple-exchange.d.ts +1 -0
  28. package/build/dex/simple-exchange.js +2 -0
  29. package/build/dex/simple-exchange.js.map +1 -1
  30. package/build/dex/test-aave-v3/config.d.ts +4 -0
  31. package/build/dex/test-aave-v3/config.js +15 -0
  32. package/build/dex/test-aave-v3/config.js.map +1 -0
  33. package/build/dex/test-aave-v3/test-aave-v3-pool.d.ts +39 -0
  34. package/build/dex/test-aave-v3/test-aave-v3-pool.js +63 -0
  35. package/build/dex/test-aave-v3/test-aave-v3-pool.js.map +1 -0
  36. package/build/dex/test-aave-v3/test-aave-v3.d.ts +37 -0
  37. package/build/dex/test-aave-v3/test-aave-v3.js +126 -0
  38. package/build/dex/test-aave-v3/test-aave-v3.js.map +1 -0
  39. package/build/dex/test-aave-v3/types.d.ts +6 -0
  40. package/build/dex/test-aave-v3/types.js +3 -0
  41. package/build/dex/test-aave-v3/types.js.map +1 -0
  42. package/build/lib/decoders.d.ts +2 -1
  43. package/build/lib/decoders.js +8 -1
  44. package/build/lib/decoders.js.map +1 -1
  45. package/build/lib/fetcher/fetcher.js +3 -0
  46. package/build/lib/fetcher/fetcher.js.map +1 -1
  47. package/build/lib/multi-wrapper.d.ts +2 -1
  48. package/build/lib/multi-wrapper.js.map +1 -1
  49. package/build/lib/tokens/balancer-fetcher.js +4 -5
  50. package/build/lib/tokens/balancer-fetcher.js.map +1 -1
  51. package/build/lib/tokens/utils.d.ts +2 -2
  52. package/build/lib/tokens/utils.js +1 -1
  53. package/build/lib/tokens/utils.js.map +1 -1
  54. package/build/types.d.ts +3 -0
  55. package/dex-template/__DexName__-e2e.test.ts.template +1 -0
  56. package/dex-template/__DexName__-events.test.ts.template +1 -0
  57. package/dex-template/__DexName__-integration.test.ts.template +1 -0
  58. package/dex-template/__DexName__-pool.ts.template +8 -5
  59. package/package.json +1 -1
  60. package/scripts/generate-bytecode.ts +2 -0
  61. package/src/abi/woo-fi-v2/IntegrationHelper.abi.json +116 -0
  62. package/src/abi/{woo-fi/Wooracle.abi.json → woo-fi-v2/WooOracleV2.abi.json} +148 -50
  63. package/src/abi/{woo-fi/WooPP.abi.json → woo-fi-v2/WooPPV2.abi.json} +201 -169
  64. package/src/config.ts +27 -5
  65. package/src/dex/balancer-v2/balancer-v2-e2e.test.ts +0 -1
  66. package/src/dex/generic-rfq/e2e-test-config.ts +37 -0
  67. package/src/dex/generic-rfq/generic-rfq-e2e.test.ts +63 -258
  68. package/src/dex/generic-rfq/generic-rfq-local-e2e.test.ts +121 -0
  69. package/src/dex/generic-rfq/rate-fetcher.ts +2 -0
  70. package/src/dex/generic-rfq/validators.test.ts +6 -32
  71. package/src/dex/generic-rfq/validators.ts +57 -32
  72. package/src/dex/index.ts +2 -2
  73. package/src/dex/simple-exchange.ts +3 -0
  74. package/src/dex/woo-fi-v2/config.ts +87 -0
  75. package/src/dex/woo-fi-v2/constants.ts +22 -0
  76. package/src/dex/woo-fi-v2/decoders.ts +45 -0
  77. package/src/dex/woo-fi-v2/types.ts +63 -0
  78. package/src/dex/woo-fi-v2/utils.ts +19 -0
  79. package/src/dex/woo-fi-v2/woo-fi-v2-e2e.test.ts +216 -0
  80. package/src/dex/woo-fi-v2/woo-fi-v2-integration.test.ts +495 -0
  81. package/src/dex/woo-fi-v2/woo-fi-v2-math.ts +195 -0
  82. package/src/dex/woo-fi-v2/woo-fi-v2-pool.ts +112 -0
  83. package/src/dex/woo-fi-v2/woo-fi-v2.ts +487 -0
  84. package/src/lib/decoders.ts +12 -1
  85. package/src/lib/fetcher/fetcher.ts +6 -0
  86. package/src/lib/log-messages-suppressor.ts +173 -0
  87. package/src/lib/multi-wrapper.ts +2 -1
  88. package/src/lib/stateful-rpc-poller/state-polling-manager.ts +320 -0
  89. package/src/lib/stateful-rpc-poller/stateful-rpc-poller.ts +604 -0
  90. package/src/lib/stateful-rpc-poller/types.ts +66 -0
  91. package/src/lib/stateful-rpc-poller/utils.ts +23 -0
  92. package/src/lib/tokens/balancer-fetcher.ts +5 -6
  93. package/src/lib/tokens/utils.ts +5 -3
  94. package/src/types.ts +4 -0
  95. package/tests/constants-e2e.ts +10 -3
  96. package/tests/tenderly-simulation.ts +1 -1
  97. package/tests/utils-e2e.ts +61 -48
  98. package/tests/utils.ts +2 -0
  99. package/src/abi/woo-fi/WooFeeManager.abi.json +0 -318
  100. package/src/dex/woo-fi/config.ts +0 -141
  101. package/src/dex/woo-fi/constants.ts +0 -21
  102. package/src/dex/woo-fi/types.ts +0 -55
  103. package/src/dex/woo-fi/woo-fi-decimal-math.ts +0 -45
  104. package/src/dex/woo-fi/woo-fi-e2e.test.ts +0 -183
  105. package/src/dex/woo-fi/woo-fi-integration.test.ts +0 -416
  106. package/src/dex/woo-fi/woo-fi-math.ts +0 -527
  107. package/src/dex/woo-fi/woo-fi.ts +0 -693
@@ -1,416 +0,0 @@
1
- import dotenv from 'dotenv';
2
- dotenv.config();
3
-
4
- import { Result, Interface } from '@ethersproject/abi';
5
- import { DummyDexHelper, IDexHelper } from '../../dex-helper/index';
6
- import { Network, SwapSide } from '../../constants';
7
- import { BI_POWS } from '../../bigint-constants';
8
- import { WooFi } from './woo-fi';
9
- import {
10
- checkPoolPrices,
11
- checkPoolsLiquidity,
12
- checkConstantPoolPrices,
13
- } from '../../../tests/utils';
14
- import { Tokens } from '../../../tests/constants-e2e';
15
- import { Token } from '../../types';
16
-
17
- const network = Network.BSC;
18
- const TokenASymbol = 'WBNB';
19
- const TokenA = Tokens[network][TokenASymbol];
20
-
21
- const TokenBSymbol = 'USDT';
22
- const TokenB = Tokens[network][TokenBSymbol];
23
-
24
- const amounts = [0n, BI_POWS[18], 2n * BI_POWS[18], 3n * BI_POWS[18]];
25
-
26
- const dexKey = 'WooFi';
27
- const dexHelper = new DummyDexHelper(network);
28
-
29
- function getReaderCalldata(
30
- exchangeAddress: string,
31
- readerIface: Interface,
32
- amounts: bigint[],
33
- funcName: string,
34
- baseToken: Token,
35
- ) {
36
- return amounts.map(amount => ({
37
- target: exchangeAddress,
38
- callData: readerIface.encodeFunctionData(funcName, [
39
- baseToken.address,
40
- amount,
41
- ]),
42
- }));
43
- }
44
-
45
- function decodeReaderResult(
46
- results: Result,
47
- readerIface: Interface,
48
- funcName: string,
49
- ) {
50
- return results.map(result => {
51
- const parsed = readerIface.decodeFunctionResult(funcName, result);
52
- return BigInt(parsed[0]._hex);
53
- });
54
- }
55
-
56
- async function checkOnChainPricing(
57
- wooFi: WooFi,
58
- funcName: string,
59
- blockNumber: number,
60
- prices: bigint[],
61
- dexHelper: IDexHelper,
62
- amounts: bigint[],
63
- baseToken: Token,
64
- ) {
65
- const readerCallData = getReaderCalldata(
66
- wooFi.config.wooPPAddress,
67
- WooFi.ifaces.PP,
68
- amounts.slice(1),
69
- funcName,
70
- baseToken,
71
- );
72
- const readerResult = (
73
- await dexHelper.multiContract.methods
74
- .aggregate(readerCallData)
75
- .call({}, blockNumber)
76
- ).returnData;
77
- const expectedPrices = [0n].concat(
78
- decodeReaderResult(readerResult, WooFi.ifaces.PP, funcName),
79
- );
80
-
81
- expect(prices).toEqual(expectedPrices);
82
- }
83
-
84
- describe('WooFi', function () {
85
- let blockNumber: number;
86
- let wooFi: WooFi;
87
- let wooFiNotInitialized: WooFi;
88
-
89
- beforeAll(async () => {
90
- blockNumber = await dexHelper.web3Provider.eth.getBlockNumber();
91
-
92
- wooFi = new WooFi(network, dexKey, dexHelper);
93
- await wooFi.initializePricing(blockNumber);
94
-
95
- wooFiNotInitialized = new WooFi(network, dexKey, dexHelper);
96
-
97
- console.log(`Current state for block number ${blockNumber} is:`);
98
- console.log(
99
- JSON.stringify(
100
- wooFi.latestState,
101
- (key, value) => (typeof value === 'bigint' ? value.toString() : value), // return everything else unchanged
102
- ),
103
- );
104
- });
105
-
106
- it('getPoolIdentifiers and getPricesVolume SELL Base', async function () {
107
- const pools = await wooFi.getPoolIdentifiers(
108
- TokenA,
109
- TokenB,
110
- SwapSide.SELL,
111
- blockNumber,
112
- );
113
- console.log(`${TokenASymbol} <> ${TokenBSymbol} Pool Identifiers: `, pools);
114
-
115
- expect(pools.length).toBeGreaterThan(0);
116
-
117
- const poolPrices = await wooFi.getPricesVolume(
118
- TokenA,
119
- TokenB,
120
- amounts,
121
- SwapSide.SELL,
122
- blockNumber,
123
- pools,
124
- );
125
- console.log(`${TokenASymbol} <> ${TokenBSymbol} Pool Prices: `, poolPrices);
126
-
127
- expect(poolPrices).not.toBeNull();
128
- if (wooFi.hasConstantPriceLargeAmounts) {
129
- checkConstantPoolPrices(poolPrices!, amounts, dexKey);
130
- } else {
131
- checkPoolPrices(poolPrices!, amounts, SwapSide.SELL, dexKey);
132
- }
133
-
134
- // Check if onchain pricing equals to calculated ones
135
- await checkOnChainPricing(
136
- wooFi,
137
- 'querySellBase',
138
- blockNumber,
139
- poolPrices![0].prices,
140
- dexHelper,
141
- amounts,
142
- TokenA,
143
- );
144
- });
145
-
146
- it('getPoolIdentifiers and getPricesVolume SELL Quote', async function () {
147
- const pools = await wooFi.getPoolIdentifiers(
148
- TokenB,
149
- TokenA,
150
- SwapSide.SELL,
151
- blockNumber,
152
- );
153
- console.log(`${TokenBSymbol} <> ${TokenASymbol} Pool Identifiers: `, pools);
154
-
155
- expect(pools.length).toBeGreaterThan(0);
156
-
157
- const poolPrices = await wooFi.getPricesVolume(
158
- TokenB,
159
- TokenA,
160
- amounts,
161
- SwapSide.SELL,
162
- blockNumber,
163
- pools,
164
- );
165
- console.log(`${TokenBSymbol} <> ${TokenASymbol} Pool Prices: `, poolPrices);
166
-
167
- expect(poolPrices).not.toBeNull();
168
- if (wooFi.hasConstantPriceLargeAmounts) {
169
- checkConstantPoolPrices(poolPrices!, amounts, dexKey);
170
- } else {
171
- checkPoolPrices(poolPrices!, amounts, SwapSide.SELL, dexKey);
172
- }
173
-
174
- await checkOnChainPricing(
175
- wooFi,
176
- 'querySellQuote',
177
- blockNumber,
178
- poolPrices![0].prices,
179
- dexHelper,
180
- amounts,
181
- TokenA,
182
- );
183
- });
184
-
185
- it('getPoolIdentifiers and getPricesVolume SELL Quote Avalanche', async function () {
186
- // TODO: refactor this test to more clear state. Need to put
187
- // reusable variables in in `beforeAll` and use separate describe for each network
188
- const _network = Network.AVALANCHE;
189
- const _dexHelper = new DummyDexHelper(_network);
190
- const _amounts = [
191
- 0n,
192
- 1n * BI_POWS[6],
193
- 2n * BI_POWS[6],
194
- 3n * BI_POWS[6],
195
- 4n * BI_POWS[6],
196
- ];
197
-
198
- const _blockNumber = await _dexHelper.web3Provider.eth.getBlockNumber();
199
-
200
- const _wooFi = new WooFi(_network, dexKey, _dexHelper);
201
- await _wooFi.initializePricing(_blockNumber);
202
-
203
- console.log(`Current state for block number ${_blockNumber} is:`);
204
- console.log(
205
- JSON.stringify(
206
- _wooFi.latestState,
207
- (key, value) => (typeof value === 'bigint' ? value.toString() : value), // return everything else unchanged
208
- ),
209
- );
210
-
211
- const pools = await _wooFi.getPoolIdentifiers(
212
- Tokens[_network]['USDC'],
213
- Tokens[_network]['WAVAX'],
214
- SwapSide.SELL,
215
- _blockNumber,
216
- );
217
- console.log(`USDC <> WAVAX Pool Identifiers: `, pools);
218
-
219
- expect(pools.length).toBeGreaterThan(0);
220
-
221
- const poolPrices = await _wooFi.getPricesVolume(
222
- Tokens[_network]['USDC'],
223
- Tokens[_network]['WAVAX'],
224
- _amounts,
225
- SwapSide.SELL,
226
- _blockNumber,
227
- pools,
228
- );
229
- console.log(`USDC <> WAVAX Pool Prices: `, poolPrices);
230
-
231
- expect(poolPrices).not.toBeNull();
232
- if (_wooFi.hasConstantPriceLargeAmounts) {
233
- checkConstantPoolPrices(poolPrices!, _amounts, dexKey);
234
- } else {
235
- checkPoolPrices(poolPrices!, _amounts, SwapSide.SELL, dexKey);
236
- }
237
-
238
- await checkOnChainPricing(
239
- _wooFi,
240
- 'querySellQuote',
241
- _blockNumber,
242
- poolPrices![0].prices,
243
- _dexHelper,
244
- _amounts,
245
- Tokens[_network]['WAVAX'],
246
- );
247
- });
248
-
249
- it('getPoolIdentifiers and getPricesVolume SELL Quote Fantom', async function () {
250
- // TODO: refactor this test to more clear state. Need to put
251
- // reusable variables in in `beforeAll` and use separate describe for each network
252
- const _network = Network.FANTOM;
253
- const _dexHelper = new DummyDexHelper(_network);
254
- const _amounts = [
255
- 0n,
256
- 10n * BI_POWS[6],
257
- 100n * BI_POWS[6],
258
- 1000n * BI_POWS[6],
259
- 10000n * BI_POWS[6],
260
- ];
261
-
262
- const _blockNumber = await _dexHelper.web3Provider.eth.getBlockNumber();
263
-
264
- const _wooFi = new WooFi(_network, dexKey, _dexHelper);
265
- await _wooFi.initializePricing(_blockNumber);
266
-
267
- console.log(`Current state for block number ${_blockNumber} is:`);
268
- console.log(
269
- JSON.stringify(
270
- _wooFi.latestState,
271
- (key, value) => (typeof value === 'bigint' ? value.toString() : value), // return everything else unchanged
272
- ),
273
- );
274
-
275
- const pools = await _wooFi.getPoolIdentifiers(
276
- Tokens[_network]['USDC'],
277
- Tokens[_network]['WFTM'],
278
- SwapSide.SELL,
279
- _blockNumber,
280
- );
281
- console.log(`USDC <> FTM Pool Identifiers: `, pools);
282
-
283
- expect(pools.length).toBeGreaterThan(0);
284
-
285
- const poolPrices = await _wooFi.getPricesVolume(
286
- Tokens[_network]['USDC'],
287
- Tokens[_network]['WFTM'],
288
- _amounts,
289
- SwapSide.SELL,
290
- _blockNumber,
291
- pools,
292
- );
293
- console.log(`USDC <> WFTM Pool Prices: `, poolPrices);
294
-
295
- expect(poolPrices).not.toBeNull();
296
- // if (_wooFi.hasConstantPriceLargeAmounts) {
297
- // checkConstantPoolPrices(poolPrices!, _amounts, dexKey);
298
- // } else {
299
- // checkPoolPrices(poolPrices!, _amounts, SwapSide.SELL, dexKey);
300
- // }
301
-
302
- await checkOnChainPricing(
303
- _wooFi,
304
- 'querySellQuote',
305
- _blockNumber,
306
- poolPrices![0].prices,
307
- _dexHelper,
308
- _amounts,
309
- Tokens[_network]['WFTM'],
310
- );
311
- });
312
-
313
- it('receive 0 on very small input amount', async () => {
314
- const pools = await wooFi.getPoolIdentifiers(
315
- TokenB,
316
- TokenA,
317
- SwapSide.SELL,
318
- blockNumber,
319
- );
320
- console.log(`${TokenBSymbol} <> ${TokenASymbol} Pool Identifiers: `, pools);
321
-
322
- expect(pools.length).toBeGreaterThan(0);
323
-
324
- const poolPrices = await wooFi.getPricesVolume(
325
- TokenB,
326
- TokenA,
327
- amounts.concat([10n]),
328
- SwapSide.SELL,
329
- blockNumber,
330
- pools,
331
- );
332
- console.log(`${TokenBSymbol} <> ${TokenASymbol} Pool Prices: `, poolPrices);
333
-
334
- expect(poolPrices).not.toBeNull();
335
- expect(poolPrices![0].prices.slice(-1)[0].toString()).toEqual('0');
336
- });
337
-
338
- it('getTopPoolsForToken Base', async function () {
339
- await wooFiNotInitialized.updatePoolState();
340
-
341
- const poolLiquidity = await wooFiNotInitialized.getTopPoolsForToken(
342
- TokenA.address,
343
- 10,
344
- );
345
- console.log(`${TokenASymbol} Top Pools:`, poolLiquidity);
346
-
347
- if (!wooFiNotInitialized.hasConstantPriceLargeAmounts) {
348
- checkPoolsLiquidity(poolLiquidity, TokenA.address, dexKey);
349
- }
350
- });
351
-
352
- it('getTopPoolsForToken Quote', async function () {
353
- await wooFiNotInitialized.updatePoolState();
354
-
355
- const poolLiquidity = await wooFiNotInitialized.getTopPoolsForToken(
356
- TokenB.address,
357
- 10,
358
- );
359
- console.log(`${TokenBSymbol} Top Pools:`, poolLiquidity);
360
-
361
- if (!wooFiNotInitialized.hasConstantPriceLargeAmounts) {
362
- checkPoolsLiquidity(poolLiquidity, TokenB.address, dexKey);
363
- }
364
- });
365
-
366
- it('handle woo guardian unreliable price', async () => {
367
- // I found this test condition state only for Fantom
368
- const _network = Network.FANTOM;
369
- const _dexHelper = new DummyDexHelper(_network);
370
-
371
- const _blockNumber = 38145311;
372
-
373
- const _wooFi = new WooFi(_network, dexKey, _dexHelper);
374
- await _wooFi.initializePricing(_blockNumber);
375
-
376
- const baseSymbol = 'WFTM';
377
- const quoteSymbol = 'USDC';
378
-
379
- const pools = await _wooFi.getPoolIdentifiers(
380
- Tokens[_network][baseSymbol],
381
- Tokens[_network][quoteSymbol],
382
- SwapSide.SELL,
383
- blockNumber,
384
- );
385
- console.log(`${baseSymbol} <> ${quoteSymbol} Pool Identifiers: `, pools);
386
-
387
- expect(pools.length).toBeGreaterThan(0);
388
-
389
- const poolPrices = await _wooFi.getPricesVolume(
390
- Tokens[_network][baseSymbol],
391
- Tokens[_network][quoteSymbol],
392
- amounts,
393
- SwapSide.SELL,
394
- blockNumber,
395
- pools,
396
- );
397
- console.log(`${baseSymbol} <> ${quoteSymbol} Pool Prices: `, poolPrices);
398
-
399
- expect(poolPrices).toBeNull();
400
-
401
- const priceToTest =
402
- _wooFi.latestState!.tokenStates[
403
- Tokens[_network][baseSymbol].address.toLowerCase()
404
- ].priceNow;
405
-
406
- expect(() =>
407
- _wooFi.math.checkSwapPrice(
408
- priceToTest,
409
- Tokens[_network][baseSymbol].address.toLowerCase(),
410
- _wooFi.quoteTokenAddress,
411
- ),
412
- ).toThrowError(
413
- 'WooGuardian: PRICE_UNRELIABLE in checkSwapPrice fromToken=0x21be370d5312f44cb42ce377bc9b8a0cef1a4c83 and toToken=0x04068da6c83afcfa0e13ba15a6696662335d5b75',
414
- );
415
- });
416
- });