@paraswap/dex-lib 2.17.0 → 2.17.1-pancakeswapv3.1

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 (131) hide show
  1. package/build/abi/pancakeswap-v3/PancakeV3StateMulticall.abi.json +781 -0
  2. package/build/abi/pancakeswap-v3/PancakeswapV3Pool.abi.json +1039 -0
  3. package/build/abi/pancakeswap-v3/PancakeswapV3Quoter.abi.json +285 -0
  4. package/build/abi/pancakeswap-v3/PancakeswapV3Router.abi.json +583 -0
  5. package/build/dex/balancer-v2/balancer-v2.js +8 -6
  6. package/build/dex/balancer-v2/balancer-v2.js.map +1 -1
  7. package/build/dex/balancer-v2/config.js.map +1 -1
  8. package/build/dex/balancer-v2/pools/linear/LinearMath.js.map +1 -1
  9. package/build/dex/balancer-v2/pools/linear/LinearPool.js.map +1 -1
  10. package/build/dex/balancer-v2/pools/phantom-stable/PhantomStableMath.js.map +1 -1
  11. package/build/dex/balancer-v2/pools/phantom-stable/PhantomStablePool.js.map +1 -1
  12. package/build/dex/balancer-v2/pools/stable/StableMath.js +0 -1
  13. package/build/dex/balancer-v2/pools/stable/StableMath.js.map +1 -1
  14. package/build/dex/balancer-v2/pools/stable/StablePool.js +2 -1
  15. package/build/dex/balancer-v2/pools/stable/StablePool.js.map +1 -1
  16. package/build/dex/balancer-v2/pools/weighted/WeightedPool.js.map +1 -1
  17. package/build/dex/index.js +1 -1
  18. package/build/dex/index.js.map +1 -1
  19. package/build/dex/pancakeswap-v3/config.d.ts +4 -0
  20. package/build/dex/pancakeswap-v3/config.js +62 -0
  21. package/build/dex/pancakeswap-v3/config.js.map +1 -0
  22. package/build/dex/pancakeswap-v3/constants.d.ts +23 -0
  23. package/build/dex/pancakeswap-v3/constants.js +29 -0
  24. package/build/dex/pancakeswap-v3/constants.js.map +1 -0
  25. package/build/dex/pancakeswap-v3/contract-math/BitMath.d.ts +4 -0
  26. package/build/dex/pancakeswap-v3/contract-math/BitMath.js +93 -0
  27. package/build/dex/pancakeswap-v3/contract-math/BitMath.js.map +1 -0
  28. package/build/dex/pancakeswap-v3/contract-math/FixedPoint128.d.ts +3 -0
  29. package/build/dex/pancakeswap-v3/contract-math/FixedPoint128.js +8 -0
  30. package/build/dex/pancakeswap-v3/contract-math/FixedPoint128.js.map +1 -0
  31. package/build/dex/pancakeswap-v3/contract-math/FixedPoint96.d.ts +4 -0
  32. package/build/dex/pancakeswap-v3/contract-math/FixedPoint96.js +9 -0
  33. package/build/dex/pancakeswap-v3/contract-math/FixedPoint96.js.map +1 -0
  34. package/build/dex/pancakeswap-v3/contract-math/FullMath.d.ts +4 -0
  35. package/build/dex/pancakeswap-v3/contract-math/FullMath.js +19 -0
  36. package/build/dex/pancakeswap-v3/contract-math/FullMath.js.map +1 -0
  37. package/build/dex/pancakeswap-v3/contract-math/LiquidityMath.d.ts +3 -0
  38. package/build/dex/pancakeswap-v3/contract-math/LiquidityMath.js +22 -0
  39. package/build/dex/pancakeswap-v3/contract-math/LiquidityMath.js.map +1 -0
  40. package/build/dex/pancakeswap-v3/contract-math/Oracle.d.ts +10 -0
  41. package/build/dex/pancakeswap-v3/contract-math/Oracle.js +133 -0
  42. package/build/dex/pancakeswap-v3/contract-math/Oracle.js.map +1 -0
  43. package/build/dex/pancakeswap-v3/contract-math/SqrtPriceMath.d.ts +10 -0
  44. package/build/dex/pancakeswap-v3/contract-math/SqrtPriceMath.js +91 -0
  45. package/build/dex/pancakeswap-v3/contract-math/SqrtPriceMath.js.map +1 -0
  46. package/build/dex/pancakeswap-v3/contract-math/SwapMath.d.ts +8 -0
  47. package/build/dex/pancakeswap-v3/contract-math/SwapMath.js +70 -0
  48. package/build/dex/pancakeswap-v3/contract-math/SwapMath.js.map +1 -0
  49. package/build/dex/pancakeswap-v3/contract-math/Tick.d.ts +7 -0
  50. package/build/dex/pancakeswap-v3/contract-math/Tick.js +45 -0
  51. package/build/dex/pancakeswap-v3/contract-math/Tick.js.map +1 -0
  52. package/build/dex/pancakeswap-v3/contract-math/TickBitMap.d.ts +8 -0
  53. package/build/dex/pancakeswap-v3/contract-math/TickBitMap.js +87 -0
  54. package/build/dex/pancakeswap-v3/contract-math/TickBitMap.js.map +1 -0
  55. package/build/dex/pancakeswap-v3/contract-math/TickMath.d.ts +8 -0
  56. package/build/dex/pancakeswap-v3/contract-math/TickMath.js +162 -0
  57. package/build/dex/pancakeswap-v3/contract-math/TickMath.js.map +1 -0
  58. package/build/dex/pancakeswap-v3/contract-math/UnsafeMath.d.ts +3 -0
  59. package/build/dex/pancakeswap-v3/contract-math/UnsafeMath.js +10 -0
  60. package/build/dex/pancakeswap-v3/contract-math/UnsafeMath.js.map +1 -0
  61. package/build/dex/pancakeswap-v3/contract-math/pancakeswap-v3-math.d.ts +18 -0
  62. package/build/dex/pancakeswap-v3/contract-math/pancakeswap-v3-math.js +407 -0
  63. package/build/dex/pancakeswap-v3/contract-math/pancakeswap-v3-math.js.map +1 -0
  64. package/build/dex/pancakeswap-v3/contract-math/utils.d.ts +3 -0
  65. package/build/dex/pancakeswap-v3/contract-math/utils.js +16 -0
  66. package/build/dex/pancakeswap-v3/contract-math/utils.js.map +1 -0
  67. package/build/dex/pancakeswap-v3/pancakeswap-v3-pool.d.ts +45 -0
  68. package/build/dex/pancakeswap-v3/pancakeswap-v3-pool.js +340 -0
  69. package/build/dex/pancakeswap-v3/pancakeswap-v3-pool.js.map +1 -0
  70. package/build/dex/pancakeswap-v3/pancakeswap-v3.d.ts +55 -0
  71. package/build/dex/pancakeswap-v3/pancakeswap-v3.js +605 -0
  72. package/build/dex/pancakeswap-v3/pancakeswap-v3.js.map +1 -0
  73. package/build/dex/pancakeswap-v3/types.d.ts +14 -0
  74. package/build/dex/pancakeswap-v3/types.js +3 -0
  75. package/build/dex/pancakeswap-v3/types.js.map +1 -0
  76. package/build/dex/pancakeswap-v3/utils.d.ts +4 -0
  77. package/build/dex/pancakeswap-v3/utils.js +57 -0
  78. package/build/dex/pancakeswap-v3/utils.js.map +1 -0
  79. package/build/dex/uniswap-v3/config.js +4 -32
  80. package/build/dex/uniswap-v3/config.js.map +1 -1
  81. package/build/dex/uniswap-v3/uniswap-v3-pool.d.ts +1 -2
  82. package/build/dex/uniswap-v3/uniswap-v3-pool.js +2 -3
  83. package/build/dex/uniswap-v3/uniswap-v3-pool.js.map +1 -1
  84. package/build/dex/uniswap-v3/uniswap-v3.js +15 -11
  85. package/build/dex/uniswap-v3/uniswap-v3.js.map +1 -1
  86. package/package.json +1 -1
  87. package/src/abi/pancakeswap-v3/PancakeV3StateMulticall.abi.json +781 -0
  88. package/src/abi/pancakeswap-v3/PancakeswapV3Pool.abi.json +1039 -0
  89. package/src/dex/balancer-v2/balancer-v2-e2e.test.ts +13 -19
  90. package/src/dex/balancer-v2/balancer-v2-integration.test.ts +47 -36
  91. package/src/dex/balancer-v2/balancer-v2.ts +46 -24
  92. package/src/dex/balancer-v2/config.ts +1 -1
  93. package/src/dex/balancer-v2/pools/linear/LinearMath.ts +2 -2
  94. package/src/dex/balancer-v2/pools/linear/LinearPool.ts +1 -1
  95. package/src/dex/balancer-v2/pools/phantom-stable/PhantomStableMath.ts +19 -19
  96. package/src/dex/balancer-v2/pools/phantom-stable/PhantomStablePool.ts +0 -1
  97. package/src/dex/balancer-v2/pools/stable/StableMath.ts +13 -14
  98. package/src/dex/balancer-v2/pools/stable/StablePool.ts +4 -2
  99. package/src/dex/balancer-v2/pools/weighted/WeightedPool.ts +0 -1
  100. package/src/dex/index.ts +1 -1
  101. package/src/dex/pancakeswap-v3/config.ts +68 -0
  102. package/src/dex/pancakeswap-v3/constants.ts +35 -0
  103. package/src/dex/pancakeswap-v3/contract-math/BitMath.ts +90 -0
  104. package/src/dex/pancakeswap-v3/contract-math/FixedPoint128.ts +3 -0
  105. package/src/dex/pancakeswap-v3/contract-math/FixedPoint96.ts +4 -0
  106. package/src/dex/pancakeswap-v3/contract-math/FullMath.ts +30 -0
  107. package/src/dex/pancakeswap-v3/contract-math/LiquidityMath.ts +17 -0
  108. package/src/dex/pancakeswap-v3/contract-math/Oracle.ts +226 -0
  109. package/src/dex/pancakeswap-v3/contract-math/SqrtPriceMath.ts +226 -0
  110. package/src/dex/pancakeswap-v3/contract-math/SwapMath.ts +139 -0
  111. package/src/dex/pancakeswap-v3/contract-math/Tick.ts +82 -0
  112. package/src/dex/pancakeswap-v3/contract-math/TickBitMap.ts +128 -0
  113. package/src/dex/pancakeswap-v3/contract-math/TickMath.ts +211 -0
  114. package/src/dex/pancakeswap-v3/contract-math/UnsafeMath.ts +5 -0
  115. package/src/dex/pancakeswap-v3/contract-math/pancakeswap-v3-math.ts +691 -0
  116. package/src/dex/pancakeswap-v3/contract-math/utils.ts +11 -0
  117. package/src/dex/pancakeswap-v3/pancakeswap-v3-e2e.test.ts +235 -0
  118. package/src/dex/pancakeswap-v3/pancakeswap-v3-events.test.ts +124 -0
  119. package/src/dex/pancakeswap-v3/pancakeswap-v3-integration.test.ts +540 -0
  120. package/src/dex/pancakeswap-v3/pancakeswap-v3-pool.ts +554 -0
  121. package/src/dex/pancakeswap-v3/pancakeswap-v3.ts +1000 -0
  122. package/src/dex/pancakeswap-v3/utils.ts +68 -0
  123. package/src/dex/uniswap-v3/config.ts +4 -36
  124. package/src/dex/uniswap-v3/uniswap-v3-e2e.test.ts +0 -156
  125. package/src/dex/uniswap-v3/uniswap-v3-events.test.ts +4 -0
  126. package/src/dex/uniswap-v3/uniswap-v3-pool.ts +1 -2
  127. package/src/dex/uniswap-v3/uniswap-v3.ts +8 -5
  128. package/tests/constants-e2e.ts +1 -1
  129. package/src/dex/uniswap-v3/forks/pancakeswap-v3/pancakeswap-v3.ts +0 -28
  130. /package/src/abi/{uniswap-v3/pancakeswap-v3/PancakeswapV3QuoterV2.abi.json → pancakeswap-v3/PancakeswapV3Quoter.abi.json} +0 -0
  131. /package/src/abi/{uniswap-v3/pancakeswap-v3 → pancakeswap-v3}/PancakeswapV3Router.abi.json +0 -0
