@paraswap/dex-lib 3.11.5-fluid-dex-changes.2 → 3.11.5

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 (196) hide show
  1. package/.vscode/launch.json +53 -0
  2. package/.vscode/settings.json +2 -0
  3. package/.vscode/tasks.json +15 -0
  4. package/build/abi/fluid-dex/resolver.abi.json +4 -731
  5. package/build/abi/{cables/CablesMainnetRFQ.json → inception/inception-ineth-pool.json} +489 -510
  6. package/build/abi/inception/inception-ratio-feed.json +329 -0
  7. package/build/abi/inception/inception-vault.json +991 -0
  8. package/build/abi/uniswap-v3/AlienBaseV3Router.abi.json +52 -0
  9. package/build/abi/{integral/relayer.json → wombat/pool.json} +474 -721
  10. package/build/dex/aave-v1/aave-v1.d.ts +2 -1
  11. package/build/dex/aave-v1/aave-v1.js +23 -0
  12. package/build/dex/aave-v1/aave-v1.js.map +1 -1
  13. package/build/dex/aave-v2/tokens-avalanche.json +44 -0
  14. package/build/dex/aave-v2/tokens-mainnet.json +188 -0
  15. package/build/dex/aave-v2/tokens-polygon.json +44 -0
  16. package/build/dex/algebra/algebra.js +10 -18
  17. package/build/dex/algebra/algebra.js.map +1 -1
  18. package/build/dex/algebra/config.js +1 -1
  19. package/build/dex/algebra/config.js.map +1 -1
  20. package/build/dex/balancer-v3/abi/balancerBatchRouter.d.ts +729 -0
  21. package/build/dex/balancer-v3/abi/balancerBatchRouter.js +732 -0
  22. package/build/dex/balancer-v3/abi/balancerBatchRouter.js.map +1 -0
  23. package/build/dex/balancer-v3/abi/balancerRouter.d.ts +1406 -0
  24. package/build/dex/balancer-v3/abi/balancerRouter.js +1175 -0
  25. package/build/dex/balancer-v3/abi/balancerRouter.js.map +1 -0
  26. package/build/dex/balancer-v3/abi/vaultExtension.V3.d.ts +2081 -0
  27. package/build/dex/balancer-v3/abi/vaultExtension.V3.js +1851 -0
  28. package/build/dex/balancer-v3/abi/vaultExtension.V3.js.map +1 -0
  29. package/build/dex/balancer-v3/balancer-v3-pool.d.ts +76 -0
  30. package/build/dex/balancer-v3/balancer-v3-pool.js +383 -0
  31. package/build/dex/balancer-v3/balancer-v3-pool.js.map +1 -0
  32. package/build/dex/balancer-v3/balancer-v3.d.ts +57 -0
  33. package/build/dex/balancer-v3/balancer-v3.js +373 -0
  34. package/build/dex/balancer-v3/balancer-v3.js.map +1 -0
  35. package/build/dex/balancer-v3/config.d.ts +8 -0
  36. package/build/dex/balancer-v3/config.js +24 -0
  37. package/build/dex/balancer-v3/config.js.map +1 -0
  38. package/build/dex/balancer-v3/getOnChainState.d.ts +10 -0
  39. package/build/dex/balancer-v3/getOnChainState.js +158 -0
  40. package/build/dex/balancer-v3/getOnChainState.js.map +1 -0
  41. package/build/dex/balancer-v3/getPoolsApi.d.ts +2 -0
  42. package/build/dex/balancer-v3/getPoolsApi.js +81 -0
  43. package/build/dex/balancer-v3/getPoolsApi.js.map +1 -0
  44. package/build/dex/balancer-v3/getTopPoolsApi.d.ts +13 -0
  45. package/build/dex/balancer-v3/getTopPoolsApi.js +62 -0
  46. package/build/dex/balancer-v3/getTopPoolsApi.js.map +1 -0
  47. package/build/dex/balancer-v3/types.d.ts +63 -0
  48. package/build/dex/balancer-v3/types.js +3 -0
  49. package/build/dex/balancer-v3/types.js.map +1 -0
  50. package/build/dex/fluid-dex/config.js +1 -1
  51. package/build/dex/fluid-dex/fluid-dex-liquidity-proxy.js +1 -28
  52. package/build/dex/fluid-dex/fluid-dex-liquidity-proxy.js.map +1 -1
  53. package/build/dex/fluid-dex/fluid-dex.d.ts +10 -17
  54. package/build/dex/fluid-dex/fluid-dex.js +67 -219
  55. package/build/dex/fluid-dex/fluid-dex.js.map +1 -1
  56. package/build/dex/fluid-dex/types.d.ts +2 -22
  57. package/build/dex/inception/config.d.ts +4 -0
  58. package/build/dex/inception/config.js +109 -0
  59. package/build/dex/inception/config.js.map +1 -0
  60. package/build/dex/inception/inception-event-pool.d.ts +16 -0
  61. package/build/dex/inception/inception-event-pool.js +49 -0
  62. package/build/dex/inception/inception-event-pool.js.map +1 -0
  63. package/build/dex/inception/inception.d.ts +43 -0
  64. package/build/dex/inception/inception.js +180 -0
  65. package/build/dex/inception/inception.js.map +1 -0
  66. package/build/dex/inception/tokens.d.ts +9 -0
  67. package/build/dex/inception/tokens.js +28 -0
  68. package/build/dex/inception/tokens.js.map +1 -0
  69. package/build/dex/inception/types.d.ts +22 -0
  70. package/build/dex/inception/types.js +3 -0
  71. package/build/dex/inception/types.js.map +1 -0
  72. package/build/dex/inception/utils.d.ts +7 -0
  73. package/build/dex/inception/utils.js +58 -0
  74. package/build/dex/inception/utils.js.map +1 -0
  75. package/build/dex/jarvis.d.ts +2 -1
  76. package/build/dex/jarvis.js +58 -0
  77. package/build/dex/jarvis.js.map +1 -1
  78. package/build/dex/maker-psm/scripts/gem.abi.json +136 -0
  79. package/build/dex/maker-psm/scripts/validate-state.d.ts +0 -0
  80. package/build/dex/maker-psm/scripts/validate-state.js +185 -0
  81. package/build/dex/maker-psm/scripts/validate-state.js.map +1 -0
  82. package/build/dex/maker-psm/scripts/vat.abi.json +243 -0
  83. package/build/dex/platypus/platypus.d.ts +2 -1
  84. package/build/dex/platypus/platypus.js +45 -0
  85. package/build/dex/platypus/platypus.js.map +1 -1
  86. package/build/dex/solidly-v3/scripts/check-event-pool-event.d.ts +1 -0
  87. package/build/dex/solidly-v3/scripts/check-event-pool-event.js +53 -0
  88. package/build/dex/solidly-v3/scripts/check-event-pool-event.js.map +1 -0
  89. package/build/dex/stable-pool.js +1 -8
  90. package/build/dex/stable-pool.js.map +1 -1
  91. package/build/dex/swaap-v1/swaap-v1.d.ts +2 -1
  92. package/build/dex/swaap-v1/swaap-v1.js +14 -0
  93. package/build/dex/swaap-v1/swaap-v1.js.map +1 -1
  94. package/build/dex/uniswap-v3/config.js +1 -1
  95. package/build/dex/uniswap-v3/config.js.map +1 -1
  96. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.d.ts +17 -0
  97. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js +64 -0
  98. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js.map +1 -0
  99. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.d.ts +17 -0
  100. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js +79 -0
  101. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js.map +1 -0
  102. package/build/dex/uniswap-v3/uniswap-v3-pricing.d.ts +4 -0
  103. package/build/dex/uniswap-v3/uniswap-v3-pricing.js +43 -0
  104. package/build/dex/uniswap-v3/uniswap-v3-pricing.js.map +1 -0
  105. package/build/dex/uniswap-v3/uniswap-v3-worker.d.ts +1 -0
  106. package/build/dex/uniswap-v3/uniswap-v3-worker.js +10 -0
  107. package/build/dex/uniswap-v3/uniswap-v3-worker.js.map +1 -0
  108. package/build/dex/zerox/index.d.ts +4 -26
  109. package/build/dex/zerox/index.js +50 -42
  110. package/build/dex/zerox/index.js.map +1 -1
  111. package/build/dex/zerox/types.d.ts +29 -0
  112. package/build/dex/zerox/types.js +8 -1
  113. package/build/dex/zerox/types.js.map +1 -1
  114. package/build/executor/Executor03BytecodeBuilder.js +3 -8
  115. package/build/executor/Executor03BytecodeBuilder.js.map +1 -1
  116. package/package.json +1 -1
  117. package/src/abi/fluid-dex/resolver.abi.json +4 -731
  118. package/src/dex/algebra/algebra.ts +20 -19
  119. package/src/dex/algebra/config.ts +1 -2
  120. package/src/dex/fluid-dex/config.ts +1 -1
  121. package/src/dex/fluid-dex/fluid-dex-e2e.test.ts +6 -153
  122. package/src/dex/fluid-dex/fluid-dex-events.test.ts +4 -2
  123. package/src/dex/fluid-dex/fluid-dex-integration.test.ts +11 -79
  124. package/src/dex/fluid-dex/fluid-dex-liquidity-proxy.ts +0 -33
  125. package/src/dex/fluid-dex/fluid-dex.ts +104 -303
  126. package/src/dex/fluid-dex/types.ts +1 -25
  127. package/src/dex/uniswap-v3/config.ts +1 -2
  128. package/src/executor/Executor03BytecodeBuilder.ts +2 -10
  129. package/.idea/aws.xml +0 -17
  130. package/.idea/codeStyles/Project.xml +0 -19
  131. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  132. package/.idea/misc.xml +0 -6
  133. package/.idea/modules.xml +0 -8
  134. package/.idea/paraswap-dex-lib.iml +0 -9
  135. package/.idea/prettier.xml +0 -7
  136. package/.idea/vcs.xml +0 -6
  137. package/build/abi/integral/factory.json +0 -333
  138. package/build/abi/integral/oracle.json +0 -501
  139. package/build/abi/integral/pool.json +0 -1041
  140. package/build/dex/cables/cables.d.ts +0 -59
  141. package/build/dex/cables/cables.js +0 -599
  142. package/build/dex/cables/cables.js.map +0 -1
  143. package/build/dex/cables/config.d.ts +0 -4
  144. package/build/dex/cables/config.js +0 -15
  145. package/build/dex/cables/config.js.map +0 -1
  146. package/build/dex/cables/constants.d.ts +0 -20
  147. package/build/dex/cables/constants.js +0 -24
  148. package/build/dex/cables/constants.js.map +0 -1
  149. package/build/dex/cables/rate-fetcher.d.ts +0 -34
  150. package/build/dex/cables/rate-fetcher.js +0 -106
  151. package/build/dex/cables/rate-fetcher.js.map +0 -1
  152. package/build/dex/cables/types.d.ts +0 -113
  153. package/build/dex/cables/types.js +0 -19
  154. package/build/dex/cables/types.js.map +0 -1
  155. package/build/dex/cables/validators.d.ts +0 -5
  156. package/build/dex/cables/validators.js +0 -49
  157. package/build/dex/cables/validators.js.map +0 -1
  158. package/build/dex/fluid-dex/fluid-dex-generate-pool.d.ts +0 -49
  159. package/build/dex/fluid-dex/fluid-dex-generate-pool.js +0 -104
  160. package/build/dex/fluid-dex/fluid-dex-generate-pool.js.map +0 -1
  161. package/build/dex/integral/config.d.ts +0 -4
  162. package/build/dex/integral/config.js +0 -20
  163. package/build/dex/integral/config.js.map +0 -1
  164. package/build/dex/integral/context.d.ts +0 -40
  165. package/build/dex/integral/context.js +0 -158
  166. package/build/dex/integral/context.js.map +0 -1
  167. package/build/dex/integral/integral-factory.d.ts +0 -24
  168. package/build/dex/integral/integral-factory.js +0 -95
  169. package/build/dex/integral/integral-factory.js.map +0 -1
  170. package/build/dex/integral/integral-pool.d.ts +0 -50
  171. package/build/dex/integral/integral-pool.js +0 -149
  172. package/build/dex/integral/integral-pool.js.map +0 -1
  173. package/build/dex/integral/integral-pricing.d.ts +0 -18
  174. package/build/dex/integral/integral-pricing.js +0 -114
  175. package/build/dex/integral/integral-pricing.js.map +0 -1
  176. package/build/dex/integral/integral-relayer.d.ts +0 -42
  177. package/build/dex/integral/integral-relayer.js +0 -192
  178. package/build/dex/integral/integral-relayer.js.map +0 -1
  179. package/build/dex/integral/integral-token.d.ts +0 -27
  180. package/build/dex/integral/integral-token.js +0 -59
  181. package/build/dex/integral/integral-token.js.map +0 -1
  182. package/build/dex/integral/integral.d.ts +0 -41
  183. package/build/dex/integral/integral.js +0 -376
  184. package/build/dex/integral/integral.js.map +0 -1
  185. package/build/dex/integral/types.d.ts +0 -85
  186. package/build/dex/integral/types.js +0 -9
  187. package/build/dex/integral/types.js.map +0 -1
  188. package/build/dex/integral/utils-e2e.d.ts +0 -9
  189. package/build/dex/integral/utils-e2e.js +0 -131
  190. package/build/dex/integral/utils-e2e.js.map +0 -1
  191. package/build/dex/integral/utils.d.ts +0 -17
  192. package/build/dex/integral/utils.js +0 -94
  193. package/build/dex/integral/utils.js.map +0 -1
  194. package/build/dex/trader-joe-v2.1/optimizer.d.ts +0 -2
  195. package/build/dex/trader-joe-v2.1/optimizer.js +0 -47
  196. package/build/dex/trader-joe-v2.1/optimizer.js.map +0 -1
