@paraswap/dex-lib 2.16.6 → 2.16.7-balancer-v2-buy
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/.idea/aws.xml +17 -0
- package/.idea/codeStyles/Project.xml +19 -0
- package/.idea/inspectionProfiles/Project_Default.xml +6 -0
- package/.idea/modules.xml +1 -1
- package/.idea/paraswap-dex-lib.iml +9 -0
- package/build/config.js +4 -4
- package/build/dex/aave-v3/tokens/arbitrum.json +20 -20
- package/build/dex/aave-v3/tokens/avalanche.json +44 -20
- package/build/dex/aave-v3/tokens/fantom.json +30 -30
- package/build/dex/aave-v3/tokens/optimism.json +18 -18
- package/build/dex/aave-v3/tokens/polygon.json +60 -42
- package/build/dex/balancer-v2/balancer-v2.d.ts +9 -14
- package/build/dex/balancer-v2/balancer-v2.js +19 -16
- package/build/dex/balancer-v2/balancer-v2.js.map +1 -1
- package/build/dex/balancer-v2/config.d.ts +2 -7
- package/build/dex/balancer-v2/config.js +20 -30
- package/build/dex/balancer-v2/config.js.map +1 -1
- package/build/dex/balancer-v2/pools/balancer-v2-pool.d.ts +25 -0
- package/build/dex/balancer-v2/pools/balancer-v2-pool.js +107 -0
- package/build/dex/balancer-v2/pools/balancer-v2-pool.js.map +1 -0
- package/build/dex/balancer-v2/pools/linear/LinearMath.d.ts +19 -0
- package/build/dex/balancer-v2/pools/linear/LinearMath.js +179 -0
- package/build/dex/balancer-v2/pools/linear/LinearMath.js.map +1 -0
- package/build/dex/balancer-v2/pools/linear/LinearPool.d.ts +50 -0
- package/build/dex/balancer-v2/pools/linear/LinearPool.js +458 -0
- package/build/dex/balancer-v2/pools/linear/LinearPool.js.map +1 -0
- package/build/dex/balancer-v2/pools/phantom-stable/PhantomStableMath.d.ts +10 -0
- package/build/dex/balancer-v2/pools/phantom-stable/PhantomStableMath.js +314 -0
- package/build/dex/balancer-v2/pools/phantom-stable/PhantomStableMath.js.map +1 -0
- package/build/dex/balancer-v2/pools/phantom-stable/PhantomStablePool.d.ts +50 -0
- package/build/dex/balancer-v2/pools/phantom-stable/PhantomStablePool.js +367 -0
- package/build/dex/balancer-v2/pools/phantom-stable/PhantomStablePool.js.map +1 -0
- package/build/dex/balancer-v2/pools/stable/StableMath.d.ts +7 -0
- package/build/dex/balancer-v2/pools/stable/StableMath.js +111 -0
- package/build/dex/balancer-v2/pools/stable/StableMath.js.map +1 -0
- package/build/dex/balancer-v2/pools/stable/StablePool.d.ts +30 -0
- package/build/dex/balancer-v2/pools/stable/StablePool.js +129 -0
- package/build/dex/balancer-v2/pools/stable/StablePool.js.map +1 -0
- package/build/dex/balancer-v2/pools/weighted/WeightedMath.d.ts +6 -0
- package/build/dex/balancer-v2/pools/weighted/WeightedMath.js +61 -0
- package/build/dex/balancer-v2/pools/weighted/WeightedMath.js.map +1 -0
- package/build/dex/balancer-v2/pools/weighted/WeightedPool.d.ts +30 -0
- package/build/dex/balancer-v2/pools/weighted/WeightedPool.js +112 -0
- package/build/dex/balancer-v2/pools/weighted/WeightedPool.js.map +1 -0
- package/build/dex/balancer-v2/types.d.ts +1 -0
- package/build/dex/balancer-v2/types.js.map +1 -1
- package/package.json +1 -1
- package/src/config.ts +4 -4
- package/src/dex/balancer-v2/balancer-v2-e2e.test.ts +219 -224
- package/src/dex/balancer-v2/balancer-v2.ts +41 -29
- package/src/dex/balancer-v2/config.ts +23 -35
- package/src/dex/balancer-v2/pools/balancer-v2-pool.ts +274 -0
- package/src/dex/balancer-v2/{LinearMath.ts → pools/linear/LinearMath.ts} +10 -10
- package/src/dex/balancer-v2/{LinearPool.ts → pools/linear/LinearPool.ts} +209 -6
- package/src/dex/balancer-v2/{StableMath.ts → pools/phantom-stable/PhantomStableMath.ts} +101 -21
- package/src/dex/balancer-v2/{PhantomStablePool.ts → pools/phantom-stable/PhantomStablePool.ts} +165 -11
- package/src/dex/balancer-v2/pools/stable/StableMath.ts +204 -0
- package/src/dex/balancer-v2/pools/stable/StablePool.ts +221 -0
- package/src/dex/balancer-v2/pools/weighted/WeightedMath.ts +84 -0
- package/src/dex/balancer-v2/pools/weighted/WeightedPool.ts +189 -0
- package/src/dex/balancer-v2/types.ts +1 -0
- package/tests/constants-e2e.ts +10 -5
- package/.vscode/launch.json +0 -21
- package/build/abi/AugustusRFQRouter.abi.json +0 -1248
- package/build/dex/aave-v3/tokens/mainnet.json +0 -44
- package/build/dex/balancer-v2/VirtualBoostedPool.d.ts +0 -79
- package/build/dex/balancer-v2/VirtualBoostedPool.js +0 -382
- package/build/dex/balancer-v2/VirtualBoostedPool.js.map +0 -1
- package/build/dex/test-aave-v3/config.d.ts +0 -4
- package/build/dex/test-aave-v3/config.js +0 -15
- package/build/dex/test-aave-v3/config.js.map +0 -1
- package/build/dex/test-aave-v3/test-aave-v3-pool.d.ts +0 -39
- package/build/dex/test-aave-v3/test-aave-v3-pool.js +0 -63
- package/build/dex/test-aave-v3/test-aave-v3-pool.js.map +0 -1
- package/build/dex/test-aave-v3/test-aave-v3.d.ts +0 -37
- package/build/dex/test-aave-v3/test-aave-v3.js +0 -126
- package/build/dex/test-aave-v3/test-aave-v3.js.map +0 -1
- package/build/dex/test-aave-v3/types.d.ts +0 -6
- package/build/dex/test-aave-v3/types.js +0 -3
- package/build/dex/test-aave-v3/types.js.map +0 -1
- package/src/dex/balancer-v2/balancer-v2-pool.ts +0 -744
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DexParams } from './types';
|
|
2
|
-
import { DexConfigMap } from '../../types';
|
|
3
|
-
import { Network } from '../../constants';
|
|
2
|
+
import { AdapterMappings, DexConfigMap } from '../../types';
|
|
3
|
+
import { Network, SwapSide } from '../../constants';
|
|
4
4
|
|
|
5
5
|
export const BalancerConfig: DexConfigMap<DexParams> = {
|
|
6
6
|
BalancerV2: {
|
|
@@ -42,37 +42,25 @@ export const BalancerConfig: DexConfigMap<DexParams> = {
|
|
|
42
42
|
// },
|
|
43
43
|
};
|
|
44
44
|
|
|
45
|
-
export const Adapters: {
|
|
46
|
-
[
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
},
|
|
53
|
-
|
|
54
|
-
[Network.
|
|
55
|
-
{
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
},
|
|
65
|
-
|
|
66
|
-
[Network.ARBITRUM]: [
|
|
67
|
-
{
|
|
68
|
-
name: 'ArbitrumAdapter01',
|
|
69
|
-
index: 5,
|
|
70
|
-
},
|
|
71
|
-
],
|
|
72
|
-
[Network.OPTIMISM]: [
|
|
73
|
-
{
|
|
74
|
-
name: 'OptimismAdapter01',
|
|
75
|
-
index: 4,
|
|
76
|
-
},
|
|
77
|
-
],
|
|
45
|
+
export const Adapters: Record<number, AdapterMappings> = {
|
|
46
|
+
[Network.MAINNET]: {
|
|
47
|
+
[SwapSide.SELL]: [{ name: 'Adapter02', index: 9 }],
|
|
48
|
+
[SwapSide.BUY]: [{ name: 'BuyAdapter', index: 9 }],
|
|
49
|
+
},
|
|
50
|
+
[Network.POLYGON]: {
|
|
51
|
+
[SwapSide.SELL]: [{ name: 'PolygonAdapter01', index: 9 }],
|
|
52
|
+
[SwapSide.BUY]: [{ name: 'PolygonBuyAdapter', index: 6 }],
|
|
53
|
+
},
|
|
54
|
+
[Network.FANTOM]:{
|
|
55
|
+
[SwapSide.SELL]: [{ name: 'FantomAdapter01', index: 5 }],
|
|
56
|
+
[SwapSide.BUY]: [{ name: 'FantomBuyAdapter', index: 4 }],
|
|
57
|
+
},
|
|
58
|
+
[Network.ARBITRUM]: {
|
|
59
|
+
[SwapSide.SELL]: [{ name: 'ArbitrumAdapter01', index: 5 }],
|
|
60
|
+
[SwapSide.BUY]: [{ name: 'ArbitrumBuyAdapter', index: 6 }],
|
|
61
|
+
},
|
|
62
|
+
[Network.OPTIMISM]: {
|
|
63
|
+
[SwapSide.SELL]: [{ name: 'OptimismAdapter01', index: 4 }],
|
|
64
|
+
[SwapSide.BUY]: [{ name: 'OptimismBuyAdapter', index: 5 }],
|
|
65
|
+
},
|
|
78
66
|
};
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { MathSol } from '../balancer-v2-math';
|
|
2
|
+
import { WeightedPoolPairData } from './weighted/WeightedPool';
|
|
3
|
+
import { StablePoolPairData } from './stable/StablePool';
|
|
4
|
+
|
|
5
|
+
export class BasePool {
|
|
6
|
+
_subtractSwapFeeAmount(amount: bigint, _swapFeePercentage: bigint): bigint {
|
|
7
|
+
// This returns amount - fee amount, so we round up (favoring a higher fee amount).
|
|
8
|
+
const feeAmount = MathSol.mulUpFixed(amount, _swapFeePercentage);
|
|
9
|
+
return amount - feeAmount;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
_addFeeAmount(amount: bigint, _swapFeePercentage: bigint): bigint {
|
|
13
|
+
return MathSol.divUpFixed(
|
|
14
|
+
amount,
|
|
15
|
+
MathSol.complementFixed(_swapFeePercentage),
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// These methods use fixed versions to match SC scaling
|
|
20
|
+
_upscaleArray(amounts: bigint[], scalingFactors: bigint[]): bigint[] {
|
|
21
|
+
return amounts.map((a, i) => MathSol.mulUpFixed(a, scalingFactors[i]));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
_upscale(amount: bigint, scalingFactor: bigint): bigint {
|
|
25
|
+
return MathSol.mulUpFixed(amount, scalingFactor);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
_downscaleDown(amount: bigint, scalingFactor: bigint): bigint {
|
|
29
|
+
return MathSol.divDownFixed(amount, scalingFactor);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
_downscaleUp(amount: bigint, scalingFactor: bigint): bigint {
|
|
33
|
+
return MathSol.divUpFixed(amount, scalingFactor);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
_nullifyIfMaxAmountExceeded(amountToTrade: bigint, swapMax: bigint): bigint {
|
|
37
|
+
return swapMax >= amountToTrade ? amountToTrade : 0n;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export abstract class BaseGeneralPool extends BasePool {
|
|
42
|
+
// Swap Hooks
|
|
43
|
+
onBuy(amounts: bigint[], poolPairData: StablePoolPairData): bigint[] {
|
|
44
|
+
return this._swapGivenOut(
|
|
45
|
+
amounts,
|
|
46
|
+
poolPairData.balances,
|
|
47
|
+
poolPairData.indexIn,
|
|
48
|
+
poolPairData.indexOut,
|
|
49
|
+
poolPairData.scalingFactors,
|
|
50
|
+
poolPairData.swapFee,
|
|
51
|
+
poolPairData.amp,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Modification: this is inspired from the function onSwap which is in the original contract
|
|
56
|
+
onSell(amounts: bigint[], poolPairData: StablePoolPairData): bigint[] {
|
|
57
|
+
// _validateIndexes(indexIn, indexOut, _getTotalTokens());
|
|
58
|
+
// uint256[] memory scalingFactors = _scalingFactors();
|
|
59
|
+
return this._swapGivenIn(
|
|
60
|
+
amounts,
|
|
61
|
+
poolPairData.balances,
|
|
62
|
+
poolPairData.indexIn,
|
|
63
|
+
poolPairData.indexOut,
|
|
64
|
+
poolPairData.scalingFactors,
|
|
65
|
+
poolPairData.swapFee,
|
|
66
|
+
poolPairData.amp,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
_swapGivenIn(
|
|
71
|
+
tokenAmountsIn: bigint[],
|
|
72
|
+
balances: bigint[],
|
|
73
|
+
indexIn: number,
|
|
74
|
+
indexOut: number,
|
|
75
|
+
scalingFactors: bigint[],
|
|
76
|
+
_swapFeePercentage: bigint,
|
|
77
|
+
_amplificationParameter: bigint,
|
|
78
|
+
): bigint[] {
|
|
79
|
+
// Fees are subtracted before scaling, to reduce the complexity of the rounding direction analysis.
|
|
80
|
+
const tokenAmountsInWithFee = tokenAmountsIn.map(a =>
|
|
81
|
+
this._subtractSwapFeeAmount(a, _swapFeePercentage),
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const balancesUpscaled = this._upscaleArray(balances, scalingFactors);
|
|
85
|
+
const tokenAmountsInScaled = tokenAmountsInWithFee.map(a =>
|
|
86
|
+
this._upscale(a, scalingFactors[indexIn]),
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const amountsOut = this._onSwapGivenIn(
|
|
90
|
+
tokenAmountsInScaled,
|
|
91
|
+
balancesUpscaled,
|
|
92
|
+
indexIn,
|
|
93
|
+
indexOut,
|
|
94
|
+
_amplificationParameter,
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
// amountOut tokens are exiting the Pool, so we round down.
|
|
98
|
+
return amountsOut.map(a =>
|
|
99
|
+
this._downscaleDown(a, scalingFactors[indexOut]),
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
_swapGivenOut(
|
|
104
|
+
tokenAmountsOut: bigint[],
|
|
105
|
+
balances: bigint[],
|
|
106
|
+
indexIn: number,
|
|
107
|
+
indexOut: number,
|
|
108
|
+
scalingFactors: bigint[],
|
|
109
|
+
_swapFeePercentage: bigint,
|
|
110
|
+
_amplificationParameter: bigint,
|
|
111
|
+
): bigint[] {
|
|
112
|
+
const balancesUpscaled = this._upscaleArray(balances, scalingFactors);
|
|
113
|
+
const tokenAmountsOutScaled = tokenAmountsOut.map(a =>
|
|
114
|
+
this._upscale(a, scalingFactors[indexOut]),
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
let amountsIn: bigint[];
|
|
118
|
+
try {
|
|
119
|
+
amountsIn = this._onSwapGivenOut(
|
|
120
|
+
tokenAmountsOutScaled,
|
|
121
|
+
balancesUpscaled,
|
|
122
|
+
indexIn,
|
|
123
|
+
indexOut,
|
|
124
|
+
_amplificationParameter,
|
|
125
|
+
);
|
|
126
|
+
} catch (e) {
|
|
127
|
+
amountsIn = new Array(tokenAmountsOut.length).fill(0n);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// amountIn tokens are entering the Pool, so we round up.
|
|
131
|
+
const amountsInDownscaled = amountsIn.map(a =>
|
|
132
|
+
this._downscaleUp(a, scalingFactors[indexIn]),
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
// Fees are added after scaling happens, to reduce the complexity of the rounding direction analysis.
|
|
136
|
+
return amountsInDownscaled.map(a =>
|
|
137
|
+
this._subtractSwapFeeAmount(a, _swapFeePercentage),
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/*
|
|
142
|
+
* @dev Called when a swap with the Pool occurs, where the amount of tokens entering the Pool is known.
|
|
143
|
+
*
|
|
144
|
+
* Returns the amount of tokens that will be taken from the Pool in return.
|
|
145
|
+
*
|
|
146
|
+
* All amounts inside `swapRequest` and `balances` are upscaled. The swap fee has already been deducted from
|
|
147
|
+
* `swapRequest.amount`.
|
|
148
|
+
*
|
|
149
|
+
* The return value is also considered upscaled, and will be downscaled (rounding down) before returning it to the
|
|
150
|
+
* Vault.
|
|
151
|
+
*/
|
|
152
|
+
abstract _onSwapGivenIn(
|
|
153
|
+
tokenAmountsIn: bigint[],
|
|
154
|
+
balances: bigint[],
|
|
155
|
+
indexIn: number,
|
|
156
|
+
indexOut: number,
|
|
157
|
+
_amplificationParameter: bigint,
|
|
158
|
+
): bigint[];
|
|
159
|
+
|
|
160
|
+
abstract _onSwapGivenOut(
|
|
161
|
+
tokenAmountsOut: bigint[],
|
|
162
|
+
balances: bigint[],
|
|
163
|
+
indexIn: number,
|
|
164
|
+
indexOut: number,
|
|
165
|
+
_amplificationParameter: bigint,
|
|
166
|
+
): bigint[];
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export abstract class BaseMinimalSwapInfoPool extends BasePool {
|
|
170
|
+
// Swap Hooks
|
|
171
|
+
|
|
172
|
+
// This is inspired from the function onSwap which is in the original contract
|
|
173
|
+
onBuy(
|
|
174
|
+
tokenAmountsOut: bigint[],
|
|
175
|
+
poolPairData: WeightedPoolPairData,
|
|
176
|
+
): bigint[] {
|
|
177
|
+
// uint256 scalingFactorTokenIn = _scalingFactor(request.tokenIn);
|
|
178
|
+
// uint256 scalingFactorTokenOut = _scalingFactor(request.tokenOut);
|
|
179
|
+
|
|
180
|
+
// All token amounts are upscaled.
|
|
181
|
+
const balanceTokenIn = this._upscale(
|
|
182
|
+
poolPairData.tokenInBalance,
|
|
183
|
+
poolPairData.tokenInScalingFactor,
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
const balanceTokenOut = this._upscale(
|
|
187
|
+
poolPairData.tokenOutBalance,
|
|
188
|
+
poolPairData.tokenOutScalingFactor,
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
const tokenAmountsOutScaled = tokenAmountsOut.map(a =>
|
|
192
|
+
this._upscale(a, poolPairData.tokenOutScalingFactor),
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
let amountsIn: bigint[];
|
|
196
|
+
try {
|
|
197
|
+
amountsIn = this._onSwapGivenOut(
|
|
198
|
+
tokenAmountsOutScaled,
|
|
199
|
+
balanceTokenIn,
|
|
200
|
+
balanceTokenOut,
|
|
201
|
+
poolPairData.tokenInWeight,
|
|
202
|
+
poolPairData.tokenOutWeight,
|
|
203
|
+
);
|
|
204
|
+
} catch (e) {
|
|
205
|
+
amountsIn = new Array(tokenAmountsOut.length).fill(0n);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// amountIn tokens are entering the Pool, so we round up.
|
|
209
|
+
const amountsInDownscaled = amountsIn.map(a =>
|
|
210
|
+
this._downscaleUp(a, poolPairData.tokenInScalingFactor),
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
// Fees are added after scaling happens, to reduce the complexity of the rounding direction analysis.
|
|
214
|
+
return amountsInDownscaled.map(a =>
|
|
215
|
+
this._addFeeAmount(a, poolPairData.swapFee),
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Modification: this is inspired from the function onSwap which is in the original contract
|
|
220
|
+
onSell(
|
|
221
|
+
tokenAmountsIn: bigint[],
|
|
222
|
+
poolPairData: WeightedPoolPairData,
|
|
223
|
+
): bigint[] {
|
|
224
|
+
// uint256 _scalingFactorTokenIn = _scalingFactor(request.tokenIn);
|
|
225
|
+
// uint256 _scalingFactorTokenOut = _scalingFactor(request.tokenOut);
|
|
226
|
+
|
|
227
|
+
// Fees are subtracted before scaling, to reduce the complexity of the rounding direction analysis.
|
|
228
|
+
const tokenAmountsInWithFee = tokenAmountsIn.map(a =>
|
|
229
|
+
this._subtractSwapFeeAmount(a, poolPairData.swapFee),
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
// All token amounts are upscaled.
|
|
233
|
+
const balanceTokenIn = this._upscale(
|
|
234
|
+
poolPairData.tokenInBalance,
|
|
235
|
+
poolPairData.tokenInScalingFactor,
|
|
236
|
+
);
|
|
237
|
+
const balanceTokenOut = this._upscale(
|
|
238
|
+
poolPairData.tokenOutBalance,
|
|
239
|
+
poolPairData.tokenOutScalingFactor,
|
|
240
|
+
);
|
|
241
|
+
const tokenAmountsInScaled = tokenAmountsInWithFee.map(a =>
|
|
242
|
+
this._upscale(a, poolPairData.tokenInScalingFactor),
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
const amountsOut = this._onSwapGivenIn(
|
|
246
|
+
tokenAmountsInScaled,
|
|
247
|
+
balanceTokenIn,
|
|
248
|
+
balanceTokenOut,
|
|
249
|
+
poolPairData.tokenInWeight,
|
|
250
|
+
poolPairData.tokenOutWeight,
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
// amountOut tokens are exiting the Pool, so we round down.
|
|
254
|
+
return amountsOut.map(a =>
|
|
255
|
+
this._downscaleDown(a, poolPairData.tokenOutScalingFactor),
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
abstract _onSwapGivenIn(
|
|
260
|
+
tokenAmountsIn: bigint[],
|
|
261
|
+
currentBalanceTokenIn: bigint,
|
|
262
|
+
currentBalanceTokenOut: bigint,
|
|
263
|
+
_weightIn: bigint,
|
|
264
|
+
_weightOut: bigint,
|
|
265
|
+
): bigint[];
|
|
266
|
+
|
|
267
|
+
abstract _onSwapGivenOut(
|
|
268
|
+
tokenAmountsOut: bigint[],
|
|
269
|
+
currentBalanceTokenIn: bigint,
|
|
270
|
+
currentBalanceTokenOut: bigint,
|
|
271
|
+
_weightIn: bigint,
|
|
272
|
+
_weightOut: bigint,
|
|
273
|
+
): bigint[];
|
|
274
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MathSol } from '
|
|
1
|
+
import { MathSol } from '../../balancer-v2-math';
|
|
2
2
|
|
|
3
3
|
/*
|
|
4
4
|
Note when comparing vs smart contract:
|
|
@@ -262,16 +262,16 @@ export function _calcTokensOutGivenExactBptIn(
|
|
|
262
262
|
bptIndex: number,
|
|
263
263
|
): bigint[] {
|
|
264
264
|
/**********************************************************************************************
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
265
|
+
// exactBPTInForTokensOut //
|
|
266
|
+
// (per token) //
|
|
267
|
+
// aO = tokenAmountOut / bptIn \ //
|
|
268
|
+
// b = tokenBalance a0 = b * | --------------------- | //
|
|
269
|
+
// bptIn = bptAmountIn \ bptTotalSupply / //
|
|
270
|
+
// bpt = bptTotalSupply //
|
|
271
|
+
**********************************************************************************************/
|
|
272
272
|
|
|
273
|
-
|
|
274
|
-
|
|
273
|
+
// Since we're computing an amount out, we round down overall. This means rounding down on both the
|
|
274
|
+
// multiplication and division.
|
|
275
275
|
|
|
276
276
|
const bptRatio = MathSol.divDownFixed(bptAmountIn, bptTotalSupply);
|
|
277
277
|
const amountsOut: bigint[] = new Array(balances.length);
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Interface } from '@ethersproject/abi';
|
|
2
2
|
import { BigNumber } from '@ethersproject/bignumber';
|
|
3
|
-
import { isSameAddress, decodeThrowError } from '
|
|
3
|
+
import { isSameAddress, decodeThrowError } from '../../utils';
|
|
4
4
|
import * as LinearMath from './LinearMath';
|
|
5
|
-
import { BasePool } from '
|
|
6
|
-
import { callData, SubgraphPoolBase, PoolState, TokenState } from '
|
|
7
|
-
import LinearPoolABI from '
|
|
8
|
-
import { SwapSide } from '
|
|
5
|
+
import { BasePool } from '../balancer-v2-pool';
|
|
6
|
+
import { callData, SubgraphPoolBase, PoolState, TokenState } from '../../types';
|
|
7
|
+
import LinearPoolABI from '../../../../abi/balancer-v2/linearPoolAbi.json';
|
|
8
|
+
import { SwapSide } from '../../../../constants';
|
|
9
9
|
import { keyBy } from 'lodash';
|
|
10
10
|
|
|
11
11
|
export enum PairTypes {
|
|
@@ -82,6 +82,209 @@ export class LinearPool extends BasePool {
|
|
|
82
82
|
);
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
+
onBuy(amounts: bigint[], poolPairData: LinearPoolPairData): bigint[] {
|
|
86
|
+
return this._swapGivenOut(
|
|
87
|
+
amounts,
|
|
88
|
+
poolPairData.tokens,
|
|
89
|
+
poolPairData.balances,
|
|
90
|
+
poolPairData.indexIn,
|
|
91
|
+
poolPairData.indexOut,
|
|
92
|
+
poolPairData.bptIndex,
|
|
93
|
+
poolPairData.wrappedIndex,
|
|
94
|
+
poolPairData.mainIndex,
|
|
95
|
+
poolPairData.scalingFactors,
|
|
96
|
+
poolPairData.swapFee,
|
|
97
|
+
poolPairData.lowerTarget,
|
|
98
|
+
poolPairData.upperTarget,
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
_swapGivenOut(
|
|
103
|
+
tokenAmountsOut: bigint[],
|
|
104
|
+
tokens: string[],
|
|
105
|
+
balances: bigint[],
|
|
106
|
+
indexIn: number,
|
|
107
|
+
indexOut: number,
|
|
108
|
+
bptIndex: number,
|
|
109
|
+
wrappedIndex: number,
|
|
110
|
+
mainIndex: number,
|
|
111
|
+
scalingFactors: bigint[],
|
|
112
|
+
swapFeePercentage: bigint,
|
|
113
|
+
lowerTarget: bigint,
|
|
114
|
+
upperTarget: bigint,
|
|
115
|
+
): bigint[] {
|
|
116
|
+
let pairType: PairTypes;
|
|
117
|
+
if (isSameAddress(tokens[indexIn], tokens[bptIndex])) {
|
|
118
|
+
if (isSameAddress(tokens[indexOut], tokens[wrappedIndex]))
|
|
119
|
+
pairType = PairTypes.BptToWrappedToken;
|
|
120
|
+
else pairType = PairTypes.BptToMainToken;
|
|
121
|
+
} else if (isSameAddress(tokens[indexOut], tokens[bptIndex])) {
|
|
122
|
+
if (isSameAddress(tokens[indexIn], tokens[wrappedIndex]))
|
|
123
|
+
pairType = PairTypes.WrappedTokenToBpt;
|
|
124
|
+
else pairType = PairTypes.MainTokenToBpt;
|
|
125
|
+
} else {
|
|
126
|
+
if (isSameAddress(tokens[indexIn], tokens[wrappedIndex]))
|
|
127
|
+
pairType = PairTypes.WrappedTokenToMainToken;
|
|
128
|
+
else pairType = PairTypes.MainTokenToWrappedToken;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const balancesUpscaled = this._upscaleArray(balances, scalingFactors);
|
|
132
|
+
const tokenAmountsOutScaled = tokenAmountsOut.map(a =>
|
|
133
|
+
this._upscale(a, scalingFactors[indexIn]),
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
// VirtualBPTSupply must be used for the maths
|
|
137
|
+
const virtualBptSupply = this.MAX_TOKEN_BALANCE.sub(
|
|
138
|
+
balances[bptIndex],
|
|
139
|
+
).toBigInt();
|
|
140
|
+
|
|
141
|
+
const amountsIn = this._onSwapGivenOut(
|
|
142
|
+
tokenAmountsOutScaled,
|
|
143
|
+
balancesUpscaled[mainIndex],
|
|
144
|
+
balancesUpscaled[wrappedIndex],
|
|
145
|
+
swapFeePercentage,
|
|
146
|
+
lowerTarget,
|
|
147
|
+
upperTarget,
|
|
148
|
+
virtualBptSupply,
|
|
149
|
+
pairType,
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
return amountsIn.map(a => this._downscaleUp(a, scalingFactors[indexOut]));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
_onSwapGivenOut(
|
|
156
|
+
tokenAmountsIn: bigint[],
|
|
157
|
+
mainBalance: bigint,
|
|
158
|
+
wrappedBalance: bigint,
|
|
159
|
+
fee: bigint,
|
|
160
|
+
lowerTarget: bigint,
|
|
161
|
+
upperTarget: bigint,
|
|
162
|
+
virtualBptSupply: bigint,
|
|
163
|
+
pairType: PairTypes,
|
|
164
|
+
): bigint[] {
|
|
165
|
+
const amountsIn: bigint[] = [];
|
|
166
|
+
|
|
167
|
+
switch (pairType) {
|
|
168
|
+
case PairTypes.BptToMainToken:
|
|
169
|
+
tokenAmountsIn.forEach(amountIn => {
|
|
170
|
+
let amt: bigint;
|
|
171
|
+
try {
|
|
172
|
+
amt = LinearMath._calcBptInPerMainOut(
|
|
173
|
+
amountIn,
|
|
174
|
+
mainBalance,
|
|
175
|
+
wrappedBalance,
|
|
176
|
+
virtualBptSupply,
|
|
177
|
+
{
|
|
178
|
+
fee: fee,
|
|
179
|
+
lowerTarget: lowerTarget,
|
|
180
|
+
upperTarget: upperTarget,
|
|
181
|
+
},
|
|
182
|
+
);
|
|
183
|
+
} catch (err) {
|
|
184
|
+
amt = 0n;
|
|
185
|
+
}
|
|
186
|
+
amountsIn.push(amt);
|
|
187
|
+
});
|
|
188
|
+
break;
|
|
189
|
+
case PairTypes.MainTokenToBpt:
|
|
190
|
+
tokenAmountsIn.forEach(amountIn => {
|
|
191
|
+
let amt: bigint;
|
|
192
|
+
try {
|
|
193
|
+
amt = LinearMath._calcMainInPerBptOut(
|
|
194
|
+
amountIn,
|
|
195
|
+
mainBalance,
|
|
196
|
+
wrappedBalance,
|
|
197
|
+
virtualBptSupply,
|
|
198
|
+
{
|
|
199
|
+
fee: fee,
|
|
200
|
+
lowerTarget: lowerTarget,
|
|
201
|
+
upperTarget: upperTarget,
|
|
202
|
+
},
|
|
203
|
+
);
|
|
204
|
+
} catch (err) {
|
|
205
|
+
amt = 0n;
|
|
206
|
+
}
|
|
207
|
+
amountsIn.push(amt);
|
|
208
|
+
});
|
|
209
|
+
break;
|
|
210
|
+
case PairTypes.MainTokenToWrappedToken:
|
|
211
|
+
tokenAmountsIn.forEach(amountIn => {
|
|
212
|
+
let amt: bigint;
|
|
213
|
+
try {
|
|
214
|
+
amt = LinearMath._calcMainInPerWrappedOut(amountIn, mainBalance, {
|
|
215
|
+
fee: fee,
|
|
216
|
+
lowerTarget: lowerTarget,
|
|
217
|
+
upperTarget: upperTarget,
|
|
218
|
+
});
|
|
219
|
+
} catch (err) {
|
|
220
|
+
amt = 0n;
|
|
221
|
+
}
|
|
222
|
+
amountsIn.push(amt);
|
|
223
|
+
});
|
|
224
|
+
break;
|
|
225
|
+
case PairTypes.WrappedTokenToMainToken:
|
|
226
|
+
tokenAmountsIn.forEach(amountIn => {
|
|
227
|
+
let amt: bigint;
|
|
228
|
+
try {
|
|
229
|
+
amt = LinearMath._calcWrappedInPerMainOut(amountIn, mainBalance, {
|
|
230
|
+
fee: fee,
|
|
231
|
+
lowerTarget: lowerTarget,
|
|
232
|
+
upperTarget: upperTarget,
|
|
233
|
+
});
|
|
234
|
+
} catch (err) {
|
|
235
|
+
amt = 0n;
|
|
236
|
+
}
|
|
237
|
+
amountsIn.push(amt);
|
|
238
|
+
});
|
|
239
|
+
break;
|
|
240
|
+
case PairTypes.BptToWrappedToken:
|
|
241
|
+
tokenAmountsIn.forEach(amountIn => {
|
|
242
|
+
let amt: bigint;
|
|
243
|
+
try {
|
|
244
|
+
amt = LinearMath._calcBptInPerWrappedOut(
|
|
245
|
+
amountIn,
|
|
246
|
+
mainBalance,
|
|
247
|
+
wrappedBalance,
|
|
248
|
+
virtualBptSupply,
|
|
249
|
+
{
|
|
250
|
+
fee: fee,
|
|
251
|
+
lowerTarget: lowerTarget,
|
|
252
|
+
upperTarget: upperTarget,
|
|
253
|
+
},
|
|
254
|
+
);
|
|
255
|
+
} catch (err) {
|
|
256
|
+
amt = 0n;
|
|
257
|
+
}
|
|
258
|
+
amountsIn.push(amt);
|
|
259
|
+
});
|
|
260
|
+
break;
|
|
261
|
+
case PairTypes.WrappedTokenToBpt:
|
|
262
|
+
tokenAmountsIn.forEach(amountIn => {
|
|
263
|
+
let amt: bigint;
|
|
264
|
+
try {
|
|
265
|
+
amt = LinearMath._calcWrappedInPerBptOut(
|
|
266
|
+
amountIn,
|
|
267
|
+
mainBalance,
|
|
268
|
+
wrappedBalance,
|
|
269
|
+
virtualBptSupply,
|
|
270
|
+
{
|
|
271
|
+
fee: fee,
|
|
272
|
+
lowerTarget: lowerTarget,
|
|
273
|
+
upperTarget: upperTarget,
|
|
274
|
+
},
|
|
275
|
+
);
|
|
276
|
+
} catch (err) {
|
|
277
|
+
amt = 0n;
|
|
278
|
+
}
|
|
279
|
+
amountsIn.push(amt);
|
|
280
|
+
});
|
|
281
|
+
break;
|
|
282
|
+
default:
|
|
283
|
+
amountsIn.push(0n);
|
|
284
|
+
}
|
|
285
|
+
return amountsIn;
|
|
286
|
+
}
|
|
287
|
+
|
|
85
288
|
_swapGivenIn(
|
|
86
289
|
tokenAmountsIn: bigint[],
|
|
87
290
|
tokens: string[],
|
|
@@ -427,7 +630,7 @@ export class LinearPool extends BasePool {
|
|
|
427
630
|
(this._upscale(
|
|
428
631
|
poolPairData.balances[poolPairData.indexOut],
|
|
429
632
|
poolPairData.scalingFactors[poolPairData.indexOut],
|
|
430
|
-
|
|
633
|
+
) *
|
|
431
634
|
99n) /
|
|
432
635
|
100n
|
|
433
636
|
);
|