@paraswap/dex-lib 4.1.3 → 4.1.4-bal-v3-hooks.0

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 (56) hide show
  1. package/build/abi/balancer-v3/directionalFeeHook.json +684 -0
  2. package/build/abi/balancer-v3/stableSurgeHook.json +976 -0
  3. package/build/dex/balancer-v3/balancer-test-helpers.d.ts +5 -0
  4. package/build/dex/balancer-v3/balancer-test-helpers.js +122 -0
  5. package/build/dex/balancer-v3/balancer-test-helpers.js.map +1 -0
  6. package/build/dex/balancer-v3/balancer-v3-pool.d.ts +4 -1
  7. package/build/dex/balancer-v3/balancer-v3-pool.js +20 -13
  8. package/build/dex/balancer-v3/balancer-v3-pool.js.map +1 -1
  9. package/build/dex/balancer-v3/balancer-v3.d.ts +4 -0
  10. package/build/dex/balancer-v3/balancer-v3.js +29 -3
  11. package/build/dex/balancer-v3/balancer-v3.js.map +1 -1
  12. package/build/dex/balancer-v3/config.js +39 -0
  13. package/build/dex/balancer-v3/config.js.map +1 -1
  14. package/build/dex/balancer-v3/getOnChainState.d.ts +1 -2
  15. package/build/dex/balancer-v3/getOnChainState.js +0 -2
  16. package/build/dex/balancer-v3/getOnChainState.js.map +1 -1
  17. package/build/dex/balancer-v3/getPoolsApi.d.ts +2 -1
  18. package/build/dex/balancer-v3/getPoolsApi.js +17 -10
  19. package/build/dex/balancer-v3/getPoolsApi.js.map +1 -1
  20. package/build/dex/balancer-v3/getTopPoolsApi.d.ts +5 -1
  21. package/build/dex/balancer-v3/getTopPoolsApi.js +6 -3
  22. package/build/dex/balancer-v3/getTopPoolsApi.js.map +1 -1
  23. package/build/dex/balancer-v3/hooks/balancer-hook-event-subscriber.d.ts +54 -0
  24. package/build/dex/balancer-v3/hooks/balancer-hook-event-subscriber.js +153 -0
  25. package/build/dex/balancer-v3/hooks/balancer-hook-event-subscriber.js.map +1 -0
  26. package/build/dex/balancer-v3/hooks/directionalFeeHook.d.ts +26 -0
  27. package/build/dex/balancer-v3/hooks/directionalFeeHook.js +41 -0
  28. package/build/dex/balancer-v3/hooks/directionalFeeHook.js.map +1 -0
  29. package/build/dex/balancer-v3/hooks/loadHooksConfig.d.ts +2 -0
  30. package/build/dex/balancer-v3/hooks/loadHooksConfig.js +11 -0
  31. package/build/dex/balancer-v3/hooks/loadHooksConfig.js.map +1 -0
  32. package/build/dex/balancer-v3/hooks/stableSurgeHook.d.ts +39 -0
  33. package/build/dex/balancer-v3/hooks/stableSurgeHook.js +99 -0
  34. package/build/dex/balancer-v3/hooks/stableSurgeHook.js.map +1 -0
  35. package/build/dex/balancer-v3/types.d.ts +4 -1
  36. package/package.json +2 -2
  37. package/scripts/run-dex-integration-v3.ts +4 -4
  38. package/src/abi/balancer-v3/directionalFeeHook.json +684 -0
  39. package/src/abi/balancer-v3/stableSurgeHook.json +976 -0
  40. package/src/dex/balancer-v3/balancer-hooks-events.test.ts +99 -0
  41. package/src/dex/balancer-v3/balancer-stableSurge.test.ts +110 -0
  42. package/src/dex/balancer-v3/balancer-test-helpers.ts +199 -0
  43. package/src/dex/balancer-v3/balancer-v3-e2e.test.ts +20 -0
  44. package/src/dex/balancer-v3/balancer-v3-integration.test.ts +2 -197
  45. package/src/dex/balancer-v3/balancer-v3-pool.ts +37 -13
  46. package/src/dex/balancer-v3/balancer-v3.ts +42 -1
  47. package/src/dex/balancer-v3/config.ts +39 -0
  48. package/src/dex/balancer-v3/getOnChainState.ts +1 -4
  49. package/src/dex/balancer-v3/getPoolsApi.ts +42 -23
  50. package/src/dex/balancer-v3/getTopPoolsApi.ts +13 -2
  51. package/src/dex/balancer-v3/hooks/balancer-hook-event-subscriber.ts +226 -0
  52. package/src/dex/balancer-v3/hooks/directionalFeeHook.ts +51 -0
  53. package/src/dex/balancer-v3/hooks/loadHooksConfig.ts +12 -0
  54. package/src/dex/balancer-v3/hooks/stableSurgeHook.ts +163 -0
  55. package/src/dex/balancer-v3/types.ts +4 -2
  56. package/tests/constants-e2e.ts +6 -1