@@ -20,8 +20,6 @@ import {
20
20
  FluidDexData,
21
21
  FluidDexPool,
22
22
  Pool,
23
- DexLimits,
24
- TokenLimit,
25
23
  } from './types';
26
24
  import { SimpleExchange } from '../simple-exchange';
27
25
  import FluidDexPoolABI from '../../abi/fluid-dex/fluid-dex.abi.json';
@@ -147,6 +145,8 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
147
145
  side: SwapSide,
148
146
  blockNumber: number,
149
147
  ): Promise<string[]> {
148
+ if (side === SwapSide.BUY) return [];
149
+
150
150
  const pool = this.getPoolByTokenPair(srcToken.address, destToken.address);
151
151
  return pool ? [pool.id] : [];
152
152
  }
@@ -187,53 +187,42 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
187
187
  try {
188
188
  if (srcToken.address.toLowerCase() === destToken.address.toLowerCase())
189
189
  return null;
190
+
191
+ if (side === SwapSide.BUY) return null;
190
192
  // Get the pool to use.
191
193
  const pool = this.getPoolByTokenPair(srcToken.address, destToken.address);
192
194
  if (!pool) return null;
195
+
193
196
  // Make sure the pool meets the optional limitPools filter.
194
197
  if (limitPools && !limitPools.includes(pool.id)) return null;
195
198
 
196
199
  const liquidityProxyState = await this.liquidityProxy.getStateOrGenerate(
197
200
  blockNumber,
198
201
  );
202
+
199
203
  const currentPoolReserves = liquidityProxyState.poolsReserves.find(
200
204
  poolReserve =>
201
205
  poolReserve.pool.toLowerCase() === pool.address.toLowerCase(),
202
206
  );
203
- if (!currentPoolReserves) {
204
- return null;
205
- }
207
+ if (!currentPoolReserves) return null;
208
+
206
209
  const prices = amounts.map(amount => {
207
- if (side === SwapSide.SELL) {
208
- return this.swapIn(
209
- srcToken.address.toLowerCase() === pool.token0.toLowerCase(),
210
- amount,
211
- currentPoolReserves.collateralReserves,
212
- currentPoolReserves.debtReserves,
213
- srcToken.decimals,
214
- destToken.decimals,
215
- BigInt(currentPoolReserves.fee),
216
- currentPoolReserves.dexLimits,
217
- Math.floor(Date.now() / 1000),
218
- );
219
- } else {
220
- return this.swapOut(
221
- srcToken.address.toLowerCase() === pool.token0.toLowerCase(),
222
- amount,
223
- currentPoolReserves.collateralReserves,
224
- currentPoolReserves.debtReserves,
225
- srcToken.decimals,
226
- destToken.decimals,
227
- BigInt(currentPoolReserves.fee),
228
- currentPoolReserves.dexLimits,
229
- Math.floor(Date.now() / 1000),
230
- );
231
- }
210
+ return this.swapIn(
211
+ srcToken.address.toLowerCase() === pool.token0.toLowerCase(),
212
+ amount,
213
+ currentPoolReserves.collateralReserves,
214
+ currentPoolReserves.debtReserves,
215
+ srcToken.decimals,
216
+ destToken.decimals,
217
+ BigInt(currentPoolReserves.fee),
218
+ );
232
219
  });
233
220
  return [
234
221
  {
235
222
  prices: prices,
236
- unit: getBigIntPow(destToken.decimals),
223
+ unit: getBigIntPow(
224
+ (side === SwapSide.SELL ? destToken : srcToken).decimals,
225
+ ),
237
226
  data: {},
238
227
  exchange: this.dexKey,
239
228
  poolIdentifier: pool.id,
@@ -248,6 +237,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
248
237
  }, ${side}:`,
249
238
  e,
250
239
  );
240
+
251
241
  return null;
252
242
  }
253
243
  }
@@ -269,6 +259,8 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
269
259
  data: FluidDexData,
270
260
  side: SwapSide,
271
261
  ): AdapterExchangeParam {
262
+ if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
263
+
272
264
  // Encode here the payload for adapter
273
265
  const payload = '';
274
266
  const pool = this.getPoolByTokenPair(srcToken, destToken);
@@ -301,40 +293,35 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
301
293
  context: Context,
302
294
  executorAddress: Address,
303
295
  ): Promise<DexExchangeParam> {
296
+ if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
297
+
304
298
  let args: any;
305
299
  let returnAmountPos: number | undefined;
306
300
 
307
- const method = side === SwapSide.SELL ? 'swapIn' : 'swapOut';
301
+ const method = 'swapIn';
308
302
 
309
303
  returnAmountPos = extractReturnAmountPosition(
310
304
  this.fluidDexPoolIface,
311
305
  method,
312
- side === SwapSide.SELL ? 'amountOut_' : 'amountIn_',
306
+ 'amountOut_',
313
307
  );
314
308
 
315
309
  const pool = this.getPoolByTokenPair(srcToken, destToken);
316
310
 
317
- if (side === SwapSide.SELL) {
318
- if (pool!.token0.toLowerCase() !== srcToken.toLowerCase()) {
319
- args = [false, BigInt(srcAmount), BigInt(destAmount), recipient];
320
- } else {
321
- args = [true, BigInt(srcAmount), BigInt(destAmount), recipient];
322
- }
311
+ if (pool!.token0.toLowerCase() !== srcToken.toLowerCase()) {
312
+ args = [false, BigInt(srcAmount), BigInt(destAmount), recipient];
323
313
  } else {
324
- if (pool!.token0.toLowerCase() !== srcToken.toLowerCase()) {
325
- args = [false, BigInt(destAmount), BigInt(srcAmount), recipient];
326
- } else {
327
- args = [true, BigInt(destAmount), BigInt(srcAmount), recipient];
328
- }
314
+ args = [true, BigInt(srcAmount), BigInt(destAmount), recipient];
329
315
  }
316
+
330
317
  const swapData = this.fluidDexPoolIface.encodeFunctionData(method, args);
318
+
331
319
  return {
332
320
  needWrapNative: this.needWrapNative,
333
321
  dexFuncHasRecipient: true,
334
322
  exchangeData: swapData,
335
323
  targetExchange: pool!.address,
336
324
  returnAmountPos,
337
- sendEthButSupportsInsertFromAmount: true,
338
325
  };
339
326
  }
340
327
 
@@ -348,7 +335,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
348
335
  * @param outDecimals - The number of decimals for the output token.
349
336
  * @returns The calculated output amount (as a BigInt).
350
337
  */
351
- public swapIn(
338
+ private swapIn(
352
339
  swap0To1: boolean,
353
340
  amountIn: bigint,
354
341
  colReserves: CollateralReserves,
@@ -356,8 +343,6 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
356
343
  inDecimals: number,
357
344
  outDecimals: number,
358
345
  fee: bigint,
359
- currentLimits: DexLimits,
360
- syncTime: number,
361
346
  ): bigint {
362
347
  if (amountIn === 0n) {
363
348
  return 0n; // Return 0 if input amount is 0
@@ -369,19 +354,17 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
369
354
  return 0n;
370
355
  }
371
356
  const amountInAdjusted =
372
- (((amountIn * (this.FEE_100_PERCENT - fee)) / this.FEE_100_PERCENT) *
373
- BigInt(10 ** 12)) /
374
- BigInt(10 ** inDecimals);
357
+ (amountIn * BigInt(10 ** 12)) / BigInt(10 ** inDecimals);
358
+
375
359
  const amountOut = this.swapInAdjusted(
376
360
  swap0To1,
377
- amountInAdjusted,
361
+ amountInAdjusted, // Convert back to number for internal calculations
378
362
  colReserves,
379
363
  debtReserves,
380
- outDecimals,
381
- currentLimits,
382
- syncTime,
364
+ fee,
383
365
  );
384
- return (amountOut * BigInt(10 ** outDecimals)) / BigInt(10 ** 12);
366
+ const result = (amountOut * BigInt(10 ** outDecimals)) / BigInt(10 ** 12);
367
+ return result;
385
368
  }
386
369
 
387
370
  /**
@@ -392,14 +375,12 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
392
375
  * @param debtReserves - The reserves of the debt pool.
393
376
  * @returns The calculated output amount.
394
377
  */
395
- public swapInAdjusted(
378
+ private swapInAdjusted(
396
379
  swap0To1: boolean,
397
380
  amountToSwap: bigint,
398
381
  colReserves: CollateralReserves,
399
382
  debtReserves: DebtReserves,
400
- outDecimals: number,
401
- currentLimits: DexLimits,
402
- syncTime: number,
383
+ fee: bigint,
403
384
  ): bigint {
404
385
  const {
405
386
  token0RealReserves,
@@ -407,6 +388,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
407
388
  token0ImaginaryReserves,
408
389
  token1ImaginaryReserves,
409
390
  } = colReserves;
391
+
410
392
  const {
411
393
  token0RealReserves: debtToken0RealReserves,
412
394
  token1RealReserves: debtToken1RealReserves,
@@ -416,21 +398,26 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
416
398
 
417
399
  // Check if all reserves of collateral pool are greater than 0
418
400
  const colPoolEnabled =
419
- token0RealReserves > 0 &&
420
- token1RealReserves > 0 &&
421
- token0ImaginaryReserves > 0 &&
422
- token1ImaginaryReserves > 0;
401
+ token0RealReserves > BigInt(0) &&
402
+ token1RealReserves > BigInt(0) &&
403
+ token0ImaginaryReserves > BigInt(0) &&
404
+ token1ImaginaryReserves > BigInt(0);
423
405
 
424
406
  // Check if all reserves of debt pool are greater than 0
425
407
  const debtPoolEnabled =
426
- debtToken0RealReserves > 0 &&
427
- debtToken1RealReserves > 0 &&
428
- debtToken0ImaginaryReserves > 0 &&
429
- debtToken1ImaginaryReserves > 0;
408
+ debtToken0RealReserves > BigInt(0) &&
409
+ debtToken1RealReserves > BigInt(0) &&
410
+ debtToken0ImaginaryReserves > BigInt(0) &&
411
+ debtToken1ImaginaryReserves > BigInt(0);
430
412
 
431
- let colReserveIn, colReserveOut, debtReserveIn, debtReserveOut;
432
- let colIReserveIn, colIReserveOut, debtIReserveIn, debtIReserveOut;
433
- let borrowable, withdrawable;
413
+ let colReserveIn: bigint,
414
+ colReserveOut: bigint,
415
+ debtReserveIn: bigint,
416
+ debtReserveOut: bigint;
417
+ let colIReserveIn: bigint,
418
+ colIReserveOut: bigint,
419
+ debtIReserveIn: bigint,
420
+ debtIReserveOut: bigint;
434
421
 
435
422
  if (swap0To1) {
436
423
  colReserveIn = token0RealReserves;
@@ -441,14 +428,6 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
441
428
  debtReserveOut = debtToken1RealReserves;
442
429
  debtIReserveIn = debtToken0ImaginaryReserves;
443
430
  debtIReserveOut = debtToken1ImaginaryReserves;
444
- borrowable = this.getExpandedLimit(
445
- syncTime,
446
- currentLimits.borrowableToken1,
447
- );
448
- withdrawable = this.getExpandedLimit(
449
- syncTime,
450
- currentLimits.withdrawableToken1,
451
- );
452
431
  } else {
453
432
  colReserveIn = token1RealReserves;
454
433
  colReserveOut = token0RealReserves;
@@ -458,21 +437,8 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
458
437
  debtReserveOut = debtToken0RealReserves;
459
438
  debtIReserveIn = debtToken1ImaginaryReserves;
460
439
  debtIReserveOut = debtToken0ImaginaryReserves;
461
- borrowable = this.getExpandedLimit(
462
- syncTime,
463
- currentLimits.borrowableToken0,
464
- );
465
- withdrawable = this.getExpandedLimit(
466
- syncTime,
467
- currentLimits.withdrawableToken0,
468
- );
469
440
  }
470
441
 
471
- // bring borrowable and withdrawable from token decimals to 1e12 decimals, same as amounts
472
- borrowable = (borrowable * BigInt(10 ** 12)) / BigInt(10 ** outDecimals);
473
- withdrawable =
474
- (withdrawable * BigInt(10 ** 12)) / BigInt(10 ** outDecimals);
475
-
476
442
  let a: bigint;
477
443
  if (colPoolEnabled && debtPoolEnabled) {
478
444
  a = this.swapRoutingIn(
@@ -483,41 +449,39 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
483
449
  debtIReserveIn,
484
450
  );
485
451
  } else if (debtPoolEnabled) {
486
- a = -1n; // Route from debt pool
452
+ a = BigInt(-1); // Route from debt pool
487
453
  } else if (colPoolEnabled) {
488
- a = amountToSwap + 1n; // Route from collateral pool
454
+ a = amountToSwap + BigInt(1); // Route from collateral pool
489
455
  } else {
490
456
  throw new Error('No pools are enabled');
491
457
  }
492
458
 
493
- let amountOutCollateral = 0n;
494
- let amountOutDebt = 0n;
495
- let amountInCollateral = 0n;
496
- let amountInDebt = 0n;
459
+ let amountOutCollateral = BigInt(0);
460
+ let amountOutDebt = BigInt(0);
497
461
 
498
- if (a <= 0n) {
462
+ if (a <= BigInt(0)) {
499
463
  // Entire trade routes through debt pool
500
- amountInDebt = amountToSwap;
501
464
  amountOutDebt = this.getAmountOut(
502
- amountToSwap,
465
+ this.applyFee(amountToSwap, fee),
503
466
  debtIReserveIn,
504
467
  debtIReserveOut,
505
468
  );
506
469
  } else if (a >= amountToSwap) {
507
470
  // Entire trade routes through collateral pool
508
- amountInCollateral = amountToSwap;
509
471
  amountOutCollateral = this.getAmountOut(
510
- amountToSwap,
472
+ this.applyFee(amountToSwap, fee),
511
473
  colIReserveIn,
512
474
  colIReserveOut,
513
475
  );
514
476
  } else {
515
477
  // Trade routes through both pools
516
- amountInCollateral = a;
517
- amountOutCollateral = this.getAmountOut(a, colIReserveIn, colIReserveOut);
518
- amountInDebt = amountToSwap - a;
478
+ amountOutCollateral = this.getAmountOut(
479
+ this.applyFee(a, fee),
480
+ colIReserveIn,
481
+ colIReserveOut,
482
+ );
519
483
  amountOutDebt = this.getAmountOut(
520
- amountInDebt,
484
+ this.applyFee(amountToSwap - a, fee),
521
485
  debtIReserveIn,
522
486
  debtIReserveOut,
523
487
  );
@@ -526,91 +490,17 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
526
490
  if (amountOutDebt > debtReserveOut) {
527
491
  return 0n;
528
492
  }
529
- if (amountOutDebt > borrowable) {
530
- return 0n;
531
- }
532
493
 
533
494
  if (amountOutCollateral > colReserveOut) {
534
495
  return 0n;
535
496
  }
536
-
537
- if (amountOutCollateral > withdrawable) {
538
- return 0n;
539
- }
540
-
541
- // For price calculations, we'll use a precision factor for bigint division
542
- const PRECISION = 1000000000000000000000000000n; // 1e27
543
-
544
- let oldPrice: bigint;
545
- let newPrice: bigint;
546
-
547
- if (amountInCollateral > amountInDebt) {
548
- // new pool price from col pool
549
- oldPrice = swap0To1
550
- ? (colIReserveOut * PRECISION) / colIReserveIn
551
- : (colIReserveIn * PRECISION) / colIReserveOut;
552
-
553
- newPrice = swap0To1
554
- ? ((colIReserveOut - amountOutCollateral) * PRECISION) /
555
- (colIReserveIn + amountInCollateral)
556
- : ((colIReserveIn + amountInCollateral) * PRECISION) /
557
- (colIReserveOut - amountOutCollateral);
558
- } else {
559
- // new pool price from debt pool
560
- oldPrice = swap0To1
561
- ? (debtIReserveOut * PRECISION) / debtIReserveIn
562
- : (debtIReserveIn * PRECISION) / debtIReserveOut;
563
-
564
- newPrice = swap0To1
565
- ? ((debtIReserveOut - amountOutDebt) * PRECISION) /
566
- (debtIReserveIn + amountInDebt)
567
- : ((debtIReserveIn + amountInDebt) * PRECISION) /
568
- (debtIReserveOut - amountOutDebt);
569
- }
570
- // Calculate price difference using bigint arithmetic
571
- const MAX_PRICE_DIFF = 5n; // 5%
572
- const priceDiff =
573
- oldPrice > newPrice ? oldPrice - newPrice : newPrice - oldPrice;
574
- const maxAllowedDiff = (oldPrice * MAX_PRICE_DIFF) / 100n;
575
-
576
- if (priceDiff > maxAllowedDiff) {
577
- return 0n;
578
- }
579
497
  const totalAmountOut = amountOutCollateral + amountOutDebt;
580
498
 
581
499
  return totalAmountOut;
582
500
  }
583
501
 
584
- /**
585
- * Calculates the currently available swappable amount for a token limit considering expansion since last syncTime.
586
- * @param syncTime - timestamp in seconds when the limits were synced
587
- * @param limit - token limit object containing available amount, expandsTo amount, and expandDuration
588
- * @returns The calculated available swappable amount (borrowable or withdrawable)
589
- */
590
- public getExpandedLimit(syncTime: number, limit: TokenLimit): bigint {
591
- const currentTime = Math.floor(Date.now() / 1000); // convert milliseconds to seconds
592
- const elapsedTime = currentTime - syncTime;
593
- limit.expandsDuration = limit.expandsDuration || 0n;
594
- if (elapsedTime < 10n) {
595
- // if almost no time has elapsed, return available amount
596
- return limit.available;
597
- }
598
-
599
- if (elapsedTime >= limit.expandsDuration) {
600
- // if duration has passed, return max amount
601
- return limit.expandsTo;
602
- }
603
-
604
- // Calculate expansion ratio using bigint arithmetic
605
- // Multiply by a large factor for precision in integer arithmetic
606
- const PRECISION = 1000000;
607
- const ratio = BigInt(elapsedTime * PRECISION) / limit.expandsDuration;
608
-
609
- // Calculate expanded amount with precision factor
610
- const expansion =
611
- ((limit.expandsTo - limit.available) * ratio) / BigInt(PRECISION);
612
- const expandedAmount = limit.available + expansion;
613
- return expandedAmount;
502
+ applyFee(amount: bigint, fee: bigint): bigint {
503
+ return (amount * (this.FEE_100_PERCENT - fee)) / this.FEE_100_PERCENT;
614
504
  }
615
505
 
616
506
  /**
@@ -620,7 +510,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
620
510
  * @param iReserveOut - Imaginary token reserve of output amount.
621
511
  * @returns The maximum output amount of the other asset.
622
512
  */
623
- public getAmountOut(
513
+ private getAmountOut(
624
514
  amountIn: bigint,
625
515
  iReserveIn: bigint,
626
516
  iReserveOut: bigint,
@@ -642,7 +532,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
642
532
  * @param iReserveOut - Imaginary token reserve of output amount.
643
533
  * @returns The input amount of the other asset.
644
534
  */
645
- public getAmountIn(
535
+ private getAmountIn(
646
536
  amountOut: bigint,
647
537
  iReserveIn: bigint,
648
538
  iReserveOut: bigint,
@@ -667,7 +557,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
667
557
  * @note If a > t then entire trade route through col pool and col pool arbitrage with debt pool.
668
558
  * @note If a > 0 & a < t then swap will route through both pools.
669
559
  */
670
- public swapRoutingOut(
560
+ private swapRoutingOut(
671
561
  t: bigint,
672
562
  x: bigint,
673
563
  y: bigint,
@@ -704,7 +594,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
704
594
  * @note If a > t then entire trade route through col pool and col pool arbitrage with debt pool.
705
595
  * @note If a > 0 & a < t then swap will route through both pools.
706
596
  */
707
- public swapRoutingIn(
597
+ private swapRoutingIn(
708
598
  t: bigint,
709
599
  x: bigint,
710
600
  y: bigint,
@@ -734,7 +624,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
734
624
  * @param {number} fee - The fee for the swap. 1e4 = 1%
735
625
  * @returns {bigint} amountIn - The calculated input amount required for the swap.
736
626
  */
737
- public swapOut(
627
+ private swapOut(
738
628
  swap0to1: boolean,
739
629
  amountOut: bigint,
740
630
  colReserves: CollateralReserves,
@@ -742,8 +632,6 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
742
632
  inDecimals: number,
743
633
  outDecimals: number,
744
634
  fee: bigint,
745
- currentLimits: DexLimits,
746
- syncTime: number,
747
635
  ): bigint {
748
636
  const amountOutAdjusted =
749
637
  (amountOut * BigInt(10 ** 12)) / BigInt(10 ** outDecimals);
@@ -752,19 +640,15 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
752
640
  amountOutAdjusted,
753
641
  colReserves,
754
642
  debtReserves,
755
- outDecimals,
756
- currentLimits,
757
- syncTime,
758
643
  );
759
644
 
760
- if (amountIn == 2n ** 256n - 1n) {
761
- return amountIn;
762
- }
763
- const ans =
764
- (amountIn * this.FEE_100_PERCENT * BigInt(10 ** inDecimals)) /
765
- BigInt(10 ** 12) /
766
- (this.FEE_100_PERCENT - fee);
767
- return ans;
645
+ const FEE_100_PERCENT = BigInt(1e6); // Assuming this constant is defined elsewhere
646
+
647
+ const result =
648
+ ((amountIn * FEE_100_PERCENT) / (FEE_100_PERCENT - fee)) *
649
+ BigInt(10 ** (inDecimals - 12));
650
+
651
+ return result;
768
652
  }
769
653
 
770
654
  /**
@@ -775,18 +659,12 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
775
659
  * @param {DebtReserves} debtReserves - The reserves of the debt pool.
776
660
  * @returns {bigint} The calculated input amount required for the swap.
777
661
  */
778
- public swapOutAdjusted(
662
+ private swapOutAdjusted(
779
663
  swap0to1: boolean,
780
664
  amountOut: bigint,
781
665
  colReserves: CollateralReserves,
782
666
  debtReserves: DebtReserves,
783
- outDecimals: number,
784
- currentLimits: DexLimits,
785
- syncTime: number,
786
667
  ): bigint {
787
- if (amountOut === 0n) {
788
- return 0n; // Return 0 if output amount is 0
789
- }
790
668
  const {
791
669
  token0RealReserves,
792
670
  token1RealReserves,
@@ -803,17 +681,17 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
803
681
 
804
682
  // Check if all reserves of collateral pool are greater than 0
805
683
  const colPoolEnabled =
806
- token0RealReserves > BigInt(0) &&
807
- token1RealReserves > BigInt(0) &&
808
- token0ImaginaryReserves > BigInt(0) &&
809
- token1ImaginaryReserves > BigInt(0);
684
+ token0RealReserves > 0n &&
685
+ token1RealReserves > 0n &&
686
+ token0ImaginaryReserves > 0n &&
687
+ token1ImaginaryReserves > 0n;
810
688
 
811
689
  // Check if all reserves of debt pool are greater than 0
812
690
  const debtPoolEnabled =
813
- debtToken0RealReserves > BigInt(0) &&
814
- debtToken1RealReserves > BigInt(0) &&
815
- debtToken0ImaginaryReserves > BigInt(0) &&
816
- debtToken1ImaginaryReserves > BigInt(0);
691
+ debtToken0RealReserves > 0n &&
692
+ debtToken1RealReserves > 0n &&
693
+ debtToken0ImaginaryReserves > 0n &&
694
+ debtToken1ImaginaryReserves > 0n;
817
695
 
818
696
  let colReserveIn: bigint,
819
697
  colReserveOut: bigint,
@@ -823,7 +701,6 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
823
701
  colIReserveOut: bigint,
824
702
  debtIReserveIn: bigint,
825
703
  debtIReserveOut: bigint;
826
- let borrowable: bigint, withdrawable: bigint;
827
704
 
828
705
  if (swap0to1) {
829
706
  colReserveIn = token0RealReserves;
@@ -834,14 +711,6 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
834
711
  debtReserveOut = debtToken1RealReserves;
835
712
  debtIReserveIn = debtToken0ImaginaryReserves;
836
713
  debtIReserveOut = debtToken1ImaginaryReserves;
837
- borrowable = this.getExpandedLimit(
838
- syncTime,
839
- currentLimits.borrowableToken1,
840
- );
841
- withdrawable = this.getExpandedLimit(
842
- syncTime,
843
- currentLimits.withdrawableToken1,
844
- );
845
714
  } else {
846
715
  colReserveIn = token1RealReserves;
847
716
  colReserveOut = token0RealReserves;
@@ -851,21 +720,8 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
851
720
  debtReserveOut = debtToken0RealReserves;
852
721
  debtIReserveIn = debtToken1ImaginaryReserves;
853
722
  debtIReserveOut = debtToken0ImaginaryReserves;
854
- borrowable = this.getExpandedLimit(
855
- syncTime,
856
- currentLimits.borrowableToken0,
857
- );
858
- withdrawable = this.getExpandedLimit(
859
- syncTime,
860
- currentLimits.withdrawableToken0,
861
- );
862
723
  }
863
724
 
864
- // bring borrowable and withdrawable from token decimals to 1e12 decimals, same as amounts
865
- borrowable = (borrowable * BigInt(10 ** 12)) / BigInt(10 ** outDecimals);
866
- withdrawable =
867
- (withdrawable * BigInt(10 ** 12)) / BigInt(10 ** outDecimals);
868
-
869
725
  let a: bigint;
870
726
  if (colPoolEnabled && debtPoolEnabled) {
871
727
  a = this.swapRoutingOut(
@@ -876,106 +732,51 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
876
732
  debtIReserveOut,
877
733
  );
878
734
  } else if (debtPoolEnabled) {
879
- a = BigInt(-1); // Route from debt pool
735
+ a = -1n; // Route from debt pool
880
736
  } else if (colPoolEnabled) {
881
- a = amountOut + BigInt(1); // Route from collateral pool
737
+ a = amountOut + 1n; // Route from collateral pool
882
738
  } else {
883
739
  throw new Error('No pools are enabled');
884
740
  }
885
741
 
886
- let amountInCollateral: bigint = BigInt(0);
887
- let amountInDebt: bigint = BigInt(0);
888
- let amountOutCollateral: bigint = BigInt(0);
889
- let amountOutDebt: bigint = BigInt(0);
742
+ let amountInCollateral = 0n;
743
+ let amountInDebt = 0n;
890
744
 
891
- if (a <= BigInt(0)) {
745
+ if (a <= 0n) {
892
746
  // Entire trade routes through debt pool
893
- amountOutDebt = amountOut;
894
747
  amountInDebt = this.getAmountIn(
895
748
  amountOut,
896
749
  debtIReserveIn,
897
750
  debtIReserveOut,
898
751
  );
899
752
  if (amountOut > debtReserveOut) {
900
- return 2n ** 256n - 1n;
901
- }
902
- if (amountOut > borrowable) {
903
- return 2n ** 256n - 1n;
753
+ return 2n ** 64n - 1n; // BigInt max value
904
754
  }
905
755
  } else if (a >= amountOut) {
906
756
  // Entire trade routes through collateral pool
907
- amountOutCollateral = amountOut;
908
757
  amountInCollateral = this.getAmountIn(
909
758
  amountOut,
910
759
  colIReserveIn,
911
760
  colIReserveOut,
912
761
  );
913
762
  if (amountOut > colReserveOut) {
914
- return 2n ** 256n - 1n;
915
- }
916
- if (amountOut > withdrawable) {
917
- return 2n ** 256n - 1n;
763
+ return 2n ** 64n - 1n; // BigInt max value
918
764
  }
919
765
  } else {
920
766
  // Trade routes through both pools
921
- amountOutCollateral = a;
922
767
  amountInCollateral = this.getAmountIn(a, colIReserveIn, colIReserveOut);
923
- amountOutDebt = amountOut - a;
924
768
  amountInDebt = this.getAmountIn(
925
- amountOutDebt,
769
+ amountOut - a,
926
770
  debtIReserveIn,
927
771
  debtIReserveOut,
928
772
  );
929
- if (amountOutDebt > debtReserveOut || a > colReserveOut) {
930
- return 2n ** 256n - 1n;
931
- }
932
- if (amountOutDebt > borrowable || a > withdrawable) {
933
- return 2n ** 256n - 1n;
773
+ if (amountOut - a > debtReserveOut || a > debtReserveOut) {
774
+ return 2n ** 64n - 1n; // BigInt max value
934
775
  }
935
776
  }
936
777
 
937
- let oldPrice: bigint;
938
- let newPrice: bigint;
939
- const SCALE = BigInt(1e27);
940
-
941
- // from whatever pool higher amount of swap is routing we are taking that as final price, does not matter much because both pools final price should be same
942
- if (amountOutCollateral > amountOutDebt) {
943
- // new pool price from col pool
944
- oldPrice = swap0to1
945
- ? (colIReserveOut * SCALE) / colIReserveIn
946
- : (colIReserveIn * SCALE) / colIReserveOut;
947
- newPrice = swap0to1
948
- ? ((colIReserveOut - amountOutCollateral) * SCALE) /
949
- (colIReserveIn + amountInCollateral)
950
- : ((colIReserveIn + amountInCollateral) * SCALE) /
951
- (colIReserveOut - amountOutCollateral);
952
- } else {
953
- // new pool price from debt pool
954
- oldPrice = swap0to1
955
- ? (debtIReserveOut * SCALE) / debtIReserveIn
956
- : (debtIReserveIn * SCALE) / debtIReserveOut;
957
- newPrice = swap0to1
958
- ? ((debtIReserveOut - amountOutDebt) * SCALE) /
959
- (debtIReserveIn + amountInDebt)
960
- : ((debtIReserveIn + amountInDebt) * SCALE) /
961
- (debtIReserveOut - amountOutDebt);
962
- }
963
-
964
- const MAX_PRICE_DIFF = BigInt(5); // 5%
965
- if (
966
- this.abs(oldPrice - newPrice) >
967
- (oldPrice / BigInt(100)) * MAX_PRICE_DIFF
968
- ) {
969
- // if price diff is > 5% then swap would revert.
970
- return 2n ** 256n - 1n;
971
- }
972
-
973
778
  const totalAmountIn = amountInCollateral + amountInDebt;
974
779
 
975
780
  return totalAmountIn;
976
781
  }
977
-
978
- private abs(value: bigint): bigint {
979
- return value < 0 ? -value : value;
980
- }
981
782
  }