@paraswap/dex-lib 2.17.0 → 2.17.1-pancakeswapv3.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 (165) 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/{woo-fi → pancakeswap-v3}/config.d.ts +2 -2
  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 +137 -0
  74. package/build/dex/pancakeswap-v3/types.js +9 -0
  75. package/build/dex/pancakeswap-v3/types.js.map +1 -0
  76. package/build/dex/pancakeswap-v3/utils.d.ts +6 -0
  77. package/build/dex/pancakeswap-v3/utils.js +65 -0
  78. package/build/dex/pancakeswap-v3/utils.js.map +1 -0
  79. package/build/dex/uniswap-v3/config.js +0 -28
  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 +1 -1
  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 +168 -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 +460 -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/uniswap-v3/config.ts +0 -32
  123. package/src/dex/uniswap-v3/uniswap-v3-e2e.test.ts +0 -156
  124. package/src/dex/uniswap-v3/uniswap-v3-events.test.ts +4 -0
  125. package/src/dex/uniswap-v3/uniswap-v3-pool.ts +1 -2
  126. package/src/dex/uniswap-v3/uniswap-v3.ts +0 -1
  127. package/tests/constants-e2e.ts +1 -1
  128. package/.idea/aws.xml +0 -17
  129. package/.idea/codeStyles/Project.xml +0 -19
  130. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  131. package/.idea/misc.xml +0 -6
  132. package/.idea/modules.xml +0 -8
  133. package/.idea/paraswap-dex-lib.iml +0 -9
  134. package/.idea/vcs.xml +0 -6
  135. package/build/abi/woo-fi/WooFeeManager.abi.json +0 -318
  136. package/build/abi/woo-fi/WooPP.abi.json +0 -548
  137. package/build/abi/woo-fi/Wooracle.abi.json +0 -260
  138. package/build/dex/aave-v3/tokens/arbitrum.json +0 -50
  139. package/build/dex/aave-v3/tokens/avalanche.json +0 -74
  140. package/build/dex/aave-v3/tokens/fantom.json +0 -62
  141. package/build/dex/aave-v3/tokens/optimism.json +0 -50
  142. package/build/dex/aave-v3/tokens/polygon.json +0 -116
  143. package/build/dex/quickswap-v3.d.ts +0 -21
  144. package/build/dex/quickswap-v3.js +0 -40
  145. package/build/dex/quickswap-v3.js.map +0 -1
  146. package/build/dex/woo-fi/config.js +0 -141
  147. package/build/dex/woo-fi/config.js.map +0 -1
  148. package/build/dex/woo-fi/constants.d.ts +0 -5
  149. package/build/dex/woo-fi/constants.js +0 -20
  150. package/build/dex/woo-fi/constants.js.map +0 -1
  151. package/build/dex/woo-fi/types.d.ts +0 -48
  152. package/build/dex/woo-fi/types.js +0 -3
  153. package/build/dex/woo-fi/types.js.map +0 -1
  154. package/build/dex/woo-fi/woo-fi-decimal-math.d.ts +0 -14
  155. package/build/dex/woo-fi/woo-fi-decimal-math.js +0 -42
  156. package/build/dex/woo-fi/woo-fi-decimal-math.js.map +0 -1
  157. package/build/dex/woo-fi/woo-fi-math.d.ts +0 -31
  158. package/build/dex/woo-fi/woo-fi-math.js +0 -300
  159. package/build/dex/woo-fi/woo-fi-math.js.map +0 -1
  160. package/build/dex/woo-fi/woo-fi.d.ts +0 -65
  161. package/build/dex/woo-fi/woo-fi.js +0 -480
  162. package/build/dex/woo-fi/woo-fi.js.map +0 -1
  163. package/src/dex/uniswap-v3/forks/pancakeswap-v3/pancakeswap-v3.ts +0 -28
  164. /package/src/abi/{uniswap-v3/pancakeswap-v3/PancakeswapV3QuoterV2.abi.json → pancakeswap-v3/PancakeswapV3Quoter.abi.json} +0 -0
  165. /package/src/abi/{uniswap-v3/pancakeswap-v3 → pancakeswap-v3}/PancakeswapV3Router.abi.json +0 -0
