@paraswap/dex-lib 2.16.3 → 2.16.4-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/codeStyles/codeStyleConfig.xml +5 -0
- package/.idea/inspectionProfiles/Project_Default.xml +6 -0
- package/.idea/misc.xml +6 -0
- package/.idea/modules.xml +8 -0
- package/.idea/paraswap-dex-lib.iml +9 -0
- package/.idea/vcs.xml +6 -0
- package/build/config.js +5 -5
- 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 +20 -0
- package/build/dex/balancer-v2/pools/linear/LinearMath.js +186 -0
- package/build/dex/balancer-v2/pools/linear/LinearMath.js.map +1 -0
- package/build/dex/balancer-v2/pools/linear/LinearPool.d.ts +51 -0
- package/build/dex/balancer-v2/pools/linear/LinearPool.js +477 -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 +5 -5
- package/src/dex/balancer-v2/balancer-v2-e2e.test.ts +204 -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} +81 -23
- package/src/dex/balancer-v2/{LinearPool.ts → pools/linear/LinearPool.ts} +241 -5
- 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 +9 -4
- package/build/abi/DirectSwap.json +0 -970
- package/build/dex/test-dex/config.d.ts +0 -4
- package/build/dex/test-dex/config.js +0 -15
- package/build/dex/test-dex/config.js.map +0 -1
- package/build/dex/test-dex/test-dex-pool.d.ts +0 -39
- package/build/dex/test-dex/test-dex-pool.js +0 -63
- package/build/dex/test-dex/test-dex-pool.js.map +0 -1
- package/build/dex/test-dex/test-dex.d.ts +0 -37
- package/build/dex/test-dex/test-dex.js +0 -126
- package/build/dex/test-dex/test-dex.js.map +0 -1
- package/build/dex/test-dex/types.d.ts +0 -6
- package/build/dex/test-dex/types.js +0 -3
- package/build/dex/test-dex/types.js.map +0 -1
- package/build/dex/woo-fi-v2/woo-fi-math.d.ts +0 -16
- package/build/dex/woo-fi-v2/woo-fi-math.js +0 -137
- package/build/dex/woo-fi-v2/woo-fi-math.js.map +0 -1
- package/src/dex/balancer-v2/balancer-v2-pool.ts +0 -744
|
@@ -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,12 +816,12 @@ export const Holders: {
|
|
|
812
816
|
} = {
|
|
813
817
|
[Network.MAINNET]: {
|
|
814
818
|
ETH: '0x176F3DAb24a159341c0509bB36B833E7fdd0a132',
|
|
815
|
-
USDC: '
|
|
816
|
-
WBTC: '
|
|
819
|
+
USDC: '0x915dA56c5995EAaf66fd06BA3AeD62a4D3BD011A',
|
|
820
|
+
WBTC: '0x1Cb17a66DC606a52785f69F08F4256526aBd4943',
|
|
817
821
|
BADGER: '0x34e2741a3f8483dbe5231f61c005110ff4b9f50a',
|
|
818
822
|
STETH: '0x9bdb521a97e95177bf252c253e256a60c3e14447',
|
|
819
823
|
wstETH: '0x6cE0F913F035ec6195bC3cE885aec4C66E485BC4',
|
|
820
|
-
WETH: '
|
|
824
|
+
WETH: '0x2fEb1512183545f48f6b9C5b4EbfCaF49CfCa6F3',
|
|
821
825
|
USDT: '0x5754284f345afc66a98fbb0a0afe71e0f007b949',
|
|
822
826
|
XAUT: '0xc4e161e8d8a4bc4ac762ab33a28bbac5474203d7',
|
|
823
827
|
CVX: '0x0aCA67Fa70B142A3b9bF2eD89A81B40ff85dACdC',
|
|
@@ -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',
|