@paraswap/dex-lib 4.1.6 → 4.1.7-mwrappedm.0

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 (137) hide show
  1. package/README.md +1 -1
  2. package/build/abi/fluid-dex/resolver.abi.json +0 -30
  3. package/build/abi/m-token/WrappedM.abi.json +1708 -0
  4. package/build/dex/aave-v3-stata-v2/config.js +0 -13
  5. package/build/dex/aave-v3-stata-v2/config.js.map +1 -1
  6. package/build/dex/algebra/config.js +1 -1
  7. package/build/dex/algebra/config.js.map +1 -1
  8. package/build/dex/balancer-v2/pools/gyro/GyroEPool.js.map +1 -1
  9. package/build/dex/balancer-v3/balancer-v3-pool.d.ts +1 -6
  10. package/build/dex/balancer-v3/balancer-v3-pool.js +17 -45
  11. package/build/dex/balancer-v3/balancer-v3-pool.js.map +1 -1
  12. package/build/dex/balancer-v3/balancer-v3.d.ts +0 -4
  13. package/build/dex/balancer-v3/balancer-v3.js +4 -30
  14. package/build/dex/balancer-v3/balancer-v3.js.map +1 -1
  15. package/build/dex/balancer-v3/config.js +0 -51
  16. package/build/dex/balancer-v3/config.js.map +1 -1
  17. package/build/dex/balancer-v3/getOnChainState.d.ts +2 -1
  18. package/build/dex/balancer-v3/getOnChainState.js +2 -0
  19. package/build/dex/balancer-v3/getOnChainState.js.map +1 -1
  20. package/build/dex/balancer-v3/getPoolsApi.d.ts +1 -2
  21. package/build/dex/balancer-v3/getPoolsApi.js +10 -17
  22. package/build/dex/balancer-v3/getPoolsApi.js.map +1 -1
  23. package/build/dex/balancer-v3/getTopPoolsApi.d.ts +1 -5
  24. package/build/dex/balancer-v3/getTopPoolsApi.js +3 -6
  25. package/build/dex/balancer-v3/getTopPoolsApi.js.map +1 -1
  26. package/build/dex/balancer-v3/types.d.ts +1 -4
  27. package/build/dex/cables/cables.d.ts +1 -1
  28. package/build/dex/cables/cables.js +7 -26
  29. package/build/dex/cables/cables.js.map +1 -1
  30. package/build/dex/fluid-dex/config.js +2 -2
  31. package/build/dex/fluid-dex/constants.d.ts +1 -1
  32. package/build/dex/fluid-dex/constants.js +1 -2
  33. package/build/dex/fluid-dex/constants.js.map +1 -1
  34. package/build/dex/fluid-dex/fluid-dex-liquidity-proxy.js +1 -3
  35. package/build/dex/fluid-dex/fluid-dex-liquidity-proxy.js.map +1 -1
  36. package/build/dex/fluid-dex/fluid-dex.d.ts +5 -4
  37. package/build/dex/fluid-dex/fluid-dex.js +59 -61
  38. package/build/dex/fluid-dex/fluid-dex.js.map +1 -1
  39. package/build/dex/fluid-dex/types.d.ts +0 -3
  40. package/build/dex/index.js +2 -0
  41. package/build/dex/index.js.map +1 -1
  42. package/build/dex/m-token/config.d.ts +3 -0
  43. package/build/dex/m-token/config.js +19 -0
  44. package/build/dex/m-token/config.js.map +1 -0
  45. package/build/dex/m-token/m-token.d.ts +29 -0
  46. package/build/dex/m-token/m-token.js +145 -0
  47. package/build/dex/m-token/m-token.js.map +1 -0
  48. package/build/dex/m-token/m-wrapped-m.d.ts +17 -0
  49. package/build/dex/m-token/m-wrapped-m.js +50 -0
  50. package/build/dex/m-token/m-wrapped-m.js.map +1 -0
  51. package/build/dex/m-token/types.d.ts +12 -0
  52. package/build/dex/m-token/types.js +3 -0
  53. package/build/dex/m-token/types.js.map +1 -0
  54. package/build/dex/pancakeswap-v3/pancakeswap-v3-pool.js +0 -1
  55. package/build/dex/pancakeswap-v3/pancakeswap-v3-pool.js.map +1 -1
  56. package/build/dex/uniswap-v3/constants.d.ts +0 -2
  57. package/build/dex/uniswap-v3/constants.js +1 -8
  58. package/build/dex/uniswap-v3/constants.js.map +1 -1
  59. package/build/dex/uniswap-v3/contract-math/TickBitMap.d.ts +2 -2
  60. package/build/dex/uniswap-v3/contract-math/TickBitMap.js +8 -10
  61. package/build/dex/uniswap-v3/contract-math/TickBitMap.js.map +1 -1
  62. package/build/dex/uniswap-v3/contract-math/uniswap-v3-math.js +3 -3
  63. package/build/dex/uniswap-v3/contract-math/uniswap-v3-math.js.map +1 -1
  64. package/build/dex/uniswap-v3/forks/ramses-v2/ramses-v2-pool.js +0 -1
  65. package/build/dex/uniswap-v3/forks/ramses-v2/ramses-v2-pool.js.map +1 -1
  66. package/build/dex/uniswap-v3/forks/velodrome-slipstream/velodrome-slipstream-pool.js +0 -1
  67. package/build/dex/uniswap-v3/forks/velodrome-slipstream/velodrome-slipstream-pool.js.map +1 -1
  68. package/build/dex/uniswap-v3/types.d.ts +0 -1
  69. package/build/dex/uniswap-v3/types.js.map +1 -1
  70. package/build/dex/uniswap-v3/uniswap-v3-pool.js +1 -5
  71. package/build/dex/uniswap-v3/uniswap-v3-pool.js.map +1 -1
  72. package/build/dex/wusdm/config.js +0 -6
  73. package/build/dex/wusdm/config.js.map +1 -1
  74. package/build/generic-swap-transaction-builder.d.ts +1 -1
  75. package/build/generic-swap-transaction-builder.js +1 -6
  76. package/build/generic-swap-transaction-builder.js.map +1 -1
  77. package/build/implementations/local-paraswap-sdk.js +1 -1
  78. package/build/implementations/local-paraswap-sdk.js.map +1 -1
  79. package/package.json +3 -3
  80. package/scripts/run-dex-integration-v3.ts +4 -4
  81. package/src/abi/fluid-dex/resolver.abi.json +0 -30
  82. package/src/abi/m-token/WrappedM.abi.json +1708 -0
  83. package/src/dex/aave-v3-stata-v2/aave-v3-stata-v2-e2e.test.ts +32 -11
  84. package/src/dex/aave-v3-stata-v2/config.ts +0 -16
  85. package/src/dex/algebra/config.ts +1 -1
  86. package/src/dex/balancer-v2/balancer-v2-integration.test.ts +12 -11
  87. package/src/dex/balancer-v2/pools/gyro/GyroEPool.ts +1 -0
  88. package/src/dex/balancer-v3/balancer-v3-e2e.test.ts +0 -130
  89. package/src/dex/balancer-v3/balancer-v3-events.test.ts +84 -157
  90. package/src/dex/balancer-v3/balancer-v3-integration.test.ts +202 -103
  91. package/src/dex/balancer-v3/balancer-v3-pool.ts +26 -85
  92. package/src/dex/balancer-v3/balancer-v3.ts +2 -43
  93. package/src/dex/balancer-v3/config.ts +0 -51
  94. package/src/dex/balancer-v3/getOnChainState.ts +4 -1
  95. package/src/dex/balancer-v3/getPoolsApi.ts +23 -42
  96. package/src/dex/balancer-v3/getTopPoolsApi.ts +2 -13
  97. package/src/dex/balancer-v3/types.ts +2 -4
  98. package/src/dex/cables/cables-e2e.test.ts +40 -40
  99. package/src/dex/cables/cables.ts +10 -37
  100. package/src/dex/fluid-dex/config.ts +2 -2
  101. package/src/dex/fluid-dex/constants.ts +1 -2
  102. package/src/dex/fluid-dex/fluid-dex-e2e.test.ts +117 -61
  103. package/src/dex/fluid-dex/fluid-dex-liquidity-proxy.ts +0 -3
  104. package/src/dex/fluid-dex/fluid-dex.ts +94 -96
  105. package/src/dex/fluid-dex/types.ts +0 -3
  106. package/src/dex/index.ts +2 -0
  107. package/src/dex/m-token/config.ts +18 -0
  108. package/src/dex/m-token/m-token-e2e.test.ts +93 -0
  109. package/src/dex/m-token/m-token-integration.test.ts +199 -0
  110. package/src/dex/m-token/m-token.ts +167 -0
  111. package/src/dex/m-token/m-wrapped-m.ts +68 -0
  112. package/src/dex/m-token/types.ts +10 -0
  113. package/src/dex/pancakeswap-v3/pancakeswap-v3-pool.ts +0 -1
  114. package/src/dex/uniswap-v3/constants.ts +0 -10
  115. package/src/dex/uniswap-v3/contract-math/TickBitMap.ts +9 -29
  116. package/src/dex/uniswap-v3/contract-math/uniswap-v3-math.ts +1 -3
  117. package/src/dex/uniswap-v3/forks/ramses-v2/ramses-v2-pool.ts +0 -1
  118. package/src/dex/uniswap-v3/forks/velodrome-slipstream/velodrome-slipstream-pool.ts +0 -1
  119. package/src/dex/uniswap-v3/types.ts +0 -1
  120. package/src/dex/uniswap-v3/uniswap-v3-integration.test.ts +302 -647
  121. package/src/dex/uniswap-v3/uniswap-v3-pool.ts +1 -11
  122. package/src/dex/wusdm/config.ts +0 -7
  123. package/src/dex/wusdm/wusdm-e2e.test.ts +0 -20
  124. package/src/generic-swap-transaction-builder.ts +2 -10
  125. package/src/implementations/local-paraswap-sdk.ts +1 -1
  126. package/tests/constants-e2e.ts +8 -63
  127. package/tests/tenderly-simulation.ts +0 -3
  128. package/tests/utils-e2e.ts +7 -4
  129. package/src/abi/balancer-v3/directionalFeeHook.json +0 -684
  130. package/src/abi/balancer-v3/stableSurgeHook.json +0 -976
  131. package/src/dex/balancer-v3/balancer-hooks-events.test.ts +0 -166
  132. package/src/dex/balancer-v3/balancer-stableSurge.test.ts +0 -110
  133. package/src/dex/balancer-v3/balancer-test-helpers.ts +0 -199
  134. package/src/dex/balancer-v3/hooks/balancer-hook-event-subscriber.ts +0 -212
  135. package/src/dex/balancer-v3/hooks/directionalFeeHook.ts +0 -51
  136. package/src/dex/balancer-v3/hooks/loadHooksConfig.ts +0 -12
  137. package/src/dex/balancer-v3/hooks/stableSurgeHook.ts +0 -198
