@paraswap/dex-lib 2.47.4 → 2.47.5

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.
Files changed (142) hide show
  1. package/build/abi/Redstone.json +739 -0
  2. package/build/abi/angle/stagToken.json +963 -0
  3. package/build/abi/angle-transmuter/Pyth.json +1534 -0
  4. package/build/abi/angle-transmuter/Transmuter.json +1719 -0
  5. package/build/abi/smardex/smardex-factory.json +185 -0
  6. package/build/abi/smardex/smardex-pool.json +578 -0
  7. package/build/abi/smardex/smardex-router.json +648 -0
  8. package/build/abi/trader-joe-v2_1/FactoryABI.json +1208 -0
  9. package/build/abi/trader-joe-v2_1/PairABI.json +1493 -0
  10. package/build/abi/trader-joe-v2_1/RouterABI.json +1489 -0
  11. package/build/abi/trader-joe-v2_1/StateMulticall.json +193 -0
  12. package/build/abi/wombat/pool.json +1289 -0
  13. package/build/dex/angle-staked-stable/angle-staked-stable-pool.d.ts +53 -0
  14. package/build/dex/angle-staked-stable/angle-staked-stable-pool.js +175 -0
  15. package/build/dex/angle-staked-stable/angle-staked-stable-pool.js.map +1 -0
  16. package/build/dex/angle-staked-stable/angle-staked-stable.d.ts +42 -0
  17. package/build/dex/angle-staked-stable/angle-staked-stable.js +232 -0
  18. package/build/dex/angle-staked-stable/angle-staked-stable.js.map +1 -0
  19. package/build/dex/angle-staked-stable/config.d.ts +4 -0
  20. package/build/dex/angle-staked-stable/config.js +26 -0
  21. package/build/dex/angle-staked-stable/config.js.map +1 -0
  22. package/build/dex/angle-staked-stable/types.d.ts +15 -0
  23. package/build/dex/angle-staked-stable/types.js +3 -0
  24. package/build/dex/angle-staked-stable/types.js.map +1 -0
  25. package/build/dex/angle-transmuter/angle-transmuter-pool.d.ts +43 -0
  26. package/build/dex/angle-transmuter/angle-transmuter-pool.js +309 -0
  27. package/build/dex/angle-transmuter/angle-transmuter-pool.js.map +1 -0
  28. package/build/dex/angle-transmuter/angle-transmuter.d.ts +46 -0
  29. package/build/dex/angle-transmuter/angle-transmuter.js +233 -0
  30. package/build/dex/angle-transmuter/angle-transmuter.js.map +1 -0
  31. package/build/dex/angle-transmuter/backedOracle.d.ts +19 -0
  32. package/build/dex/angle-transmuter/backedOracle.js +56 -0
  33. package/build/dex/angle-transmuter/backedOracle.js.map +1 -0
  34. package/build/dex/angle-transmuter/config.d.ts +4 -0
  35. package/build/dex/angle-transmuter/config.js +20 -0
  36. package/build/dex/angle-transmuter/config.js.map +1 -0
  37. package/build/dex/angle-transmuter/constants.d.ts +4 -0
  38. package/build/dex/angle-transmuter/constants.js +8 -0
  39. package/build/dex/angle-transmuter/constants.js.map +1 -0
  40. package/build/dex/angle-transmuter/pyth.d.ts +19 -0
  41. package/build/dex/angle-transmuter/pyth.js +71 -0
  42. package/build/dex/angle-transmuter/pyth.js.map +1 -0
  43. package/build/dex/angle-transmuter/redstone.d.ts +20 -0
  44. package/build/dex/angle-transmuter/redstone.js +73 -0
  45. package/build/dex/angle-transmuter/redstone.js.map +1 -0
  46. package/build/dex/angle-transmuter/transmuter.d.ts +50 -0
  47. package/build/dex/angle-transmuter/transmuter.js +314 -0
  48. package/build/dex/angle-transmuter/transmuter.js.map +1 -0
  49. package/build/dex/angle-transmuter/types.d.ts +144 -0
  50. package/build/dex/angle-transmuter/types.js +31 -0
  51. package/build/dex/angle-transmuter/types.js.map +1 -0
  52. package/build/dex/angle-transmuter/utils.d.ts +9 -0
  53. package/build/dex/angle-transmuter/utils.js +264 -0
  54. package/build/dex/angle-transmuter/utils.js.map +1 -0
  55. package/build/dex/balancer-v2/balancer-v2.d.ts +3 -6
  56. package/build/dex/balancer-v2/balancer-v2.js +35 -55
  57. package/build/dex/balancer-v2/balancer-v2.js.map +1 -1
  58. package/build/dex/balancer-v2/types.d.ts +1 -24
  59. package/build/dex/balancer-v2/types.js +0 -2
  60. package/build/dex/balancer-v2/types.js.map +1 -1
  61. package/build/dex/balancer-v2/utils.d.ts +0 -8
  62. package/build/dex/balancer-v2/utils.js +1 -17
  63. package/build/dex/balancer-v2/utils.js.map +1 -1
  64. package/build/dex/hashflow/hashflow.js +2 -1
  65. package/build/dex/hashflow/hashflow.js.map +1 -1
  66. package/build/dex/smardex/subgraph-queries.d.ts +1 -0
  67. package/build/dex/smardex/subgraph-queries.js +17 -0
  68. package/build/dex/smardex/subgraph-queries.js.map +1 -0
  69. package/build/dex/smardex/usd-reserves.d.ts +23 -0
  70. package/build/dex/smardex/usd-reserves.js +86 -0
  71. package/build/dex/smardex/usd-reserves.js.map +1 -0
  72. package/build/dex/trader-joe-v2-1-2/config.d.ts +4 -0
  73. package/build/dex/trader-joe-v2-1-2/config.js +47 -0
  74. package/build/dex/trader-joe-v2-1-2/config.js.map +1 -0
  75. package/build/dex/trader-joe-v2-1-2/constants.d.ts +20 -0
  76. package/build/dex/trader-joe-v2-1-2/constants.js +22 -0
  77. package/build/dex/trader-joe-v2-1-2/constants.js.map +1 -0
  78. package/build/dex/trader-joe-v2-1-2/math/index.d.ts +37 -0
  79. package/build/dex/trader-joe-v2-1-2/math/index.js +291 -0
  80. package/build/dex/trader-joe-v2-1-2/math/index.js.map +1 -0
  81. package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2-pool.d.ts +60 -0
  82. package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2-pool.js +248 -0
  83. package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2-pool.js.map +1 -0
  84. package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2.d.ts +53 -0
  85. package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2.js +307 -0
  86. package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2.js.map +1 -0
  87. package/build/dex/trader-joe-v2-1-2/types.d.ts +82 -0
  88. package/build/dex/trader-joe-v2-1-2/types.js +9 -0
  89. package/build/dex/trader-joe-v2-1-2/types.js.map +1 -0
  90. package/build/dex/trader-joe-v2_1/config.d.ts +4 -0
  91. package/build/dex/trader-joe-v2_1/config.js +47 -0
  92. package/build/dex/trader-joe-v2_1/config.js.map +1 -0
  93. package/build/dex/trader-joe-v2_1/constants.d.ts +20 -0
  94. package/build/dex/trader-joe-v2_1/constants.js +22 -0
  95. package/build/dex/trader-joe-v2_1/constants.js.map +1 -0
  96. package/build/dex/trader-joe-v2_1/math/index.d.ts +37 -0
  97. package/build/dex/trader-joe-v2_1/math/index.js +291 -0
  98. package/build/dex/trader-joe-v2_1/math/index.js.map +1 -0
  99. package/build/dex/trader-joe-v2_1/trader-joe-v2.1.d.ts +0 -0
  100. package/build/dex/trader-joe-v2_1/trader-joe-v2.1.js +384 -0
  101. package/build/dex/trader-joe-v2_1/trader-joe-v2.1.js.map +1 -0
  102. package/build/dex/trader-joe-v2_1/trader-joe-v2_1-pool.d.ts +60 -0
  103. package/build/dex/trader-joe-v2_1/trader-joe-v2_1-pool.js +247 -0
  104. package/build/dex/trader-joe-v2_1/trader-joe-v2_1-pool.js.map +1 -0
  105. package/build/dex/trader-joe-v2_1/trader-joe-v2_1.d.ts +53 -0
  106. package/build/dex/trader-joe-v2_1/trader-joe-v2_1.js +307 -0
  107. package/build/dex/trader-joe-v2_1/trader-joe-v2_1.js.map +1 -0
  108. package/build/dex/trader-joe-v2_1/types.d.ts +82 -0
  109. package/build/dex/trader-joe-v2_1/types.js +9 -0
  110. package/build/dex/trader-joe-v2_1/types.js.map +1 -0
  111. package/build/dex/wombat/wombat-pool-poller.d.ts +22 -0
  112. package/build/dex/wombat/wombat-pool-poller.js +164 -0
  113. package/build/dex/wombat/wombat-pool-poller.js.map +1 -0
  114. package/package.json +1 -2
  115. package/src/dex/balancer-v2/balancer-v2-e2e.test.ts +0 -94
  116. package/src/dex/balancer-v2/balancer-v2-integration.test.ts +112 -253
  117. package/src/dex/balancer-v2/balancer-v2.ts +69 -105
  118. package/src/dex/balancer-v2/types.ts +0 -26
  119. package/src/dex/balancer-v2/utils.ts +0 -16
  120. package/src/dex/hashflow/hashflow.ts +4 -1
  121. package/src/dex/synthetix/synthetix-state.ts +2 -2
  122. package/tests/constants-e2e.ts +0 -26
  123. package/build/abi/balancer-v2/gyro-e.abi.json +0 -1595
  124. package/build/dex/balancer-v2/pools/gyro/Gyro3Pool.d.ts +0 -32
  125. package/build/dex/balancer-v2/pools/gyro/Gyro3Pool.js +0 -149
  126. package/build/dex/balancer-v2/pools/gyro/Gyro3Pool.js.map +0 -1
  127. package/build/dex/balancer-v2/pools/gyro/GyroEPool.d.ts +0 -37
  128. package/build/dex/balancer-v2/pools/gyro/GyroEPool.js +0 -222
  129. package/build/dex/balancer-v2/pools/gyro/GyroEPool.js.map +0 -1
  130. package/build/dex/balancer-v2/pools/gyro/gyro-helpers.d.ts +0 -2
  131. package/build/dex/balancer-v2/pools/gyro/gyro-helpers.js +0 -19
  132. package/build/dex/balancer-v2/pools/gyro/gyro-helpers.js.map +0 -1
  133. package/build/dex/balancer-v2/queryOnChain.d.ts +0 -1
  134. package/build/dex/balancer-v2/queryOnChain.js +0 -40
  135. package/build/dex/balancer-v2/queryOnChain.js.map +0 -1
  136. package/src/abi/balancer-v2/gyro-e.abi.json +0 -1595
  137. package/src/dex/balancer-v2/balancer-v2-gyro3.test.ts +0 -268
  138. package/src/dex/balancer-v2/balancer-v2-gyroE.test.ts +0 -401
  139. package/src/dex/balancer-v2/pools/gyro/Gyro3Pool.ts +0 -255
  140. package/src/dex/balancer-v2/pools/gyro/GyroEPool.ts +0 -403
  141. package/src/dex/balancer-v2/pools/gyro/gyro-helpers.ts +0 -15
  142. package/src/dex/balancer-v2/queryOnChain.ts +0 -48