@@ -0,0 +1,99 @@
1
+ // npx jest src/dex/balancer-v3/balancer-hooks-events.test.ts
2
+ /* eslint-disable no-console */
3
+ import dotenv from 'dotenv';
4
+ dotenv.config();
5
+ import _ from 'lodash';
6
+ import { Network } from '../../constants';
7
+ import { Address } from '../../types';
8
+ import { DummyDexHelper } from '../../dex-helper/index';
9
+ import { testEventSubscriber } from '../../../tests/utils-events';
10
+ import {
11
+ BalancerEventHook,
12
+ HookStateMap,
13
+ } from './hooks/balancer-hook-event-subscriber';
14
+
15
+ jest.setTimeout(50 * 1000);
16
+
17
+ async function fetchHookState(
18
+ balancerEventHook: BalancerEventHook,
19
+ blockNumber: number,
20
+ hookAddress: string,
21
+ ): Promise<HookStateMap> {
22
+ const hookStateMap = await balancerEventHook.generateState(blockNumber);
23
+ // Filter to hook of interest
24
+ return Object.entries(_.cloneDeep(hookStateMap) as HookStateMap)
25
+ .filter(([address]) => {
26
+ return address.toLowerCase() === hookAddress.toLowerCase();
27
+ })
28
+ .reduce((acc, [address, pool]) => {
29
+ acc[address] = pool;
30
+ return acc;
31
+ }, {} as HookStateMap);
32
+ }
33
+
34
+ // eventName -> blockNumbers
35
+ type EventMappings = Record<string, EventData>;
36
+ type EventData = { blockNumbers: number[]; hookAddress: string[] };
37
+
38
+ describe('BalancerEventHook', function () {
39
+ const dexKey = 'BalancerV3';
40
+ const network = Network.SEPOLIA;
41
+ const dexHelper = new DummyDexHelper(network);
42
+ const logger = dexHelper.getLogger(dexKey);
43
+ let balancerEventHook: BalancerEventHook;
44
+
45
+ // hook -> EventMappings
46
+ const eventsToTest: Record<Address, EventMappings> = {
47
+ ['StableSurgeHook']: {
48
+ // https://sepolia.etherscan.io/tx/0xa8d363d9c78b7b66e6f93c4417de67e636c13691437aecf85a631987b200abd8
49
+ ThresholdSurgePercentageChanged: {
50
+ blockNumbers: [7596856],
51
+ hookAddress: ['0xC0cbcdD6b823A4f22aA6BbDDe44C17e754266AEF'],
52
+ },
53
+ // https://sepolia.etherscan.io/tx/0xd1a9e40ed3c3d32bf8f2264daa4c66879ac5eae1f4029ae9993badd653b26bad
54
+ MaxSurgeFeePercentageChanged: {
55
+ blockNumbers: [7596855],
56
+ hookAddress: ['0xC0cbcdD6b823A4f22aA6BbDDe44C17e754266AEF'],
57
+ },
58
+ },
59
+ };
60
+
61
+ beforeEach(async () => {
62
+ balancerEventHook = new BalancerEventHook(
63
+ dexKey,
64
+ network,
65
+ dexHelper,
66
+ logger,
67
+ );
68
+ });
69
+
70
+ Object.entries(eventsToTest).forEach(
71
+ ([hookAddress, events]: [string, EventMappings]) => {
72
+ describe(`Events for Hook: ${hookAddress}`, () => {
73
+ Object.entries(events).forEach(
74
+ ([eventName, eventData]: [string, EventData]) => {
75
+ describe(`${eventName}`, () => {
76
+ eventData.blockNumbers.forEach((blockNumber: number, i) => {
77
+ it(`Hook: ${eventData.hookAddress[i]} State after ${blockNumber}`, async function () {
78
+ await testEventSubscriber(
79
+ balancerEventHook,
80
+ balancerEventHook.addressesSubscribed,
81
+ (_blockNumber: number) =>
82
+ fetchHookState(
83
+ balancerEventHook,
84
+ _blockNumber,
85
+ eventData.hookAddress[i],
86
+ ),
87
+ blockNumber,
88
+ `${dexKey}_${hookAddress}`,
89
+ dexHelper.provider,
90
+ );
91
+ });
92
+ });
93
+ });
94
+ },
95
+ );
96
+ });
97
+ },
98
+ );
99
+ });
@@ -0,0 +1,110 @@
1
+ // npx jest src/dex/balancer-v3/balancer-stableSurge.test.ts
2
+ import dotenv from 'dotenv';
3
+ dotenv.config();
4
+ import { Tokens } from '../../../tests/constants-e2e';
5
+ import { Network, SwapSide } from '../../constants';
6
+ import { DummyDexHelper } from '../../dex-helper';
7
+ import { BalancerV3 } from './balancer-v3';
8
+ import { checkOnChainPricingNonMulti } from './balancer-test-helpers';
9
+ import { ExchangePrices, Token } from '../../types';
10
+ import { BalancerV3Data } from './types';
11
+
12
+ const dexKey = 'BalancerV3';
13
+ const blockNumber = 7651340;
14
+ let balancerV3: BalancerV3;
15
+ const network = Network.SEPOLIA;
16
+ const dexHelper = new DummyDexHelper(network);
17
+ const tokens = Tokens[network];
18
+ const bal = tokens['bal'];
19
+ const weth = tokens['WETH'];
20
+ // https://sepolia.etherscan.io/address/0x132F4bAa39330d9062fC52d81dF72F601DF8C01f
21
+ const stableSurgePool =
22
+ '0x132F4bAa39330d9062fC52d81dF72F601DF8C01f'.toLowerCase();
23
+
24
+ describe('BalancerV3 stableSurge hook tests', function () {
25
+ beforeAll(async () => {
26
+ balancerV3 = new BalancerV3(network, dexKey, dexHelper);
27
+ if (balancerV3.initializePricing) {
28
+ await balancerV3.initializePricing(blockNumber);
29
+ }
30
+ });
31
+
32
+ describe('pool with stableSurge hook should be returned', function () {
33
+ it('getPoolIdentifiers', async function () {
34
+ const pools = await balancerV3.getPoolIdentifiers(
35
+ bal,
36
+ weth,
37
+ SwapSide.SELL,
38
+ blockNumber,
39
+ );
40
+ expect(pools.some(pool => pool === stableSurgePool)).toBe(true);
41
+ });
42
+
43
+ it('getTopPoolsForToken', async function () {
44
+ const pools = await balancerV3.getTopPoolsForToken(weth.address, 10);
45
+ expect(pools.some(pool => pool.address === stableSurgePool)).toBe(true);
46
+ });
47
+ });
48
+
49
+ describe('should match onchain pricing', function () {
50
+ describe('using staticFee', function () {
51
+ it('SELL', async function () {
52
+ const amounts = [0n, 10000000000000000n];
53
+ const side = SwapSide.SELL;
54
+ await testPricesVsOnchain(amounts, weth, bal, side, blockNumber, [
55
+ stableSurgePool,
56
+ ]);
57
+ });
58
+ it('BUY', async function () {
59
+ const amounts = [0n, 500000000000000000n];
60
+ const side = SwapSide.BUY;
61
+ await testPricesVsOnchain(amounts, weth, bal, side, blockNumber, [
62
+ stableSurgePool,
63
+ ]);
64
+ });
65
+ });
66
+ describe('using surge fee', function () {
67
+ it('SELL', async function () {
68
+ const amounts = [0n, 8000000000000000000n];
69
+ const side = SwapSide.SELL;
70
+ await testPricesVsOnchain(amounts, bal, weth, side, blockNumber, [
71
+ stableSurgePool,
72
+ ]);
73
+ });
74
+ it('BUY', async function () {
75
+ const amounts = [0n, 4000000000000000n];
76
+ const side = SwapSide.BUY;
77
+ await testPricesVsOnchain(amounts, bal, weth, side, blockNumber, [
78
+ stableSurgePool,
79
+ ]);
80
+ });
81
+ });
82
+ });
83
+ });
84
+
85
+ async function testPricesVsOnchain(
86
+ amounts: bigint[],
87
+ srcToken: Token,
88
+ dstToken: Token,
89
+ side: SwapSide,
90
+ blockNumber: number,
91
+ limitPools: string[],
92
+ ) {
93
+ const prices = await balancerV3.getPricesVolume(
94
+ srcToken,
95
+ dstToken,
96
+ amounts,
97
+ side,
98
+ blockNumber,
99
+ limitPools,
100
+ );
101
+ expect(prices).not.toBeNull();
102
+ await checkOnChainPricingNonMulti(
103
+ network,
104
+ side,
105
+ balancerV3,
106
+ blockNumber,
107
+ prices as ExchangePrices<BalancerV3Data>,
108
+ amounts,
109
+ );
110
+ }
@@ -0,0 +1,199 @@
1
+ /* eslint-disable no-console */
2
+ // Note - this is currently needed because queries won't work with multicall but should be updated in future
3
+ export async function checkOnChainPricingNonMulti(
4
+ network: number,
5
+ side: SwapSide,
6
+ balancerV3: BalancerV3,
7
+ blockNumber: number,
8
+ prices: ExchangePrices<BalancerV3Data>,
9
+ amounts: bigint[],
10
+ ) {
11
+ // test match for each returned price
12
+ for (const price of prices) {
13
+ let expectedPrices: bigint[] = [];
14
+ if (price.data.steps.length === 1 && !price.data.steps[0].isBuffer)
15
+ expectedPrices = await querySinglePathPrices(
16
+ network,
17
+ side,
18
+ balancerV3,
19
+ blockNumber,
20
+ price,
21
+ amounts,
22
+ );
23
+ else
24
+ expectedPrices = await queryMultiPathPrices(
25
+ network,
26
+ side,
27
+ balancerV3,
28
+ blockNumber,
29
+ price,
30
+ amounts,
31
+ );
32
+ expect(price.prices).toEqual(expectedPrices);
33
+ }
34
+ }
35
+
36
+ async function querySinglePathPrices(
37
+ network: number,
38
+ side: SwapSide,
39
+ balancerV3: BalancerV3,
40
+ blockNumber: number,
41
+ price: PoolPrices<BalancerV3Data>,
42
+ amounts: bigint[],
43
+ ) {
44
+ const balancerRouter = new Interface(balancerRouterAbi);
45
+ const readerCallData = getQuerySwapSingleTokenCalldata(
46
+ BalancerV3Config.BalancerV3[network].balancerRouterAddress,
47
+ balancerRouter,
48
+ amounts,
49
+ price.data.steps[0],
50
+ side,
51
+ );
52
+
53
+ const expectedPrices = [0n];
54
+ for (const call of readerCallData) {
55
+ try {
56
+ const result = await balancerV3.dexHelper.provider.call(
57
+ {
58
+ to: call.target,
59
+ data: call.callData,
60
+ },
61
+ blockNumber,
62
+ );
63
+ const parsed = balancerRouter.decodeFunctionResult(
64
+ side === SwapSide.SELL
65
+ ? `querySwapSingleTokenExactIn`
66
+ : `querySwapSingleTokenExactOut`,
67
+ result,
68
+ );
69
+ expectedPrices.push(BigInt(parsed[0]._hex));
70
+ } catch (error) {
71
+ console.log('Error in querySinglePathPrices', error);
72
+ expectedPrices.push(0n);
73
+ }
74
+ }
75
+ return expectedPrices;
76
+ }
77
+
78
+ async function queryMultiPathPrices(
79
+ network: number,
80
+ side: SwapSide,
81
+ balancerV3: BalancerV3,
82
+ blockNumber: number,
83
+ price: PoolPrices<BalancerV3Data>,
84
+ amounts: bigint[],
85
+ ) {
86
+ const balancerBatchRouter = new Interface(balancerBatchRouterAbi);
87
+ const readerCallData = getQuerySwapMultiTokenCalldata(
88
+ BalancerV3Config.BalancerV3[network].balancerBatchRouterAddress,
89
+ balancerBatchRouter,
90
+ amounts,
91
+ price.data.steps,
92
+ side,
93
+ );
94
+
95
+ const expectedPrices = [0n];
96
+ for (const call of readerCallData) {
97
+ try {
98
+ const result = await balancerV3.dexHelper.provider.call(
99
+ {
100
+ to: call.target,
101
+ data: call.callData,
102
+ },
103
+ blockNumber,
104
+ );
105
+ const parsed = balancerBatchRouter.decodeFunctionResult(
106
+ side === SwapSide.SELL ? `querySwapExactIn` : `querySwapExactOut`,
107
+ result,
108
+ );
109
+ expectedPrices.push(BigInt(parsed[2][0]._hex));
110
+ } catch (error) {
111
+ console.log('Error in queryMultiPathPrices', error);
112
+ expectedPrices.push(0n);
113
+ }
114
+ }
115
+ return expectedPrices;
116
+ }
117
+
118
+ function getQuerySwapSingleTokenCalldata(
119
+ routerAddress: Address,
120
+ routerInterface: Interface,
121
+ amounts: bigint[],
122
+ step: Step,
123
+ side: SwapSide,
124
+ ) {
125
+ return amounts
126
+ .filter(amount => amount !== 0n)
127
+ .map(amount => {
128
+ return {
129
+ target: routerAddress,
130
+ callData: routerInterface.encodeFunctionData(
131
+ side === SwapSide.SELL
132
+ ? `querySwapSingleTokenExactIn`
133
+ : `querySwapSingleTokenExactOut`,
134
+ [
135
+ step.pool,
136
+ step.swapInput.tokenIn,
137
+ step.swapInput.tokenOut,
138
+ amount,
139
+ NULL_ADDRESS,
140
+ '0x',
141
+ ],
142
+ ),
143
+ };
144
+ });
145
+ }
146
+ /* eslint-disable no-console */
147
+ import { Interface } from '@ethersproject/abi';
148
+ import { NULL_ADDRESS, SwapSide } from '../../constants';
149
+ import { BalancerV3 } from './balancer-v3';
150
+ import { BalancerV3Config } from './config';
151
+ import { BalancerV3Data, Step } from './types';
152
+ import { Address, ExchangePrices, PoolPrices } from '../../types';
153
+ import balancerBatchRouterAbi from '../../abi/balancer-v3/batch-router.json';
154
+ import balancerRouterAbi from '../../abi/balancer-v3/router.json';
155
+
156
+ function getQuerySwapMultiTokenCalldata(
157
+ routerAddress: Address,
158
+ routerInterface: Interface,
159
+ amounts: bigint[],
160
+ steps: Step[],
161
+ side: SwapSide,
162
+ ) {
163
+ const tokenIn = steps[0].swapInput.tokenIn;
164
+ const stepsNew = steps.map(s => ({
165
+ pool: s.pool,
166
+ tokenOut: s.swapInput.tokenOut,
167
+ isBuffer: s.isBuffer,
168
+ }));
169
+ return amounts
170
+ .filter(amount => amount !== 0n)
171
+ .map(amount => {
172
+ let args: any[] = [];
173
+ if (side === SwapSide.SELL)
174
+ args = [
175
+ {
176
+ tokenIn,
177
+ steps: stepsNew,
178
+ exactAmountIn: amount,
179
+ minAmountOut: 0n,
180
+ },
181
+ ];
182
+ else
183
+ args = [
184
+ {
185
+ tokenIn,
186
+ steps: stepsNew,
187
+ exactAmountOut: amount,
188
+ maxAmountIn: 0n,
189
+ },
190
+ ];
191
+ return {
192
+ target: routerAddress,
193
+ callData: routerInterface.encodeFunctionData(
194
+ side === SwapSide.SELL ? `querySwapExactIn` : `querySwapExactOut`,
195
+ [args, NULL_ADDRESS, '0x'],
196
+ ),
197
+ };
198
+ });
199
+ }
@@ -385,6 +385,26 @@ describe('BalancerV3 E2E', () => {
385
385
  );
386
386
  });
