@paraswap/dex-lib 3.8.22-infusion → 3.8.22

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 (235) hide show
  1. package/.idea/aws.xml +17 -0
  2. package/.idea/codeStyles/Project.xml +19 -0
  3. package/.idea/codeStyles/codeStyleConfig.xml +5 -0
  4. package/.idea/misc.xml +6 -0
  5. package/.idea/modules.xml +8 -0
  6. package/.idea/paraswap-dex-lib.iml +9 -0
  7. package/.idea/prettier.xml +7 -0
  8. package/.idea/vcs.xml +6 -0
  9. package/build/abi/maverick-v2/MaverickV2Router.json +778 -0
  10. package/build/dex/index.js +0 -2
  11. package/build/dex/index.js.map +1 -1
  12. package/build/dex/lite-psm/lite-psm.js +1 -1
  13. package/build/dex/lite-psm/lite-psm.js.map +1 -1
  14. package/build/dex/maker-psm/maker-psm.js +1 -1
  15. package/build/dex/trader-joe-v2.1/optimizer.d.ts +2 -0
  16. package/build/dex/trader-joe-v2.1/optimizer.js +47 -0
  17. package/build/dex/trader-joe-v2.1/optimizer.js.map +1 -0
  18. package/build/dex/trader-joe-v2.1/trader-joe-v2.1-new.d.ts +7 -0
  19. package/build/dex/trader-joe-v2.1/trader-joe-v2.1-new.js +20 -0
  20. package/build/dex/trader-joe-v2.1/trader-joe-v2.1-new.js.map +1 -0
  21. package/package.json +1 -1
  22. package/src/dex/index.ts +0 -2
  23. package/src/dex/lite-psm/lite-psm.ts +1 -1
  24. package/src/dex/maker-psm/maker-psm.ts +1 -1
  25. package/tests/constants-e2e.ts +1 -1
  26. package/build/abi/AaveV1_lending_pool.json +0 -27
  27. package/build/abi/inception/inception-ineth-pool.json +0 -1062
  28. package/build/abi/inception/inception-ineth.json +0 -609
  29. package/build/abi/inception/inception-ratio-feed.json +0 -93
  30. package/build/abi/inception/inception-vault.json +0 -991
  31. package/build/abi/infusion/InfusionFactory.json +0 -147
  32. package/build/abi/infusion/InfusionPair.json +0 -658
  33. package/build/abi/infusion/InfusionRouter.json +0 -442
  34. package/build/abi/oswap/oswap.abi.json +0 -452
  35. package/build/abi/platypus/asset.json +0 -490
  36. package/build/abi/platypus/avax-pool.json +0 -820
  37. package/build/abi/platypus/oracle.json +0 -119
  38. package/build/abi/platypus/pool.json +0 -677
  39. package/build/abi/smardex/all/smardex-router.json +0 -648
  40. package/build/abi/smardex/layer-1/smardex-factory.json +0 -242
  41. package/build/abi/smardex/layer-1/smardex-pool.json +0 -506
  42. package/build/abi/smardex/layer-2/smardex-factory.json +0 -185
  43. package/build/abi/smardex/layer-2/smardex-pool.json +0 -578
  44. package/build/abi/swaap-v1/pool.json +0 -1346
  45. package/build/dex/aave-v1/aave-v1.d.ts +0 -34
  46. package/build/dex/aave-v1/aave-v1.js +0 -180
  47. package/build/dex/aave-v1/aave-v1.js.map +0 -1
  48. package/build/dex/aave-v1/config.d.ts +0 -11
  49. package/build/dex/aave-v1/config.js +0 -20
  50. package/build/dex/aave-v1/config.js.map +0 -1
  51. package/build/dex/aave-v1/tokens-mainnet.json +0 -134
  52. package/build/dex/aave-v1/tokens.d.ts +0 -3
  53. package/build/dex/aave-v1/tokens.js +0 -50
  54. package/build/dex/aave-v1/tokens.js.map +0 -1
  55. package/build/dex/aave-v1/types.d.ts +0 -12
  56. package/build/dex/aave-v1/types.js +0 -3
  57. package/build/dex/aave-v1/types.js.map +0 -1
  58. package/build/dex/idle-dao/idle-dao-pool.d.ts +0 -56
  59. package/build/dex/idle-dao/idle-dao-pool.js +0 -176
  60. package/build/dex/idle-dao/idle-dao-pool.js.map +0 -1
  61. package/build/dex/idle-dao/idle-dao-pooling-pool.d.ts +0 -16
  62. package/build/dex/idle-dao/idle-dao-pooling-pool.js +0 -57
  63. package/build/dex/idle-dao/idle-dao-pooling-pool.js.map +0 -1
  64. package/build/dex/idle-dao/scripts.d.ts +0 -9
  65. package/build/dex/idle-dao/scripts.js +0 -552
  66. package/build/dex/idle-dao/scripts.js.map +0 -1
  67. package/build/dex/inception/config.d.ts +0 -5
  68. package/build/dex/inception/config.js +0 -112
  69. package/build/dex/inception/config.js.map +0 -1
  70. package/build/dex/inception/inception-event-pool.d.ts +0 -23
  71. package/build/dex/inception/inception-event-pool.js +0 -34
  72. package/build/dex/inception/inception-event-pool.js.map +0 -1
  73. package/build/dex/inception/inception-native.d.ts +0 -43
  74. package/build/dex/inception/inception-native.js +0 -131
  75. package/build/dex/inception/inception-native.js.map +0 -1
  76. package/build/dex/inception/inception-pool.d.ts +0 -18
  77. package/build/dex/inception/inception-pool.js +0 -32
  78. package/build/dex/inception/inception-pool.js.map +0 -1
  79. package/build/dex/inception/inception-price-feed.d.ts +0 -19
  80. package/build/dex/inception/inception-price-feed.js +0 -51
  81. package/build/dex/inception/inception-price-feed.js.map +0 -1
  82. package/build/dex/inception/inception.d.ts +0 -44
  83. package/build/dex/inception/inception.js +0 -162
  84. package/build/dex/inception/inception.js.map +0 -1
  85. package/build/dex/inception/tokens.d.ts +0 -9
  86. package/build/dex/inception/tokens.js +0 -28
  87. package/build/dex/inception/tokens.js.map +0 -1
  88. package/build/dex/inception/types.d.ts +0 -22
  89. package/build/dex/inception/types.js +0 -3
  90. package/build/dex/inception/types.js.map +0 -1
  91. package/build/dex/inception/utils.d.ts +0 -7
  92. package/build/dex/inception/utils.js +0 -58
  93. package/build/dex/inception/utils.js.map +0 -1
  94. package/build/dex/infusion/config.d.ts +0 -4
  95. package/build/dex/infusion/config.js +0 -25
  96. package/build/dex/infusion/config.js.map +0 -1
  97. package/build/dex/infusion/infusion-stable-pool.d.ts +0 -4
  98. package/build/dex/infusion/infusion-stable-pool.js +0 -74
  99. package/build/dex/infusion/infusion-stable-pool.js.map +0 -1
  100. package/build/dex/infusion/infusion.d.ts +0 -54
  101. package/build/dex/infusion/infusion.js +0 -510
  102. package/build/dex/infusion/infusion.js.map +0 -1
  103. package/build/dex/infusion/types.d.ts +0 -47
  104. package/build/dex/infusion/types.js +0 -3
  105. package/build/dex/infusion/types.js.map +0 -1
  106. package/build/dex/infusion/utils/isStablePair.d.ts +0 -2
  107. package/build/dex/infusion/utils/isStablePair.js +0 -18
  108. package/build/dex/infusion/utils/isStablePair.js.map +0 -1
  109. package/build/dex/oswap/config.d.ts +0 -5
  110. package/build/dex/oswap/config.js +0 -31
  111. package/build/dex/oswap/config.js.map +0 -1
  112. package/build/dex/oswap/oswap-pool.d.ts +0 -51
  113. package/build/dex/oswap/oswap-pool.js +0 -150
  114. package/build/dex/oswap/oswap-pool.js.map +0 -1
  115. package/build/dex/oswap/oswap.d.ts +0 -46
  116. package/build/dex/oswap/oswap.js +0 -273
  117. package/build/dex/oswap/oswap.js.map +0 -1
  118. package/build/dex/oswap/types.d.ts +0 -20
  119. package/build/dex/oswap/types.js +0 -3
  120. package/build/dex/oswap/types.js.map +0 -1
  121. package/build/dex/platypus/asset.d.ts +0 -14
  122. package/build/dex/platypus/asset.js +0 -97
  123. package/build/dex/platypus/asset.js.map +0 -1
  124. package/build/dex/platypus/config.d.ts +0 -11
  125. package/build/dex/platypus/config.js +0 -69
  126. package/build/dex/platypus/config.js.map +0 -1
  127. package/build/dex/platypus/platypus.d.ts +0 -63
  128. package/build/dex/platypus/platypus.js +0 -473
  129. package/build/dex/platypus/platypus.js.map +0 -1
  130. package/build/dex/platypus/pool-avax.d.ts +0 -8
  131. package/build/dex/platypus/pool-avax.js +0 -54
  132. package/build/dex/platypus/pool-avax.js.map +0 -1
  133. package/build/dex/platypus/pool-base.d.ts +0 -19
  134. package/build/dex/platypus/pool-base.js +0 -86
  135. package/build/dex/platypus/pool-base.js.map +0 -1
  136. package/build/dex/platypus/pool-pure.d.ts +0 -8
  137. package/build/dex/platypus/pool-pure.js +0 -37
  138. package/build/dex/platypus/pool-pure.js.map +0 -1
  139. package/build/dex/platypus/pool-subscriber.d.ts +0 -14
  140. package/build/dex/platypus/pool-subscriber.js +0 -167
  141. package/build/dex/platypus/pool-subscriber.js.map +0 -1
  142. package/build/dex/platypus/pool.d.ts +0 -8
  143. package/build/dex/platypus/pool.js +0 -58
  144. package/build/dex/platypus/pool.js.map +0 -1
  145. package/build/dex/platypus/types.d.ts +0 -92
  146. package/build/dex/platypus/types.js +0 -10
  147. package/build/dex/platypus/types.js.map +0 -1
  148. package/build/dex/quickswap/spiritswap-v3.d.ts +0 -6
  149. package/build/dex/quickswap/spiritswap-v3.js +0 -19
  150. package/build/dex/quickswap/spiritswap-v3.js.map +0 -1
  151. package/build/dex/smardex/config.d.ts +0 -4
  152. package/build/dex/smardex/config.js +0 -64
  153. package/build/dex/smardex/config.js.map +0 -1
  154. package/build/dex/smardex/constants.d.ts +0 -18
  155. package/build/dex/smardex/constants.js +0 -33
  156. package/build/dex/smardex/constants.js.map +0 -1
  157. package/build/dex/smardex/sdk/constants.d.ts +0 -6
  158. package/build/dex/smardex/sdk/constants.js +0 -13
  159. package/build/dex/smardex/sdk/constants.js.map +0 -1
  160. package/build/dex/smardex/sdk/core.d.ts +0 -113
  161. package/build/dex/smardex/sdk/core.js +0 -499
  162. package/build/dex/smardex/sdk/core.js.map +0 -1
  163. package/build/dex/smardex/sdk/errors.d.ts +0 -15
  164. package/build/dex/smardex/sdk/errors.js +0 -22
  165. package/build/dex/smardex/sdk/errors.js.map +0 -1
  166. package/build/dex/smardex/sdk/types.d.ts +0 -62
  167. package/build/dex/smardex/sdk/types.js +0 -3
  168. package/build/dex/smardex/sdk/types.js.map +0 -1
  169. package/build/dex/smardex/sdk/utils.d.ts +0 -44
  170. package/build/dex/smardex/sdk/utils.js +0 -133
  171. package/build/dex/smardex/sdk/utils.js.map +0 -1
  172. package/build/dex/smardex/smardex-event-pool.d.ts +0 -28
  173. package/build/dex/smardex/smardex-event-pool.js +0 -119
  174. package/build/dex/smardex/smardex-event-pool.js.map +0 -1
  175. package/build/dex/smardex/smardex.d.ts +0 -65
  176. package/build/dex/smardex/smardex.js +0 -519
  177. package/build/dex/smardex/smardex.js.map +0 -1
  178. package/build/dex/smardex/types.d.ts +0 -55
  179. package/build/dex/smardex/types.js +0 -3
  180. package/build/dex/smardex/types.js.map +0 -1
  181. package/build/dex/solidly/forks-override/cone.d.ts +0 -22
  182. package/build/dex/solidly/forks-override/cone.js +0 -53
  183. package/build/dex/solidly/forks-override/cone.js.map +0 -1
  184. package/build/dex/solidly/forks-override/solisnek.d.ts +0 -8
  185. package/build/dex/solidly/forks-override/solisnek.js +0 -15
  186. package/build/dex/solidly/forks-override/solisnek.js.map +0 -1
  187. package/build/dex/swaap-v1/config.d.ts +0 -17
  188. package/build/dex/swaap-v1/config.js +0 -56
  189. package/build/dex/swaap-v1/config.js.map +0 -1
  190. package/build/dex/swaap-v1/constants.d.ts +0 -6
  191. package/build/dex/swaap-v1/constants.js +0 -17
  192. package/build/dex/swaap-v1/constants.js.map +0 -1
  193. package/build/dex/swaap-v1/libraries/ChainlinkUtils.d.ts +0 -25
  194. package/build/dex/swaap-v1/libraries/ChainlinkUtils.js +0 -62
  195. package/build/dex/swaap-v1/libraries/ChainlinkUtils.js.map +0 -1
  196. package/build/dex/swaap-v1/libraries/Const.d.ts +0 -18
  197. package/build/dex/swaap-v1/libraries/Const.js +0 -26
  198. package/build/dex/swaap-v1/libraries/Const.js.map +0 -1
  199. package/build/dex/swaap-v1/libraries/GeometricBrownianMotion.d.ts +0 -77
  200. package/build/dex/swaap-v1/libraries/GeometricBrownianMotion.js +0 -316
  201. package/build/dex/swaap-v1/libraries/GeometricBrownianMotion.js.map +0 -1
  202. package/build/dex/swaap-v1/libraries/LogExpMath.d.ts +0 -75
  203. package/build/dex/swaap-v1/libraries/LogExpMath.js +0 -418
  204. package/build/dex/swaap-v1/libraries/LogExpMath.js.map +0 -1
  205. package/build/dex/swaap-v1/libraries/MathMMM.d.ts +0 -185
  206. package/build/dex/swaap-v1/libraries/MathMMM.js +0 -453
  207. package/build/dex/swaap-v1/libraries/MathMMM.js.map +0 -1
  208. package/build/dex/swaap-v1/libraries/Num.d.ts +0 -31
  209. package/build/dex/swaap-v1/libraries/Num.js +0 -167
  210. package/build/dex/swaap-v1/libraries/Num.js.map +0 -1
  211. package/build/dex/swaap-v1/libraries/PoolQuotations.d.ts +0 -23
  212. package/build/dex/swaap-v1/libraries/PoolQuotations.js +0 -131
  213. package/build/dex/swaap-v1/libraries/PoolQuotations.js.map +0 -1
  214. package/build/dex/swaap-v1/libraries/Struct.d.ts +0 -56
  215. package/build/dex/swaap-v1/libraries/Struct.js +0 -3
  216. package/build/dex/swaap-v1/libraries/Struct.js.map +0 -1
  217. package/build/dex/swaap-v1/swaap-v1-pool.d.ts +0 -45
  218. package/build/dex/swaap-v1/swaap-v1-pool.js +0 -656
  219. package/build/dex/swaap-v1/swaap-v1-pool.js.map +0 -1
  220. package/build/dex/swaap-v1/swaap-v1.d.ts +0 -62
  221. package/build/dex/swaap-v1/swaap-v1.js +0 -379
  222. package/build/dex/swaap-v1/swaap-v1.js.map +0 -1
  223. package/build/dex/swaap-v1/types.d.ts +0 -106
  224. package/build/dex/swaap-v1/types.js +0 -9
  225. package/build/dex/swaap-v1/types.js.map +0 -1
  226. package/src/abi/infusion/InfusionFactory.json +0 -147
  227. package/src/abi/infusion/InfusionPair.json +0 -658
  228. package/src/abi/infusion/InfusionRouter.json +0 -442
  229. package/src/dex/infusion/config.ts +0 -27
  230. package/src/dex/infusion/infusion-e2e.test.ts +0 -110
  231. package/src/dex/infusion/infusion-integration.test.ts +0 -232
  232. package/src/dex/infusion/infusion-stable-pool.ts +0 -91
  233. package/src/dex/infusion/infusion.ts +0 -749
  234. package/src/dex/infusion/types.ts +0 -60
  235. package/src/dex/infusion/utils/isStablePair.ts +0 -18