@@ -31,10 +31,7 @@ describe('BalancerV2 E2E', () => {
31
31
  ContractMethod.megaSwap,
32
32
  ],
33
33
  ],
34
- [SwapSide.BUY, [
35
- ContractMethod.simpleBuy,
36
- ContractMethod.buy
37
- ]],
34
+ [SwapSide.BUY, [ContractMethod.simpleBuy, ContractMethod.buy]],
38
35
  ]);
39
36
 
40
37
  const pairs: { name: string; sellAmount: string; buyAmount: string }[][] =
@@ -98,7 +95,7 @@ describe('BalancerV2 E2E', () => {
98
95
  sellAmount: '200000000000000',
99
96
  buyAmount: '10000000000',
100
97
  },
101
- ]
98
+ ],
102
99
  ];
103
100
 
104
101
  sideToContractMethods.forEach((contractMethods, side) =>
@@ -153,11 +150,14 @@ describe('BalancerV2 E2E', () => {
153
150
  ContractMethod.megaSwap,
154
151
  ],
155
152
  ],
156
- [SwapSide.BUY, [
157
- // Buy support is enabled only for Weighted Pools
158
- // ContractMethod.simpleBuy,
159
- // ContractMethod.buy
160
- ]],
153
+ [
154
+ SwapSide.BUY,
155
+ [
156
+ // Buy support is enabled only for Weighted Pools
157
+ // ContractMethod.simpleBuy,
158
+ // ContractMethod.buy
159
+ ],
160
+ ],
161
161
  ]);
162
162
 
163
163
  const pairs: { name: string; sellAmount: string; buyAmount: string }[][] =
@@ -555,10 +555,7 @@ describe('BalancerV2 E2E', () => {
555
555
  ContractMethod.megaSwap,
556
556
  ],
557
557
  ],
558
- [SwapSide.BUY, [
559
- ContractMethod.simpleBuy,
560
- ContractMethod.buy
561
- ]],
558
+ [SwapSide.BUY, [ContractMethod.simpleBuy, ContractMethod.buy]],
562
559
  ]);
563
560
 
564
561
  const pairs: { name: string; sellAmount: string; buyAmount: string }[][] =
@@ -857,10 +854,7 @@ describe('BalancerV2 E2E', () => {
857
854
  ContractMethod.megaSwap,
858
855
  ],
859
856
  ],
860
- [SwapSide.BUY, [
861
- ContractMethod.simpleBuy,
862
- ContractMethod.buy
863
- ]],
857
+ [SwapSide.BUY, [ContractMethod.simpleBuy, ContractMethod.buy]],
864
858
  ]);
865
859
 
866
860
  const pairs: { name: string; sellAmount: string; buyAmount: string }[][] =
@@ -932,7 +926,7 @@ describe('BalancerV2 E2E', () => {
932
926
  }),
933
927
  );
934
928
  });
935
- })
929
+ });
936
930
 