@@ -1,255 +0,0 @@
1
- import { Interface } from '@ethersproject/abi';
2
- import { BigNumber } from '@ethersproject/bignumber';
3
-
4
- import { BasePool } from '../balancer-v2-pool';
5
- import { SwapSide } from '../../../../constants';
6
- import {
7
- decodeThrowError,
8
- getTokenScalingFactor,
9
- safeParseFixed,
10
- } from '../../utils';
11
- import { callData, SubgraphPoolBase, PoolState, TokenState } from '../../types';
12
- import { MathSol } from '../../balancer-v2-math';
13
- import { Gyro3Maths } from '@balancer-labs/sor';
14
- import { _addFee, _reduceFee } from './gyro-helpers';
15
-
16
- // Swap Limit factor
17
- const SWAP_LIMIT_FACTOR = BigInt('999999000000000000');
18
-
19
- // All values should be normalised to 18 decimals. e.g. 1USDC = 1e18 not 1e6
20
- export type Gyro3PoolPairData = {
21
- balances: BigNumber[];
22
- indexIn: number;
23
- indexOut: number;
24
- swapFee: bigint;
25
- balanceTertiary: BigNumber; // Balance of the unchanged asset
26
- decimalsTertiary: number; // Decimals of the unchanged asset
27
- scalingFactors: bigint[];
28
- root3Alpha: BigNumber;
29
- };
30
-
31
- export class Gyro3Pool extends BasePool {
32
- vaultAddress: string;
33
- vaultInterface: Interface;
34
- poolInterface = new Interface([
35
- 'function getSwapFeePercentage() view returns (uint256)',
36
- ]);
37
-
38
- constructor(vaultAddress: string, vaultInterface: Interface) {
39
- super();
40
- this.vaultAddress = vaultAddress;
41
- this.vaultInterface = vaultInterface;
42
- }
43
-
44
- parsePoolPairData(
45
- pool: SubgraphPoolBase,
46
- poolState: PoolState,
47
- tokenIn: string,
48
- tokenOut: string,
49
- ): Gyro3PoolPairData {
50
- // All values should be normalised to 18 decimals. e.g. 1USDC = 1e18 not 1e6
51
- let indexIn = 0,
52
- indexOut = 0,
53
- indexTertiary = 0;
54
- const scalingFactors: bigint[] = [];
55
- const balances = pool.tokens.map((t, i) => {
56
- if (t.address.toLowerCase() === tokenIn.toLowerCase()) {
57
- indexIn = i;
58
- } else if (t.address.toLowerCase() === tokenOut.toLowerCase()) {
59
- indexOut = i;
60
- } else {
61
- indexTertiary = i;
62
- }
63
-
64
- const scalingFactor = getTokenScalingFactor(t.decimals);
65
- scalingFactors.push(scalingFactor);
66
-
67
- return BigNumber.from(
68
- this._upscale(
69
- BigInt(poolState.tokens[t.address.toLowerCase()].balance),
70
- scalingFactor,
71
- ),
72
- );
73
- });
74
-
75
- const balanceTertiary = balances[indexTertiary];
76
- const decimalsTertiary = pool.tokens[indexTertiary].decimals;
77
-
78
- const poolPairData: Gyro3PoolPairData = {
79
- balances,
80
- indexIn,
81
- indexOut,
82
- scalingFactors,
83
- swapFee: poolState.swapFee,
84
- balanceTertiary,
85
- decimalsTertiary,
86
- root3Alpha: safeParseFixed(pool.root3Alpha, 18),
87
- };
88
-
89
- return poolPairData;
90
- }
91
-
92
- getSwapMaxAmount(poolPairData: Gyro3PoolPairData, side: SwapSide): bigint {
93
- if (side === SwapSide.SELL) {
94
- // Approximation - similar to traditional stable pools
95
- return this._downscaleDown(
96
- MathSol.mulDownFixed(
97
- poolPairData.balances[poolPairData.indexOut].toBigInt(),
98
- SWAP_LIMIT_FACTOR,
99
- ),
100
- poolPairData.scalingFactors[poolPairData.indexIn],
101
- );
102
- } else {
103
- return this._downscaleDown(
104
- MathSol.mulDownFixed(
105
- poolPairData.balances[poolPairData.indexIn].toBigInt(),
106
- SWAP_LIMIT_FACTOR,
107
- ),
108
- poolPairData.scalingFactors[poolPairData.indexOut],
109
- );
110
- }
111
- }
112
-
113
- /*
114
- Helper function to construct onchain multicall data for Pool.
115
- */
116
- getOnChainCalls(pool: SubgraphPoolBase): callData[] {
117
- const poolCallData: callData[] = [
118
- {
119
- target: this.vaultAddress,
120
- callData: this.vaultInterface.encodeFunctionData('getPoolTokens', [
121
- pool.id,
122
- ]),
123
- },
124
- {
125
- target: pool.address,
126
- callData: this.poolInterface.encodeFunctionData('getSwapFeePercentage'),
127
- },
128
- ];
129
- return poolCallData;
130
- }
131
-
132
- /*
133
- Helper function to decode multicall data for Pool.
134
- data must contain returnData
135
- startIndex is where to start in returnData. Allows this decode function to be called along with other pool types.
136
- */
137
- decodeOnChainCalls(
138
- pool: SubgraphPoolBase,
139
- data: { success: boolean; returnData: any }[],
140
- startIndex: number,
141
- ): [{ [address: string]: PoolState }, number] {
142
- const pools = {} as { [address: string]: PoolState };
143
-
144
- const poolTokens = decodeThrowError(
145
- this.vaultInterface,
146
- 'getPoolTokens',
147
- data[startIndex++],
148
- pool.address,
149
- );
150
- const swapFee = decodeThrowError(
151
- this.poolInterface,
152
- 'getSwapFeePercentage',
153
- data[startIndex++],
154
- pool.address,
155
- )[0];
156
-
157
- const poolState: PoolState = {
158
- swapFee: BigInt(swapFee.toString()),
159
- orderedTokens: poolTokens.tokens,
160
- tokens: poolTokens.tokens.reduce(
161
- (ptAcc: { [address: string]: TokenState }, pt: string, j: number) => {
162
- const tokenState: TokenState = {
163
- balance: BigInt(poolTokens.balances[j].toString()),
164
- };
165
- ptAcc[pt.toLowerCase()] = tokenState;
166
- return ptAcc;
167
- },
168
- {},
169
- ),
170
- };
171
- pools[pool.address] = poolState;
172
- return [pools, startIndex];
173
- }
174
-
175
- onSell(amounts: bigint[], poolPairData: Gyro3PoolPairData): bigint[] {
176
- try {
177
- const invariant = Gyro3Maths._calculateInvariant(
178
- poolPairData.balances,
179
- poolPairData.root3Alpha,
180
- );
181
-
182
- const virtualOffsetInOut = MathSol.mulDownFixed(
183
- invariant.toBigInt(),
184
- poolPairData.root3Alpha.toBigInt(),
185
- );
186
-
187
- const tokenAmountsInWithFee = amounts.map(a => {
188
- const withFee = _reduceFee(a, poolPairData.swapFee);
189
- return this._upscale(
190
- withFee,
191
- poolPairData.scalingFactors[poolPairData.indexIn],
192
- );
193
- });
194
-
195
- return tokenAmountsInWithFee.map(amount => {
196
- try {
197
- const amountOut = Gyro3Maths._calcOutGivenIn(
198
- poolPairData.balances[poolPairData.indexIn],
199
- poolPairData.balances[poolPairData.indexOut],
200
- BigNumber.from(amount),
201
- BigNumber.from(virtualOffsetInOut),
202
- ).toBigInt();
203
-
204
- return this._downscaleDown(
205
- amountOut,
206
- poolPairData.scalingFactors[poolPairData.indexOut],
207
- );
208
- } catch (error) {
209
- return BigInt(0);
210
- }
211
- });
212
- } catch (error) {
213
- return [];
214
- }
215
- }
216
-
217
- onBuy(amounts: bigint[], poolPairData: Gyro3PoolPairData): bigint[] {
218
- try {
219
- const invariant = Gyro3Maths._calculateInvariant(
220
- poolPairData.balances,
221
- poolPairData.root3Alpha,
222
- );
223
-
224
- const virtualOffsetInOut = MathSol.mulDownFixed(
225
- invariant.toBigInt(),
226
- poolPairData.root3Alpha.toBigInt(),
227
- );
228
-
229
- const tokenAmountsUpscaled = amounts.map(a =>
230
- this._upscale(a, poolPairData.scalingFactors[poolPairData.indexOut]),
231
- );
232
-
233
- return tokenAmountsUpscaled.map(amount => {
234
- try {
235
- const amountIn = Gyro3Maths._calcInGivenOut(
236
- poolPairData.balances[poolPairData.indexIn],
237
- poolPairData.balances[poolPairData.indexOut],
238
- BigNumber.from(amount),
239
- BigNumber.from(virtualOffsetInOut),
240
- ).toBigInt();
241
-
242
- const downScaledAmount = this._downscaleUp(
243
- amountIn,
244
- poolPairData.scalingFactors[poolPairData.indexIn],
245
- );
246
- return _addFee(downScaledAmount, poolPairData.swapFee);
247
- } catch (error) {
248
- return BigInt(0);
249
- }
250
- });
251
- } catch (error) {
252
- return [];
253
- }
254
- }
255
- }
@@ -1,403 +0,0 @@
1
- import { Interface } from '@ethersproject/abi';
2
- import { BigNumber } from '@ethersproject/bignumber';
3
-
4
- import { BasePool } from '../balancer-v2-pool';
5
- import { NULL_ADDRESS, SwapSide } from '../../../../constants';
6
- import {
7
- decodeThrowError,
8
- getTokenScalingFactor,
9
- safeParseFixed,
10
- } from '../../utils';
11
- import { callData, SubgraphPoolBase, PoolState, TokenState } from '../../types';
12
- import { MathSol } from '../../balancer-v2-math';
13
- import {
14
- GyroEMaths,
15
- DerivedGyroEParams,
16
- GyroEParams,
17
- balancesFromTokenInOut,
18
- Vector2,
19
- } from '@balancer-labs/sor';
20
- import GyroEAbi from '../../../../abi/balancer-v2/gyro-e.abi.json';
21
- import * as util from 'util';
22
-
23
- // Swap Limit factor
24
- const SWAP_LIMIT_FACTOR = BigInt('999999000000000000');
25
-
26
- // All values should be normalised to 18 decimals. e.g. 1USDC = 1e18 not 1e6
27
- export type GyroEPoolPairData = {
28
- balances: BigNumber[];
29
- tokenRates: bigint[];
30
- gyroParams: GyroEParams;
31
- gyroDerivedParams: DerivedGyroEParams;
32
- rateProviders: string[];
33
- indexIn: number;
34
- indexOut: number;
35
- swapFee: bigint;
36
- scalingFactors: bigint[];
37
- tokenInIsToken0: boolean;
38
- };
39
-
40
- export class GyroEPool extends BasePool {
41
- vaultAddress: string;
42
- vaultInterface: Interface;
43
- poolInterface = new Interface(GyroEAbi);
44
-
45
- constructor(vaultAddress: string, vaultInterface: Interface) {
46
- super();
47
- this.vaultAddress = vaultAddress;
48
- this.vaultInterface = vaultInterface;
49
- }
50
-
51
- parsePoolPairData(
52
- pool: SubgraphPoolBase,
53
- poolState: PoolState,
54
- tokenIn: string,
55
- tokenOut: string,
56
- ): GyroEPoolPairData {
57
- // All values should be normalised to 18 decimals. e.g. 1USDC = 1e18 not 1e6
58
- let indexIn = 0,
59
- indexOut = 0;
60
- const scalingFactors: bigint[] = [];
61
- const balances = pool.tokens.map((t, i) => {
62
- if (t.address.toLowerCase() === tokenIn.toLowerCase()) indexIn = i;
63
- else if (t.address.toLowerCase() === tokenOut.toLowerCase()) indexOut = i;
64
- const scalingFactor = getTokenScalingFactor(t.decimals);
65
- scalingFactors.push(scalingFactor);
66
- return BigNumber.from(
67
- this._upscale(
68
- BigInt(poolState.tokens[t.address.toLowerCase()].balance),
69
- scalingFactor,
70
- ),
71
- );
72
- });
73
-
74
- const tokenInIsToken0 = indexIn === 0;
75
-
76
- const tokenRates = poolState.tokenRates
77
- ? [poolState.tokenRates![0], poolState.tokenRates![1]]
78
- : [];
79
-
80
- const rateProviders = poolState.rateProviders ?? [];
81
-
82
- const poolPairData: GyroEPoolPairData = {
83
- gyroParams: poolState.gyroParams!,
84
- gyroDerivedParams: poolState.gyroDerivedParams!,
85
- tokenRates,
86
- rateProviders,
87
- balances,
88
- indexIn,
89
- indexOut,
90
- scalingFactors,
91
- swapFee: poolState.swapFee,
92
- tokenInIsToken0,
93
- };
94
- return poolPairData;
95
- }
96
-
97
- getSwapMaxAmount(poolPairData: GyroEPoolPairData, side: SwapSide): bigint {
98
- if (side === SwapSide.SELL) {
99
- // Approximation - similar to traditional stable pools
100
- return this._downscaleDown(
101
- MathSol.mulDownFixed(
102
- poolPairData.balances[poolPairData.indexOut].toBigInt(),
103
- SWAP_LIMIT_FACTOR,
104
- ),
105
- poolPairData.scalingFactors[poolPairData.indexIn],
106
- );
107
- } else {
108
- return this._downscaleDown(
109
- MathSol.mulDownFixed(
110
- poolPairData.balances[poolPairData.indexIn].toBigInt(),
111
- SWAP_LIMIT_FACTOR,
112
- ),
113
- poolPairData.scalingFactors[poolPairData.indexOut],
114
- );
115
- }
116
- }
117
-
118
- /*
119
- Helper function to construct onchain multicall data for Pool.
120
- */
121
- getOnChainCalls(pool: SubgraphPoolBase): callData[] {
122
- const poolCallData: callData[] = [
123
- {
124
- target: this.vaultAddress,
125
- callData: this.vaultInterface.encodeFunctionData('getPoolTokens', [
126
- pool.id,
127
- ]),
128
- },
129
- {
130
- target: pool.address,
131
- callData: this.poolInterface.encodeFunctionData('getSwapFeePercentage'),
132
- },
133
- {
134
- target: pool.address,
135
- callData: this.poolInterface.encodeFunctionData('getECLPParams'),
136
- },
137
- ];
138
-
139
- // Only version 2 of GyroE uses token rates
140
- const v2Calls =
141
- pool.poolTypeVersion === 2
142
- ? [
143
- {
144
- target: pool.address,
145
- callData: this.poolInterface.encodeFunctionData('getTokenRates'),
146
- },
147
- {
148
- target: pool.address,
149
- callData: this.poolInterface.encodeFunctionData('rateProvider0'),
150
- },
151
- {
152
- target: pool.address,
153
- callData: this.poolInterface.encodeFunctionData('rateProvider1'),
154
- },
155
- ]
156
- : [];
157
-
158
- return [...poolCallData, ...v2Calls];
159
- }
160
-
161
- /*
162
- Helper function to decode multicall data for Pool.
163
- data must contain returnData
164
- startIndex is where to start in returnData. Allows this decode function to be called along with other pool types.
165
- */
166
- decodeOnChainCalls(
167
- pool: SubgraphPoolBase,
168
- data: { success: boolean; returnData: any }[],
169
- startIndex: number,
170
- ): [{ [address: string]: PoolState }, number] {
171
- const pools = {} as { [address: string]: PoolState };
172
-
173
- const poolTokens = decodeThrowError(
174
- this.vaultInterface,
175
- 'getPoolTokens',
176
- data[startIndex++],
177
- pool.address,
178
- );
179
- const swapFee = decodeThrowError(
180
- this.poolInterface,
181
- 'getSwapFeePercentage',
182
- data[startIndex++],
183
- pool.address,
184
- )[0];
185
-
186
- const eclParams = decodeThrowError(
187
- this.poolInterface,
188
- 'getECLPParams',
189
- data[startIndex++],
190
- pool.address,
191
- );
192
-
193
- let tokenRates;
194
- let rateProviders: string[] = [];
195
- if (pool.poolTypeVersion === 2) {
196
- tokenRates = decodeThrowError(
197
- this.poolInterface,
198
- 'getTokenRates',
199
- data[startIndex++],
200
- pool.address,
201
- );
202
- tokenRates = tokenRates.map(t => BigInt(t.toString()));
203
-
204
- const rateProvider0 = decodeThrowError(
205
- this.poolInterface,
206
- 'rateProvider0',
207
- data[startIndex++],
208
- pool.address,
209
- )[0];
210
-
211
- const rateProvider1 = decodeThrowError(
212
- this.poolInterface,
213
- 'rateProvider1',
214
- data[startIndex++],
215
- pool.address,
216
- )[0];
217
- rateProviders = [rateProvider0, rateProvider1];
218
- }
219
-
220
- const poolState: PoolState = {
221
- gyroParams: eclParams.params,
222
- gyroDerivedParams: eclParams.d,
223
- swapFee: BigInt(swapFee.toString()),
224
- tokenRates,
225
- rateProviders,
226
- orderedTokens: poolTokens.tokens,
227
- tokens: poolTokens.tokens.reduce(
228
- (ptAcc: { [address: string]: TokenState }, pt: string, j: number) => {
229
- const tokenState: TokenState = {
230
- balance: BigInt(poolTokens.balances[j].toString()),
231
- };
232
- ptAcc[pt.toLowerCase()] = tokenState;
233
- return ptAcc;
234
- },
235
- {},
236
- ),
237
- };
238
- pools[pool.address] = poolState;
239
- return [pools, startIndex];
240
- }
241
-
242
- onSell(amounts: bigint[], poolPairData: GyroEPoolPairData): bigint[] {
243
- try {
244
- const scalingFactorTokenIn = this._scalingFactor(
245
- poolPairData.tokenInIsToken0,
246
- poolPairData,
247
- );
248
- const scalingFactorTokenOut = this._scalingFactor(
249
- !poolPairData.tokenInIsToken0,
250
- poolPairData,
251
- );
252
- const normalizedBalances = this._normalizeBalances(
253
- poolPairData.balances,
254
- poolPairData.tokenRates,
255
- );
256
-
257
- const orderedNormalizedBalances = balancesFromTokenInOut(
258
- normalizedBalances[poolPairData.indexIn],
259
- normalizedBalances[poolPairData.indexOut],
260
- poolPairData.tokenInIsToken0,
261
- );
262
-
263
- const [currentInvariant, invErr] = GyroEMaths.calculateInvariantWithError(
264
- orderedNormalizedBalances,
265
- poolPairData.gyroParams,
266
- poolPairData.gyroDerivedParams,
267
- );
268
-
269
- const invariant: Vector2 = {
270
- x: currentInvariant.add(invErr.mul(2)),
271
- y: currentInvariant,
272
- };
273
-
274
- const tokenAmountsInScaled = amounts.map(a =>
275
- this._upscale(a, scalingFactorTokenIn),
276
- );
277
-
278
- const tokenAmountsInWithFee = tokenAmountsInScaled.map(a =>
279
- this._subtractSwapFeeAmount(a, poolPairData.swapFee),
280
- );
281
-
282
- const amountsOut: bigint[] = tokenAmountsInWithFee.map(amount => {
283
- try {
284
- const amountOut = GyroEMaths.calcOutGivenIn(
285
- orderedNormalizedBalances,
286
- BigNumber.from(amount),
287
- poolPairData.tokenInIsToken0,
288
- poolPairData.gyroParams,
289
- poolPairData.gyroDerivedParams,
290
- invariant,
291
- ).toBigInt();
292
-
293
- return this._downscaleDown(amountOut, scalingFactorTokenOut);
294
- } catch (error) {
295
- return BigInt(0);
296
- }
297
- });
298
-
299
- return amountsOut;
300
- } catch (error) {
301
- return [];
302
- }
303
- }
304
-
305
- onBuy(amounts: bigint[], poolPairData: GyroEPoolPairData): bigint[] {
306
- try {
307
- const scalingFactorTokenIn = this._scalingFactor(
308
- poolPairData.tokenInIsToken0,
309
- poolPairData,
310
- );
311
- const scalingFactorTokenOut = this._scalingFactor(
312
- !poolPairData.tokenInIsToken0,
313
- poolPairData,
314
- );
315
-
316
- const normalizedBalances = this._normalizeBalances(
317
- poolPairData.balances,
318
- poolPairData.tokenRates,
319
- );
320
-
321
- const orderedNormalizedBalances = balancesFromTokenInOut(
322
- normalizedBalances[poolPairData.indexIn],
323
- normalizedBalances[poolPairData.indexOut],
324
- poolPairData.tokenInIsToken0,
325
- );
326
-
327
- const [currentInvariant, invErr] = GyroEMaths.calculateInvariantWithError(
328
- orderedNormalizedBalances,
329
- poolPairData.gyroParams,
330
- poolPairData.gyroDerivedParams,
331
- );
332
-
333
- const invariant: Vector2 = {
334
- x: currentInvariant.add(invErr.mul(2)),
335
- y: currentInvariant,
336
- };
337
-
338
- const tokenAmountsOutScaled = amounts.map(a =>
339
- this._upscale(a, scalingFactorTokenOut),
340
- );
341
-
342
- const amountsIn: bigint[] = tokenAmountsOutScaled.map(amount => {
343
- try {
344
- const amountIn = GyroEMaths.calcInGivenOut(
345
- orderedNormalizedBalances,
346
- BigNumber.from(amount),
347
- poolPairData.tokenInIsToken0,
348
- poolPairData.gyroParams,
349
- poolPairData.gyroDerivedParams,
350
- invariant,
351
- ).toBigInt();
352
- return amountIn;
353
- } catch (error) {
354
- return BigInt(0);
355
- }
356
- });
357
-
358
- const downScaled = amountsIn.map(amount =>
359
- this._downscaleUp(amount, scalingFactorTokenIn),
360
- );
361
-
362
- return downScaled.map(a => this._addFeeAmount(a, poolPairData.swapFee));
363
- } catch (error) {
364
- return [];
365
- }
366
- }
367
-
368
- private _scalingFactor(
369
- token0: boolean,
370
- poolPairData: GyroEPoolPairData,
371
- ): bigint {
372
- const tokenIndex = token0 ? 0 : 1;
373
- const scalingFactor = poolPairData.scalingFactors[tokenIndex];
374
- if (
375
- poolPairData.rateProviders.length === 0 ||
376
- poolPairData.rateProviders[tokenIndex].toLowerCase() === NULL_ADDRESS
377
- ) {
378
- return scalingFactor;
379
- } else {
380
- return MathSol.mulDownFixed(
381
- scalingFactor,
382
- poolPairData.tokenRates[tokenIndex],
383
- );
384
- }
385
- }
386
-
387
- private _normalizeBalances(
388
- balances: BigNumber[],
389
- tokenRates: bigint[],
390
- ): BigNumber[] {
391
- // If pool is V1, e.g. has no tokenRates return balances
392
- if (tokenRates.length === 0) return balances;
393
- /*
394
- Apply tokenRates
395
- SOR upscales here first using scalingFactors but this is already being done in parse pair data so no need to repeat.
396
- */
397
- return balances.map((bal, i) => {
398
- return BigNumber.from(
399
- MathSol.mulDownFixed(bal.toBigInt(), tokenRates[i]),
400
- );
401
- });
402
- }
403
- }
@@ -1,15 +0,0 @@
1
- export function _reduceFee(amountIn: bigint, swapFee: bigint): bigint {
2
- const product = amountIn * swapFee;
3
- const feeAmount = (product - 1n) / 1000000000000000000n + 1n;
4
- return amountIn - feeAmount;
5
- }
6
-
7
- export function _addFee(amountIn: bigint, swapFee: bigint): bigint {
8
- const fee = 1000000000000000000n - swapFee;
9
- return divUp(amountIn, fee);
10
- }
11
-
12
- function divUp(a: bigint, b: bigint): bigint {
13
- const aInflated = a * 1000000000000000000n;
14
- return (aInflated - 1n) / b + 1n;
15
- }