@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,268 +0,0 @@
1
- // npx jest src/dex/balancer-v2/balancer-v2-gyro3.test.ts
2
- import dotenv from 'dotenv';
3
- dotenv.config();
4
-
5
- import { Interface } from '@ethersproject/abi';
6
- import { BigNumber } from '@ethersproject/bignumber';
7
-
8
- import { Tokens } from '../../../tests/constants-e2e';
9
- import { DummyDexHelper } from '../../dex-helper/index';
10
- import { Network, SwapSide } from '../../constants';
11
- import { BalancerV2EventPool } from './balancer-v2';
12
- import { BalancerConfig } from './config';
13
- import { PoolState, SubgraphPoolBase } from './types';
14
- import { Gyro3Pool, Gyro3PoolPairData } from './pools/gyro/Gyro3Pool';
15
- import { queryOnChain } from './queryOnChain';
16
-
17
- import VaultABI from '../../abi/balancer-v2/vault.json';
18
-
19
- const rpcUrl = process.env.HTTP_PROVIDER_137 as string;
20
-
21
- describe('BalancerV2', () => {
22
- const dexKey = 'BalancerV2';
23
- const network = Network.POLYGON;
24
- const config = BalancerConfig[dexKey][network];
25
- const vaultInterface = new Interface(VaultABI);
26
- const gyro3Pool = new Gyro3Pool(config.vaultAddress, vaultInterface);
27
- const tokens = Tokens[network];
28
-
29
- const dexHelper = new DummyDexHelper(network);
30
- const logger = dexHelper.getLogger(dexKey);
31
- const balancerPools = new BalancerV2EventPool(
32
- dexKey,
33
- network,
34
- config.vaultAddress,
35
- config.subgraphURL,
36
- dexHelper,
37
- logger,
38
- );
39
-
40
- describe('Gyro3 Pool', () => {
41
- describe('Pool Fetching', () => {
42
- let Gyro3PoolSg: SubgraphPoolBase;
43
- let Gyro3PoolState: PoolState;
44
-
45
- it('should be supported pool type', async function () {
46
- expect(balancerPools.isSupportedPool('Gyro3')).toBe(true);
47
- });
48
-
49
- it('should fetch Gyro3 Pool with correct fields from Subgraph', async function () {
50
- const pools = await balancerPools.fetchAllSubgraphPools();
51
- // USDC/BUSD/USDT
52
- const Gyro3PoolId =
53
- '0x17f1ef81707811ea15d9ee7c741179bbe2a63887000100000000000000000799';
54
- const Gyro3 = pools.filter(p => p.id === Gyro3PoolId);
55
- expect(Gyro3.length).toBe(1);
56
- Gyro3PoolSg = Gyro3[0];
57
- expect(Gyro3PoolSg.tokens.length).toBe(3);
58
- const root3Alpha = parseFloat(Gyro3PoolSg.root3Alpha);
59
- expect(root3Alpha > 0).toBe(true);
60
- Gyro3PoolSg = { ...Gyro3PoolSg };
61
- Gyro3PoolSg.root3Alpha = '0.9995';
62
- });
63
-
64
- it('getOnChainState', async function () {
65
- const blocknumber = 40038397;
66
- const state = await balancerPools.getOnChainState(
67
- [Gyro3PoolSg],
68
- blocknumber,
69
- );
70
- Gyro3PoolState = state[Gyro3PoolSg.address];
71
- expect(Gyro3PoolState.swapFee).toBe(BigInt('300000000000000'));
72
- expect(Gyro3PoolState.tokens[tokens.USDC.address].balance).toBe(
73
- BigInt('25755475662'),
74
- );
75
- expect(
76
- Gyro3PoolState.tokens[tokens.BUSD.address.toLowerCase()].balance,
77
- ).toBe(BigInt('41279070738726640173873'));
78
- expect(
79
- Gyro3PoolState.tokens[tokens.USDT.address.toLowerCase()].balance,
80
- ).toBe(BigInt('25601127342'));
81
- });
82
-
83
- it('parsePoolPairData', async function () {
84
- const tokenIn = tokens.BUSD.address;
85
- const tokenOut = tokens.USDT.address;
86
- const pairData = gyro3Pool.parsePoolPairData(
87
- Gyro3PoolSg,
88
- Gyro3PoolState,
89
- tokenIn,
90
- tokenOut,
91
- );
92
- expect(pairData.indexIn).toBe(1);
93
- expect(pairData.indexOut).toBe(2);
94
- expect(pairData.swapFee).toBe(BigInt('300000000000000'));
95
- expect(pairData.balanceTertiary.toString()).toBe(
96
- '25755475662000000000000',
97
- );
98
- expect(pairData.decimalsTertiary.toString()).toBe('6');
99
- expect(pairData.balances).toStrictEqual([
100
- BigNumber.from('25755475662000000000000'),
101
- BigNumber.from('41279070738726640173873'),
102
- BigNumber.from('25601127342000000000000'),
103
- ]);
104
- expect(pairData.root3Alpha.toString()).toBe('999500000000000000');
105
- });
106
- });
107
-
108
- describe('Swap Functions', () => {
109
- describe('Swap Functions - 6decimals>18decimals', () => {
110
- const pairData: Gyro3PoolPairData = {
111
- balances: [
112
- BigNumber.from('25755475662000000000000'),
113
- BigNumber.from('41279070738726640173873'),
114
- BigNumber.from('25601127342000000000000'),
115
- ],
116
- indexIn: 0,
117
- indexOut: 1,
118
- swapFee: BigInt('300000000000000'),
119
- scalingFactors: [
120
- BigInt('1000000000000000000000000000000'),
121
- BigInt('1000000000000000000'),
122
- BigInt('1000000000000000000000000000000'),
123
- ],
124
- balanceTertiary: BigNumber.from('25601127342000000000000'),
125
- decimalsTertiary: 6,
126
- root3Alpha: BigNumber.from('999500000000000000'),
127
- };
128
-
129
- it('getSwapMaxAmount (SELL)', async () => {
130
- const swapMaxAmount = gyro3Pool.getSwapMaxAmount(
131
- pairData,
132
- SwapSide.SELL,
133
- );
134
- expect(swapMaxAmount).toBe(BigInt('41279029459'));
135
- });
136
-
137
- it('swap', async function () {
138
- const amountIn = BigInt('13500000');
139
- const amountOut = gyro3Pool.onSell([amountIn], pairData);
140
- expect(amountOut.length).toBe(1);
141
- expect(amountOut[0].toString()).toBe('13499339792532290017');
142
- });
143
-
144
- it('buy', async function () {
145
- const amountOut = BigInt('13500000');
146
- const amountIn = gyro3Pool.onBuy([amountOut], pairData);
147
- expect(amountIn.length).toBe(1);
148
- });
149
- });
150
-
151
- describe('Swap Functions - 18decimals>6decimals', () => {
152
- const pairData: Gyro3PoolPairData = {
153
- balances: [
154
- BigNumber.from('25755475662000000000000'),
155
- BigNumber.from('41279070738726640173873'),
156
- BigNumber.from('25601127342000000000000'),
157
- ],
158
- indexIn: 1,
159
- indexOut: 2,
160
- swapFee: BigInt('300000000000000'),
161
- scalingFactors: [
162
- BigInt('1000000000000000000000000000000'),
163
- BigInt('1000000000000000000'),
164
- BigInt('1000000000000000000000000000000'),
165
- ],
166
- balanceTertiary: BigNumber.from('25601127342000000000000'),
167
- decimalsTertiary: 6,
168
- root3Alpha: BigNumber.from('999500000000000000'),
169
- };
170
-
171
- it('getSwapMaxAmount', async () => {
172
- const swapMaxAmount = gyro3Pool.getSwapMaxAmount(
173
- pairData,
174
- SwapSide.SELL,
175
- );
176
- expect(swapMaxAmount).toBe(BigInt('25601101740872658000000'));
177
- });
178
-
179
- it('swap', async function () {
180
- const amountIn = BigInt('13500000000000000000');
181
- const amountOut = gyro3Pool.onSell([amountIn], pairData);
182
- expect(amountOut.length).toBe(1);
183
- expect(amountOut[0].toString()).toBe('13492521');
184
- });
185
- });
186
- });
187
-
188
- describe('Onchain Compare', () => {
189
- it('_exactTokenInForTokenOut', async function () {
190
- const blocknumber = 55433090;
191
- // usdc/busd/usdt
192
- const poolId =
193
- '0x17f1ef81707811ea15d9ee7c741179bbe2a63887000100000000000000000799';
194
- const tokenIn = tokens.USDC.address;
195
- const tokenOut = tokens.BUSD.address;
196
- const amountIn = BigInt('1000400');
197
-
198
- const pools = await balancerPools.fetchAllSubgraphPools();
199
- const poolSg = pools.filter(p => p.id === poolId)[0];
200
- const state = await balancerPools.getOnChainState(
201
- [poolSg],
202
- blocknumber,
203
- );
204
- const poolState = state[poolSg.address];
205
-
206
- const pairData = gyro3Pool.parsePoolPairData(
207
- poolSg,
208
- poolState,
209
- tokenIn,
210
- tokenOut,
211
- );
212
- const amountOut = gyro3Pool.onSell([amountIn], pairData);
213
- const deltas = await queryOnChain(
214
- rpcUrl,
215
- blocknumber,
216
- poolId,
217
- 0,
218
- tokenIn,
219
- tokenOut,
220
- amountIn,
221
- );
222
- expect(amountIn).toEqual(deltas[0]);
223
- expect(amountOut[0] + deltas[1]).toEqual(0n);
224
- });
225
-
226
- it('_exactTokenOutForTokenIn', async function () {
227
- const blocknumber = 54571344;
228
- const poolId =
229
- '0x17f1ef81707811ea15d9ee7c741179bbe2a63887000100000000000000000799';
230
-
231
- const tokenIn = tokens.USDT.address;
232
- const tokenOut = tokens.USDC.address;
233
-
234
- const amountOut = BigInt('11000000');
235
-
236
- const pools = await balancerPools.fetchAllSubgraphPools();
237
- const poolSg = pools.filter(p => p.id === poolId)[0];
238
- const state = await balancerPools.getOnChainState(
239
- [poolSg],
240
- blocknumber,
241
- );
242
- const poolState = state[poolSg.address];
243
-
244
- const pairData = gyro3Pool.parsePoolPairData(
245
- poolSg,
246
- poolState,
247
- tokenIn,
248
- tokenOut,
249
- );
250
-
251
- const amountIn = gyro3Pool.onBuy([amountOut], pairData);
252
-
253
- const deltas = await queryOnChain(
254
- rpcUrl,
255
- blocknumber,
256
- poolId,
257
- 1,
258
- tokenIn,
259
- tokenOut,
260
- amountOut,
261
- );
262
-
263
- expect(amountIn[0]).toEqual(deltas[0]); // amountIn should equal deltas[0]
264
- expect(deltas[1] + amountOut).toEqual(0n); // deltas[1] (consumed) + amountOut should equal 0
265
- });
266
- });
267
- });
268
- });
@@ -1,401 +0,0 @@
1
- /* eslint-disable no-console */
2
- // npx jest src/dex/balancer-v2/balancer-v2-gyroE.test.ts
3
- import dotenv from 'dotenv';
4
- dotenv.config();
5
-
6
- import { Interface } from '@ethersproject/abi';
7
- import { BigNumber } from '@ethersproject/bignumber';
8
-
9
- import { Tokens } from '../../../tests/constants-e2e';
10
- import { DummyDexHelper } from '../../dex-helper/index';
11
- import { Network, SwapSide } from '../../constants';
12
- import { BalancerV2EventPool } from './balancer-v2';
13
- import { BalancerConfig } from './config';
14
- import { PoolState, SubgraphPoolBase } from './types';
15
- import { GyroEPool, GyroEPoolPairData } from './pools/gyro/GyroEPool';
16
- import { queryOnChain } from './queryOnChain';
17
- import VaultABI from '../../abi/balancer-v2/vault.json';
18
-
19
- const rpcUrl = process.env.HTTP_PROVIDER_137 as string;
20
-
21
- describe('BalancerV2', () => {
22
- const dexKey = 'BalancerV2';
23
- const network = Network.POLYGON;
24
- const config = BalancerConfig[dexKey][network];
25
- const vaultInterface = new Interface(VaultABI);
26
- const gyroEPool = new GyroEPool(config.vaultAddress, vaultInterface);
27
- const tokens = Tokens[network];
28
- let GyroEPoolSg: SubgraphPoolBase;
29
- let GyroEPoolState: PoolState;
30
- const dexHelper = new DummyDexHelper(network);
31
- const logger = dexHelper.getLogger(dexKey);
32
- const balancerPools = new BalancerV2EventPool(
33
- dexKey,
34
- network,
35
- config.vaultAddress,
36
- config.subgraphURL,
37
- dexHelper,
38
- logger,
39
- );
40
-
41
- describe('GyroE Pool V1', () => {
42
- describe('Pool Fetching', () => {
43
- it('should be supported pool type', async function () {
44
- expect(balancerPools.isSupportedPool('GyroE')).toBe(true);
45
- });
46
-
47
- it('should fetch GyroE Pool with correct fields from Subgraph', async function () {
48
- const pools = await balancerPools.fetchAllSubgraphPools();
49
- // USDC/TUSD
50
- const GyroEPoolId =
51
- '0x97469e6236bd467cd147065f77752b00efadce8a0002000000000000000008c0';
52
- const GyroE = pools.filter(p => p.id === GyroEPoolId);
53
- expect(GyroE.length).toBe(1);
54
- GyroEPoolSg = GyroE[0];
55
- expect(GyroEPoolSg.tokens.length).toBe(2);
56
- expect(GyroEPoolSg.poolTypeVersion).toEqual(1);
57
- });
58
-
59
- it('getOnChainState', async function () {
60
- const blocknumber = 40068730;
61
- const state = await balancerPools.getOnChainState(
62
- [GyroEPoolSg],
63
- blocknumber,
64
- );
65
- GyroEPoolState = state[GyroEPoolSg.address];
66
- expect(GyroEPoolState.swapFee).toBe(BigInt('200000000000000'));
67
- expect(GyroEPoolState.tokens[tokens.USDC.address].balance).toBe(
68
- BigInt('18186193501'),
69
- );
70
- expect(
71
- GyroEPoolState.tokens[tokens.TUSD.address.toLowerCase()].balance,
72
- ).toBe(BigInt('26629907962671451455387'));
73
- });
74
-
75
- it('parsePoolPairData', async function () {
76
- const tokenIn = tokens.USDC.address;
77
- const tokenOut = tokens.TUSD.address;
78
- const pairData = gyroEPool.parsePoolPairData(
79
- GyroEPoolSg,
80
- GyroEPoolState,
81
- tokenIn,
82
- tokenOut,
83
- );
84
- expect(pairData.indexIn).toBe(0);
85
- expect(pairData.indexOut).toBe(1);
86
- expect(pairData.swapFee).toBe(BigInt('200000000000000'));
87
- expect(pairData.balances).toStrictEqual([
88
- BigNumber.from('18186193501000000000000'),
89
- BigNumber.from('26629907962671451455387'),
90
- ]);
91
- expect(pairData.tokenInIsToken0).toBe(true);
92
- });
93
- });
94
-
95
- describe('Swap Functions', () => {
96
- describe('Swap Functions - 6decimals>18decimals', () => {
97
- const pairData: GyroEPoolPairData = {
98
- tokenInIsToken0: true,
99
- balances: [
100
- BigNumber.from('18186193501000000000000'),
101
- BigNumber.from('26629907962671451455387'),
102
- ],
103
- indexIn: 0,
104
- indexOut: 1,
105
- scalingFactors: [
106
- 1000000000000000000000000000000n,
107
- 1000000000000000000n,
108
- ],
109
- swapFee: 200000000000000n,
110
- gyroDerivedParams: {
111
- tauAlpha: {
112
- x: BigNumber.from('-99921684096872623630266893017017590000'),
113
- y: BigNumber.from('3956898690236155895758568963473897000'),
114
- },
115
- tauBeta: {
116
- x: BigNumber.from('99921684096872623626859806443439160000'),
117
- y: BigNumber.from('3956898690236155981796108700303143000'),
118
- },
119
- u: BigNumber.from('99921684096872623515276234437562470000'),
120
- v: BigNumber.from('3956898690236155934291169066298950000'),
121
- w: BigNumber.from('43018769868414623130'),
122
- z: BigNumber.from('-1703543286789219094'),
123
- dSq: BigNumber.from('99999999999999999886624093342106120000'),
124
- },
125
- gyroParams: {
126
- alpha: BigNumber.from('980000000000000000'),
127
- beta: BigNumber.from('1020408163265306122'),
128
- c: BigNumber.from('707106781186547524'),
129
- s: BigNumber.from('707106781186547524'),
130
- lambda: BigNumber.from('2500000000000000000000'),
131
- },
132
- tokenRates: [],
133
- rateProviders: [],
134
- };
135
-
136
- it('getSwapMaxAmount', async () => {
137
- const swapMaxAmount = gyroEPool.getSwapMaxAmount(
138
- pairData,
139
- SwapSide.SELL,
140
- );
141
- expect(swapMaxAmount).toBe(BigInt('26629881332'));
142
- });
143
-
144
- it('swap', async function () {
145
- const amountIn = BigInt('13500000');
146
- const amountOut = gyroEPool.onSell([amountIn], pairData);
147
- expect(amountOut.length).toBe(1);
148
- expect(amountOut[0].toString()).toBe('13499365736688193741');
149
- });
150
- });
151
-
152
- describe('Swap Functions - 18decimals>6decimals', () => {
153
- const pairData: GyroEPoolPairData = {
154
- tokenInIsToken0: false,
155
- balances: [
156
- BigNumber.from('18186193501000000000000'),
157
- BigNumber.from('26629907962671451455387'),
158
- ],
159
- indexIn: 1,
160
- indexOut: 0,
161
- scalingFactors: [
162
- 1000000000000000000000000000000n,
163
- 1000000000000000000n,
164
- ],
165
- swapFee: 200000000000000n,
166
- gyroDerivedParams: {
167
- tauAlpha: {
168
- x: BigNumber.from('-99921684096872623630266893017017590000'),
169
- y: BigNumber.from('3956898690236155895758568963473897000'),
170
- },
171
- tauBeta: {
172
- x: BigNumber.from('99921684096872623626859806443439160000'),
173
- y: BigNumber.from('3956898690236155981796108700303143000'),
174
- },
175
- u: BigNumber.from('99921684096872623515276234437562470000'),
176
- v: BigNumber.from('3956898690236155934291169066298950000'),
177
- w: BigNumber.from('43018769868414623130'),
178
- z: BigNumber.from('-1703543286789219094'),
179
- dSq: BigNumber.from('99999999999999999886624093342106120000'),
180
- },
181
- gyroParams: {
182
- alpha: BigNumber.from('980000000000000000'),
183
- beta: BigNumber.from('1020408163265306122'),
184
- c: BigNumber.from('707106781186547524'),
185
- s: BigNumber.from('707106781186547524'),
186
- lambda: BigNumber.from('2500000000000000000000'),
187
- },
188
- tokenRates: [],
189
- rateProviders: [],
190
- };
191
-
192
- it('getSwapMaxAmount', async () => {
193
- const swapMaxAmount = gyroEPool.getSwapMaxAmount(
194
- pairData,
195
- SwapSide.SELL,
196
- );
197
- expect(swapMaxAmount).toBe(BigInt('18186175314806499000000'));
198
- });
199
-
200
- it('swap', async function () {
201
- const amountIn = BigInt('13500000000000000000');
202
- const amountOut = gyroEPool.onSell([amountIn], pairData);
203
- expect(amountOut.length).toBe(1);
204
- expect(amountOut[0].toString()).toBe('13495227');
205
- });
206
- });
207
- });
208
-
209
- describe('Onchain Compare', () => {
210
- it('exactIn', async function () {
211
- const blocknumber = 54571344;
212
- // USDC/TUSD
213
- const poolId =
214
- '0x97469e6236bd467cd147065f77752b00efadce8a0002000000000000000008c0';
215
- const tokenIn = tokens.USDC.address;
216
- const tokenOut = tokens.TUSD.address;
217
- const amountIn = BigInt('1000000');
218
-
219
- const pools = await balancerPools.fetchAllSubgraphPools();
220
- const poolSg = pools.filter(p => p.id === poolId)[0];
221
- const state = await balancerPools.getOnChainState(
222
- [poolSg],
223
- blocknumber,
224
- );
225
- const poolState = state[poolSg.address];
226
-
227
- const pairData = gyroEPool.parsePoolPairData(
228
- poolSg,
229
- poolState,
230
- tokenIn,
231
- tokenOut,
232
- );
233
- const amountOut = gyroEPool.onSell([amountIn], pairData);
234
- const deltas = await queryOnChain(
235
- rpcUrl,
236
- blocknumber,
237
- poolId,
238
- 0,
239
- tokenIn,
240
- tokenOut,
241
- amountIn,
242
- );
243
- expect(amountIn).toEqual(deltas[0]);
244
- expect(amountOut[0] + deltas[1]).toEqual(0n);
245
- });
246
- it('exactOut', async function () {
247
- const blocknumber = 54571344;
248
- // USDC/TUSD
249
- const poolId =
250
- '0x97469e6236bd467cd147065f77752b00efadce8a0002000000000000000008c0';
251
- const tokenIn = tokens.USDC.address;
252
- const tokenOut = tokens.TUSD.address;
253
- const amountOut = BigInt('2000000000000000000');
254
-
255
- const pools = await balancerPools.fetchAllSubgraphPools();
256
- const poolSg = pools.filter(p => p.id === poolId)[0];
257
- const state = await balancerPools.getOnChainState(
258
- [poolSg],
259
- blocknumber,
260
- );
261
- const poolState = state[poolSg.address];
262
-
263
- const pairData = gyroEPool.parsePoolPairData(
264
- poolSg,
265
- poolState,
266
- tokenIn,
267
- tokenOut,
268
- );
269
- const amountIn = gyroEPool.onBuy([amountOut], pairData);
270
- const deltas = await queryOnChain(
271
- rpcUrl,
272
- blocknumber,
273
- poolId,
274
- 1,
275
- tokenIn,
276
- tokenOut,
277
- amountOut,
278
- );
279
- expect(amountIn[0]).toEqual(deltas[0]);
280
- expect(amountOut + deltas[1]).toEqual(0n);
281
- });
282
- });
283
- });
284
-
285
- describe('GyroE Pool V2', () => {
286
- describe('Pool Fetching', () => {
287
- it('should fetch GyroE Pool with correct fields from Subgraph', async function () {
288
- const pools = await balancerPools.fetchAllSubgraphPools();
289
- // wmatic/xmatic
290
- const GyroEPoolId =
291
- '0xee278d943584dd8640eaf4cc6c7a5c80c0073e85000200000000000000000bc7';
292
- const GyroE = pools.filter(p => p.id === GyroEPoolId);
293
- expect(GyroE.length).toBe(1);
294
- GyroEPoolSg = GyroE[0];
295
- expect(GyroEPoolSg.tokens.length).toBe(2);
296
- expect(GyroEPoolSg.poolTypeVersion).toEqual(2);
297
- });
298
-
299
- it('getOnChainState', async function () {
300
- const blocknumber = 54571344;
301
- const state = await balancerPools.getOnChainState(
302
- [GyroEPoolSg],
303
- blocknumber,
304
- );
305
- GyroEPoolState = state[GyroEPoolSg.address];
306
- expect(GyroEPoolState.swapFee).toBe(BigInt('100000000000000'));
307
- expect(
308
- GyroEPoolState.tokens[tokens.WMATIC.address.toLowerCase()].balance,
309
- ).toBe(231918780712184629424641n);
310
- expect(
311
- GyroEPoolState.tokens[tokens.MATICX.address.toLowerCase()].balance,
312
- ).toBe(1614748628144003818699851n);
313
- expect(GyroEPoolState.rateProviders![0]).toBe(
314
- '0x0000000000000000000000000000000000000000',
315
- );
316
- expect(GyroEPoolState.rateProviders![1].toLowerCase()).toBe(
317
- '0xee652bbf72689aa59f0b8f981c9c90e2a8af8d8f',
318
- );
319
- // Token has no rate provider so rate should be 1
320
- expect(GyroEPoolState.tokenRates![0]).toBe(1000000000000000000n);
321
- // Token has a rate provider so should use that rate
322
- expect(GyroEPoolState.tokenRates![1]).toBe(1103299689517375348n);
323
- });
324
- });
325
-
326
- describe('Onchain Compare', () => {
327
- it('exactIn', async function () {
328
- const blocknumber = 54571344;
329
- // wmatic/xmatic
330
- const poolId =
331
- '0xee278d943584dd8640eaf4cc6c7a5c80c0073e85000200000000000000000bc7';
332
- const tokenIn = tokens.MATICX.address;
333
- const tokenOut = tokens.WMATIC.address;
334
- const amountIn = BigInt('110000000000000000');
335
-
336
- const pools = await balancerPools.fetchAllSubgraphPools();
337
- const poolSg = pools.filter(p => p.id === poolId)[0];
338
- const state = await balancerPools.getOnChainState(
339
- [poolSg],
340
- blocknumber,
341
- );
342
- const poolState = state[poolSg.address];
343
-
344
- const pairData = gyroEPool.parsePoolPairData(
345
- poolSg,
346
- poolState,
347
- tokenIn,
348
- tokenOut,
349
- );
350
- const amountOut = gyroEPool.onSell([amountIn], pairData);
351
- const deltas = await queryOnChain(
352
- rpcUrl,
353
- blocknumber,
354
- poolId,
355
- 0,
356
- tokenIn,
357
- tokenOut,
358
- amountIn,
359
- );
360
- expect(amountIn).toEqual(deltas[0]);
361
- expect(amountOut[0] + deltas[1]).toEqual(0n);
362
- });
363
- it('exactOut', async function () {
364
- const blocknumber = 54571344;
365
- // wmatic/xmatic
366
- const poolId =
367
- '0xee278d943584dd8640eaf4cc6c7a5c80c0073e85000200000000000000000bc7';
368
- const tokenIn = tokens.MATICX.address;
369
- const tokenOut = tokens.WMATIC.address;
370
- const amountOut = BigInt('2000000000000000000');
371
-
372
- const pools = await balancerPools.fetchAllSubgraphPools();
373
- const poolSg = pools.filter(p => p.id === poolId)[0];
374
- const state = await balancerPools.getOnChainState(
375
- [poolSg],
376
- blocknumber,
377
- );
378
- const poolState = state[poolSg.address];
379
-
380
- const pairData = gyroEPool.parsePoolPairData(
381
- poolSg,
382
- poolState,
383
- tokenIn,
384
- tokenOut,
385
- );
386
- const amountIn = gyroEPool.onBuy([amountOut], pairData);
387
- const deltas = await queryOnChain(
388
- rpcUrl,
389
- blocknumber,
390
- poolId,
391
- 1,
392
- tokenIn,
393
- tokenOut,
394
- amountOut,
395
- );
396
- expect(amountIn[0]).toEqual(deltas[0]);
397
- expect(amountOut + deltas[1]).toEqual(0n);
398
- });
399
- });
400
- });
401
- });