937
931
  describe('BeetsFi OPTIMISM', () => {
938
932
  const dexKey = 'BeetsFi';
@@ -11,7 +11,7 @@ import { Tokens } from '../../../tests/constants-e2e';
11
11
  import { BalancerV2Data } from './types';
12
12
  import { PoolPrices } from '../../types';
13
13
 
14
- const amounts = [ 0n, BI_POWS[18], 2000000000000000000n ];
14
+ const amounts = [0n, BI_POWS[18], 2000000000000000000n];
15
15
 
16
16
  const dexKey = 'BalancerV2';
17
17
 
@@ -23,42 +23,53 @@ async function getOnChainPricingForWeightedPool(
23
23
  srcTokenAddress: string,
24
24
  destTokenAddress: string,
25
25
  side: SwapSide,
26
- ): Promise<BigInt[]>{
27
- return Promise.all(poolPrice.prices.map(async (price, index) => {
28
- if(amounts[index] === 0n) {
29
- return 0n;
30
- }
31
-
32
- const params = balancerV2.getBalancerParam(
33
- srcTokenAddress,
34
- destTokenAddress,
35
- '0',
36
- '0',
37
- {
38
- swaps: [
39
- { poolId: poolPrice.data.poolId, amount: amounts[index].toString() },
40
- ]
41
- },
42
- side,
43
- );
26
+ ): Promise<BigInt[]> {
27
+ return Promise.all(
28
+ poolPrice.prices.map(async (price, index) => {
29
+ if (amounts[index] === 0n) {
30
+ return 0n;
31
+ }
32
+
33
+ const params = balancerV2.getBalancerParam(
34
+ srcTokenAddress,
35
+ destTokenAddress,
36
+ '0',
37
+ '0',
38
+ {
39
+ swaps: [
40
+ {
41
+ poolId: poolPrice.data.poolId,
42
+ amount: amounts[index].toString(),
43
+ },
44
+ ],
45
+ },
46
+ side,
47
+ );
44
48
 
45
- const calldata = [
46
- {
47
- target: balancerV2.vaultAddress,
48
- callData: balancerV2.eventPools.vaultInterface.encodeFunctionData('queryBatchSwap', params.slice(0, 4)),
49
- },
50
- ];
51
-
52
- const results = (
53
- await balancerV2.dexHelper.multiContract.methods
54
- .aggregate(calldata)
55
- .call({}, blockNumber)
56
- ).returnData;
57
-
58
- const parsed = balancerV2.eventPools.vaultInterface.decodeFunctionResult('queryBatchSwap', results[0]);
59
- const resultIndex = side === SwapSide.SELL ? parsed[0].length - 1 : 0;
60
- return BigInt(parsed[0][resultIndex]._hex.replace('-', ''));
61
- }));
49
+ const calldata = [
50
+ {
51
+ target: balancerV2.vaultAddress,
52
+ callData: balancerV2.eventPools.vaultInterface.encodeFunctionData(
53
+ 'queryBatchSwap',
54
+ params.slice(0, 4),
55
+ ),
56
+ },
57
+ ];
58
+
59
+ const results = (
60
+ await balancerV2.dexHelper.multiContract.methods
61
+ .aggregate(calldata)
62
+ .call({}, blockNumber)
63
+ ).returnData;
64
+
65
+ const parsed = balancerV2.eventPools.vaultInterface.decodeFunctionResult(
66
+ 'queryBatchSwap',
67
+ results[0],
68
+ );
69
+ const resultIndex = side === SwapSide.SELL ? parsed[0].length - 1 : 0;
70
+ return BigInt(parsed[0][resultIndex]._hex.replace('-', ''));
71
+ }),
72
+ );
62
73
  }
63
74
 
64
75
  describe('BalancerV2', function () {
@@ -12,7 +12,15 @@ import {
12
12
  SimpleExchangeParam,
13
13
  Token,
14
14
  } from '../../types';
15
- import { ETHER_ADDRESS, MAX_INT, MAX_UINT, Network, NULL_ADDRESS, SUBGRAPH_TIMEOUT, SwapSide, } from '../../constants';
15
+ import {
16
+ ETHER_ADDRESS,
17
+ MAX_INT,
18
+ MAX_UINT,
19
+ Network,
20
+ NULL_ADDRESS,
21
+ SUBGRAPH_TIMEOUT,
22
+ SwapSide,
23
+ } from '../../constants';
16
24
  import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
17
25
  import { StablePool } from './pools/stable/StablePool';
18
26
  import { WeightedPool } from './pools/weighted/WeightedPool';
@@ -38,8 +46,17 @@ import {
38
46
  } from './types';
39
47
  import { SimpleExchange } from '../simple-exchange';
40
48
  import { Adapters, BalancerConfig } from './config';
41
- import { getAllPoolsUsedInPaths, isSameAddress, poolGetMainTokens, poolGetPathForTokenInOut, } from './utils';
42
- import { MIN_USD_LIQUIDITY_TO_FETCH, STABLE_GAS_COST, VARIABLE_GAS_COST_PER_CYCLE } from './constants';
49
+ import {
50
+ getAllPoolsUsedInPaths,
51
+ isSameAddress,
52
+ poolGetMainTokens,
53
+ poolGetPathForTokenInOut,
54
+ } from './utils';
55
+ import {
56
+ MIN_USD_LIQUIDITY_TO_FETCH,
57
+ STABLE_GAS_COST,
58
+ VARIABLE_GAS_COST_PER_CYCLE,
59
+ } from './constants';
43
60
 
44
61
  const fetchAllPools = `query ($count: Int) {
45
62
  pools: pools(
@@ -348,7 +365,10 @@ export class BalancerV2EventPool extends StatefulEventSubscriber<PoolStateMap> {
348
365
  return null;
349
366
  }
350
367
 
351
- if(subgraphPool.poolType !== BalancerPoolTypes.Weighted && side === SwapSide.BUY) {
368
+ if (
369
+ subgraphPool.poolType !== BalancerPoolTypes.Weighted &&
370
+ side === SwapSide.BUY
371
+ ) {
352
372
  return null;
353
373
  }
354
374
 
@@ -495,8 +515,7 @@ export class BalancerV2
495
515
  protected network: Network,
496
516
  dexKey: string,
497
517
  public dexHelper: IDexHelper,
498
- public vaultAddress: Address = BalancerConfig[dexKey][network]
499
- .vaultAddress,
518
+ public vaultAddress: Address = BalancerConfig[dexKey][network].vaultAddress,
500
519
  protected subgraphURL: string = BalancerConfig[dexKey][network].subgraphURL,
501
520
  protected adapters = Adapters[network],
502
521
  ) {
@@ -596,23 +615,22 @@ export class BalancerV2
596
615
  }
597
616
 
598
617
  getPoolsWithTokenPair(from: Token, to: Token): SubgraphPoolBase[] {
599
- const pools = this.eventPools.allPools
600
- .filter(p => {
601
- const fromMain = p.mainTokens.find(
602
- token => token.address.toLowerCase() === from.address.toLowerCase(),
603
- );
604
- const toMain = p.mainTokens.find(
605
- token => token.address.toLowerCase() === to.address.toLowerCase(),
606
- );
618
+ const pools = this.eventPools.allPools.filter(p => {
619
+ const fromMain = p.mainTokens.find(
620
+ token => token.address.toLowerCase() === from.address.toLowerCase(),
621
+ );
622
+ const toMain = p.mainTokens.find(
623
+ token => token.address.toLowerCase() === to.address.toLowerCase(),
624
+ );
607
625
 
608
- return (
609
- fromMain &&
610
- toMain &&
611
- // filter instances similar to the following:
612
- // USDC -> DAI in a pool where bbaUSD is nested (ie: MAI / bbaUSD)
613
- !(fromMain.isDeeplyNested && toMain.isDeeplyNested)
614
- );
615
- });
626
+ return (
627
+ fromMain &&
628
+ toMain &&
629
+ // filter instances similar to the following:
630
+ // USDC -> DAI in a pool where bbaUSD is nested (ie: MAI / bbaUSD)
631
+ !(fromMain.isDeeplyNested && toMain.isDeeplyNested)
632
+ );
633
+ });
616
634
 
617
635
  return pools.slice(0, 10);
618
636
  }
@@ -952,7 +970,7 @@ export class BalancerV2
952
970
  side,
953
971
  );
954
972
 
955
- if(side === SwapSide.BUY) {
973
+ if (side === SwapSide.BUY) {
956
974
  path = path.reverse();
957
975
  }
958
976
 
@@ -960,7 +978,11 @@ export class BalancerV2
960
978
  poolId: hop.pool.id,
961
979
  assetInIndex: swapOffset + index,
962
980
  assetOutIndex: swapOffset + index + 1,
963
- amount: (side === SwapSide.SELL && index === 0 || side === SwapSide.BUY && index === path.length - 1) ? swapData.amount : '0',
981
+ amount:
982
+ (side === SwapSide.SELL && index === 0) ||
983
+ (side === SwapSide.BUY && index === path.length - 1)
984
+ ? swapData.amount
985
+ : '0',
964
986
  userData: '0x',
965
987
  }));
966
988
 
@@ -51,7 +51,7 @@ export const Adapters: Record<number, AdapterMappings> = {
51
51
  [SwapSide.SELL]: [{ name: 'PolygonAdapter01', index: 9 }],
52
52
  [SwapSide.BUY]: [{ name: 'PolygonBuyAdapter', index: 6 }],
53
53
  },
54
- [Network.FANTOM]:{
54
+ [Network.FANTOM]: {
55
55
  [SwapSide.SELL]: [{ name: 'FantomAdapter01', index: 5 }],
56
56
  [SwapSide.BUY]: [{ name: 'FantomBuyAdapter', index: 4 }],
57
57
  },
@@ -271,8 +271,8 @@ export function _calcTokensOutGivenExactBptIn(
271
271
  // bpt = bptTotalSupply //
272
272
  **********************************************************************************************/
273
273
 
274
- // Since we're computing an amount out, we round down overall. This means rounding down on both the
275
- // multiplication and division.
274
+ // Since we're computing an amount out, we round down overall. This means rounding down on both the
275
+ // multiplication and division.
276
276
 
277
277
  const bptRatio = MathSol.divDownFixed(bptAmountIn, bptTotalSupply);
278
278
  const amountsOut: bigint[] = new Array(balances.length);
@@ -631,7 +631,7 @@ export class LinearPool extends BasePool {
631
631
  (this._upscale(
632
632
  poolPairData.balances[poolPairData.indexOut],
633
633
  poolPairData.scalingFactors[poolPairData.indexOut],
634
- ) *
634
+ ) *
635
635
  99n) /
636
636
  100n
637
637
  );
@@ -17,7 +17,7 @@ export function _calculateInvariant(
17
17
  // n = number of tokens //
18
18
  *********x************************************************************************************/
19
19
 
20
- // We support rounding up or down.
20
+ // We support rounding up or down.
21
21
 
22
22
  let sum = 0n;
23
23
  const numTokens = balances.length;
@@ -44,18 +44,18 @@ export function _calculateInvariant(
44
44
  prevInvariant = invariant;
45
45
  invariant = MathSol.div(
46
46
  MathSol.mul(MathSol.mul(BigInt(numTokens), invariant), invariant) +
47
- MathSol.div(
48
- MathSol.mul(MathSol.mul(ampTimesTotal, sum), P_D),
49
- AMP_PRECISION,
50
- roundUp,
51
- ),
47
+ MathSol.div(
48
+ MathSol.mul(MathSol.mul(ampTimesTotal, sum), P_D),
49
+ AMP_PRECISION,
50
+ roundUp,
51
+ ),
52
52
  MathSol.mul(BigInt(numTokens + 1), invariant) +
53
- // No need to use checked arithmetic for the amp precision, the amp is guaranteed to be at least 1
54
- MathSol.div(
55
- MathSol.mul(ampTimesTotal - AMP_PRECISION, P_D),
56
- AMP_PRECISION,
57
- !roundUp,
58
- ),
53
+ // No need to use checked arithmetic for the amp precision, the amp is guaranteed to be at least 1
54
+ MathSol.div(
55
+ MathSol.mul(ampTimesTotal - AMP_PRECISION, P_D),
56
+ AMP_PRECISION,
57
+ !roundUp,
58
+ ),
59
59
  roundUp,
60
60
  );
61
61
 
@@ -234,9 +234,9 @@ export function _calcTokenInGivenExactBptOut(
234
234
  const newInvariant = MathSol.mulUpFixed(
235
235
  MathSol.divUpFixed(
236
236
  MathSol.add(bptTotalSupply, bptAmountOut),
237
- bptTotalSupply
237
+ bptTotalSupply,
238
238
  ),
239
- invariant
239
+ invariant,
240
240
  );
241
241
 
242
242
  const newBalanceTokenIndex =
@@ -244,11 +244,11 @@ export function _calcTokenInGivenExactBptOut(
244
244
  amp,
245
245
  balances,
246
246
  newInvariant,
247
- tokenIndexIn
247
+ tokenIndexIn,
248
248
  );
249
249
  const amountInWithoutFee = MathSol.sub(
250
250
  newBalanceTokenIndex,
251
- balances[tokenIndexIn]
251
+ balances[tokenIndexIn],
252
252
  );
253
253
 
254
254
  let sumBalances = BigInt(0);
@@ -260,18 +260,18 @@ export function _calcTokenInGivenExactBptOut(
260
260
  // and used in virtual swaps, and charge swap fees accordingly.
261
261
  const currentWeight = MathSol.divDownFixed(
262
262
  balances[tokenIndexIn],
263
- sumBalances
263
+ sumBalances,
264
264
  );
265
265
  const taxablePercentage = MathSol.complementFixed(currentWeight);
266
266
  const taxableAmount = MathSol.mulUpFixed(
267
267
  amountInWithoutFee,
268
- taxablePercentage
268
+ taxablePercentage,
269
269
  );
270
270
  const nonTaxableAmount = MathSol.sub(amountInWithoutFee, taxableAmount);
271
271
 
272
272
  return MathSol.add(
273
273
  nonTaxableAmount,
274
- MathSol.divUpFixed(taxableAmount, MathSol.sub(MathSol.ONE, fee))
274
+ MathSol.divUpFixed(taxableAmount, MathSol.sub(MathSol.ONE, fee)),
275
275
  );
276
276
  }
277
277
 
@@ -282,7 +282,6 @@ export class PhantomStablePool extends BasePool {
282
282
  amountsIn.push(amt);
283
283
  });
284
284
  } else if (pairType === PairTypes.BptToToken) {
285
-
286
285
  tokenAmountsOut.forEach(amountOut => {
287
286
  let amt: bigint;
288
287
  try {
@@ -44,18 +44,18 @@ export class StableMath {
44
44
  prevInvariant = invariant;
45
45
  invariant = MathSol.div(
46
46
  MathSol.mul(MathSol.mul(BigInt(numTokens), invariant), invariant) +
47
- MathSol.div(
48
- MathSol.mul(MathSol.mul(ampTimesTotal, sum), P_D),
49
- this._AMP_PRECISION,
50
- roundUp,
51
- ),
47
+ MathSol.div(
48
+ MathSol.mul(MathSol.mul(ampTimesTotal, sum), P_D),
49
+ this._AMP_PRECISION,
50
+ roundUp,
51
+ ),
52
52
  MathSol.mul(BigInt(numTokens + 1), invariant) +
53
- // No need to use checked arithmetic for the amp precision, the amp is guaranteed to be at least 1
54
- MathSol.div(
55
- MathSol.mul(ampTimesTotal - this._AMP_PRECISION, P_D),
56
- this._AMP_PRECISION,
57
- !roundUp,
58
- ),
53
+ // No need to use checked arithmetic for the amp precision, the amp is guaranteed to be at least 1
54
+ MathSol.div(
55
+ MathSol.mul(ampTimesTotal - this._AMP_PRECISION, P_D),
56
+ this._AMP_PRECISION,
57
+ !roundUp,
58
+ ),
59
59
  roundUp,
60
60
  );
61
61
 
@@ -71,7 +71,6 @@ export class StableMath {
71
71
  throw new Error('Errors.STABLE_INVARIANT_DIDNT_CONVERGE');
72
72
  }
73
73
 
74
-
75
74
  static _calcInGivenOut(
76
75
  amplificationParameter: bigint,
77
76
  balances: bigint[],
@@ -101,7 +100,7 @@ export class StableMath {
101
100
  1n,
102
101
  );
103
102
  });
104
- };
103
+ }
105
104
 
106
105
  static _calcOutGivenIn(
107
106
  amplificationParameter: bigint,
@@ -117,7 +116,7 @@ export class StableMath {
117
116
  amplificationParameter,
118
117
  balances,
119
118
  true,
120
- );
119
+ );
121
120
 
122
121
  const initBalance = balances[tokenIndexIn];
123
122
  // Modification: The original code was implemented for a single tokenAmountsIn
@@ -77,7 +77,9 @@ export class StablePool extends BaseGeneralPool {
77
77
 
78
78
  const scalingFactors: bigint[] = [];
79
79
  const balances = poolState.orderedTokens.map((tokenAddress, i) => {
80
- const t = pool.tokensMap[tokenAddress.toLowerCase()] || poolState.tokens[tokenAddress.toLowerCase()];
80
+ const t =
81
+ pool.tokensMap[tokenAddress.toLowerCase()] ||
82
+ poolState.tokens[tokenAddress.toLowerCase()];
81
83
 
82
84
  if (t.address.toLowerCase() === tokenIn.toLowerCase()) {
83
85
  indexIn = i;
@@ -213,7 +215,7 @@ export class StablePool extends BaseGeneralPool {
213
215
  (this._upscale(
214
216
  poolPairData.balances[poolPairData.indexOut],
215
217
  poolPairData.scalingFactors[poolPairData.indexOut],
216
- ) *
218
+ ) *
217
219
  99n) /
218
220
  100n
219
221
  );
@@ -16,7 +16,6 @@ export type WeightedPoolPairData = {
16
16
  swapFee: bigint;
17
17
  };
18
18
 
19
-
20
19
  export class WeightedPool extends BaseMinimalSwapInfoPool {
21
20
  vaultAddress: string;
22
21
  vaultInterface: Interface;
package/src/dex/index.ts CHANGED
@@ -64,8 +64,8 @@ import { QuickSwapV3 } from './quickswap/quickswap-v3';
64
64
  import { ZyberSwapV3 } from './quickswap/zyberswap-v3';
65
65
  import { TraderJoeV2 } from './trader-joe-v2';
66
66
  import { SpiritSwapV3 } from './quickswap/spiritswap-v3';
67
- import { PancakeswapV3 } from './uniswap-v3/forks/pancakeswap-v3/pancakeswap-v3';
68
67
  import { TraderJoeV21 } from './trader-joe-v2.1';
68
+ import { PancakeswapV3 } from './pancakeswap-v3/pancakeswap-v3';
69
69
 
70
70
  const LegacyDexes = [
71
71
  CurveV2,
@@ -0,0 +1,68 @@
1
+ import { DexParams } from '../uniswap-v3/types';
2
+ import { DexConfigMap, AdapterMappings } from '../../types';
3
+ import { Network, SwapSide } from '../../constants';
4
+ import { Address } from '../../types';
5
+
6
+ const PANCAKE_SUPPORTED_FEES = [10000n, 2500n, 500n, 100n];
7
+
8
+ // Pools that will be initialized on app startup
9
+ // They are added for testing
10
+ export const PancakeswapV3Config: DexConfigMap<DexParams> = {
11
+ PancakeswapV3: {
12
+ [Network.MAINNET]: {
13
+ factory: '0x0BFbCF9fa4f9C56B0F40a671Ad40E0805A091865',
14
+ deployer: '0x41ff9AA7e16B8B1a8a8dc4f0eFacd93D02d071c9',
15
+ quoter: '0xB048Bbc1Ee6b733FFfCFb9e9CeF7375518e25997',
16
+ router: '0x1b81D678ffb9C0263b24A97847620C99d213eB14',
17
+ supportedFees: PANCAKE_SUPPORTED_FEES,
18
+ stateMulticall: '0x80898f80cFA3Fa3AbF410d90e69aDc432AE5D4c2',
19
+ uniswapMulticall: '0xac1cE734566f390A94b00eb9bf561c2625BF44ea',
20
+ chunksCount: 10,
21
+ initHash:
22
+ '0x6ce8eb472fa82df5469c6ab6d485f17c3ad13c8cd7af59b3d4a8026c5ce0f7e2',
23
+ subgraphURL:
24
+ 'https://api.thegraph.com/subgraphs/name/pancakeswap/exchange-v3-eth',
25
+ },
26
+ [Network.BSC]: {
27
+ factory: '0x0BFbCF9fa4f9C56B0F40a671Ad40E0805A091865',
28
+ deployer: '0x41ff9AA7e16B8B1a8a8dc4f0eFacd93D02d071c9',
29
+ quoter: '0xB048Bbc1Ee6b733FFfCFb9e9CeF7375518e25997',
30
+ router: '0x1b81D678ffb9C0263b24A97847620C99d213eB14',
31
+ supportedFees: PANCAKE_SUPPORTED_FEES,
32
+ stateMulticall: '0x9DAd2ED7ADc6eaacf81589Cd043579c9684E5C81',
33
+ uniswapMulticall: '0xac1cE734566f390A94b00eb9bf561c2625BF44ea',
34
+ chunksCount: 10,
35
+ initHash:
36
+ '0x6ce8eb472fa82df5469c6ab6d485f17c3ad13c8cd7af59b3d4a8026c5ce0f7e2',
37
+ subgraphURL:
38
+ 'https://api.thegraph.com/subgraphs/name/pancakeswap/exchange-v3-bsc',
39
+ },
40
+ },
41
+ };
42
+
43
+ export const Adapters: Record<number, AdapterMappings> = {
44
+ [Network.MAINNET]: {
45
+ [SwapSide.SELL]: [{ name: 'Adapter01', index: 6 }],
46
+ [SwapSide.BUY]: [{ name: 'BuyAdapter', index: 2 }],
47
+ },
48
+ [Network.BSC]: {
49
+ [SwapSide.SELL]: [{ name: 'BscAdapter02', index: 4 }],
50
+ [SwapSide.BUY]: [{ name: 'BscBuyAdapter', index: 5 }],
51
+ },
52
+ [Network.POLYGON]: {
53
+ [SwapSide.SELL]: [{ name: 'PolygonAdapter01', index: 13 }],
54
+ [SwapSide.BUY]: [{ name: 'PolygonBuyAdapter', index: 2 }],
55
+ },
56
+ [Network.ARBITRUM]: {
57
+ [SwapSide.SELL]: [{ name: 'ArbitrumAdapter01', index: 3 }],
58
+ [SwapSide.BUY]: [{ name: 'ArbitrumBuyAdapter', index: 2 }],
59
+ },
60
+ [Network.OPTIMISM]: {
61
+ [SwapSide.SELL]: [{ name: 'OptimismAdapter01', index: 3 }],
62
+ [SwapSide.BUY]: [{ name: 'OptimismBuyAdapter', index: 2 }],
63
+ },
64
+ [Network.FANTOM]: {
65
+ [SwapSide.SELL]: [{ name: 'FantomAdapter01', index: 11 }],
66
+ [SwapSide.BUY]: [{ name: 'FantomBuyAdapter', index: 3 }],
67
+ },
68
+ };
@@ -0,0 +1,35 @@
1
+ export const PANCAKESWAP3_FUNCTION_CALL_GAS_COST = 21_000; // Ceiled
2
+ export const PANCAKESWAPV3_TICK_GAS_COST = 24_000; // Ceiled
3
+
4
+ // This is used for price calculation. If out of scope, return 0n
5
+ export const TICK_BITMAP_TO_USE = 4n;
6
+
7
+ // This is used to check if the state is still valid.
8
+ export const TICK_BITMAP_BUFFER = 8n;
9
+
10
+ export const MAX_PRICING_COMPUTATION_STEPS_ALLOWED = 128;
11
+
12
+ export const PANCAKESWAPV3_SUBGRAPH_URL =
13
+ 'https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3';
14
+
15
+ export const PANCAKESWAPV3_EFFICIENCY_FACTOR = 3;
16
+
17
+ export const ZERO_TICK_INFO = {
18
+ liquidityGross: 0n,
19
+ liquidityNet: 0n,
20
+ tickCumulativeOutside: 0n,
21
+ secondsPerLiquidityOutsideX128: 0n,
22
+ secondsOutside: 0n,
23
+ initialized: false,
24
+ };
25
+
26
+ export const ZERO_ORACLE_OBSERVATION = {
27
+ blockTimestamp: 0n,
28
+ tickCumulative: 0n,
29
+ secondsPerLiquidityCumulativeX128: 0n,
30
+ initialized: false,
31
+ };
32
+
33
+ export const OUT_OF_RANGE_ERROR_POSTFIX = `INVALID_TICK_BIT_MAP_RANGES`;
34
+
35
+ export const DEFAULT_POOL_INIT_CODE_HASH = `0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54`;