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