@@ -1,749 +0,0 @@
1
- import { UniswapV2 } from '../uniswap-v2/uniswap-v2';
2
- import {
3
- Network,
4
- NULL_ADDRESS,
5
- SUBGRAPH_TIMEOUT,
6
- DEST_TOKEN_PARASWAP_TRANSFERS,
7
- SRC_TOKEN_PARASWAP_TRANSFERS,
8
- } from '../../constants';
9
- import {
10
- AdapterExchangeParam,
11
- Address,
12
- DexExchangeParam,
13
- ExchangePrices,
14
- PoolLiquidity,
15
- SimpleExchangeParam,
16
- Token,
17
- TransferFeeParams,
18
- } from '../../types';
19
- import { IDexHelper } from '../../dex-helper';
20
- import erc20ABI from '../../abi/erc20.json';
21
- import { UniswapData, UniswapV2Data } from '../uniswap-v2/types';
22
- import { getBigIntPow, getDexKeysWithNetwork, isETHAddress } from '../../utils';
23
- import infusionFactoryABI from '../../abi/infusion/InfusionFactory.json';
24
- import infusionPairABI from '../../abi/infusion/InfusionPair.json';
25
- import infusionRouterABI from '../../abi/infusion/InfusionRouter.json';
26
- import _ from 'lodash';
27
- import { NumberAsString, SwapSide } from '@paraswap/core';
28
- import { Interface, AbiCoder } from '@ethersproject/abi';
29
- import { InfusionStablePool } from './infusion-stable-pool';
30
- import { Uniswapv2ConstantProductPool } from '../uniswap-v2/uniswap-v2-constant-product-pool';
31
- import {
32
- PoolState,
33
- InfusionData,
34
- InfusionPair,
35
- InfusionPool,
36
- InfusionPoolOrderedParams,
37
- InfusionParam,
38
- } from './types';
39
- import { InfusionConfig, Adapters } from './config';
40
- import { applyTransferFee } from '../../lib/token-transfer-fee';
41
- import { isStablePair } from './utils/isStablePair';
42
-
43
- export enum InfusionRouterFunctions {
44
- sellExactEth = 'swapExactETHForTokens',
45
- sellExactToken = 'swapExactTokensForETH',
46
- swapExactIn = 'swapExactTokensForTokens',
47
- }
48
-
49
- export interface SmardexData extends Omit<UniswapV2Data, 'feeFactor'> {
50
- deadline: number;
51
- receiver: Address;
52
- }
53
-
54
- const VelodromeFactoryABI = [
55
- {
56
- inputs: [{ internalType: 'bool', name: '_stable', type: 'bool' }],
57
- name: 'getFee',
58
- outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
59
- stateMutability: 'view',
60
- type: 'function',
61
- },
62
- ];
63
-
64
- const velodromeFactoryIface = new Interface(VelodromeFactoryABI);
65
- const erc20Iface = new Interface(erc20ABI);
66
- const infusionPairIface = new Interface(infusionPairABI);
67
- const defaultAbiCoder = new AbiCoder();
68
-
69
- export class Infusion extends UniswapV2 {
70
- pairs: { [key: string]: InfusionPair } = {};
71
- stableFee?: number;
72
- volatileFee?: number;
73
-
74
- readonly isFeeOnTransferSupported: boolean = true;
75
- readonly SRC_TOKEN_DEX_TRANSFERS = 1;
76
- readonly DEST_TOKEN_DEX_TRANSFERS = 1;
77
-
78
- public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
79
- getDexKeysWithNetwork(_.pick(InfusionConfig, ['Infusion']));
80
-
81
- constructor(
82
- protected network: Network,
83
- dexKey: string,
84
- protected dexHelper: IDexHelper,
85
- isDynamicFees = true,
86
- factoryAddress?: Address,
87
- subgraphURL?: string,
88
- initCode?: string,
89
- feeCode?: number,
90
- poolGasCost?: number,
91
- routerAddress?: Address,
92
- ) {
93
- super(
94
- network,
95
- dexKey,
96
- dexHelper,
97
- isDynamicFees,
98
- factoryAddress !== undefined
99
- ? factoryAddress
100
- : InfusionConfig[dexKey][network].factoryAddress,
101
- subgraphURL === ''
102
- ? undefined
103
- : subgraphURL !== undefined
104
- ? subgraphURL
105
- : InfusionConfig[dexKey][network].subgraphURL,
106
- initCode !== undefined
107
- ? initCode
108
- : InfusionConfig[dexKey][network].initCode,
109
- feeCode !== undefined ? feeCode : InfusionConfig[dexKey][network].feeCode,
110
- poolGasCost !== undefined
111
- ? poolGasCost
112
- : InfusionConfig[dexKey][network].poolGasCost,
113
- infusionPairIface,
114
- Adapters[network] || undefined,
115
- );
116
-
117
- this.stableFee = InfusionConfig[dexKey][network].stableFee;
118
- this.volatileFee = InfusionConfig[dexKey][network].volatileFee;
119
-
120
- this.factory = new dexHelper.web3Provider.eth.Contract(
121
- infusionFactoryABI as any,
122
- factoryAddress !== undefined
123
- ? factoryAddress
124
- : InfusionConfig[dexKey][network].factoryAddress,
125
- );
126
-
127
- this.router =
128
- routerAddress !== undefined
129
- ? routerAddress
130
- : InfusionConfig[dexKey][network].router || '';
131
-
132
- this.feeFactor =
133
- InfusionConfig[dexKey][network].feeFactor || this.feeFactor;
134
-
135
- // this.exchangeRouterInterface = new Interface(infusionRouterABI);
136
- }
137
-
138
- async findInfusionPair(from: Token, to: Token, stable: boolean) {
139
- if (from.address.toLowerCase() === to.address.toLowerCase()) return null;
140
- const [token0, token1] =
141
- from.address.toLowerCase() < to.address.toLowerCase()
142
- ? [from, to]
143
- : [to, from];
144
-
145
- const typePostfix = this.poolPostfix(stable);
146
- const key = `${token0.address.toLowerCase()}-${token1.address.toLowerCase()}-${typePostfix}`;
147
- let pair = this.pairs[key];
148
- if (pair) return pair;
149
-
150
- let exchange = await this.factory.methods
151
- // Infusion has additional boolean parameter "StablePool"
152
- // At first we look for uniswap-like volatile pool
153
- .getPair(token0.address, token1.address, stable)
154
- .call();
155
-
156
- if (exchange === NULL_ADDRESS) {
157
- pair = { token0, token1, stable };
158
- } else {
159
- pair = { token0, token1, exchange, stable };
160
- }
161
- this.pairs[key] = pair;
162
- return pair;
163
- }
164
-
165
- async batchCatchUpPairs(pairs: [Token, Token][], blockNumber: number) {
166
- if (!blockNumber) return;
167
- const pairsToFetch: InfusionPair[] = [];
168
- for (const _pair of pairs) {
169
- for (const stable of [false, true]) {
170
- const pair = await this.findInfusionPair(_pair[0], _pair[1], stable);
171
- if (!(pair && pair.exchange)) continue;
172
- if (!pair.pool) {
173
- pairsToFetch.push(pair);
174
- } else if (!pair.pool.getState(blockNumber)) {
175
- pairsToFetch.push(pair);
176
- }
177
- }
178
- }
179
-
180
- if (!pairsToFetch.length) return;
181
-
182
- const reserves = await this.getManyPoolReserves(pairsToFetch, blockNumber);
183
-
184
- if (reserves.length !== pairsToFetch.length) {
185
- this.logger.error(
186
- `Error_getManyPoolReserves didn't get any pool reserves`,
187
- );
188
- }
189
-
190
- for (let i = 0; i < pairsToFetch.length; i++) {
191
- const pairState = reserves[i];
192
- const pair = pairsToFetch[i];
193
- if (!pair.pool) {
194
- await this.addPool(
195
- pair,
196
- pairState.reserves0,
197
- pairState.reserves1,
198
- pairState.feeCode,
199
- blockNumber,
200
- );
201
- } else pair.pool.setState(pairState, blockNumber);
202
- }
203
- }
204
-
205
- async getManyPoolReserves(
206
- pairs: InfusionPair[],
207
- blockNumber: number,
208
- ): Promise<PoolState[]> {
209
- try {
210
- const multiCallFeeData = pairs.map(pair =>
211
- this.getFeesMultiCallData(pair),
212
- );
213
- const calldata = pairs
214
- .map((pair, i) => {
215
- let calldata = [
216
- {
217
- target: pair.token0.address,
218
- callData: erc20Iface.encodeFunctionData('balanceOf', [
219
- pair.exchange!,
220
- ]),
221
- },
222
- {
223
- target: pair.token1.address,
224
- callData: erc20Iface.encodeFunctionData('balanceOf', [
225
- pair.exchange!,
226
- ]),
227
- },
228
- ];
229
- if (this.isDynamicFees) calldata.push(multiCallFeeData[i]!.callEntry);
230
- return calldata;
231
- })
232
- .flat();
233
-
234
- const data: { returnData: any[] } =
235
- await this.dexHelper.multiContract.methods
236
- .aggregate(calldata)
237
- .call({}, blockNumber);
238
-
239
- const returnData = _.chunk(data.returnData, this.isDynamicFees ? 3 : 2);
240
-
241
- return pairs.map((pair, i) => ({
242
- reserves0: defaultAbiCoder
243
- .decode(['uint256'], returnData[i][0])[0]
244
- .toString(),
245
- reserves1: defaultAbiCoder
246
- .decode(['uint256'], returnData[i][1])[0]
247
- .toString(),
248
- feeCode: this.isDynamicFees
249
- ? multiCallFeeData[i]!.callDecoder(returnData[i][2])
250
- : (pair.stable ? this.stableFee : this.volatileFee) || this.feeCode,
251
- }));
252
- } catch (e) {
253
- this.logger.error(
254
- `Error_getManyPoolReserves could not get reserves with error:`,
255
- e,
256
- );
257
- return [];
258
- }
259
- }
260
-
261
- async getSellPrice(
262
- priceParams: InfusionPoolOrderedParams,
263
- srcAmount: bigint,
264
- ): Promise<bigint> {
265
- return priceParams.stable
266
- ? InfusionStablePool.getSellPrice(priceParams, srcAmount, this.feeFactor)
267
- : Uniswapv2ConstantProductPool.getSellPrice(
268
- priceParams,
269
- srcAmount,
270
- this.feeFactor,
271
- );
272
- }
273
-
274
- async getBuyPrice(
275
- priceParams: InfusionPoolOrderedParams,
276
- srcAmount: bigint,
277
- ): Promise<bigint> {
278
- if (priceParams.stable) throw new Error(`Buy not supported`);
279
- return Uniswapv2ConstantProductPool.getBuyPrice(
280
- priceParams,
281
- srcAmount,
282
- this.feeFactor,
283
- );
284
- }
285
-
286
- async getPricesVolume(
287
- _from: Token,
288
- _to: Token,
289
- amounts: bigint[],
290
- side: SwapSide,
291
- blockNumber: number,
292
- // list of pool identifiers to use for pricing, if undefined use all pools
293
- limitPools?: string[],
294
- transferFees: TransferFeeParams = {
295
- srcFee: 0,
296
- destFee: 0,
297
- srcDexFee: 0,
298
- destDexFee: 0,
299
- },
300
- ): Promise<ExchangePrices<UniswapV2Data> | null> {
301
- try {
302
- if (side === SwapSide.BUY) return null; // Buy side not implemented yet
303
- const from = this.dexHelper.config.wrapETH(_from);
304
- const to = this.dexHelper.config.wrapETH(_to);
305
-
306
- if (from.address.toLowerCase() === to.address.toLowerCase()) {
307
- return null;
308
- }
309
-
310
- const tokenAddress = [
311
- from.address.toLowerCase(),
312
- to.address.toLowerCase(),
313
- ]
314
- .sort((a, b) => (a > b ? 1 : -1))
315
- .join('_');
316
-
317
- await this.batchCatchUpPairs([[from, to]], blockNumber);
318
-
319
- const resultPromises = [false, true].map(async stable => {
320
- // We don't support fee on transfer for stable pools yet
321
- if (
322
- stable &&
323
- (transferFees.srcFee !== 0 || transferFees.srcDexFee !== 0)
324
- ) {
325
- return null;
326
- }
327
-
328
- const poolIdentifier =
329
- `${this.dexKey}_${tokenAddress}` + this.poolPostfix(stable);
330
-
331
- if (limitPools && limitPools.every(p => p !== poolIdentifier))
332
- return null;
333
-
334
- const isSell = side === SwapSide.SELL;
335
- const pairParam = await this.getInfusionPairOrderedParams(
336
- from,
337
- to,
338
- blockNumber,
339
- stable,
340
- transferFees.srcDexFee,
341
- );
342
-
343
- if (!pairParam) return null;
344
-
345
- const unitAmount = getBigIntPow(
346
- // @ts-expect-error Buy side is not implemented yet
347
- side === SwapSide.BUY ? to.decimals : from.decimals,
348
- );
349
-
350
- const [unitVolumeWithFee, ...amountsWithFee] = applyTransferFee(
351
- [unitAmount, ...amounts],
352
- side,
353
- isSell ? transferFees.srcFee : transferFees.destFee,
354
- isSell ? SRC_TOKEN_PARASWAP_TRANSFERS : DEST_TOKEN_PARASWAP_TRANSFERS,
355
- );
356
-
357
- const unit =
358
- // @ts-expect-error Buy side is not implemented yet
359
- side === SwapSide.BUY
360
- ? await this.getBuyPricePath(unitVolumeWithFee, [pairParam])
361
- : await this.getSellPricePath(unitVolumeWithFee, [pairParam]);
362
-
363
- const prices =
364
- // @ts-expect-error Buy side is not implemented yet
365
- side === SwapSide.BUY
366
- ? await Promise.all(
367
- amountsWithFee.map(amount =>
368
- amount === 0n
369
- ? 0n
370
- : this.getBuyPricePath(amount, [pairParam]),
371
- ),
372
- )
373
- : await Promise.all(
374
- amountsWithFee.map(amount =>
375
- amount === 0n
376
- ? 0n
377
- : this.getSellPricePath(amount, [pairParam]),
378
- ),
379
- );
380
-
381
- const [unitOutWithFee, ...outputsWithFee] = applyTransferFee(
382
- [unit, ...prices],
383
- side,
384
- // This part is confusing, because we treat differently SELL and BUY fees
385
- // If Buy, we should apply transfer fee on srcToken on top of dexFee applied earlier
386
- // But for Sell we should apply only one dexFee
387
- isSell ? transferFees.destDexFee : transferFees.srcFee,
388
- isSell ? this.DEST_TOKEN_DEX_TRANSFERS : SRC_TOKEN_PARASWAP_TRANSFERS,
389
- );
390
-
391
- return {
392
- prices: outputsWithFee,
393
- unit: unitOutWithFee,
394
- data: {
395
- router: this.router,
396
- path: [from.address.toLowerCase(), to.address.toLowerCase()],
397
- factory: this.factoryAddress,
398
- initCode: this.initCode,
399
- feeFactor: this.feeFactor,
400
- isFeeTokenInRoute: Object.values(transferFees).some(f => f !== 0),
401
- pools: [
402
- {
403
- address: pairParam.exchange,
404
- fee: parseInt(pairParam.fee),
405
- direction: pairParam.direction,
406
- },
407
- ],
408
- },
409
- exchange: this.dexKey,
410
- poolIdentifier,
411
- gasCost: this.poolGasCost,
412
- poolAddresses: [pairParam.exchange],
413
- };
414
- });
415
-
416
- const resultPools = (await Promise.all(
417
- resultPromises,
418
- )) as ExchangePrices<UniswapV2Data>;
419
- const resultPoolsFiltered = resultPools.filter(item => !!item); // filter null elements
420
- return resultPoolsFiltered.length > 0 ? resultPoolsFiltered : null;
421
- } catch (e) {
422
- if (blockNumber === 0)
423
- this.logger.error(
424
- `Error_getPricesVolume: Aurelius block manager not yet instantiated`,
425
- );
426
- this.logger.error(`Error_getPrices:`, e);
427
- return null;
428
- }
429
- }
430
-
431
- async getTopPoolsForToken(
432
- tokenAddress: Address,
433
- count: number,
434
- ): Promise<PoolLiquidity[]> {
435
- if (!this.subgraphURL) return [];
436
-
437
- let stableFieldKey = 'isStable';
438
-
439
- // if (this.dexKey.toLowerCase() === 'infusion') {
440
- // stableFieldKey = 'stable';
441
- // } else if (this.dexKey.toLowerCase() !== 'infusionv2') {
442
- // stableFieldKey = 'isStable';
443
- // }
444
-
445
- const query = `query ($token: Bytes!, $count: Int) {
446
- pools0: pairs(first: $count, orderBy: reserveUSD, orderDirection: desc, where: {token0: $token, reserve0_gt: 1, reserve1_gt: 1}) {
447
- id
448
- ${stableFieldKey}
449
- token0 {
450
- id
451
- decimals
452
- }
453
- token1 {
454
- id
455
- decimals
456
- }
457
- reserveUSD
458
- }
459
- pools1: pairs(first: $count, orderBy: reserveUSD, orderDirection: desc, where: {token1: $token, reserve0_gt: 1, reserve1_gt: 1}) {
460
- id
461
- ${stableFieldKey}
462
- token0 {
463
- id
464
- decimals
465
- }
466
- token1 {
467
- id
468
- decimals
469
- }
470
- reserveUSD
471
- }
472
- }`;
473
-
474
- const { data } = await this.dexHelper.httpRequest.post(
475
- this.subgraphURL,
476
- {
477
- query,
478
- variables: { token: tokenAddress.toLowerCase(), count },
479
- },
480
- SUBGRAPH_TIMEOUT,
481
- );
482
-
483
- if (!(data && data.pools0 && data.pools1))
484
- throw new Error("Couldn't fetch the pools from the subgraph");
485
- const pools0 = _.map(data.pools0, pool => ({
486
- exchange: this.dexKey,
487
- stable: pool[stableFieldKey],
488
- address: pool.id.toLowerCase(),
489
- connectorTokens: [
490
- {
491
- address: pool.token1.id.toLowerCase(),
492
- decimals: parseInt(pool.token1.decimals),
493
- },
494
- ],
495
- liquidityUSD: parseFloat(pool.reserveUSD),
496
- }));
497
-
498
- const pools1 = _.map(data.pools1, pool => ({
499
- exchange: this.dexKey,
500
- stable: pool[stableFieldKey],
501
- address: pool.id.toLowerCase(),
502
- connectorTokens: [
503
- {
504
- address: pool.token0.id.toLowerCase(),
505
- decimals: parseInt(pool.token0.decimals),
506
- },
507
- ],
508
- liquidityUSD: parseFloat(pool.reserveUSD),
509
- }));
510
-
511
- return _.slice(
512
- _.sortBy(_.concat(pools0, pools1), [pool => -1 * pool.liquidityUSD]),
513
- 0,
514
- count,
515
- );
516
- }
517
-
518
- // Same as at uniswap-v2-pool.json, but extended with decimals and stable
519
- async getInfusionPairOrderedParams(
520
- from: Token,
521
- to: Token,
522
- blockNumber: number,
523
- stable: boolean,
524
- tokenDexTransferFee: number,
525
- ): Promise<InfusionPoolOrderedParams | null> {
526
- const pair = await this.findInfusionPair(from, to, stable);
527
- if (!(pair && pair.pool && pair.exchange)) return null;
528
- const pairState = pair?.pool.getState(blockNumber);
529
-
530
- if (!pairState) {
531
- this.logger.error(
532
- `Error_orderPairParams expected reserves, got none (maybe the pool doesn't exist) ${
533
- from.symbol || from.address
534
- } ${to.symbol || to.address}`,
535
- );
536
- return null;
537
- }
538
-
539
- const fee = (pairState.feeCode + tokenDexTransferFee).toString();
540
- const pairReversed =
541
- pair.token1.address.toLowerCase() === from.address.toLowerCase();
542
- if (pairReversed) {
543
- return {
544
- tokenIn: from.address,
545
- tokenOut: to.address,
546
- reservesIn: pairState.reserves1,
547
- reservesOut: pairState.reserves0,
548
- fee,
549
- direction: false,
550
- exchange: pair.exchange,
551
- decimalsIn: from.decimals,
552
- decimalsOut: to.decimals,
553
- stable,
554
- };
555
- }
556
- return {
557
- tokenIn: from.address,
558
- tokenOut: to.address,
559
- reservesIn: pairState.reserves0,
560
- reservesOut: pairState.reserves1,
561
- fee,
562
- direction: true,
563
- exchange: pair.exchange,
564
- decimalsIn: from.decimals,
565
- decimalsOut: to.decimals,
566
- stable,
567
- };
568
- }
569
-
570
- async getPoolIdentifiers(
571
- _from: Token,
572
- _to: Token,
573
- side: SwapSide,
574
- blockNumber: number,
575
- ): Promise<string[]> {
576
- if (side === SwapSide.BUY) return [];
577
-
578
- const from = this.dexHelper.config.wrapETH(_from);
579
- const to = this.dexHelper.config.wrapETH(_to);
580
-
581
- if (from.address.toLowerCase() === to.address.toLowerCase()) {
582
- return [];
583
- }
584
-
585
- const tokenAddress = [from.address.toLowerCase(), to.address.toLowerCase()]
586
- .sort((a, b) => (a > b ? 1 : -1))
587
- .join('_');
588
-
589
- const poolIdentifier = `${this.dexKey}_${tokenAddress}`;
590
- const poolIdentifierUniswap = poolIdentifier + this.poolPostfix(false);
591
- const poolIdentifierStable = poolIdentifier + this.poolPostfix(true);
592
- return [poolIdentifierUniswap, poolIdentifierStable];
593
- }
594
-
595
- poolPostfix(stable: boolean) {
596
- return stable ? 'S' : 'V';
597
- }
598
-
599
- async getSimpleParam(
600
- src: Address,
601
- dest: Address,
602
- srcAmount: NumberAsString,
603
- destAmount: NumberAsString,
604
- data: UniswapData,
605
- side: SwapSide,
606
- ): Promise<SimpleExchangeParam> {
607
- if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
608
-
609
- let routerMethod: any;
610
- let routerArgs: InfusionParam;
611
- const stable = isStablePair(this.network, src, dest);
612
-
613
- const from = isETHAddress(src)
614
- ? this.dexHelper.config.data.wrappedNativeTokenAddress
615
- : src;
616
- const to = isETHAddress(dest)
617
- ? this.dexHelper.config.data.wrappedNativeTokenAddress
618
- : dest;
619
-
620
- routerMethod = isETHAddress(src)
621
- ? InfusionRouterFunctions.sellExactEth
622
- : InfusionRouterFunctions.swapExactIn;
623
- routerMethod = isETHAddress(dest)
624
- ? InfusionRouterFunctions.sellExactToken
625
- : routerMethod;
626
-
627
- routerArgs = isETHAddress(src)
628
- ? [
629
- destAmount,
630
- [{ from, to, stable }],
631
- this.augustusAddress,
632
- Math.floor(new Date().getTime()) + 120,
633
- ]
634
- : [
635
- srcAmount,
636
- destAmount,
637
- [{ from, to, stable }],
638
- this.augustusAddress,
639
- Math.floor(new Date().getTime()) + 120,
640
- ];
641
-
642
- // console.log('routerMethod', routerMethod, 'routerArgs', routerArgs);
643
- const swapData = new Interface(infusionRouterABI).encodeFunctionData(
644
- routerMethod,
645
- routerArgs as InfusionParam,
646
- );
647
- return this.buildSimpleParamWithoutWETHConversion(
648
- src,
649
- srcAmount,
650
- dest,
651
- destAmount,
652
- swapData,
653
- data.router,
654
- );
655
- }
656
-
657
- getAdapterParam(
658
- srcToken: Address,
659
- destToken: Address,
660
- srcAmount: NumberAsString,
661
- toAmount: NumberAsString, // required for buy case
662
- data: InfusionData,
663
- side: SwapSide,
664
- ): AdapterExchangeParam {
665
- return {
666
- targetExchange: data.router,
667
- payload: '',
668
- networkFee: '0',
669
- };
670
- }
671
-
672
- getDexParam(
673
- src: Address,
674
- dest: Address,
675
- srcAmount: NumberAsString,
676
- destAmount: NumberAsString,
677
- recipient: Address,
678
- data: InfusionData,
679
- side: SwapSide,
680
- ): DexExchangeParam {
681
- if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
682
-
683
- let routerMethod: any;
684
- let routerArgs: InfusionParam;
685
- const stable = isStablePair(this.network, src, dest);
686
-
687
- const from = isETHAddress(src)
688
- ? this.dexHelper.config.data.wrappedNativeTokenAddress
689
- : src;
690
- const to = isETHAddress(dest)
691
- ? this.dexHelper.config.data.wrappedNativeTokenAddress
692
- : dest;
693
-
694
- routerMethod = isETHAddress(src)
695
- ? InfusionRouterFunctions.sellExactEth
696
- : InfusionRouterFunctions.swapExactIn;
697
- routerMethod = isETHAddress(dest)
698
- ? InfusionRouterFunctions.sellExactToken
699
- : routerMethod;
700
-
701
- routerArgs = isETHAddress(src)
702
- ? [
703
- destAmount,
704
- [{ from, to, stable }],
705
- recipient,
706
- Math.floor(new Date().getTime()) + 120,
707
- ]
708
- : [
709
- srcAmount,
710
- destAmount,
711
- [{ from, to, stable }],
712
- recipient,
713
- Math.floor(new Date().getTime()) + 120,
714
- ];
715
-
716
- const exchangeData = new Interface(infusionRouterABI).encodeFunctionData(
717
- routerMethod,
718
- routerArgs as InfusionParam,
719
- );
720
-
721
- return {
722
- needWrapNative: this.needWrapNative,
723
- dexFuncHasRecipient: true,
724
- exchangeData,
725
- targetExchange: data.router,
726
- returnAmountPos: undefined,
727
- };
728
- }
729
-
730
- protected getFeesMultiCallData(pair: InfusionPair) {
731
- const callEntry = {
732
- target: this.factoryAddress,
733
- callData: velodromeFactoryIface.encodeFunctionData('getFee', [
734
- pair.stable,
735
- ]),
736
- };
737
- const callDecoder = (values: any[]) =>
738
- parseInt(
739
- velodromeFactoryIface
740
- .decodeFunctionResult('getFee', values)[0]
741
- .toString(),
742
- );
743
-
744
- return {
745
- callEntry,
746
- callDecoder,
747
- };
748
- }
749
- }