@paraswap/dex-lib 4.1.1 → 4.1.2-fluid.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/abi/fluid-dex/resolver.abi.json +30 -0
- package/build/dex/balancer-v3/balancer-v3-pool.d.ts +2 -0
- package/build/dex/balancer-v3/balancer-v3-pool.js +14 -4
- package/build/dex/balancer-v3/balancer-v3-pool.js.map +1 -1
- package/build/dex/balancer-v3/balancer-v3.js +1 -1
- package/build/dex/balancer-v3/balancer-v3.js.map +1 -1
- package/build/dex/fluid-dex/config.js +2 -2
- package/build/dex/fluid-dex/constants.d.ts +1 -1
- package/build/dex/fluid-dex/constants.js +2 -1
- package/build/dex/fluid-dex/constants.js.map +1 -1
- package/build/dex/fluid-dex/fluid-dex-liquidity-proxy.js +3 -1
- package/build/dex/fluid-dex/fluid-dex-liquidity-proxy.js.map +1 -1
- package/build/dex/fluid-dex/fluid-dex.d.ts +4 -5
- package/build/dex/fluid-dex/fluid-dex.js +61 -56
- package/build/dex/fluid-dex/fluid-dex.js.map +1 -1
- package/build/dex/fluid-dex/types.d.ts +3 -0
- package/package.json +1 -1
- package/src/abi/fluid-dex/resolver.abi.json +30 -0
- package/src/dex/balancer-v3/balancer-v3-events.test.ts +157 -84
- package/src/dex/balancer-v3/balancer-v3-pool.ts +33 -13
- package/src/dex/balancer-v3/balancer-v3.ts +1 -1
- package/src/dex/fluid-dex/config.ts +2 -2
- package/src/dex/fluid-dex/constants.ts +2 -1
- package/src/dex/fluid-dex/fluid-dex-e2e.test.ts +61 -117
- package/src/dex/fluid-dex/fluid-dex-liquidity-proxy.ts +3 -0
- package/src/dex/fluid-dex/fluid-dex.ts +96 -91
- package/src/dex/fluid-dex/types.ts +3 -0
- package/tests/constants-e2e.ts +1 -1
- package/tests/tenderly-simulation.ts +1 -0
|
@@ -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 =
|
|
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
|
}
|
|
@@ -380,9 +370,19 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
|
|
|
380
370
|
}
|
|
381
371
|
} else {
|
|
382
372
|
if (pool!.token0.toLowerCase() !== srcToken.toLowerCase()) {
|
|
383
|
-
args = [
|
|
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
|
+
];
|
|
384
379
|
} else {
|
|
385
|
-
args = [
|
|
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
|
+
];
|
|
386
386
|
}
|
|
387
387
|
}
|
|
388
388
|
const swapData = this.fluidDexPoolIface.encodeFunctionData(method, args);
|
|
@@ -415,6 +415,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
|
|
|
415
415
|
outDecimals: number,
|
|
416
416
|
fee: bigint,
|
|
417
417
|
currentLimits: DexLimits,
|
|
418
|
+
centerPrice: bigint,
|
|
418
419
|
syncTime: number,
|
|
419
420
|
): bigint {
|
|
420
421
|
if (amountIn === 0n) {
|
|
@@ -438,6 +439,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
|
|
|
438
439
|
fee,
|
|
439
440
|
outDecimals,
|
|
440
441
|
currentLimits,
|
|
442
|
+
centerPrice,
|
|
441
443
|
syncTime,
|
|
442
444
|
);
|
|
443
445
|
return (amountOut * BigInt(10 ** outDecimals)) / BigInt(10 ** 12);
|
|
@@ -459,6 +461,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
|
|
|
459
461
|
fee: bigint,
|
|
460
462
|
outDecimals: number,
|
|
461
463
|
currentLimits: DexLimits,
|
|
464
|
+
centerPrice: bigint,
|
|
462
465
|
syncTime: number,
|
|
463
466
|
): bigint {
|
|
464
467
|
const {
|
|
@@ -603,6 +606,40 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
|
|
|
603
606
|
return 0n;
|
|
604
607
|
}
|
|
605
608
|
|
|
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
|
+
|
|
606
643
|
// For price calculations, we'll use a precision factor for bigint division
|
|
607
644
|
const PRECISION = 1000000000000000000000000000n; // 1e27
|
|
608
645
|
|
|
@@ -642,40 +679,6 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
|
|
|
642
679
|
return 0n;
|
|
643
680
|
}
|
|
644
681
|
|
|
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
682
|
const totalAmountOut = amountOutCollateral + amountOutDebt;
|
|
680
683
|
|
|
681
684
|
return totalAmountOut;
|
|
@@ -879,6 +882,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
|
|
|
879
882
|
outDecimals: number,
|
|
880
883
|
fee: bigint,
|
|
881
884
|
currentLimits: DexLimits,
|
|
885
|
+
centerPrice: bigint,
|
|
882
886
|
syncTime: number,
|
|
883
887
|
): bigint {
|
|
884
888
|
const amountOutAdjusted =
|
|
@@ -892,6 +896,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
|
|
|
892
896
|
fee,
|
|
893
897
|
outDecimals,
|
|
894
898
|
currentLimits,
|
|
899
|
+
centerPrice,
|
|
895
900
|
syncTime,
|
|
896
901
|
);
|
|
897
902
|
|
|
@@ -899,7 +904,6 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
|
|
|
899
904
|
return amountIn;
|
|
900
905
|
}
|
|
901
906
|
const ans = (amountIn * BigInt(10 ** inDecimals)) / BigInt(10 ** 12);
|
|
902
|
-
|
|
903
907
|
return ans;
|
|
904
908
|
}
|
|
905
909
|
|
|
@@ -919,6 +923,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
|
|
|
919
923
|
fee: bigint,
|
|
920
924
|
outDecimals: number,
|
|
921
925
|
currentLimits: DexLimits,
|
|
926
|
+
centerPrice: bigint,
|
|
922
927
|
syncTime: number,
|
|
923
928
|
): bigint {
|
|
924
929
|
if (amountOut === 0n) {
|
|
@@ -1036,10 +1041,10 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
|
|
|
1036
1041
|
);
|
|
1037
1042
|
amountInDebt = this.applyFeeForBuy(amountInDebt, fee);
|
|
1038
1043
|
if (amountOut > debtReserveOut) {
|
|
1039
|
-
return
|
|
1044
|
+
return 0n;
|
|
1040
1045
|
}
|
|
1041
1046
|
if (amountOut > borrowable) {
|
|
1042
|
-
return
|
|
1047
|
+
return 0n;
|
|
1043
1048
|
}
|
|
1044
1049
|
} else if (a >= amountOut) {
|
|
1045
1050
|
// Entire trade routes through collateral pool
|
|
@@ -1051,10 +1056,10 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
|
|
|
1051
1056
|
);
|
|
1052
1057
|
amountInCollateral = this.applyFeeForBuy(amountInCollateral, fee);
|
|
1053
1058
|
if (amountOut > colReserveOut) {
|
|
1054
|
-
return
|
|
1059
|
+
return 0n;
|
|
1055
1060
|
}
|
|
1056
1061
|
if (amountOut > withdrawable) {
|
|
1057
|
-
return
|
|
1062
|
+
return 0n;
|
|
1058
1063
|
}
|
|
1059
1064
|
} else {
|
|
1060
1065
|
// Trade routes through both pools
|
|
@@ -1072,10 +1077,43 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
|
|
|
1072
1077
|
|
|
1073
1078
|
amountInDebt = this.applyFeeForBuy(amountInDebt, fee);
|
|
1074
1079
|
if (amountOutDebt > debtReserveOut || a > colReserveOut) {
|
|
1075
|
-
return
|
|
1080
|
+
return 0n;
|
|
1076
1081
|
}
|
|
1077
1082
|
if (amountOutDebt > borrowable || a > withdrawable) {
|
|
1078
|
-
return
|
|
1083
|
+
return 0n;
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
if (amountInCollateral > 0) {
|
|
1088
|
+
let reservesRatioValid = swap0to1
|
|
1089
|
+
? this.verifyToken1Reserves(
|
|
1090
|
+
colReserveIn + amountInCollateral,
|
|
1091
|
+
colReserveOut - amountOutCollateral,
|
|
1092
|
+
centerPrice,
|
|
1093
|
+
)
|
|
1094
|
+
: this.verifyToken0Reserves(
|
|
1095
|
+
colReserveOut - amountOutCollateral,
|
|
1096
|
+
colReserveIn + amountInCollateral,
|
|
1097
|
+
centerPrice,
|
|
1098
|
+
);
|
|
1099
|
+
if (!reservesRatioValid) {
|
|
1100
|
+
return 0n;
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
if (amountInDebt > 0) {
|
|
1104
|
+
let reservesRatioValid = swap0to1
|
|
1105
|
+
? this.verifyToken1Reserves(
|
|
1106
|
+
debtReserveIn + amountInDebt,
|
|
1107
|
+
debtReserveOut - amountOutDebt,
|
|
1108
|
+
centerPrice,
|
|
1109
|
+
)
|
|
1110
|
+
: this.verifyToken0Reserves(
|
|
1111
|
+
debtReserveOut - amountOutDebt,
|
|
1112
|
+
debtReserveIn + amountInDebt,
|
|
1113
|
+
centerPrice,
|
|
1114
|
+
);
|
|
1115
|
+
if (!reservesRatioValid) {
|
|
1116
|
+
return 0n;
|
|
1079
1117
|
}
|
|
1080
1118
|
}
|
|
1081
1119
|
|
|
@@ -1112,40 +1150,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
|
|
|
1112
1150
|
(oldPrice / BigInt(100)) * MAX_PRICE_DIFF
|
|
1113
1151
|
) {
|
|
1114
1152
|
// if price diff is > 5% then swap would revert.
|
|
1115
|
-
return
|
|
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
|
-
}
|
|
1153
|
+
return 0n;
|
|
1149
1154
|
}
|
|
1150
1155
|
|
|
1151
1156
|
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
|
}
|
package/tests/constants-e2e.ts
CHANGED
|
@@ -1773,7 +1773,7 @@ export const Holders: {
|
|
|
1773
1773
|
BB_sUSDe: '0xaFeb95DEF3B2A3D532D74DaBd51E62048d6c07A4',
|
|
1774
1774
|
AA_iETHv2: '0xA118aD79E2152b9a3c7Df8B8791887762b0f1D49',
|
|
1775
1775
|
BB_iETHv2: '0x15079cBAa74C1df2a602fAc88Bd5b98B08FfE6A4',
|
|
1776
|
-
ETH: '
|
|
1776
|
+
ETH: '0x28C6c06298d514Db089934071355E5743bf21d60',
|
|
1777
1777
|
USDC: '0x7713974908be4bed47172370115e8b1219f4a5f0',
|
|
1778
1778
|
USDE: '0x8707f238936c12c309bfc2B9959C35828AcFc512',
|
|
1779
1779
|
AMPL: '0x223592a191ECfC7FDC38a9256c3BD96E771539A9',
|