@@ -4,13 +4,46 @@ dotenv.config();
4
4
 
5
5
  import { StaticJsonRpcProvider } from '@ethersproject/providers';
6
6
  import { testE2E } from '../../../tests/utils-e2e';
7
- import { Tokens, Holders } from '../../../tests/constants-e2e';
7
+ import {
8
+ Tokens,
9
+ Holders,
10
+ NativeTokenSymbols,
11
+ } from '../../../tests/constants-e2e';
8
12
  import { Network, ContractMethod, SwapSide } from '../../constants';
9
13
  import { generateConfig } from '../../config';
10
14
  import { CollateralReserves, DebtReserves, DexLimits } from './types';
11
15
  import { DummyDexHelper } from '../../dex-helper/index';
12
16
  import { FluidDex } from './fluid-dex';
13
17
 
18
+ /*
19
+ README
20
+ ======
21
+
22
+ The template already enumerates the basic structure which involves
23
+ testing simpleSwap, multiSwap, megaSwap contract methods for
24
+ ETH <> TOKEN and TOKEN <> TOKEN swaps. You should replace tokenA and
25
+ tokenB with any two highly liquid tokens on FluidDex for the tests
26
+ to work. If the tokens that you would like to use are not defined in
27
+ Tokens or Holders map, you can update the './tests/constants-e2e'
28
+
29
+ Other than the standard cases that are already added by the template
30
+ it is highly recommended to add test cases which could be specific
31
+ to testing FluidDex (Eg. Tests based on poolType, special tokens,
32
+ etc).
33
+
34
+ You can run this individual test script by running:
35
+ `npx jest src/dex/<dex-name>/<dex-name>-e2e.test.ts`
36
+
37
+ e2e tests use the Tenderly fork api. Please add the following to your
38
+ .env file:
39
+ TENDERLY_TOKEN=Find this under Account>Settings>Authorization.
40
+ TENDERLY_ACCOUNT_ID=Your Tenderly account name.
41
+ TENDERLY_PROJECT=Name of a Tenderly project you have created in your
42
+ dashboard.
43
+
44
+ (This comment should be removed from the final implementation)
45
+ */
46
+
14
47
  function testForNetwork(
15
48
  network: Network,
16
49
  dexKey: string,
@@ -23,14 +56,9 @@ function testForNetwork(
23
56
  generateConfig(network).privateHttpProvider,
24
57
  network,
25
58
  );
26
-
27
59
  const tokens = Tokens[network];
28
60
  const holders = Holders[network];
29
61
 
30
- // Create FluidDex instance to check reserves
31
- const dexHelper = new DummyDexHelper(network);
32
- const fluidDex = new FluidDex(network, dexKey, dexHelper);
33
-
34
62
  const sideToContractMethods = new Map([
35
63
  [SwapSide.SELL, [ContractMethod.swapExactAmountIn]],
36
64
  [SwapSide.BUY, [ContractMethod.swapExactAmountOut]],
@@ -41,11 +69,11 @@ function testForNetwork(
41
69
  describe(`${side}`, () => {
42
70
  contractMethods.forEach((contractMethod: string) => {
43
71
  describe(`${contractMethod}`, () => {
44
- it(`${tokenBSymbol} -> ${tokenASymbol}`, async () => {
72
+ it(`${tokenASymbol} -> ${tokenBSymbol}`, async () => {
45
73
  await testE2E(
46
- tokens[tokenBSymbol],
47
74
  tokens[tokenASymbol],
48
- holders[tokenBSymbol],
75
+ tokens[tokenBSymbol],
76
+ holders[tokenASymbol],
49
77
  tokenBAmount,
50
78
  side,
51
79
  dexKey,
@@ -54,13 +82,12 @@ function testForNetwork(
54
82
  provider,
55
83
  );
56
84
  });
57
-
58
- it(`${tokenASymbol} -> ${tokenBSymbol}`, async () => {
85
+ it(`${tokenBSymbol} -> ${tokenASymbol}`, async () => {
59
86
  await testE2E(
60
- tokens[tokenASymbol],
61
87
  tokens[tokenBSymbol],
62
- holders[tokenASymbol],
63
- tokenAAmount,
88
+ tokens[tokenASymbol],
89
+ holders[tokenBSymbol],
90
+ tokenBAmount,
64
91
  side,
65
92
  dexKey,
66
93
  contractMethod as ContractMethod,
@@ -81,11 +108,30 @@ describe('FluidDex E2E', () => {
81
108
  describe('Mainnet', () => {
82
109
  const network = Network.MAINNET;
83
110
 
84
- describe('limit:FLUID -> ETH', () => {
85
- const tokenASymbol: string = 'FLUID';
86
- const tokenBSymbol: string = 'ETH';
87
- const tokenAAmount: string = '16009704732281037900';
88
- const tokenBAmount: string = '7992306873300550562400';
111
+ // TODO: Uncomment & test when the issue is resolved. Currently no price as expected.
112
+ // describe.only('FLUID -> ETH', () => {
113
+ // const tokenASymbol: string = 'FLUID';
114
+ // const tokenBSymbol: string = 'ETH';
115
+ //
116
+ // const tokenAAmount: string = '160097047322810379';
117
+ // const tokenBAmount: string = '79923068733005505624';
118
+ //
119
+ // testForNetwork(
120
+ // network,
121
+ // dexKey,
122
+ // tokenASymbol,
123
+ // tokenBSymbol,
124
+ // tokenAAmount,
125
+ // tokenBAmount,
126
+ // );
127
+ // });
128
+
129
+ describe('ETH -> INST', () => {
130
+ const tokenASymbol: string = 'ETH';
131
+ const tokenBSymbol: string = 'INST';
132
+
133
+ const tokenAAmount: string = '100000000000000';
134
+ const tokenBAmount: string = '100000000000000';
89
135
 
90
136
  testForNetwork(
91
137
  network,
@@ -102,7 +148,7 @@ describe('FluidDex E2E', () => {
102
148
  const tokenBSymbol: string = 'ETH';
103
149
 
104
150
  const tokenAAmount: string = '100000000000000';
105
- const tokenBAmount: string = '3500000000000000000';
151
+ const tokenBAmount: string = '100000000000000';
106
152
 
107
153
  testForNetwork(
108
154
  network,
@@ -191,9 +237,6 @@ function NewDebtReservesOne(): DebtReserves {
191
237
  };
192
238
  }
193
239
 
194
- const centerPriceTight: bigint = 3401944443797854601871360n;
195
- const centerPriceWide: bigint = 1190081051944310368000000000n;
196
-
197
240
  const limitsTight: DexLimits = {
198
241
  withdrawableToken0: {
199
242
  available: 456740438880263n,
@@ -240,6 +283,10 @@ const limitsWide: DexLimits = {
240
283
  },
241
284
  };
242
285
 
286
+ const ErrInsufficientBorrowable = new Error('insufficient borrowable');
287
+ const ErrInsufficientMaxPrice = new Error('insufficient max price');
288
+ const ErrInsufficientReserve = new Error('insufficient reserve');
289
+
243
290
  describe('TestPoolSimulator_SwapInLimits', () => {
244
291
  const network = Network.MAINNET;
245
292
  const dexHelper = new DummyDexHelper(network);
@@ -248,18 +295,21 @@ describe('TestPoolSimulator_SwapInLimits', () => {
248
295
 
249
296
  it('when limits hit', () => {
250
297
  let outAmt;
251
- outAmt = fluidDex.swapInAdjusted(
252
- true,
253
- BigInt(1e15),
254
- NewColReservesOne(),
255
- NewDebtReservesOne(),
256
- 100n,
257
- 18,
258
- limitsTight,
259
- BigInt(centerPriceTight),
260
- Math.floor(Date.now() / 1000) - 10,
261
- );
262
- expect(outAmt).toEqual(0n);
298
+ try {
299
+ outAmt = fluidDex.swapInAdjusted(
300
+ true,
301
+ BigInt(1e15),
302
+ NewColReservesOne(),
303
+ NewDebtReservesOne(),
304
+ 100n,
305
+ 18,
306
+ limitsTight,
307
+ Math.floor(Date.now() / 1000) - 10,
308
+ );
309
+ expect(outAmt).toEqual(0n);
310
+ } catch (err: any) {
311
+ expect(err.message).toEqual(ErrInsufficientBorrowable.message);
312
+ }
263
313
  });
264
314
 
265
315
  it('when expanded', () => {
@@ -271,41 +321,47 @@ describe('TestPoolSimulator_SwapInLimits', () => {
271
321
  100n,
272
322
  18,
273
323
  limitsTight,
274
- BigInt(centerPriceTight),
275
324
  Math.floor(Date.now() / 1000) - 6000,
276
325
  );
277
- expect(outAmt?.toString()).toEqual('998163044346107');
326
+ console.log('outAmt: ', outAmt);
327
+ expect(outAmt?.toString()).toEqual('998262697204710');
278
328
  });
279
329
 
280
330
  it('when price diff hit', () => {
281
331
  let outAmt;
282
- outAmt = fluidDex.swapInAdjusted(
283
- true,
284
- BigInt(3e16),
285
- NewColReservesOne(),
286
- NewDebtReservesOne(),
287
- 100n,
288
- 18,
289
- limitsWide,
290
- BigInt(centerPriceWide),
291
- Math.floor(Date.now() / 1000) - 10,
292
- );
293
- expect(outAmt).toEqual(0n);
332
+ try {
333
+ outAmt = fluidDex.swapInAdjusted(
334
+ true,
335
+ BigInt(3e16),
336
+ NewColReservesOne(),
337
+ NewDebtReservesOne(),
338
+ 100n,
339
+ 18,
340
+ limitsWide,
341
+ Math.floor(Date.now() / 1000) - 10,
342
+ );
343
+ expect(outAmt).toEqual(0n);
344
+ } catch (err: any) {
345
+ expect(err.message).toEqual(ErrInsufficientMaxPrice.message);
346
+ }
294
347
  });
295
348
 
296
349
  it('when reserves limit is hit', () => {
297
350
  let outAmt;
298
- outAmt = fluidDex.swapInAdjusted(
299
- true,
300
- BigInt(5e16),
301
- NewColReservesOne(),
302
- NewDebtReservesOne(),
303
- 100n,
304
- 18,
305
- limitsWide,
306
- BigInt(centerPriceWide),
307
- Math.floor(Date.now() / 1000) - 10,
308
- );
309
- expect(outAmt).toEqual(0n);
351
+ try {
352
+ outAmt = fluidDex.swapInAdjusted(
353
+ true,
354
+ BigInt(5e16),
355
+ NewColReservesOne(),
356
+ NewDebtReservesOne(),
357
+ 100n,
358
+ 18,
359
+ limitsWide,
360
+ Math.floor(Date.now() / 1000) - 10,
361
+ );
362
+ expect(outAmt).toEqual(0n);
363
+ } catch (err: any) {
364
+ expect(err.message).toEqual(ErrInsufficientReserve.message);
365
+ }
310
366
  });
311
367
  });
@@ -137,14 +137,12 @@ export class FluidDexLiquidityProxy extends StatefulEventSubscriber<FluidDexLiqu
137
137
  token0,
138
138
  token1,
139
139
  feeHex,
140
- centerPriceHex,
141
140
  collateralReservesHex,
142
141
  debtReservesHex,
143
142
  dexLimitsHex,
144
143
  ] = poolReserve;
145
144
 
146
145
  const fee = Number(feeHex.toString());
147
- const centerPrice = Number(centerPriceHex.toString());
148
146
 
149
147
  const collateralReserves = {
150
148
  token0RealReserves: bigIntify(collateralReservesHex[0]),
@@ -198,7 +196,6 @@ export class FluidDexLiquidityProxy extends StatefulEventSubscriber<FluidDexLiqu
198
196
  token0,
199
197
  token1,
200
198
  fee,
201
- centerPrice,
202
199
  collateralReserves,
203
200
  debtReserves,
204
201
  dexLimits,
@@ -48,6 +48,11 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
48
48
 
49
49
  pools: FluidDexPool[] = [];
50
50
 
51
+ // temporarily limit FLUID-ETH Dex Pool.
52
+ restrictedIds: string[] = [
53
+ 'FluidDex_0xc800b0e15c40a1ff0539218100c86f4c1bac8d9c',
54
+ ];
55
+
51
56
  eventPools: FluidDexEventPool[] = [];
52
57
 
53
58
  readonly factory: FluidDexFactory;
@@ -110,7 +115,9 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
110
115
  // implement this function
111
116
  async initializePricing(blockNumber: number) {
112
117
  await this.factory.initialize(blockNumber);
113
- this.pools = await this.fetchFluidDexPools(blockNumber);
118
+ this.pools = (await this.fetchFluidDexPools(blockNumber)).filter(
119
+ pool => !this.restrictedIds.includes(pool.id),
120
+ );
114
121
  this.eventPools = await Promise.all(
115
122
  this.pools.map(async pool => {
116
123
  const eventPool = new FluidDexEventPool(
@@ -133,7 +140,9 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
133
140
  }
134
141
 
135
142
  protected onPoolCreatedUpdatePools(poolsFromFactory: readonly Pool[]) {
136
- this.pools = this.generateFluidDexPoolsFromPoolsFactory(poolsFromFactory);
143
+ this.pools = this.generateFluidDexPoolsFromPoolsFactory(
144
+ poolsFromFactory,
145
+ ).filter(pool => !this.restrictedIds.includes(pool.id));
137
146
  this.logger.info(`${this.dexKey}: pools list was updated ...`);
138
147
  }
139
148
 
@@ -173,6 +182,9 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
173
182
  // A pair must have 2 different tokens.
174
183
  if (srcAddress === destAddress) return [];
175
184
 
185
+ this.pools = this.pools.filter(
186
+ pool => !this.restrictedIds.includes(pool.id),
187
+ );
176
188
  const pools = this.pools.filter(
177
189
  pool =>
178
190
  (srcAddress === pool.token0 && destAddress === pool.token1) ||
@@ -248,7 +260,6 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
248
260
  destToken.decimals,
249
261
  BigInt(currentPoolReserves.fee),
250
262
  currentPoolReserves.dexLimits,
251
- BigInt(currentPoolReserves.centerPrice),
252
263
  Math.floor(Date.now() / 1000),
253
264
  );
254
265
  } else {
@@ -261,7 +272,6 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
261
272
  destToken.decimals,
262
273
  BigInt(currentPoolReserves.fee),
263
274
  currentPoolReserves.dexLimits,
264
- BigInt(currentPoolReserves.centerPrice),
265
275
  Math.floor(Date.now() / 1000),
266
276
  );
267
277
  }
@@ -370,19 +380,9 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
370
380
  }
371
381
  } else {
372
382
  if (pool!.token0.toLowerCase() !== srcToken.toLowerCase()) {
373
- args = [
374
- false,
375
- (BigInt(destAmount) * 1000001n) / 1000000n, // 0.0001% increase target out amount when calling Fluid Dex as it is not 100% exact. Guarantees meeting reaching exact out amount condition
376
- BigInt(srcAmount),
377
- recipient,
378
- ];
383
+ args = [false, BigInt(destAmount), BigInt(srcAmount), recipient];
379
384
  } else {
380
- args = [
381
- true,
382
- (BigInt(destAmount) * 1000001n) / 1000000n, // 0.0001% increase target out amount when calling Fluid Dex as it is not 100% exact. Guarantees meeting reaching exact out amount condition
383
- BigInt(srcAmount),
384
- recipient,
385
- ];
385
+ args = [true, BigInt(destAmount), BigInt(srcAmount), recipient];
386
386
  }
387
387
  }
388
388
  const swapData = this.fluidDexPoolIface.encodeFunctionData(method, args);
@@ -415,7 +415,6 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
415
415
  outDecimals: number,
416
416
  fee: bigint,
417
417
  currentLimits: DexLimits,
418
- centerPrice: bigint,
419
418
  syncTime: number,
420
419
  ): bigint {
421
420
  if (amountIn === 0n) {
@@ -439,7 +438,6 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
439
438
  fee,
440
439
  outDecimals,
441
440
  currentLimits,
442
- centerPrice,
443
441
  syncTime,
444
442
  );
445
443
  return (amountOut * BigInt(10 ** outDecimals)) / BigInt(10 ** 12);
@@ -461,7 +459,6 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
461
459
  fee: bigint,
462
460
  outDecimals: number,
463
461
  currentLimits: DexLimits,
464
- centerPrice: bigint,
465
462
  syncTime: number,
466
463
  ): bigint {
467
464
  const {
@@ -606,40 +603,6 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
606
603
  return 0n;
607
604
  }
608
605
 
609
- if (amountInCollateral > 0) {
610
- let reservesRatioValid = swap0To1
611
- ? this.verifyToken1Reserves(
612
- colReserveIn + amountInCollateral,
613
- colReserveOut - amountOutCollateral,
614
- centerPrice,
615
- )
616
- : this.verifyToken0Reserves(
617
- colReserveOut - amountOutCollateral,
618
- colReserveIn + amountInCollateral,
619
- centerPrice,
620
- );
621
- if (!reservesRatioValid) {
622
- return 0n;
623
- }
624
- }
625
-
626
- if (amountInDebt > 0) {
627
- let reservesRatioValid = swap0To1
628
- ? this.verifyToken1Reserves(
629
- debtReserveIn + amountInDebt,
630
- debtReserveOut - amountOutDebt,
631
- centerPrice,
632
- )
633
- : this.verifyToken0Reserves(
634
- debtReserveOut - amountOutDebt,
635
- debtReserveIn + amountInDebt,
636
- centerPrice,
637
- );
638
- if (!reservesRatioValid) {
639
- return 0n;
640
- }
641
- }
642
-
643
606
  // For price calculations, we'll use a precision factor for bigint division
644
607
  const PRECISION = 1000000000000000000000000000n; // 1e27
645
608
 
@@ -679,6 +642,40 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
679
642
  return 0n;
680
643
  }
681
644
 
645
+ if (amountInCollateral > 0) {
646
+ let reservesRatioValid = swap0To1
647
+ ? this.verifyToken1Reserves(
648
+ colReserveIn + amountInCollateral,
649
+ colReserveOut - amountOutCollateral,
650
+ oldPrice,
651
+ )
652
+ : this.verifyToken0Reserves(
653
+ colReserveOut - amountOutCollateral,
654
+ colReserveIn + amountInCollateral,
655
+ oldPrice,
656
+ );
657
+ if (!reservesRatioValid) {
658
+ return 0n;
659
+ }
660
+ }
661
+
662
+ if (amountInDebt > 0) {
663
+ let reservesRatioValid = swap0To1
664
+ ? this.verifyToken1Reserves(
665
+ debtReserveIn + amountInDebt,
666
+ debtReserveOut - amountOutDebt,
667
+ oldPrice,
668
+ )
669
+ : this.verifyToken0Reserves(
670
+ debtReserveOut - amountOutDebt,
671
+ debtReserveIn + amountInDebt,
672
+ oldPrice,
673
+ );
674
+ if (!reservesRatioValid) {
675
+ return 0n;
676
+ }
677
+ }
678
+
682
679
  const totalAmountOut = amountOutCollateral + amountOutDebt;
683
680
 
684
681
  return totalAmountOut;
@@ -882,7 +879,6 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
882
879
  outDecimals: number,
883
880
  fee: bigint,
884
881
  currentLimits: DexLimits,
885
- centerPrice: bigint,
886
882
  syncTime: number,
887
883
  ): bigint {
888
884
  const amountOutAdjusted =
@@ -896,11 +892,14 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
896
892
  fee,
897
893
  outDecimals,
898
894
  currentLimits,
899
- centerPrice,
900
895
  syncTime,
901
896
  );
902
897
 
898
+ if (amountIn === 2n ** 256n - 1n) {
899
+ return amountIn;
900
+ }
903
901
  const ans = (amountIn * BigInt(10 ** inDecimals)) / BigInt(10 ** 12);
902
+
904
903
  return ans;
905
904
  }
906
905
 
@@ -920,7 +919,6 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
920
919
  fee: bigint,
921
920
  outDecimals: number,
922
921
  currentLimits: DexLimits,
923
- centerPrice: bigint,
924
922
  syncTime: number,
925
923
  ): bigint {
926
924
  if (amountOut === 0n) {
@@ -1038,10 +1036,10 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
1038
1036
  );
1039
1037
  amountInDebt = this.applyFeeForBuy(amountInDebt, fee);
1040
1038
  if (amountOut > debtReserveOut) {
1041
- return 0n;
1039
+ return 2n ** 256n - 1n;
1042
1040
  }
1043
1041
  if (amountOut > borrowable) {
1044
- return 0n;
1042
+ return 2n ** 256n - 1n;
1045
1043
  }
1046
1044
  } else if (a >= amountOut) {
1047
1045
  // Entire trade routes through collateral pool
@@ -1053,10 +1051,10 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
1053
1051
  );
1054
1052
  amountInCollateral = this.applyFeeForBuy(amountInCollateral, fee);
1055
1053
  if (amountOut > colReserveOut) {
1056
- return 0n;
1054
+ return 2n ** 256n - 1n;
1057
1055
  }
1058
1056
  if (amountOut > withdrawable) {
1059
- return 0n;
1057
+ return 2n ** 256n - 1n;
1060
1058
  }
1061
1059
  } else {
1062
1060
  // Trade routes through both pools
@@ -1074,43 +1072,10 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
1074
1072
 
1075
1073
  amountInDebt = this.applyFeeForBuy(amountInDebt, fee);
1076
1074
  if (amountOutDebt > debtReserveOut || a > colReserveOut) {
1077
- return 0n;
1075
+ return 2n ** 256n - 1n;
1078
1076
  }
1079
1077
  if (amountOutDebt > borrowable || a > withdrawable) {
1080
- return 0n;
1081
- }
1082
- }
1083
-
1084
- if (amountInCollateral > 0) {
1085
- let reservesRatioValid = swap0to1
1086
- ? this.verifyToken1Reserves(
1087
- colReserveIn + amountInCollateral,
1088
- colReserveOut - amountOutCollateral,
1089
- centerPrice,
1090
- )
1091
- : this.verifyToken0Reserves(
1092
- colReserveOut - amountOutCollateral,
1093
- colReserveIn + amountInCollateral,
1094
- centerPrice,
1095
- );
1096
- if (!reservesRatioValid) {
1097
- return 0n;
1098
- }
1099
- }
1100
- if (amountInDebt > 0) {
1101
- let reservesRatioValid = swap0to1
1102
- ? this.verifyToken1Reserves(
1103
- debtReserveIn + amountInDebt,
1104
- debtReserveOut - amountOutDebt,
1105
- centerPrice,
1106
- )
1107
- : this.verifyToken0Reserves(
1108
- debtReserveOut - amountOutDebt,
1109
- debtReserveIn + amountInDebt,
1110
- centerPrice,
1111
- );
1112
- if (!reservesRatioValid) {
1113
- return 0n;
1078
+ return 2n ** 256n - 1n;
1114
1079
  }
1115
1080
  }
1116
1081
 
@@ -1147,7 +1112,40 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
1147
1112
  (oldPrice / BigInt(100)) * MAX_PRICE_DIFF
1148
1113
  ) {
1149
1114
  // if price diff is > 5% then swap would revert.
1150
- return 0n;
1115
+ return 2n ** 256n - 1n;
1116
+ }
1117
+
1118
+ if (amountInCollateral > 0) {
1119
+ let reservesRatioValid = swap0to1
1120
+ ? this.verifyToken1Reserves(
1121
+ colReserveIn + amountInCollateral,
1122
+ colReserveOut - amountOutCollateral,
1123
+ oldPrice,
1124
+ )
1125
+ : this.verifyToken0Reserves(
1126
+ colReserveOut - amountOutCollateral,
1127
+ colReserveIn + amountInCollateral,
1128
+ oldPrice,
1129
+ );
1130
+ if (!reservesRatioValid) {
1131
+ return 0n;
1132
+ }
1133
+ }
1134
+ if (amountInDebt > 0) {
1135
+ let reservesRatioValid = swap0to1
1136
+ ? this.verifyToken1Reserves(
1137
+ debtReserveIn + amountInDebt,
1138
+ debtReserveOut - amountOutDebt,
1139
+ oldPrice,
1140
+ )
1141
+ : this.verifyToken0Reserves(
1142
+ debtReserveOut - amountOutDebt,
1143
+ debtReserveIn + amountInDebt,
1144
+ oldPrice,
1145
+ );
1146
+ if (!reservesRatioValid) {
1147
+ return 0n;
1148
+ }
1151
1149
  }
1152
1150
 
1153
1151
  const totalAmountIn = amountInCollateral + amountInDebt;
@@ -6,7 +6,6 @@ export type PoolReserve = {
6
6
  token0: string;
7
7
  token1: string;
8
8
  fee: number;
9
- centerPrice: number;
10
9
  collateralReserves: CollateralReserves;
11
10
  debtReserves: DebtReserves;
12
11
  dexLimits: DexLimits;
@@ -17,7 +16,6 @@ export type PoolReserveResponse = [
17
16
  string,
18
17
  string,
19
18
  BigNumber,
20
- BigNumber,
21
19
  BigNumber[],
22
20
  BigNumber[],
23
21
  DexLimitResponse,
@@ -70,7 +68,6 @@ export interface PoolWithReserves {
70
68
  token0: string;
71
69
  token1: string;
72
70
  fee: number;
73
- centerPrice: number;
74
71
  collateralReserves: CollateralReserves;
75
72
  debtReserves: DebtReserves;
76
73
  }
package/src/dex/index.ts CHANGED
@@ -102,6 +102,7 @@ import { UsualBond } from './usual/usual-bond';
102
102
  import { UsualMWrappedM } from './usual/usual-m-wrapped-m';
103
103
  import { UsualMUsd0 } from './usual/usual-m-usd0';
104
104
  import { UsualPP } from './usual-pp/usual-pp';
105
+ import { MWrappedM } from './m-token/m-wrapped-m';
105
106
 
106
107
  const LegacyDexes = [
107
108
  CurveV2,
@@ -198,6 +199,7 @@ const Dexes = [
198
199
  UsualMWrappedM,
199
200
  UsualMUsd0,
200
201
  UsualPP,
202
+ MWrappedM,
201
203
  ];
202
204
 
203
205
  export type LegacyDexConstructor = new (dexHelper: IDexHelper) => IDexTxBuilder<
@@ -0,0 +1,18 @@
1
+ import { DexConfigMap } from '../../types';
2
+ import { DexParams } from './types';
3
+ import { Network } from '../../constants';
4
+
5
+ export const Config: DexConfigMap<DexParams> = {
6
+ MWrappedM: {
7
+ [Network.MAINNET]: {
8
+ MToken: {
9
+ address: '0x866A2BF4E572CbcF37D5071A7a58503Bfb36be1b',
10
+ decimals: 6,
11
+ },
12
+ WrappedM: {
13
+ address: '0x437cc33344a0B27A429f795ff6B469C72698B291',
14
+ decimals: 6,
15
+ },
16
+ },
17
+ },
18
+ };