@paraswap/dex-lib 4.1.1 → 4.1.2-fluid

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.
@@ -48,11 +48,6 @@ 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
-
56
51
  eventPools: FluidDexEventPool[] = [];
57
52
 
58
53
  readonly factory: FluidDexFactory;
@@ -115,9 +110,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
115
110
  // implement this function
116
111
  async initializePricing(blockNumber: number) {
117
112
  await this.factory.initialize(blockNumber);
118
- this.pools = (await this.fetchFluidDexPools(blockNumber)).filter(
119
- pool => !this.restrictedIds.includes(pool.id),
120
- );
113
+ this.pools = await this.fetchFluidDexPools(blockNumber);
121
114
  this.eventPools = await Promise.all(
122
115
  this.pools.map(async pool => {
123
116
  const eventPool = new FluidDexEventPool(
@@ -140,9 +133,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
140
133
  }
141
134
 
142
135
  protected onPoolCreatedUpdatePools(poolsFromFactory: readonly Pool[]) {
143
- this.pools = this.generateFluidDexPoolsFromPoolsFactory(
144
- poolsFromFactory,
145
- ).filter(pool => !this.restrictedIds.includes(pool.id));
136
+ this.pools = this.generateFluidDexPoolsFromPoolsFactory(poolsFromFactory);
146
137
  this.logger.info(`${this.dexKey}: pools list was updated ...`);
147
138
  }
148
139
 
@@ -182,9 +173,6 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
182
173
  // A pair must have 2 different tokens.
183
174
  if (srcAddress === destAddress) return [];
184
175
 
185
- this.pools = this.pools.filter(
186
- pool => !this.restrictedIds.includes(pool.id),
187
- );
188
176
  const pools = this.pools.filter(
189
177
  pool =>
190
178
  (srcAddress === pool.token0 && destAddress === pool.token1) ||
@@ -260,6 +248,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
260
248
  destToken.decimals,
261
249
  BigInt(currentPoolReserves.fee),
262
250
  currentPoolReserves.dexLimits,
251
+ BigInt(currentPoolReserves.centerPrice),
263
252
  Math.floor(Date.now() / 1000),
264
253
  );
265
254
  } else {
@@ -272,6 +261,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
272
261
  destToken.decimals,
273
262
  BigInt(currentPoolReserves.fee),
274
263
  currentPoolReserves.dexLimits,
264
+ BigInt(currentPoolReserves.centerPrice),
275
265
  Math.floor(Date.now() / 1000),
276
266
  );
277
267
  }
@@ -303,6 +293,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
303
293
  }, ${side}:`,
304
294
  e,
305
295
  );
296
+ throw e;
306
297
  return null;
307
298
  }
308
299
  }
@@ -380,9 +371,19 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
380
371
  }
381
372
  } else {
382
373
  if (pool!.token0.toLowerCase() !== srcToken.toLowerCase()) {
383
- args = [false, BigInt(destAmount), BigInt(srcAmount), recipient];
374
+ args = [
375
+ false,
376
+ (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
377
+ BigInt(srcAmount),
378
+ recipient,
379
+ ];
384
380
  } else {
385
- args = [true, BigInt(destAmount), BigInt(srcAmount), recipient];
381
+ args = [
382
+ true,
383
+ (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
384
+ BigInt(srcAmount),
385
+ recipient,
386
+ ];
386
387
  }
387
388
  }
388
389
  const swapData = this.fluidDexPoolIface.encodeFunctionData(method, args);
@@ -415,6 +416,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
415
416
  outDecimals: number,
416
417
  fee: bigint,
417
418
  currentLimits: DexLimits,
419
+ centerPrice: bigint,
418
420
  syncTime: number,
419
421
  ): bigint {
420
422
  if (amountIn === 0n) {
@@ -438,6 +440,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
438
440
  fee,
439
441
  outDecimals,
440
442
  currentLimits,
443
+ centerPrice,
441
444
  syncTime,
442
445
  );
443
446
  return (amountOut * BigInt(10 ** outDecimals)) / BigInt(10 ** 12);
@@ -459,6 +462,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
459
462
  fee: bigint,
460
463
  outDecimals: number,
461
464
  currentLimits: DexLimits,
465
+ centerPrice: bigint,
462
466
  syncTime: number,
463
467
  ): bigint {
464
468
  const {
@@ -603,6 +607,40 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
603
607
  return 0n;
604
608
  }
605
609
 
610
+ if (amountInCollateral > 0) {
611
+ let reservesRatioValid = swap0To1
612
+ ? this.verifyToken1Reserves(
613
+ colReserveIn + amountInCollateral,
614
+ colReserveOut - amountOutCollateral,
615
+ centerPrice,
616
+ )
617
+ : this.verifyToken0Reserves(
618
+ colReserveOut - amountOutCollateral,
619
+ colReserveIn + amountInCollateral,
620
+ centerPrice,
621
+ );
622
+ if (!reservesRatioValid) {
623
+ return 0n;
624
+ }
625
+ }
626
+
627
+ if (amountInDebt > 0) {
628
+ let reservesRatioValid = swap0To1
629
+ ? this.verifyToken1Reserves(
630
+ debtReserveIn + amountInDebt,
631
+ debtReserveOut - amountOutDebt,
632
+ centerPrice,
633
+ )
634
+ : this.verifyToken0Reserves(
635
+ debtReserveOut - amountOutDebt,
636
+ debtReserveIn + amountInDebt,
637
+ centerPrice,
638
+ );
639
+ if (!reservesRatioValid) {
640
+ return 0n;
641
+ }
642
+ }
643
+
606
644
  // For price calculations, we'll use a precision factor for bigint division
607
645
  const PRECISION = 1000000000000000000000000000n; // 1e27
608
646
 
@@ -642,40 +680,6 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
642
680
  return 0n;
643
681
  }
644
682
 
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
-
679
683
  const totalAmountOut = amountOutCollateral + amountOutDebt;
680
684
 
681
685
  return totalAmountOut;
@@ -879,6 +883,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
879
883
  outDecimals: number,
880
884
  fee: bigint,
881
885
  currentLimits: DexLimits,
886
+ centerPrice: bigint,
882
887
  syncTime: number,
883
888
  ): bigint {
884
889
  const amountOutAdjusted =
@@ -892,6 +897,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
892
897
  fee,
893
898
  outDecimals,
894
899
  currentLimits,
900
+ centerPrice,
895
901
  syncTime,
896
902
  );
897
903
 
@@ -900,7 +906,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
900
906
  }
901
907
  const ans = (amountIn * BigInt(10 ** inDecimals)) / BigInt(10 ** 12);
902
908
 
903
- return ans;
909
+ return (ans * (100000n - 5n)) / 100000n;
904
910
  }
905
911
 
906
912
  /**
@@ -919,6 +925,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
919
925
  fee: bigint,
920
926
  outDecimals: number,
921
927
  currentLimits: DexLimits,
928
+ centerPrice: bigint,
922
929
  syncTime: number,
923
930
  ): bigint {
924
931
  if (amountOut === 0n) {
@@ -1036,10 +1043,10 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
1036
1043
  );
1037
1044
  amountInDebt = this.applyFeeForBuy(amountInDebt, fee);
1038
1045
  if (amountOut > debtReserveOut) {
1039
- return 2n ** 256n - 1n;
1046
+ return 0n;
1040
1047
  }
1041
1048
  if (amountOut > borrowable) {
1042
- return 2n ** 256n - 1n;
1049
+ return 0n;
1043
1050
  }
1044
1051
  } else if (a >= amountOut) {
1045
1052
  // Entire trade routes through collateral pool
@@ -1051,10 +1058,10 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
1051
1058
  );
1052
1059
  amountInCollateral = this.applyFeeForBuy(amountInCollateral, fee);
1053
1060
  if (amountOut > colReserveOut) {
1054
- return 2n ** 256n - 1n;
1061
+ return 0n;
1055
1062
  }
1056
1063
  if (amountOut > withdrawable) {
1057
- return 2n ** 256n - 1n;
1064
+ return 0n;
1058
1065
  }
1059
1066
  } else {
1060
1067
  // Trade routes through both pools
@@ -1072,10 +1079,43 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
1072
1079
 
1073
1080
  amountInDebt = this.applyFeeForBuy(amountInDebt, fee);
1074
1081
  if (amountOutDebt > debtReserveOut || a > colReserveOut) {
1075
- return 2n ** 256n - 1n;
1082
+ return 0n;
1076
1083
  }
1077
1084
  if (amountOutDebt > borrowable || a > withdrawable) {
1078
- return 2n ** 256n - 1n;
1085
+ return 0n;
1086
+ }
1087
+ }
1088
+
1089
+ if (amountInCollateral > 0) {
1090
+ let reservesRatioValid = swap0to1
1091
+ ? this.verifyToken1Reserves(
1092
+ colReserveIn + amountInCollateral,
1093
+ colReserveOut - amountOutCollateral,
1094
+ centerPrice,
1095
+ )
1096
+ : this.verifyToken0Reserves(
1097
+ colReserveOut - amountOutCollateral,
1098
+ colReserveIn + amountInCollateral,
1099
+ centerPrice,
1100
+ );
1101
+ if (!reservesRatioValid) {
1102
+ return 0n;
1103
+ }
1104
+ }
1105
+ if (amountInDebt > 0) {
1106
+ let reservesRatioValid = swap0to1
1107
+ ? this.verifyToken1Reserves(
1108
+ debtReserveIn + amountInDebt,
1109
+ debtReserveOut - amountOutDebt,
1110
+ centerPrice,
1111
+ )
1112
+ : this.verifyToken0Reserves(
1113
+ debtReserveOut - amountOutDebt,
1114
+ debtReserveIn + amountInDebt,
1115
+ centerPrice,
1116
+ );
1117
+ if (!reservesRatioValid) {
1118
+ return 0n;
1079
1119
  }
1080
1120
  }
1081
1121
 
@@ -1112,40 +1152,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
1112
1152
  (oldPrice / BigInt(100)) * MAX_PRICE_DIFF
1113
1153
  ) {
1114
1154
  // if price diff is > 5% then swap would revert.
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
- }
1155
+ return 0n;
1149
1156
  }
1150
1157
 
1151
1158
  const totalAmountIn = amountInCollateral + amountInDebt;
@@ -6,6 +6,7 @@ export type PoolReserve = {
6
6
  token0: string;
7
7
  token1: string;
8
8
  fee: number;
9
+ centerPrice: number;
9
10
  collateralReserves: CollateralReserves;
10
11
  debtReserves: DebtReserves;
11
12
  dexLimits: DexLimits;
@@ -16,6 +17,7 @@ export type PoolReserveResponse = [
16
17
  string,
17
18
  string,
18
19
  BigNumber,
20
+ BigNumber,
19
21
  BigNumber[],
20
22
  BigNumber[],
21
23
  DexLimitResponse,
@@ -68,6 +70,7 @@ export interface PoolWithReserves {
68
70
  token0: string;
69
71
  token1: string;
70
72
  fee: number;
73
+ centerPrice: number;
71
74
  collateralReserves: CollateralReserves;
72
75
  debtReserves: DebtReserves;
73
76
  }
@@ -1773,7 +1773,7 @@ export const Holders: {
1773
1773
  BB_sUSDe: '0xaFeb95DEF3B2A3D532D74DaBd51E62048d6c07A4',
1774
1774
  AA_iETHv2: '0xA118aD79E2152b9a3c7Df8B8791887762b0f1D49',
1775
1775
  BB_iETHv2: '0x15079cBAa74C1df2a602fAc88Bd5b98B08FfE6A4',
1776
- ETH: '0x176F3DAb24a159341c0509bB36B833E7fdd0a132',
1776
+ ETH: '0x28C6c06298d514Db089934071355E5743bf21d60',
1777
1777
  USDC: '0x7713974908be4bed47172370115e8b1219f4a5f0',
1778
1778
  USDE: '0x8707f238936c12c309bfc2B9959C35828AcFc512',
1779
1779
  AMPL: '0x223592a191ECfC7FDC38a9256c3BD96E771539A9',
@@ -248,6 +248,7 @@ export class TenderlySimulation implements TransactionSimulator {
248
248
  };
249
249
  }
250
250
  } catch (e) {
251
+ console.error('TenderlySimulation_simulate_error', e);
251
252
  return {
252
253
  success: false,
253
254
  };