@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
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { Interface } from '@ethersproject/abi';
|
|
2
|
+
import StablePoolABI from '../../../../abi/balancer-v2/stable-pool.json';
|
|
3
|
+
import MetaStablePoolABI from '../../../../abi/balancer-v2/meta-stable-pool.json';
|
|
4
|
+
import { callData, PoolState, SubgraphPoolBase, TokenState } from '../../types';
|
|
5
|
+
import { decodeThrowError, getTokenScalingFactor } from '../../utils';
|
|
6
|
+
import { BigNumber } from '@ethersproject/bignumber';
|
|
7
|
+
import { SwapSide } from '../../../../constants';
|
|
8
|
+
import { BaseGeneralPool } from '../balancer-v2-pool';
|
|
9
|
+
import { StableMath } from './StableMath';
|
|
10
|
+
|
|
11
|
+
export type StablePoolPairData = {
|
|
12
|
+
balances: bigint[];
|
|
13
|
+
indexIn: number;
|
|
14
|
+
indexOut: number;
|
|
15
|
+
scalingFactors: bigint[];
|
|
16
|
+
swapFee: bigint;
|
|
17
|
+
amp: bigint;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export class StablePool extends BaseGeneralPool {
|
|
21
|
+
vaultAddress: string;
|
|
22
|
+
vaultInterface: Interface;
|
|
23
|
+
poolInterface: Interface;
|
|
24
|
+
metaPoolInterface: Interface;
|
|
25
|
+
|
|
26
|
+
constructor(vaultAddress: string, vaultInterface: Interface) {
|
|
27
|
+
super();
|
|
28
|
+
this.vaultAddress = vaultAddress;
|
|
29
|
+
this.vaultInterface = vaultInterface;
|
|
30
|
+
this.poolInterface = new Interface(StablePoolABI);
|
|
31
|
+
this.metaPoolInterface = new Interface(MetaStablePoolABI);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
_onSwapGivenOut(
|
|
35
|
+
tokenAmountsOut: bigint[],
|
|
36
|
+
balances: bigint[],
|
|
37
|
+
indexIn: number,
|
|
38
|
+
indexOut: number,
|
|
39
|
+
_amplificationParameter: bigint,
|
|
40
|
+
): bigint[] {
|
|
41
|
+
return StableMath._calcInGivenOut(
|
|
42
|
+
_amplificationParameter,
|
|
43
|
+
balances,
|
|
44
|
+
indexIn,
|
|
45
|
+
indexOut,
|
|
46
|
+
tokenAmountsOut,
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
_onSwapGivenIn(
|
|
51
|
+
tokenAmountsIn: bigint[],
|
|
52
|
+
balances: bigint[],
|
|
53
|
+
indexIn: number,
|
|
54
|
+
indexOut: number,
|
|
55
|
+
_amplificationParameter: bigint,
|
|
56
|
+
): bigint[] {
|
|
57
|
+
return StableMath._calcOutGivenIn(
|
|
58
|
+
_amplificationParameter,
|
|
59
|
+
balances,
|
|
60
|
+
indexIn,
|
|
61
|
+
indexOut,
|
|
62
|
+
tokenAmountsIn,
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/*
|
|
67
|
+
Helper function to parse pool data into params for onSell/onBuy functions.
|
|
68
|
+
*/
|
|
69
|
+
parsePoolPairData(
|
|
70
|
+
pool: SubgraphPoolBase,
|
|
71
|
+
poolState: PoolState,
|
|
72
|
+
tokenIn: string,
|
|
73
|
+
tokenOut: string,
|
|
74
|
+
): StablePoolPairData {
|
|
75
|
+
let indexIn = 0,
|
|
76
|
+
indexOut = 0;
|
|
77
|
+
|
|
78
|
+
const scalingFactors: bigint[] = [];
|
|
79
|
+
const balances = poolState.orderedTokens.map((tokenAddress, i) => {
|
|
80
|
+
const t = pool.tokensMap[tokenAddress.toLowerCase()] || poolState.tokens[tokenAddress.toLowerCase()];
|
|
81
|
+
|
|
82
|
+
if (t.address.toLowerCase() === tokenIn.toLowerCase()) {
|
|
83
|
+
indexIn = i;
|
|
84
|
+
}
|
|
85
|
+
if (t.address.toLowerCase() === tokenOut.toLowerCase()) {
|
|
86
|
+
indexOut = i;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (pool.poolType === 'MetaStable') {
|
|
90
|
+
scalingFactors.push(
|
|
91
|
+
poolState.tokens[t.address.toLowerCase()].scalingFactor || 0n,
|
|
92
|
+
);
|
|
93
|
+
} else scalingFactors.push(getTokenScalingFactor(t.decimals));
|
|
94
|
+
return poolState.tokens[t.address.toLowerCase()].balance;
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
const poolPairData: StablePoolPairData = {
|
|
98
|
+
balances,
|
|
99
|
+
indexIn,
|
|
100
|
+
indexOut,
|
|
101
|
+
scalingFactors,
|
|
102
|
+
swapFee: poolState.swapFee,
|
|
103
|
+
amp: poolState.amp ? poolState.amp : 0n,
|
|
104
|
+
};
|
|
105
|
+
return poolPairData;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/*
|
|
109
|
+
Helper function to construct onchain multicall data for StablePool.
|
|
110
|
+
*/
|
|
111
|
+
getOnChainCalls(pool: SubgraphPoolBase): callData[] {
|
|
112
|
+
const poolCallData: callData[] = [
|
|
113
|
+
{
|
|
114
|
+
target: this.vaultAddress,
|
|
115
|
+
callData: this.vaultInterface.encodeFunctionData('getPoolTokens', [
|
|
116
|
+
pool.id,
|
|
117
|
+
]),
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
target: pool.address,
|
|
121
|
+
callData: this.poolInterface.encodeFunctionData('getSwapFeePercentage'),
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
target: pool.address,
|
|
125
|
+
callData: this.poolInterface.encodeFunctionData(
|
|
126
|
+
'getAmplificationParameter',
|
|
127
|
+
),
|
|
128
|
+
},
|
|
129
|
+
];
|
|
130
|
+
if (pool.poolType === 'MetaStable') {
|
|
131
|
+
poolCallData.push({
|
|
132
|
+
target: pool.address,
|
|
133
|
+
callData:
|
|
134
|
+
this.metaPoolInterface.encodeFunctionData('getScalingFactors'),
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return poolCallData;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/*
|
|
141
|
+
Helper function to decodes multicall data for a Stable Pool.
|
|
142
|
+
data must contain returnData
|
|
143
|
+
startIndex is where to start in returnData. Allows this decode function to be called along with other pool types.
|
|
144
|
+
*/
|
|
145
|
+
decodeOnChainCalls(
|
|
146
|
+
pool: SubgraphPoolBase,
|
|
147
|
+
data: { success: boolean; returnData: any }[],
|
|
148
|
+
startIndex: number,
|
|
149
|
+
): [{ [address: string]: PoolState }, number] {
|
|
150
|
+
const pools = {} as { [address: string]: PoolState };
|
|
151
|
+
|
|
152
|
+
const poolTokens = decodeThrowError(
|
|
153
|
+
this.vaultInterface,
|
|
154
|
+
'getPoolTokens',
|
|
155
|
+
data[startIndex++],
|
|
156
|
+
pool.address,
|
|
157
|
+
);
|
|
158
|
+
const swapFee = decodeThrowError(
|
|
159
|
+
this.poolInterface,
|
|
160
|
+
'getSwapFeePercentage',
|
|
161
|
+
data[startIndex++],
|
|
162
|
+
pool.address,
|
|
163
|
+
)[0];
|
|
164
|
+
const amp = decodeThrowError(
|
|
165
|
+
this.poolInterface,
|
|
166
|
+
'getAmplificationParameter',
|
|
167
|
+
data[startIndex++],
|
|
168
|
+
pool.address,
|
|
169
|
+
);
|
|
170
|
+
let scalingFactors: BigNumber[] | undefined;
|
|
171
|
+
if (pool.poolType === 'MetaStable') {
|
|
172
|
+
scalingFactors = decodeThrowError(
|
|
173
|
+
this.metaPoolInterface,
|
|
174
|
+
'getScalingFactors',
|
|
175
|
+
data[startIndex++],
|
|
176
|
+
pool.address,
|
|
177
|
+
)[0];
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const poolState: PoolState = {
|
|
181
|
+
swapFee: BigInt(swapFee.toString()),
|
|
182
|
+
tokens: poolTokens.tokens.reduce(
|
|
183
|
+
(ptAcc: { [address: string]: TokenState }, pt: string, j: number) => {
|
|
184
|
+
const tokenState: TokenState = {
|
|
185
|
+
balance: BigInt(poolTokens.balances[j].toString()),
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
if (scalingFactors)
|
|
189
|
+
tokenState.scalingFactor = BigInt(scalingFactors[j].toString());
|
|
190
|
+
|
|
191
|
+
ptAcc[pt.toLowerCase()] = tokenState;
|
|
192
|
+
return ptAcc;
|
|
193
|
+
},
|
|
194
|
+
{},
|
|
195
|
+
),
|
|
196
|
+
orderedTokens: poolTokens.tokens,
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
if (amp) {
|
|
200
|
+
poolState.amp = BigInt(amp.value.toString());
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
pools[pool.address] = poolState;
|
|
204
|
+
|
|
205
|
+
return [pools, startIndex];
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/*
|
|
209
|
+
For stable pools there is no Swap limit. As an approx - use almost the total balance of token out as we can add any amount of tokenIn and expect some back.
|
|
210
|
+
*/
|
|
211
|
+
getSwapMaxAmount(poolPairData: StablePoolPairData, side: SwapSide): bigint {
|
|
212
|
+
return (
|
|
213
|
+
(this._upscale(
|
|
214
|
+
poolPairData.balances[poolPairData.indexOut],
|
|
215
|
+
poolPairData.scalingFactors[poolPairData.indexOut],
|
|
216
|
+
) *
|
|
217
|
+
99n) /
|
|
218
|
+
100n
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { MathSol } from '../../balancer-v2-math';
|
|
2
|
+
|
|
3
|
+
const _require = (b: boolean, message: string) => {
|
|
4
|
+
if (!b) throw new Error(message);
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export class WeightedMath {
|
|
8
|
+
static _MAX_IN_RATIO = 300000000000000000n;
|
|
9
|
+
static _MAX_OUT_RATIO = 300000000000000000n;
|
|
10
|
+
|
|
11
|
+
static _calcInGivenOut(
|
|
12
|
+
balanceIn: bigint,
|
|
13
|
+
weightIn: bigint,
|
|
14
|
+
balanceOut: bigint,
|
|
15
|
+
weightOut: bigint,
|
|
16
|
+
amountsOut: bigint[],
|
|
17
|
+
): bigint[] {
|
|
18
|
+
/* https://docs.balancer.fi/reference/math/weighted-math.html#ingivenout */
|
|
19
|
+
/**********************************************************************************************
|
|
20
|
+
// outGivenOut //
|
|
21
|
+
// aI = amountIn //
|
|
22
|
+
// bI = balanceIn //
|
|
23
|
+
// bO = balanceOut / / bO \ (wO / wI) \ //
|
|
24
|
+
// aO = amountOut aI = bI * | | -------------------------- | ^ - 1 | //
|
|
25
|
+
// wI = weightIn \ \ ( bO - aO ) / / //
|
|
26
|
+
// wO = weightOut //
|
|
27
|
+
*********************************************************************************************/
|
|
28
|
+
const exponent = MathSol.divUpFixed(weightOut, weightIn);
|
|
29
|
+
return amountsOut.map(amountOut => {
|
|
30
|
+
_require(
|
|
31
|
+
amountOut <= MathSol.mulDownFixed(balanceOut, this._MAX_OUT_RATIO),
|
|
32
|
+
'Errors.MAX_OUT_RATIO',
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
const denominator = balanceOut - amountOut;
|
|
36
|
+
const base = MathSol.divUpFixed(balanceOut, denominator);
|
|
37
|
+
const power = MathSol.powUpFixed(base, exponent);
|
|
38
|
+
const ratio = MathSol.sub(power, MathSol.ONE);
|
|
39
|
+
|
|
40
|
+
return MathSol.mulUpFixed(balanceIn, ratio);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Computes how many tokens can be taken out of a pool if `amountIn` are sent, given the
|
|
45
|
+
// current balances and weights.
|
|
46
|
+
static _calcOutGivenIn(
|
|
47
|
+
balanceIn: bigint,
|
|
48
|
+
weightIn: bigint,
|
|
49
|
+
balanceOut: bigint,
|
|
50
|
+
weightOut: bigint,
|
|
51
|
+
amountsIn: bigint[],
|
|
52
|
+
): bigint[] {
|
|
53
|
+
/* https://docs.balancer.fi/reference/math/weighted-math.html#outgivenin */
|
|
54
|
+
/**********************************************************************************************
|
|
55
|
+
// outGivenIn //
|
|
56
|
+
// aO = amountOut //
|
|
57
|
+
// bO = balanceOut //
|
|
58
|
+
// bI = balanceIn / / bI \ (wI / wO) \ //
|
|
59
|
+
// aI = amountIn aO = bO * | 1 - | -------------------------- | ^ | //
|
|
60
|
+
// wI = weightIn \ \ ( bI + aI ) / / //
|
|
61
|
+
// wO = weightOut //
|
|
62
|
+
**********************************************************************************************/
|
|
63
|
+
|
|
64
|
+
// Amount out, so we round down overall.
|
|
65
|
+
|
|
66
|
+
// The multiplication rounds down, and the subtrahend (power) rounds up (so the base rounds up too).
|
|
67
|
+
// Because bI / (bI + aI) <= 1, the exponent rounds down.
|
|
68
|
+
|
|
69
|
+
// Cannot exceed maximum in ratio
|
|
70
|
+
|
|
71
|
+
const exponent = MathSol.divDownFixed(weightIn, weightOut);
|
|
72
|
+
return amountsIn.map(amountIn => {
|
|
73
|
+
_require(
|
|
74
|
+
amountIn <= MathSol.mulDownFixed(balanceIn, this._MAX_IN_RATIO),
|
|
75
|
+
'Errors.MAX_IN_RATIO',
|
|
76
|
+
);
|
|
77
|
+
const denominator = balanceIn + amountIn;
|
|
78
|
+
const base = MathSol.divUpFixed(balanceIn, denominator);
|
|
79
|
+
const power = MathSol.powUpFixed(base, exponent);
|
|
80
|
+
|
|
81
|
+
return MathSol.mulDownFixed(balanceOut, MathSol.complementFixed(power));
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { Interface } from '@ethersproject/abi';
|
|
2
|
+
import WeightedPoolABI from '../../../../abi/balancer-v2/weighted-pool.json';
|
|
3
|
+
import { callData, PoolState, SubgraphPoolBase, TokenState } from '../../types';
|
|
4
|
+
import { decodeThrowError, getTokenScalingFactor } from '../../utils';
|
|
5
|
+
import { SwapSide } from '../../../../constants';
|
|
6
|
+
import { BaseMinimalSwapInfoPool } from '../balancer-v2-pool';
|
|
7
|
+
import { WeightedMath } from './WeightedMath';
|
|
8
|
+
|
|
9
|
+
export type WeightedPoolPairData = {
|
|
10
|
+
tokenInBalance: bigint;
|
|
11
|
+
tokenOutBalance: bigint;
|
|
12
|
+
tokenInScalingFactor: bigint;
|
|
13
|
+
tokenOutScalingFactor: bigint;
|
|
14
|
+
tokenInWeight: bigint;
|
|
15
|
+
tokenOutWeight: bigint;
|
|
16
|
+
swapFee: bigint;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
export class WeightedPool extends BaseMinimalSwapInfoPool {
|
|
21
|
+
vaultAddress: string;
|
|
22
|
+
vaultInterface: Interface;
|
|
23
|
+
poolInterface: Interface;
|
|
24
|
+
|
|
25
|
+
constructor(vaultAddress: string, vaultInterface: Interface) {
|
|
26
|
+
super();
|
|
27
|
+
this.vaultAddress = vaultAddress;
|
|
28
|
+
this.vaultInterface = vaultInterface;
|
|
29
|
+
this.poolInterface = new Interface(WeightedPoolABI);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
_onSwapGivenOut(
|
|
33
|
+
tokenAmountsOut: bigint[],
|
|
34
|
+
currentBalanceTokenIn: bigint,
|
|
35
|
+
currentBalanceTokenOut: bigint,
|
|
36
|
+
_weightIn: bigint,
|
|
37
|
+
_weightOut: bigint,
|
|
38
|
+
): bigint[] {
|
|
39
|
+
return WeightedMath._calcInGivenOut(
|
|
40
|
+
currentBalanceTokenIn,
|
|
41
|
+
_weightIn,
|
|
42
|
+
currentBalanceTokenOut,
|
|
43
|
+
_weightOut,
|
|
44
|
+
tokenAmountsOut,
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
_onSwapGivenIn(
|
|
49
|
+
tokenAmountsIn: bigint[],
|
|
50
|
+
currentBalanceTokenIn: bigint,
|
|
51
|
+
currentBalanceTokenOut: bigint,
|
|
52
|
+
_weightIn: bigint,
|
|
53
|
+
_weightOut: bigint,
|
|
54
|
+
): bigint[] {
|
|
55
|
+
return WeightedMath._calcOutGivenIn(
|
|
56
|
+
currentBalanceTokenIn,
|
|
57
|
+
_weightIn,
|
|
58
|
+
currentBalanceTokenOut,
|
|
59
|
+
_weightOut,
|
|
60
|
+
tokenAmountsIn,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/*
|
|
65
|
+
Helper function to parse pool data into params for onSell function.
|
|
66
|
+
*/
|
|
67
|
+
parsePoolPairData(
|
|
68
|
+
pool: SubgraphPoolBase,
|
|
69
|
+
poolState: PoolState,
|
|
70
|
+
tokenIn: string,
|
|
71
|
+
tokenOut: string,
|
|
72
|
+
): WeightedPoolPairData {
|
|
73
|
+
const inAddress = tokenIn.toLowerCase();
|
|
74
|
+
const outAddress = tokenOut.toLowerCase();
|
|
75
|
+
|
|
76
|
+
const tIn = pool.tokens.find(t => t.address.toLowerCase() === inAddress);
|
|
77
|
+
const tOut = pool.tokens.find(t => t.address.toLowerCase() === outAddress);
|
|
78
|
+
|
|
79
|
+
if (!tIn || !tOut) return {} as WeightedPoolPairData;
|
|
80
|
+
|
|
81
|
+
const tokenInBalance = poolState.tokens[inAddress].balance;
|
|
82
|
+
const tokenOutBalance = poolState.tokens[outAddress].balance;
|
|
83
|
+
const tokenInWeight = poolState.tokens[inAddress].weight || 0n;
|
|
84
|
+
const tokenOutWeight = poolState.tokens[outAddress].weight || 0n;
|
|
85
|
+
const tokenInScalingFactor = getTokenScalingFactor(tIn.decimals);
|
|
86
|
+
const tokenOutScalingFactor = getTokenScalingFactor(tOut.decimals);
|
|
87
|
+
|
|
88
|
+
const poolPairData: WeightedPoolPairData = {
|
|
89
|
+
tokenInBalance,
|
|
90
|
+
tokenOutBalance,
|
|
91
|
+
tokenInScalingFactor,
|
|
92
|
+
tokenOutScalingFactor,
|
|
93
|
+
tokenInWeight,
|
|
94
|
+
tokenOutWeight,
|
|
95
|
+
swapFee: poolState.swapFee,
|
|
96
|
+
};
|
|
97
|
+
return poolPairData;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/*
|
|
101
|
+
Helper function to construct onchain multicall data for WeightedPool (also 'LiquidityBootstrapping' and 'Investment' pool types).
|
|
102
|
+
*/
|
|
103
|
+
getOnChainCalls(pool: SubgraphPoolBase): callData[] {
|
|
104
|
+
return [
|
|
105
|
+
{
|
|
106
|
+
target: this.vaultAddress,
|
|
107
|
+
callData: this.vaultInterface.encodeFunctionData('getPoolTokens', [
|
|
108
|
+
pool.id,
|
|
109
|
+
]),
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
target: pool.address,
|
|
113
|
+
callData: this.poolInterface.encodeFunctionData('getSwapFeePercentage'),
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
target: pool.address,
|
|
117
|
+
callData: this.poolInterface.encodeFunctionData('getNormalizedWeights'),
|
|
118
|
+
},
|
|
119
|
+
];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/*
|
|
123
|
+
Helper function to decodes multicall data for a Weighted Pool.
|
|
124
|
+
data must contain returnData
|
|
125
|
+
startIndex is where to start in returnData. Allows this decode function to be called along with other pool types.
|
|
126
|
+
*/
|
|
127
|
+
decodeOnChainCalls(
|
|
128
|
+
pool: SubgraphPoolBase,
|
|
129
|
+
data: { success: boolean; returnData: any }[],
|
|
130
|
+
startIndex: number,
|
|
131
|
+
): [{ [address: string]: PoolState }, number] {
|
|
132
|
+
const pools = {} as { [address: string]: PoolState };
|
|
133
|
+
|
|
134
|
+
const poolTokens = decodeThrowError(
|
|
135
|
+
this.vaultInterface,
|
|
136
|
+
'getPoolTokens',
|
|
137
|
+
data[startIndex++],
|
|
138
|
+
pool.address,
|
|
139
|
+
);
|
|
140
|
+
const swapFee = decodeThrowError(
|
|
141
|
+
this.poolInterface,
|
|
142
|
+
'getSwapFeePercentage',
|
|
143
|
+
data[startIndex++],
|
|
144
|
+
pool.address,
|
|
145
|
+
)[0];
|
|
146
|
+
const normalisedWeights = decodeThrowError(
|
|
147
|
+
this.poolInterface,
|
|
148
|
+
'getNormalizedWeights',
|
|
149
|
+
data[startIndex++],
|
|
150
|
+
pool.address,
|
|
151
|
+
)[0];
|
|
152
|
+
|
|
153
|
+
const poolState: PoolState = {
|
|
154
|
+
swapFee: BigInt(swapFee.toString()),
|
|
155
|
+
tokens: poolTokens.tokens.reduce(
|
|
156
|
+
(ptAcc: { [address: string]: TokenState }, pt: string, j: number) => {
|
|
157
|
+
const tokenState: TokenState = {
|
|
158
|
+
balance: BigInt(poolTokens.balances[j].toString()),
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
if (normalisedWeights)
|
|
162
|
+
tokenState.weight = BigInt(normalisedWeights[j].toString());
|
|
163
|
+
|
|
164
|
+
ptAcc[pt.toLowerCase()] = tokenState;
|
|
165
|
+
return ptAcc;
|
|
166
|
+
},
|
|
167
|
+
{},
|
|
168
|
+
),
|
|
169
|
+
orderedTokens: poolTokens.tokens,
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
pools[pool.address] = poolState;
|
|
173
|
+
|
|
174
|
+
return [pools, startIndex];
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/*
|
|
178
|
+
For weighted pools there is a Swap limit of 30%: amounts swapped may not be larger than this percentage of total balance.
|
|
179
|
+
*/
|
|
180
|
+
getSwapMaxAmount(poolPairData: WeightedPoolPairData, side: SwapSide): bigint {
|
|
181
|
+
return (
|
|
182
|
+
((side === SwapSide.SELL
|
|
183
|
+
? poolPairData.tokenInBalance
|
|
184
|
+
: poolPairData.tokenOutBalance) *
|
|
185
|
+
3n) /
|
|
186
|
+
10n
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
}
|
package/tests/constants-e2e.ts
CHANGED
|
@@ -67,6 +67,10 @@ export const Tokens: {
|
|
|
67
67
|
addBalance: balanceOfFn,
|
|
68
68
|
addAllowance: allowanceFn,
|
|
69
69
|
},
|
|
70
|
+
PSP: {
|
|
71
|
+
address: '0xcafe001067cdef266afb7eb5a286dcfd277f3de5',
|
|
72
|
+
decimals: 18,
|
|
73
|
+
},
|
|
70
74
|
SETH: {
|
|
71
75
|
address: '0x5e74C9036fb86BD7eCdcb084a0673EFc32eA31cb',
|
|
72
76
|
decimals: 18,
|
|
@@ -812,13 +816,13 @@ export const Holders: {
|
|
|
812
816
|
} = {
|
|
813
817
|
[Network.MAINNET]: {
|
|
814
818
|
ETH: '0x176F3DAb24a159341c0509bB36B833E7fdd0a132',
|
|
815
|
-
USDC: '
|
|
816
|
-
WBTC: '
|
|
819
|
+
USDC: '0x79E2Ba942B0e8fDB6ff3d406e930289d10B49ADe',
|
|
820
|
+
WBTC: '0x1Cb17a66DC606a52785f69F08F4256526aBd4943',
|
|
817
821
|
BADGER: '0x34e2741a3f8483dbe5231f61c005110ff4b9f50a',
|
|
818
822
|
STETH: '0x9bdb521a97e95177bf252c253e256a60c3e14447',
|
|
819
823
|
wstETH: '0x6cE0F913F035ec6195bC3cE885aec4C66E485BC4',
|
|
820
|
-
WETH: '
|
|
821
|
-
USDT: '
|
|
824
|
+
WETH: '0x2fEb1512183545f48f6b9C5b4EbfCaF49CfCa6F3',
|
|
825
|
+
USDT: '0x8A446971dbB112f3be15bc38C14D44B94D9E94b9',
|
|
822
826
|
XAUT: '0xc4e161e8d8a4bc4ac762ab33a28bbac5474203d7',
|
|
823
827
|
CVX: '0x0aCA67Fa70B142A3b9bF2eD89A81B40ff85dACdC',
|
|
824
828
|
MIM: '0xa046a8660e66d178ee07ec97c585eeb6aa18c26c',
|
|
@@ -864,7 +868,8 @@ export const Holders: {
|
|
|
864
868
|
SHIBA: '0x73af3bcf944a6559933396c1577b257e2054d935',
|
|
865
869
|
aEthUSDC: '0x3178490d60B5cceaA5a79FD4D9050C7405Bab80c',
|
|
866
870
|
aEthWETH: '0x645C4c0c95C1Aa6EF25d12f4a25038cA9b0C6Cc7',
|
|
867
|
-
dUSDC: '
|
|
871
|
+
dUSDC: '0x2FC2F705110A7F46Ce85F701d7217EF1018f01A3',
|
|
872
|
+
PSP: '0xE5E5440a1CE69C5cf67BFFA74d185e57c31b43E5',
|
|
868
873
|
},
|
|
869
874
|
[Network.ROPSTEN]: {
|
|
870
875
|
ETH: '0x43262A12d8610AA70C15DbaeAC321d51613c9071',
|
package/.vscode/launch.json
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
// Use IntelliSense to learn about possible attributes.
|
|
3
|
-
// Hover to view descriptions of existing attributes.
|
|
4
|
-
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
|
5
|
-
"version": "0.2.0",
|
|
6
|
-
"configurations": [
|
|
7
|
-
{
|
|
8
|
-
"type": "node",
|
|
9
|
-
"request": "launch",
|
|
10
|
-
"name": "Launch Program",
|
|
11
|
-
"skipFiles": [
|
|
12
|
-
"<node_internals>/**"
|
|
13
|
-
],
|
|
14
|
-
"program": "${workspaceFolder}/build/index.js",
|
|
15
|
-
"preLaunchTask": "tsc: build - tsconfig.json",
|
|
16
|
-
"outFiles": [
|
|
17
|
-
"${workspaceFolder}/build/**/*.js"
|
|
18
|
-
]
|
|
19
|
-
}
|
|
20
|
-
]
|
|
21
|
-
}
|