@@ -0,0 +1,540 @@
1
+ /* eslint-disable no-console */
2
+ import dotenv from 'dotenv';
3
+ dotenv.config();
4
+
5
+ import { Interface, Result } from '@ethersproject/abi';
6
+ import { DummyDexHelper } from '../../dex-helper/index';
7
+ import { Network, SwapSide } from '../../constants';
8
+ import { BI_POWS } from '../../bigint-constants';
9
+ import { checkPoolPrices, checkPoolsLiquidity } from '../../../tests/utils';
10
+ import { Tokens } from '../../../tests/constants-e2e';
11
+ import PancakeswapV3QuoterABI from '../../abi/pancakeswap-v3/PancakeswapV3Quoter.abi.json';
12
+ import { Address } from '@paraswap/core';
13
+ import { PancakeswapV3 } from './pancakeswap-v3';
14
+ import * as net from 'net';
15
+
16
+ const networks = [Network.MAINNET, Network.BSC];
17
+
18
+ const dexKey = 'PancakeswapV3';
19
+
20
+ const quoterIface = new Interface(PancakeswapV3QuoterABI);
21
+
22
+ const testingData: Partial<{ [key in Network]: any }> = {
23
+ [Network.MAINNET]: {
24
+ tokenA: Tokens[Network.MAINNET]['WBTC'],
25
+ tokenASymbol: 'WBTC',
26
+ stableA: Tokens[Network.MAINNET]['USDC'],
27
+ stableASymbol: 'USDC',
28
+ tokenB: Tokens[Network.MAINNET]['WETH'],
29
+ tokenBSymbol: 'WETH',
30
+ stableB: Tokens[Network.MAINNET]['USDT'],
31
+ stableBSymbol: 'USDT',
32
+ stableSellAmounts: [
33
+ 0n,
34
+ 10_000n * BI_POWS[6],
35
+ 20_000n * BI_POWS[6],
36
+ 30_000n * BI_POWS[6],
37
+ ],
38
+ stableBuyAmounts: [0n, 1n * BI_POWS[6], 2n * BI_POWS[6], 3n * BI_POWS[6]],
39
+ regularSellAmounts: [0n, 1n * BI_POWS[8], 2n * BI_POWS[8], 3n * BI_POWS[8]],
40
+ regularBuyAmounts: [
41
+ 0n,
42
+ 1n * BI_POWS[18],
43
+ 2n * BI_POWS[18],
44
+ 3n * BI_POWS[18],
45
+ ],
46
+ },
47
+ [Network.BSC]: {
48
+ tokenA: Tokens[Network.BSC]['bBTC'],
49
+ tokenASymbol: 'bBTC',
50
+ stableA: Tokens[Network.BSC]['USDC'],
51
+ stableASymbol: 'USDC',
52
+ tokenB: Tokens[Network.BSC]['WBNB'],
53
+ tokenBSymbol: 'WBNB',
54
+ stableB: Tokens[Network.BSC]['USDT'],
55
+ stableBSymbol: 'USDT',
56
+ stableSellAmounts: [
57
+ 0n,
58
+ 10_000n * BI_POWS[6],
59
+ 20_000n * BI_POWS[6],
60
+ 30_000n * BI_POWS[6],
61
+ ],
62
+ stableBuyAmounts: [0n, 1n * BI_POWS[6], 2n * BI_POWS[6], 3n * BI_POWS[6]],
63
+ regularSellAmounts: [
64
+ 0n,
65
+ 1n * BI_POWS[18],
66
+ 2n * BI_POWS[18],
67
+ 3n * BI_POWS[18],
68
+ ],
69
+ regularBuyAmounts: [
70
+ 0n,
71
+ 1n * BI_POWS[18],
72
+ 2n * BI_POWS[18],
73
+ 3n * BI_POWS[18],
74
+ ],
75
+ },
76
+ };
77
+
78
+ describe('PancakeswapV3', function () {
79
+
80
+ describe('BSC', () => {
81
+ describe('WBNB -> USDT', () => {
82
+ it('getPoolIdentifiers and getPricesVolume SELL', async function () {
83
+ const network = Network.BSC;
84
+ const dexHelper = new DummyDexHelper(network);
85
+ const blockNumber = await dexHelper.web3Provider.eth.getBlockNumber();
86
+ const pancakeswapV3 = new PancakeswapV3(network, dexKey, dexHelper);
87
+
88
+ const WBNB = Tokens[network]['WBNB'];
89
+ const USDT = Tokens[network]['USDT'];
90
+
91
+ const amounts = [
92
+ 0n,
93
+ 1n * BI_POWS[18],
94
+ 2n * BI_POWS[18],
95
+ ];
96
+
97
+ const pools = await pancakeswapV3.getPoolIdentifiers(
98
+ WBNB,
99
+ USDT,
100
+ SwapSide.SELL,
101
+ blockNumber,
102
+ );
103
+ console.log(
104
+ `WBNB <> USDT Pool Identifiers: `,
105
+ pools,
106
+ );
107
+
108
+ expect(pools.length).toBeGreaterThan(0);
109
+
110
+ const poolPrices = await pancakeswapV3.getPricesVolume(
111
+ WBNB,
112
+ USDT,
113
+ amounts,
114
+ SwapSide.SELL,
115
+ blockNumber,
116
+ pools,
117
+ );
118
+ console.log(
119
+ `WBNB <> USDT Pool Prices: `,
120
+ poolPrices,
121
+ );
122
+
123
+ expect(poolPrices).not.toBeNull();
124
+ checkPoolPrices(
125
+ poolPrices!.filter(pp => pp.unit !== 0n),
126
+ amounts,
127
+ SwapSide.SELL,
128
+ dexKey,
129
+ );
130
+
131
+ // Check if onchain pricing equals to calculated ones
132
+ let falseChecksCounter = 0;
133
+ await Promise.all(
134
+ poolPrices!.map(async price => {
135
+ const fee =
136
+ pancakeswapV3.eventPools[price.poolIdentifier!]!.feeCode;
137
+ const res = await checkOnChainPricing(
138
+ pancakeswapV3,
139
+ dexHelper,
140
+ 'quoteExactInputSingle',
141
+ blockNumber,
142
+ price.prices,
143
+ WBNB.address,
144
+ USDT.address,
145
+ fee,
146
+ amounts,
147
+ );
148
+ if (res === false) falseChecksCounter++;
149
+ }),
150
+ );
151
+ expect(falseChecksCounter).toBeLessThan(poolPrices!.length);
152
+ });
153
+ });
154
+ });
155
+
156
+ networks.forEach(network =>
157
+ describe(network, function () {
158
+ let blockNumber: number;
159
+ let pancakeswapV3: PancakeswapV3;
160
+ const testData = testingData[network];
161
+ if (testData) {
162
+ const {
163
+ stableA,
164
+ stableASymbol,
165
+ stableB,
166
+ stableBSymbol,
167
+ stableSellAmounts,
168
+ stableBuyAmounts,
169
+ tokenA,
170
+ tokenASymbol,
171
+ tokenB,
172
+ tokenBSymbol,
173
+ regularSellAmounts,
174
+ regularBuyAmounts,
175
+ } = testData;
176
+ const dexHelper = new DummyDexHelper(network);
177
+
178
+ beforeEach(async () => {
179
+ blockNumber = await dexHelper.web3Provider.eth.getBlockNumber();
180
+ pancakeswapV3 = new PancakeswapV3(network, dexKey, dexHelper);
181
+ });
182
+
183
+ describe('Stable pairs', function () {
184
+ it('getPoolIdentifiers and getPricesVolume SELL stable pairs', async function () {
185
+ const pools = await pancakeswapV3.getPoolIdentifiers(
186
+ stableA,
187
+ stableB,
188
+ SwapSide.SELL,
189
+ blockNumber,
190
+ );
191
+ console.log(
192
+ `${stableASymbol} <> ${stableBSymbol} Pool Identifiers: `,
193
+ pools,
194
+ );
195
+
196
+ expect(pools.length).toBeGreaterThan(0);
197
+
198
+ const poolPrices = await pancakeswapV3.getPricesVolume(
199
+ stableA,
200
+ stableB,
201
+ stableSellAmounts,
202
+ SwapSide.SELL,
203
+ blockNumber,
204
+ pools,
205
+ );
206
+ console.log(
207
+ `${stableASymbol} <> ${stableBSymbol} Pool Prices: `,
208
+ poolPrices,
209
+ );
210
+
211
+ expect(poolPrices).not.toBeNull();
212
+ checkPoolPrices(
213
+ poolPrices!.filter(pp => pp.unit !== 0n),
214
+ stableSellAmounts,
215
+ SwapSide.SELL,
216
+ dexKey,
217
+ );
218
+
219
+ // Check if onchain pricing equals to calculated ones
220
+ let falseChecksCounter = 0;
221
+ await Promise.all(
222
+ poolPrices!.map(async price => {
223
+ const fee =
224
+ pancakeswapV3.eventPools[price.poolIdentifier!]!.feeCode;
225
+ const res = await checkOnChainPricing(
226
+ pancakeswapV3,
227
+ dexHelper,
228
+ 'quoteExactInputSingle',
229
+ blockNumber,
230
+ price.prices,
231
+ stableA.address,
232
+ stableB.address,
233
+ fee,
234
+ stableSellAmounts,
235
+ );
236
+ if (res === false) falseChecksCounter++;
237
+ }),
238
+ );
239
+ expect(falseChecksCounter).toBeLessThan(poolPrices!.length);
240
+ });
241
+
242
+ it('getPoolIdentifiers and getPricesVolume BUY stable pairs', async function () {
243
+ const pools = await pancakeswapV3.getPoolIdentifiers(
244
+ stableA,
245
+ stableB,
246
+ SwapSide.BUY,
247
+ blockNumber,
248
+ );
249
+ console.log(
250
+ `${stableASymbol} <> ${stableBSymbol} Pool Identifiers: `,
251
+ pools,
252
+ );
253
+
254
+ expect(pools.length).toBeGreaterThan(0);
255
+
256
+ const poolPrices = await pancakeswapV3.getPricesVolume(
257
+ stableA,
258
+ stableB,
259
+ stableBuyAmounts,
260
+ SwapSide.BUY,
261
+ blockNumber,
262
+ pools,
263
+ );
264
+ console.log(
265
+ `${stableASymbol} <> ${stableBSymbol} Pool Prices: `,
266
+ poolPrices,
267
+ );
268
+
269
+ expect(poolPrices).not.toBeNull();
270
+ checkPoolPrices(
271
+ poolPrices!.filter(pp => pp.unit !== 0n),
272
+ stableBuyAmounts,
273
+ SwapSide.BUY,
274
+ dexKey,
275
+ );
276
+
277
+ // Check if onchain pricing equals to calculated ones
278
+ let falseChecksCounter = 0;
279
+ await Promise.all(
280
+ poolPrices!.map(async price => {
281
+ const fee =
282
+ pancakeswapV3.eventPools[price.poolIdentifier!]!.feeCode;
283
+ const res = await checkOnChainPricing(
284
+ pancakeswapV3,
285
+ dexHelper,
286
+ 'quoteExactOutputSingle',
287
+ blockNumber,
288
+ price.prices,
289
+ stableA.address,
290
+ stableB.address,
291
+ fee,
292
+ stableBuyAmounts,
293
+ );
294
+ if (res === false) falseChecksCounter++;
295
+ }),
296
+ );
297
+ expect(falseChecksCounter).toBeLessThan(poolPrices!.length);
298
+ });
299
+
300
+ it('getTopPoolsForToken', async function () {
301
+ const poolLiquidity = await pancakeswapV3.getTopPoolsForToken(
302
+ stableA.address,
303
+ 10,
304
+ );
305
+ console.log(`${stableASymbol} Top Pools:`, poolLiquidity);
306
+
307
+ if (!pancakeswapV3.hasConstantPriceLargeAmounts) {
308
+ checkPoolsLiquidity(poolLiquidity, stableA.address, dexKey);
309
+ }
310
+ });
311
+ });
312
+
313
+ describe('Regular pairs', function () {
314
+ it('getPoolIdentifiers and getPricesVolume SELL', async function () {
315
+ const pools = await pancakeswapV3.getPoolIdentifiers(
316
+ tokenA,
317
+ tokenB,
318
+ SwapSide.SELL,
319
+ blockNumber,
320
+ );
321
+ console.log(
322
+ `${tokenASymbol} <> ${tokenBSymbol} Pool Identifiers: `,
323
+ pools,
324
+ );
325
+
326
+ expect(pools.length).toBeGreaterThan(0);
327
+
328
+ const poolPrices = await pancakeswapV3.getPricesVolume(
329
+ tokenA,
330
+ tokenB,
331
+ regularSellAmounts,
332
+ SwapSide.SELL,
333
+ blockNumber,
334
+ pools,
335
+ );
336
+ console.log(
337
+ `${tokenASymbol} <> ${tokenBSymbol} Pool Prices: `,
338
+ poolPrices,
339
+ );
340
+
341
+ expect(poolPrices).not.toBeNull();
342
+ checkPoolPrices(
343
+ poolPrices!,
344
+ regularSellAmounts,
345
+ SwapSide.SELL,
346
+ dexKey,
347
+ );
348
+
349
+ let falseChecksCounter = 0;
350
+ await Promise.all(
351
+ poolPrices!.map(async price => {
352
+ const fee =
353
+ pancakeswapV3.eventPools[price.poolIdentifier!]!.feeCode;
354
+ const res = await checkOnChainPricing(
355
+ pancakeswapV3,
356
+ dexHelper,
357
+ 'quoteExactInputSingle',
358
+ blockNumber,
359
+ price.prices,
360
+ tokenA.address,
361
+ tokenB.address,
362
+ fee,
363
+ regularSellAmounts,
364
+ );
365
+ if (res === false) falseChecksCounter++;
366
+ }),
367
+ );
368
+
369
+ expect(falseChecksCounter).toBeLessThan(poolPrices!.length);
370
+ });
371
+
372
+ it('getPoolIdentifiers and getPricesVolume BUY', async function () {
373
+ const pools = await pancakeswapV3.getPoolIdentifiers(
374
+ tokenA,
375
+ tokenB,
376
+ SwapSide.BUY,
377
+ blockNumber,
378
+ );
379
+ console.log(
380
+ `${stableASymbol} <> ${stableBSymbol} Pool Identifiers: `,
381
+ pools,
382
+ );
383
+
384
+ expect(pools.length).toBeGreaterThan(0);
385
+
386
+ const poolPrices = await pancakeswapV3.getPricesVolume(
387
+ tokenA,
388
+ tokenB,
389
+ regularBuyAmounts,
390
+ SwapSide.BUY,
391
+ blockNumber,
392
+ pools,
393
+ );
394
+ console.log(
395
+ `${tokenASymbol} <> ${tokenBSymbol} Pool Prices: `,
396
+ poolPrices,
397
+ );
398
+
399
+ expect(poolPrices).not.toBeNull();
400
+ checkPoolPrices(
401
+ poolPrices!,
402
+ regularBuyAmounts,
403
+ SwapSide.BUY,
404
+ dexKey,
405
+ );
406
+
407
+ // Check if onchain pricing equals to calculated ones
408
+ let falseChecksCounter = 0;
409
+ await Promise.all(
410
+ poolPrices!.map(async price => {
411
+ const fee =
412
+ pancakeswapV3.eventPools[price.poolIdentifier!]!.feeCode;
413
+ const res = await checkOnChainPricing(
414
+ pancakeswapV3,
415
+ dexHelper,
416
+ 'quoteExactOutputSingle',
417
+ blockNumber,
418
+ price.prices,
419
+ tokenA.address,
420
+ tokenB.address,
421
+ fee,
422
+ regularBuyAmounts,
423
+ );
424
+ if (res === false) falseChecksCounter++;
425
+ }),
426
+ );
427
+ expect(falseChecksCounter).toBeLessThan(poolPrices!.length);
428
+ });
429
+
430
+ it('getTopPoolsForToken', async function () {
431
+ const poolLiquidity = await pancakeswapV3.getTopPoolsForToken(
432
+ tokenA.address,
433
+ 10,
434
+ );
435
+ console.log(`${stableASymbol} Top Pools:`, poolLiquidity);
436
+
437
+ if (!pancakeswapV3.hasConstantPriceLargeAmounts) {
438
+ checkPoolsLiquidity(poolLiquidity, stableA.address, dexKey);
439
+ }
440
+ });
441
+ });
442
+ }
443
+ }),
444
+ );
445
+ });
446
+
447
+ function getReaderCalldata(
448
+ exchangeAddress: string,
449
+ readerIface: Interface,
450
+ amounts: bigint[],
451
+ funcName: string,
452
+ tokenIn: Address,
453
+ tokenOut: Address,
454
+ fee: bigint,
455
+ ) {
456
+ return amounts.map(amount => ({
457
+ target: exchangeAddress,
458
+ callData: readerIface.encodeFunctionData(funcName, [
459
+ [tokenIn, tokenOut, amount, fee, 0n],
460
+ ]),
461
+ }));
462
+ }
463
+
464
+ function decodeReaderResult(
465
+ results: Result,
466
+ readerIface: Interface,
467
+ funcName: string,
468
+ ) {
469
+ return results.map(result => {
470
+ const parsed = readerIface.decodeFunctionResult(funcName, result);
471
+ return BigInt(parsed[0]._hex);
472
+ });
473
+ }
474
+
475
+ async function checkOnChainPricing(
476
+ pancakeswapV3: PancakeswapV3,
477
+ dexHelper: DummyDexHelper,
478
+ funcName: string,
479
+ blockNumber: number,
480
+ prices: bigint[],
481
+ tokenIn: Address,
482
+ tokenOut: Address,
483
+ fee: bigint,
484
+ _amounts: bigint[],
485
+ ) {
486
+ // Quoter address
487
+ const exchangeAddress = '0xB048Bbc1Ee6b733FFfCFb9e9CeF7375518e25997';
488
+ const readerIface = quoterIface;
489
+
490
+ const sum = prices.reduce((acc, curr) => (acc += curr), 0n);
491
+
492
+ if (sum === 0n) {
493
+ console.log(
494
+ `Prices were not calculated for tokenIn=${tokenIn}, tokenOut=${tokenOut}, fee=${fee.toString()}. Most likely price impact is too big for requested amount`,
495
+ );
496
+ return false;
497
+ }
498
+
499
+ const readerCallData = getReaderCalldata(
500
+ exchangeAddress,
501
+ readerIface,
502
+ _amounts.slice(1),
503
+ funcName,
504
+ tokenIn,
505
+ tokenOut,
506
+ fee,
507
+ );
508
+
509
+ let readerResult;
510
+ try {
511
+ readerResult = (
512
+ await dexHelper.multiContract.methods
513
+ .aggregate(readerCallData)
514
+ .call({}, blockNumber)
515
+ ).returnData;
516
+ } catch (e) {
517
+ console.log(
518
+ `Can not fetch on-chain pricing for fee ${fee}. It happens for low liquidity pools`,
519
+ );
520
+ return false;
521
+ }
522
+
523
+ const expectedPrices = [0n].concat(
524
+ decodeReaderResult(readerResult, readerIface, funcName),
525
+ );
526
+
527
+ let firstZeroIndex = prices.slice(1).indexOf(0n);
528
+
529
+ // we skipped first, so add +1 on result
530
+ firstZeroIndex = firstZeroIndex === -1 ? prices.length : firstZeroIndex;
531
+
532
+ console.log('PRICE: ', prices);
533
+ console.log('ON-chain prices: ', prices);
534
+
535
+ // Compare only the ones for which we were able to calculate prices
536
+ expect(prices.slice(0, firstZeroIndex)).toEqual(
537
+ expectedPrices.slice(0, firstZeroIndex),
538
+ );
539
+ return true;
540
+ }