387
387
 
388
+ describe('StableSurge Hook', () => {
389
+ const tokenASymbol: string = 'waBasGHO';
390
+ const tokenBSymbol: string = 'waBasUSDC';
391
+
392
+ const tokenAAmount: string = '500000000000000000';
393
+ const tokenBAmount: string = '500000';
394
+ const nativeTokenAmount = '0';
395
+
396
+ testForNetwork(
397
+ network,
398
+ dexKey,
399
+ tokenASymbol,
400
+ tokenBSymbol,
401
+ tokenAAmount,
402
+ tokenBAmount,
403
+ nativeTokenAmount,
404
+ false,
405
+ );
406
+ });
407
+
388
408
  // No holders for waBasWETH
389
409
  // describe('Boosted Path', () => {
390
410
  // const tokenASymbol: string = 'waBaswstETH';
@@ -2,9 +2,9 @@
2
2
  import dotenv from 'dotenv';
3
3
  dotenv.config();
4
4
 
5
- import { Interface, Result } from '@ethersproject/abi';
5
+ import { checkOnChainPricingNonMulti } from './balancer-test-helpers';
6
6
  import { DummyDexHelper } from '../../dex-helper/index';
7
- import { Network, NULL_ADDRESS, SwapSide } from '../../constants';
7
+ import { Network, SwapSide } from '../../constants';
8
8
  import { BI_POWS } from '../../bigint-constants';
9
9
  import { BalancerV3 } from './balancer-v3';
10
10
  import {
@@ -13,201 +13,6 @@ import {
13
13
  checkPoolsLiquidity,
14
14
  } from '../../../tests/utils';
15
15
  import { Tokens } from '../../../tests/constants-e2e';
16
- import { BalancerV3Config } from './config';
17
- import { BalancerV3Data, Step } from './types';
18
- import { Address, ExchangePrices, PoolPrices } from '../../types';
19
- import balancerBatchRouterAbi from '../../abi/balancer-v3/batch-router.json';
20
- import balancerRouterAbi from '../../abi/balancer-v3/router.json';
21
-
22
- function getQuerySwapSingleTokenCalldata(
23
- routerAddress: Address,
24
- routerInterface: Interface,
25
- amounts: bigint[],
26
- step: Step,
27
- side: SwapSide,
28
- ) {
29
- return amounts
30
- .filter(amount => amount !== 0n)
31
- .map(amount => {
32
- return {
33
- target: routerAddress,
34
- callData: routerInterface.encodeFunctionData(
35
- side === SwapSide.SELL
36
- ? `querySwapSingleTokenExactIn`
37
- : `querySwapSingleTokenExactOut`,
38
- [
39
- step.pool,
40
- step.swapInput.tokenIn,
41
- step.swapInput.tokenOut,
42
- amount,
43
- NULL_ADDRESS,
44
- '0x',
45
- ],
46
- ),
47
- };
48
- });
49
- }
50
-
51
- function getQuerySwapMultiTokenCalldata(
52
- routerAddress: Address,
53
- routerInterface: Interface,
54
- amounts: bigint[],
55
- steps: Step[],
56
- side: SwapSide,
57
- ) {
58
- const tokenIn = steps[0].swapInput.tokenIn;
59
- const stepsNew = steps.map(s => ({
60
- pool: s.pool,
61
- tokenOut: s.swapInput.tokenOut,
62
- isBuffer: s.isBuffer,
63
- }));
64
- return amounts
65
- .filter(amount => amount !== 0n)
66
- .map(amount => {
67
- let args: any[] = [];
68
- if (side === SwapSide.SELL)
69
- args = [
70
- {
71
- tokenIn,
72
- steps: stepsNew,
73
- exactAmountIn: amount,
74
- minAmountOut: 0n,
75
- },
76
- ];
77
- else
78
- args = [
79
- {
80
- tokenIn,
81
- steps: stepsNew,
82
- exactAmountOut: amount,
83
- maxAmountIn: 0n,
84
- },
85
- ];
86
- return {
87
- target: routerAddress,
88
- callData: routerInterface.encodeFunctionData(
89
- side === SwapSide.SELL ? `querySwapExactIn` : `querySwapExactOut`,
90
- [args, NULL_ADDRESS, '0x'],
91
- ),
92
- };
93
- });
94
- }
95
-
96
- async function querySinglePathPrices(
97
- network: number,
98
- side: SwapSide,
99
- balancerV3: BalancerV3,
100
- blockNumber: number,
101
- price: PoolPrices<BalancerV3Data>,
102
- amounts: bigint[],
103
- ) {
104
- const balancerRouter = new Interface(balancerRouterAbi);
105
- const readerCallData = getQuerySwapSingleTokenCalldata(
106
- BalancerV3Config.BalancerV3[network].balancerRouterAddress,
107
- balancerRouter,
108
- amounts,
109
- price.data.steps[0],
110
- side,
111
- );
112
-
113
- const expectedPrices = [0n];
114
- for (const call of readerCallData) {
115
- try {
116
- const result = await balancerV3.dexHelper.provider.call(
117
- {
118
- to: call.target,
119
- data: call.callData,
120
- },
121
- blockNumber,
122
- );
123
- const parsed = balancerRouter.decodeFunctionResult(
124
- side === SwapSide.SELL
125
- ? `querySwapSingleTokenExactIn`
126
- : `querySwapSingleTokenExactOut`,
127
- result,
128
- );
129
- expectedPrices.push(BigInt(parsed[0]._hex));
130
- } catch (error) {
131
- console.log('Error in querySinglePathPrices', error);
132
- expectedPrices.push(0n);
133
- }
134
- }
135
- return expectedPrices;
136
- }
137
-
138
- async function queryMultiPathPrices(
139
- network: number,
140
- side: SwapSide,
141
- balancerV3: BalancerV3,
142
- blockNumber: number,
143
- price: PoolPrices<BalancerV3Data>,
144
- amounts: bigint[],
145
- ) {
146
- const balancerBatchRouter = new Interface(balancerBatchRouterAbi);
147
- const readerCallData = getQuerySwapMultiTokenCalldata(
148
- BalancerV3Config.BalancerV3[network].balancerBatchRouterAddress,
149
- balancerBatchRouter,
150
- amounts,
151
- price.data.steps,
152
- side,
153
- );
154
-
155
- const expectedPrices = [0n];
156
- for (const call of readerCallData) {
157
- try {
158
- const result = await balancerV3.dexHelper.provider.call(
159
- {
160
- to: call.target,
161
- data: call.callData,
162
- },
163
- blockNumber,
164
- );
165
- const parsed = balancerBatchRouter.decodeFunctionResult(
166
- side === SwapSide.SELL ? `querySwapExactIn` : `querySwapExactOut`,
167
- result,
168
- );
169
- expectedPrices.push(BigInt(parsed[2][0]._hex));
170
- } catch (error) {
171
- console.log('Error in queryMultiPathPrices', error);
172
- expectedPrices.push(0n);
173
- }
174
- }
175
- return expectedPrices;
176
- }
177
-
178
- // Note - this is currently needed because queries won't work with multicall but should be updated in future
179
- async function checkOnChainPricingNonMulti(
180
- network: number,
181
- side: SwapSide,
182
- balancerV3: BalancerV3,
183
- blockNumber: number,
184
- prices: ExchangePrices<BalancerV3Data>,
185
- amounts: bigint[],
186
- ) {
187
- // test match for each returned price
188
- for (const price of prices) {
189
- let expectedPrices: bigint[] = [];
190
- if (price.data.steps.length === 1 && !price.data.steps[0].isBuffer)
191
- expectedPrices = await querySinglePathPrices(
192
- network,
193
- side,
194
- balancerV3,
195
- blockNumber,
196
- price,
197
- amounts,
198
- );
199
- else
200
- expectedPrices = await queryMultiPathPrices(
201
- network,
202
- side,
203
- balancerV3,
204
- blockNumber,
205
- price,
206
- amounts,
207
- );
208
- expect(price.prices).toEqual(expectedPrices);
209
- }
210
- }
211
16
 
212
17
  async function testPricingOnNetwork(
213
18
  balancerV3: BalancerV3,