@paraswap/dex-lib 3.9.7-fluid-dex.2 → 3.10.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 (234) hide show
  1. package/.vscode/launch.json +53 -0
  2. package/.vscode/settings.json +2 -0
  3. package/.vscode/tasks.json +15 -0
  4. package/build/abi/{integral/pool.json → inception/inception-ineth-pool.json} +558 -537
  5. package/build/abi/inception/inception-ratio-feed.json +329 -0
  6. package/build/abi/inception/inception-vault.json +991 -0
  7. package/build/abi/uniswap-v3/AlienBaseV3Router.abi.json +52 -0
  8. package/build/abi/{integral/relayer.json → wombat/pool.json} +474 -721
  9. package/build/config.js +28 -0
  10. package/build/config.js.map +1 -1
  11. package/build/constants.d.ts +2 -1
  12. package/build/constants.js +1 -0
  13. package/build/constants.js.map +1 -1
  14. package/build/dex/aave-v1/aave-v1.d.ts +2 -1
  15. package/build/dex/aave-v1/aave-v1.js +23 -0
  16. package/build/dex/aave-v1/aave-v1.js.map +1 -1
  17. package/build/dex/aave-v2/tokens-avalanche.json +44 -0
  18. package/build/dex/aave-v2/tokens-mainnet.json +188 -0
  19. package/build/dex/aave-v2/tokens-polygon.json +44 -0
  20. package/build/dex/aave-v3/aave-v3.js +1 -1
  21. package/build/dex/aave-v3/aave-v3.js.map +1 -1
  22. package/build/dex/aave-v3/config.js +6 -0
  23. package/build/dex/aave-v3/config.js.map +1 -1
  24. package/build/dex/aave-v3-stata/config.js +3 -3
  25. package/build/dex/aave-v3-stata/config.js.map +1 -1
  26. package/build/dex/algebra/algebra-pool-v1_9_bidirectional_fee.js +1 -1
  27. package/build/dex/algebra/algebra-pool-v1_9_bidirectional_fee.js.map +1 -1
  28. package/build/dex/algebra/config.js +20 -0
  29. package/build/dex/algebra/config.js.map +1 -1
  30. package/build/dex/balancer-v2/balancer-v2.js +1 -1
  31. package/build/dex/balancer-v2/balancer-v2.js.map +1 -1
  32. package/build/dex/balancer-v2/config.js +4 -0
  33. package/build/dex/balancer-v2/config.js.map +1 -1
  34. package/build/dex/curve-v1/config.js +36 -0
  35. package/build/dex/curve-v1/config.js.map +1 -1
  36. package/build/dex/curve-v1/curve-v1.js +1 -1
  37. package/build/dex/curve-v1/curve-v1.js.map +1 -1
  38. package/build/dex/curve-v1-factory/constants.js +1 -0
  39. package/build/dex/curve-v1-factory/constants.js.map +1 -1
  40. package/build/dex/curve-v1-factory/curve-v1-factory.js +1 -1
  41. package/build/dex/curve-v1-factory/curve-v1-factory.js.map +1 -1
  42. package/build/dex/curve-v1-stable-ng/config.js +28 -0
  43. package/build/dex/curve-v1-stable-ng/config.js.map +1 -1
  44. package/build/dex/inception/config.d.ts +4 -0
  45. package/build/dex/inception/config.js +109 -0
  46. package/build/dex/inception/config.js.map +1 -0
  47. package/build/dex/inception/inception-event-pool.d.ts +16 -0
  48. package/build/dex/inception/inception-event-pool.js +49 -0
  49. package/build/dex/inception/inception-event-pool.js.map +1 -0
  50. package/build/dex/inception/inception.d.ts +43 -0
  51. package/build/dex/inception/inception.js +180 -0
  52. package/build/dex/inception/inception.js.map +1 -0
  53. package/build/dex/inception/tokens.d.ts +9 -0
  54. package/build/dex/inception/tokens.js +28 -0
  55. package/build/dex/inception/tokens.js.map +1 -0
  56. package/build/dex/inception/types.d.ts +22 -0
  57. package/build/dex/{fluid-dex → inception}/types.js.map +1 -1
  58. package/build/dex/inception/utils.d.ts +7 -0
  59. package/build/dex/inception/utils.js +58 -0
  60. package/build/dex/inception/utils.js.map +1 -0
  61. package/build/dex/index.js +0 -2
  62. package/build/dex/index.js.map +1 -1
  63. package/build/dex/jarvis.d.ts +2 -1
  64. package/build/dex/jarvis.js +58 -0
  65. package/build/dex/jarvis.js.map +1 -1
  66. package/build/dex/maker-psm/scripts/gem.abi.json +136 -0
  67. package/build/dex/maker-psm/scripts/validate-state.d.ts +0 -0
  68. package/build/dex/maker-psm/scripts/validate-state.js +185 -0
  69. package/build/dex/maker-psm/scripts/validate-state.js.map +1 -0
  70. package/build/dex/maker-psm/scripts/vat.abi.json +243 -0
  71. package/build/dex/platypus/platypus.d.ts +2 -1
  72. package/build/dex/platypus/platypus.js +45 -0
  73. package/build/dex/platypus/platypus.js.map +1 -1
  74. package/build/dex/solidly-v3/scripts/check-event-pool-event.d.ts +1 -0
  75. package/build/dex/solidly-v3/scripts/check-event-pool-event.js +53 -0
  76. package/build/dex/solidly-v3/scripts/check-event-pool-event.js.map +1 -0
  77. package/build/dex/stable-pool.js +1 -8
  78. package/build/dex/stable-pool.js.map +1 -1
  79. package/build/dex/swaap-v1/swaap-v1.d.ts +2 -1
  80. package/build/dex/swaap-v1/swaap-v1.js +14 -0
  81. package/build/dex/swaap-v1/swaap-v1.js.map +1 -1
  82. package/build/dex/uniswap-v2/config.js +14 -0
  83. package/build/dex/uniswap-v2/config.js.map +1 -1
  84. package/build/dex/uniswap-v2/nomiswap-v2.js +10 -0
  85. package/build/dex/uniswap-v2/nomiswap-v2.js.map +1 -1
  86. package/build/dex/uniswap-v2/uniswap-v2.js +1 -1
  87. package/build/dex/uniswap-v2/uniswap-v2.js.map +1 -1
  88. package/build/dex/uniswap-v3/config.js +14 -0
  89. package/build/dex/uniswap-v3/config.js.map +1 -1
  90. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.d.ts +17 -0
  91. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js +64 -0
  92. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js.map +1 -0
  93. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.d.ts +17 -0
  94. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js +79 -0
  95. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js.map +1 -0
  96. package/build/dex/uniswap-v3/uniswap-v3-pricing.d.ts +4 -0
  97. package/build/dex/uniswap-v3/uniswap-v3-pricing.js +43 -0
  98. package/build/dex/uniswap-v3/uniswap-v3-pricing.js.map +1 -0
  99. package/build/dex/uniswap-v3/uniswap-v3-worker.d.ts +1 -0
  100. package/build/dex/uniswap-v3/uniswap-v3-worker.js +10 -0
  101. package/build/dex/uniswap-v3/uniswap-v3-worker.js.map +1 -0
  102. package/build/dex/uniswap-v3/uniswap-v3.js +1 -0
  103. package/build/dex/uniswap-v3/uniswap-v3.js.map +1 -1
  104. package/build/dex/weth/config.js +5 -0
  105. package/build/dex/weth/config.js.map +1 -1
  106. package/build/dex/wusdm/config.js +8 -0
  107. package/build/dex/wusdm/config.js.map +1 -1
  108. package/build/dex/wusdm/wusdm.d.ts +1 -0
  109. package/build/dex/wusdm/wusdm.js +9 -4
  110. package/build/dex/wusdm/wusdm.js.map +1 -1
  111. package/build/dex/zerox/index.d.ts +4 -26
  112. package/build/dex/zerox/index.js +50 -42
  113. package/build/dex/zerox/index.js.map +1 -1
  114. package/build/dex/zerox/types.d.ts +29 -0
  115. package/build/dex/zerox/types.js +8 -1
  116. package/build/dex/zerox/types.js.map +1 -1
  117. package/build/executor/WETHBytecodeBuilder.js +1 -0
  118. package/build/executor/WETHBytecodeBuilder.js.map +1 -1
  119. package/package.json +1 -1
  120. package/src/config.ts +29 -1
  121. package/src/constants.ts +1 -0
  122. package/src/dex/aave-v3/aave-v3-e2e.test.ts +69 -0
  123. package/src/dex/aave-v3/aave-v3.ts +1 -1
  124. package/src/dex/aave-v3/config.ts +7 -0
  125. package/src/dex/aave-v3-stata/config.ts +4 -4
  126. package/src/dex/algebra/algebra-e2e.test.ts +22 -0
  127. package/src/dex/algebra/algebra-pool-v1_9_bidirectional_fee.ts +1 -1
  128. package/src/dex/algebra/config.ts +21 -0
  129. package/src/dex/balancer-v2/balancer-v2-e2e.test.ts +22 -250
  130. package/src/dex/balancer-v2/balancer-v2.ts +1 -1
  131. package/src/dex/balancer-v2/config.ts +4 -0
  132. package/src/dex/curve-v1/config.ts +36 -0
  133. package/src/dex/curve-v1/curve-v1-e2e.test.ts +69 -0
  134. package/src/dex/curve-v1/curve-v1.ts +1 -1
  135. package/src/dex/curve-v1-factory/constants.ts +1 -0
  136. package/src/dex/curve-v1-factory/curve-v1-factory.ts +2 -1
  137. package/src/dex/curve-v1-stable-ng/config.ts +28 -0
  138. package/src/dex/curve-v1-stable-ng/curve-v1-stable-ng-e2e.test.ts +20 -0
  139. package/src/dex/index.ts +0 -2
  140. package/src/dex/uniswap-v2/config.ts +16 -0
  141. package/src/dex/uniswap-v2/nomiswap-v2.ts +11 -0
  142. package/src/dex/uniswap-v2/uniswap-v2-e2e-gnosis.test.ts +283 -0
  143. package/src/dex/uniswap-v2/uniswap-v2.ts +1 -1
  144. package/src/dex/uniswap-v3/config.ts +14 -0
  145. package/src/dex/uniswap-v3/uniswap-v3-e2e.test.ts +24 -0
  146. package/src/dex/uniswap-v3/uniswap-v3.ts +1 -0
  147. package/src/dex/weth/config.ts +5 -0
  148. package/src/dex/wusdm/config.ts +8 -0
  149. package/src/dex/wusdm/wusdm-e2e.test.ts +38 -0
  150. package/src/dex/wusdm/wusdm.ts +11 -4
  151. package/src/executor/WETHBytecodeBuilder.ts +3 -2
  152. package/tests/constants-e2e.ts +77 -2
  153. package/.idea/aws.xml +0 -17
  154. package/.idea/codeStyles/Project.xml +0 -19
  155. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  156. package/.idea/misc.xml +0 -6
  157. package/.idea/modules.xml +0 -8
  158. package/.idea/paraswap-dex-lib.iml +0 -9
  159. package/.idea/prettier.xml +0 -7
  160. package/.idea/vcs.xml +0 -6
  161. package/build/abi/fluid-dex/dexFactory.abi.json +0 -506
  162. package/build/abi/fluid-dex/fluid-dex.abi.json +0 -922
  163. package/build/abi/fluid-dex/liquidityUserModule.abi.json +0 -145
  164. package/build/abi/fluid-dex/resolver.abi.json +0 -999
  165. package/build/abi/integral/factory.json +0 -333
  166. package/build/abi/integral/oracle.json +0 -501
  167. package/build/dex/fluid-dex/config.d.ts +0 -4
  168. package/build/dex/fluid-dex/config.js +0 -20
  169. package/build/dex/fluid-dex/config.js.map +0 -1
  170. package/build/dex/fluid-dex/fluid-dex-generate-pool.d.ts +0 -49
  171. package/build/dex/fluid-dex/fluid-dex-generate-pool.js +0 -104
  172. package/build/dex/fluid-dex/fluid-dex-generate-pool.js.map +0 -1
  173. package/build/dex/fluid-dex/fluid-dex-pool.d.ts +0 -47
  174. package/build/dex/fluid-dex/fluid-dex-pool.js +0 -119
  175. package/build/dex/fluid-dex/fluid-dex-pool.js.map +0 -1
  176. package/build/dex/fluid-dex/fluid-dex.d.ts +0 -127
  177. package/build/dex/fluid-dex/fluid-dex.js +0 -502
  178. package/build/dex/fluid-dex/fluid-dex.js.map +0 -1
  179. package/build/dex/fluid-dex/types.d.ts +0 -52
  180. package/build/dex/integral/config.d.ts +0 -4
  181. package/build/dex/integral/config.js +0 -20
  182. package/build/dex/integral/config.js.map +0 -1
  183. package/build/dex/integral/context.d.ts +0 -40
  184. package/build/dex/integral/context.js +0 -158
  185. package/build/dex/integral/context.js.map +0 -1
  186. package/build/dex/integral/integral-factory.d.ts +0 -24
  187. package/build/dex/integral/integral-factory.js +0 -95
  188. package/build/dex/integral/integral-factory.js.map +0 -1
  189. package/build/dex/integral/integral-pool.d.ts +0 -50
  190. package/build/dex/integral/integral-pool.js +0 -149
  191. package/build/dex/integral/integral-pool.js.map +0 -1
  192. package/build/dex/integral/integral-pricing.d.ts +0 -18
  193. package/build/dex/integral/integral-pricing.js +0 -114
  194. package/build/dex/integral/integral-pricing.js.map +0 -1
  195. package/build/dex/integral/integral-relayer.d.ts +0 -42
  196. package/build/dex/integral/integral-relayer.js +0 -192
  197. package/build/dex/integral/integral-relayer.js.map +0 -1
  198. package/build/dex/integral/integral-token.d.ts +0 -27
  199. package/build/dex/integral/integral-token.js +0 -59
  200. package/build/dex/integral/integral-token.js.map +0 -1
  201. package/build/dex/integral/integral.d.ts +0 -41
  202. package/build/dex/integral/integral.js +0 -376
  203. package/build/dex/integral/integral.js.map +0 -1
  204. package/build/dex/integral/types.d.ts +0 -85
  205. package/build/dex/integral/types.js +0 -9
  206. package/build/dex/integral/types.js.map +0 -1
  207. package/build/dex/integral/utils-e2e.d.ts +0 -9
  208. package/build/dex/integral/utils-e2e.js +0 -131
  209. package/build/dex/integral/utils-e2e.js.map +0 -1
  210. package/build/dex/integral/utils.d.ts +0 -17
  211. package/build/dex/integral/utils.js +0 -94
  212. package/build/dex/integral/utils.js.map +0 -1
  213. package/build/dex/trader-joe-v2.1/optimizer.d.ts +0 -2
  214. package/build/dex/trader-joe-v2.1/optimizer.js +0 -47
  215. package/build/dex/trader-joe-v2.1/optimizer.js.map +0 -1
  216. package/src/abi/fluid-dex/dexFactory.abi.json +0 -506
  217. package/src/abi/fluid-dex/fluid-dex.abi.json +0 -922
  218. package/src/abi/fluid-dex/liquidityUserModule.abi.json +0 -145
  219. package/src/abi/fluid-dex/resolver.abi.json +0 -999
  220. package/src/dex/fluid-dex/config.ts +0 -20
  221. package/src/dex/fluid-dex/fluid-dex-e2e.test.ts +0 -154
  222. package/src/dex/fluid-dex/fluid-dex-events.test.ts +0 -294
  223. package/src/dex/fluid-dex/fluid-dex-generate-pool.ts +0 -143
  224. package/src/dex/fluid-dex/fluid-dex-integration.test.ts +0 -304
  225. package/src/dex/fluid-dex/fluid-dex-pool.ts +0 -178
  226. package/src/dex/fluid-dex/fluid-dex.ts +0 -780
  227. package/src/dex/fluid-dex/types.ts +0 -62
  228. package/tests/constants-e2e.d.ts +0 -22
  229. package/tests/constants-e2e.js +0 -1942
  230. package/tests/constants-e2e.js.map +0 -1
  231. package/tests/smart-tokens.d.ts +0 -36
  232. package/tests/smart-tokens.js +0 -64
  233. package/tests/smart-tokens.js.map +0 -1
  234. /package/build/dex/{fluid-dex → inception}/types.js +0 -0
@@ -1,780 +0,0 @@
1
- import { BytesLike } from 'ethers/lib/utils';
2
- import { Interface } from '@ethersproject/abi';
3
- import {
4
- Token,
5
- Address,
6
- ExchangePrices,
7
- PoolPrices,
8
- AdapterExchangeParam,
9
- PoolLiquidity,
10
- DexExchangeParam,
11
- Logger,
12
- } from '../../types';
13
- import { SwapSide, Network } from '../../constants';
14
- import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
15
- import { Context, IDex } from '../../dex/idex';
16
- import { IDexHelper } from '../../dex-helper/idex-helper';
17
- import {
18
- CollateralReserves,
19
- DebtReserves,
20
- FluidDexData,
21
- FluidDexPool,
22
- FluidDexPoolState,
23
- Pool,
24
- } from './types';
25
- import { SimpleExchange } from '../simple-exchange';
26
- import FluidDexPoolABI from '../../abi/fluid-dex/fluid-dex.abi.json';
27
- import { FluidDexConfig, FLUID_DEX_GAS_COST } from './config';
28
- import { FluidDexEventPool } from './fluid-dex-pool';
29
- import { FluidDexCommonAddresses } from './fluid-dex-generate-pool';
30
- import { getDexKeysWithNetwork, getBigIntPow } from '../../utils';
31
- import { extractReturnAmountPosition } from '../../executor/utils';
32
- import { MultiResult } from '../../lib/multi-wrapper';
33
- import { generalDecoder } from '../../lib/decoders';
34
-
35
- export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
36
- eventPools: { [id: string]: FluidDexEventPool } = {};
37
- readonly hasConstantPriceLargeAmounts = false;
38
- readonly needWrapNative = false;
39
- readonly isFeeOnTransferSupported = false;
40
- public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
41
- getDexKeysWithNetwork(FluidDexConfig);
42
-
43
- logger: Logger;
44
-
45
- pools: FluidDexPool[] = [];
46
-
47
- readonly fluidCommonAddresses: FluidDexCommonAddresses;
48
-
49
- readonly fluidDexPoolIface: Interface;
50
-
51
- FEE_100_PERCENT = BigInt(1000000);
52
-
53
- constructor(
54
- readonly network: Network,
55
- readonly dexKey: string,
56
- readonly dexHelper: IDexHelper,
57
- ) {
58
- super(dexHelper, dexKey);
59
- this.logger = dexHelper.getLogger(dexKey);
60
- this.fluidCommonAddresses = new FluidDexCommonAddresses(
61
- 'FluidDex',
62
- FluidDexConfig['FluidDex'][network].commonAddresses,
63
- network,
64
- dexHelper,
65
- this.logger,
66
- );
67
- this.fluidDexPoolIface = new Interface(FluidDexPoolABI);
68
- }
69
-
70
- private async fetchFluidDexPools(
71
- blockNumber: number,
72
- ): Promise<FluidDexPool[]> {
73
- const poolsFromResolver =
74
- await this.fluidCommonAddresses.getStateOrGenerate(blockNumber, false);
75
- return poolsFromResolver.map(pool => ({
76
- id: `FluidDex_${pool.address.toLowerCase()}`,
77
- address: pool.address.toLowerCase(),
78
- token0: pool.token0.toLowerCase(),
79
- token1: pool.token1.toLowerCase(),
80
- }));
81
- }
82
-
83
- // Initialize pricing is called once in the start of
84
- // pricing service. It is intended to setup the integration
85
- // for pricing requests. It is optional for a DEX to
86
- // implement this function
87
- async initializePricing(blockNumber: number) {
88
- this.pools = await this.fetchFluidDexPools(blockNumber);
89
- for (const pool of this.pools) {
90
- if (!this.eventPools[pool.id]) {
91
- this.eventPools[pool.id] = new FluidDexEventPool(
92
- 'FluidDex',
93
- pool.address,
94
- this.fluidCommonAddresses.commonAddresses,
95
- this.network,
96
- this.dexHelper,
97
- this.logger,
98
- );
99
- }
100
- }
101
- }
102
-
103
- getAdapters(side: SwapSide) {
104
- return null;
105
- }
106
-
107
- decodePools = (result: MultiResult<BytesLike> | BytesLike): Pool[] => {
108
- return generalDecoder(
109
- result,
110
- ['tuple(address pool, address token0, address token1)[]'],
111
- undefined,
112
- decoded => {
113
- return decoded.map((decodedPool: any) => ({
114
- address: decodedPool[0][0].toLowerCase(),
115
- token0: decodedPool[0][1].toLowerCase(),
116
- token1: decodedPool[0][2].toLowerCase(),
117
- }));
118
- },
119
- );
120
- };
121
-
122
- // Returns list of pool identifiers that can be used
123
- // for a given swap. poolIdentifiers must be unique
124
- // across DEXes. It is recommended to use
125
- // ${dexKey}_${poolAddress} as a poolIdentifier
126
- async getPoolIdentifiers(
127
- srcToken: Token,
128
- destToken: Token,
129
- side: SwapSide,
130
- blockNumber: number,
131
- ): Promise<string[]> {
132
- const pool = await this.getPoolByTokenPair(
133
- srcToken.address,
134
- destToken.address,
135
- );
136
- return pool ? [pool.id] : [];
137
- }
138
-
139
- async getPoolByTokenPair(
140
- srcToken: Address,
141
- destToken: Address,
142
- ): Promise<FluidDexPool | null> {
143
- const srcAddress = srcToken.toLowerCase();
144
- const destAddress = destToken.toLowerCase();
145
-
146
- // A pair must have 2 different tokens.
147
- if (srcAddress === destAddress) return null;
148
-
149
- for (const pool of this.pools) {
150
- if (
151
- (srcAddress === pool.token0 && destAddress === pool.token1) ||
152
- (srcAddress === pool.token1 && destAddress === pool.token0)
153
- ) {
154
- return pool;
155
- }
156
- }
157
- return null;
158
- }
159
-
160
- // Returns pool prices for amounts.
161
- // If limitPools is defined only pools in limitPools
162
- // should be used. If limitPools is undefined then
163
- // any pools can be used.
164
- async getPricesVolume(
165
- srcToken: Token,
166
- destToken: Token,
167
- amounts: bigint[],
168
- side: SwapSide,
169
- blockNumber: number,
170
- limitPools?: string[],
171
- ): Promise<null | ExchangePrices<FluidDexData>> {
172
- try {
173
- if (srcToken.address.toLowerCase() === destToken.address.toLowerCase())
174
- return null;
175
-
176
- if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
177
- // Get the pool to use.
178
- const pool = await this.getPoolByTokenPair(
179
- srcToken.address,
180
- destToken.address,
181
- );
182
- if (!pool) return null;
183
-
184
- // Make sure the pool meets the optional limitPools filter.
185
- if (limitPools && !limitPools.includes(pool.id)) return null;
186
-
187
- const eventPool = this.eventPools[pool.id];
188
-
189
- if (!eventPool) {
190
- this.logger.error(`fluid-dex pool ${pool.id}: No EventPool found.`);
191
-
192
- return null;
193
- }
194
-
195
- const state = await eventPool.getStateOrGenerate(blockNumber);
196
-
197
- const prices = amounts.map(amount => {
198
- return this.swapIn(
199
- srcToken.address.toLowerCase() === pool.token0.toLowerCase(),
200
- amount,
201
- state.collateralReserves,
202
- state.debtReserves,
203
- srcToken.decimals,
204
- destToken.decimals,
205
- BigInt(state.fee),
206
- );
207
- });
208
- return [
209
- {
210
- prices: prices,
211
- unit: getBigIntPow(
212
- (side === SwapSide.SELL ? destToken : srcToken).decimals,
213
- ),
214
- data: {
215
- colReserves: state.collateralReserves,
216
- debtReserves: state.debtReserves,
217
- exchange: this.dexKey,
218
- },
219
- exchange: this.dexKey,
220
- poolIdentifier: pool.id,
221
- gasCost: FLUID_DEX_GAS_COST,
222
- poolAddresses: [pool.address],
223
- },
224
- ];
225
- } catch (e) {
226
- this.logger.error(
227
- `Error_getPricesVolume ${srcToken.address || srcToken.symbol}, ${
228
- destToken.address || destToken.symbol
229
- }, ${side}:`,
230
- e,
231
- );
232
-
233
- return null;
234
- }
235
- }
236
-
237
- // Returns estimated gas cost of calldata for this DEX in multiSwap
238
- getCalldataGasCost(poolPrices: PoolPrices<FluidDexData>): number | number[] {
239
- return CALLDATA_GAS_COST.DEX_NO_PAYLOAD;
240
- }
241
-
242
- // Encode params required by the exchange adapter
243
- // V5: Used for multiSwap, buy & megaSwap
244
- // V6: Not used, can be left blank
245
- // Hint: abiCoder.encodeParameter() could be useful
246
- getAdapterParam(
247
- srcToken: string,
248
- destToken: string,
249
- srcAmount: string,
250
- destAmount: string,
251
- data: FluidDexData,
252
- side: SwapSide,
253
- ): AdapterExchangeParam {
254
- if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
255
- const { exchange } = data;
256
-
257
- // Encode here the payload for adapter
258
- const payload = '';
259
-
260
- return {
261
- targetExchange: exchange,
262
- payload,
263
- networkFee: '0',
264
- };
265
- }
266
-
267
- // This is called once before getTopPoolsForToken is
268
- // called for multiple tokens. This can be helpful to
269
- // update common state required for calculating
270
- // getTopPoolsForToken. It is optional for a DEX
271
- // to implement this
272
- async updatePoolState(): Promise<void> {
273
- this.initializePricing(await this.dexHelper.provider.getBlockNumber());
274
- }
275
-
276
- // Returns list of top pools based on liquidity. Max
277
- // limit number pools should be returned.
278
- async getTopPoolsForToken(
279
- tokenAddress: Address,
280
- limit: number,
281
- ): Promise<PoolLiquidity[]> {
282
- //@TODO
283
- return [];
284
- }
285
-
286
- async getDexParam(
287
- srcToken: Address,
288
- destToken: Address,
289
- srcAmount: string,
290
- destAmount: string,
291
- recipient: Address,
292
- data: FluidDexData,
293
- side: SwapSide,
294
- context: Context,
295
- executorAddress: Address,
296
- ): Promise<DexExchangeParam> {
297
- if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
298
-
299
- let args: any;
300
- let returnAmountPos: number | undefined;
301
-
302
- const method = 'swapIn';
303
-
304
- returnAmountPos = extractReturnAmountPosition(
305
- this.fluidDexPoolIface,
306
- method,
307
- 'amountOut_',
308
- 1,
309
- );
310
-
311
- const pool = await this.getPoolByTokenPair(srcToken, destToken);
312
-
313
- if (pool!.token0.toLowerCase() !== srcToken.toLowerCase()) {
314
- args = [false, BigInt(srcAmount), BigInt(destAmount), recipient];
315
- } else {
316
- args = [true, BigInt(srcAmount), BigInt(destAmount), recipient];
317
- }
318
-
319
- const swapData = this.fluidDexPoolIface.encodeFunctionData(method, args);
320
-
321
- return {
322
- needWrapNative: this.needWrapNative,
323
- dexFuncHasRecipient: true,
324
- exchangeData: swapData,
325
- targetExchange: pool!.address,
326
- returnAmountPos,
327
- };
328
- }
329
-
330
- /**
331
- * Calculates the output amount for a given input amount in a swap operation.
332
- * @param swap0To1 - Direction of the swap. True if swapping token0 for token1, false otherwise.
333
- * @param amountIn - The amount of input token to be swapped (as a BigInt).
334
- * @param colReserves - The reserves of the collateral pool.
335
- * @param debtReserves - The reserves of the debt pool.
336
- * @param inDecimals - The number of decimals for the input token.
337
- * @param outDecimals - The number of decimals for the output token.
338
- * @returns The calculated output amount (as a BigInt).
339
- */
340
- private swapIn(
341
- swap0To1: boolean,
342
- amountIn: bigint,
343
- colReserves: CollateralReserves,
344
- debtReserves: DebtReserves,
345
- inDecimals: number,
346
- outDecimals: number,
347
- fee: bigint,
348
- ): bigint {
349
- if (amountIn === 0n) {
350
- return 0n; // Return 0 if input amount is 0
351
- }
352
- if (
353
- colReserves.token0RealReserves + debtReserves.token0RealReserves == 0n &&
354
- colReserves.token1RealReserves + debtReserves.token1RealReserves == 0n
355
- ) {
356
- return 0n;
357
- }
358
- const amountInAdjusted =
359
- (((amountIn * (this.FEE_100_PERCENT - fee)) / this.FEE_100_PERCENT) *
360
- BigInt(10 ** 12)) /
361
- BigInt(10 ** inDecimals);
362
-
363
- const amountOut = this.swapInAdjusted(
364
- swap0To1,
365
- amountInAdjusted, // Convert back to number for internal calculations
366
- colReserves,
367
- debtReserves,
368
- );
369
- const result = (amountOut * BigInt(10 ** outDecimals)) / BigInt(10 ** 12);
370
- return result;
371
- }
372
-
373
- /**
374
- * Calculates the output amount for a given input amount in a swap operation.
375
- * @param swap0To1 - Direction of the swap. True if swapping token0 for token1, false otherwise.
376
- * @param amountToSwap - The amount of input token to be swapped.
377
- * @param colReserves - The reserves of the collateral pool.
378
- * @param debtReserves - The reserves of the debt pool.
379
- * @returns The calculated output amount.
380
- */
381
- private swapInAdjusted(
382
- swap0To1: boolean,
383
- amountToSwap: bigint,
384
- colReserves: CollateralReserves,
385
- debtReserves: DebtReserves,
386
- ): bigint {
387
- const {
388
- token0RealReserves,
389
- token1RealReserves,
390
- token0ImaginaryReserves,
391
- token1ImaginaryReserves,
392
- } = colReserves;
393
-
394
- const {
395
- token0RealReserves: debtToken0RealReserves,
396
- token1RealReserves: debtToken1RealReserves,
397
- token0ImaginaryReserves: debtToken0ImaginaryReserves,
398
- token1ImaginaryReserves: debtToken1ImaginaryReserves,
399
- } = debtReserves;
400
-
401
- // Check if all reserves of collateral pool are greater than 0
402
- const colPoolEnabled =
403
- token0RealReserves > BigInt(0) &&
404
- token1RealReserves > BigInt(0) &&
405
- token0ImaginaryReserves > BigInt(0) &&
406
- token1ImaginaryReserves > BigInt(0);
407
-
408
- // Check if all reserves of debt pool are greater than 0
409
- const debtPoolEnabled =
410
- debtToken0RealReserves > BigInt(0) &&
411
- debtToken1RealReserves > BigInt(0) &&
412
- debtToken0ImaginaryReserves > BigInt(0) &&
413
- debtToken1ImaginaryReserves > BigInt(0);
414
-
415
- let colReserveIn: bigint,
416
- colReserveOut: bigint,
417
- debtReserveIn: bigint,
418
- debtReserveOut: bigint;
419
- let colIReserveIn: bigint,
420
- colIReserveOut: bigint,
421
- debtIReserveIn: bigint,
422
- debtIReserveOut: bigint;
423
-
424
- if (swap0To1) {
425
- colReserveIn = token0RealReserves;
426
- colReserveOut = token1RealReserves;
427
- colIReserveIn = token0ImaginaryReserves;
428
- colIReserveOut = token1ImaginaryReserves;
429
- debtReserveIn = debtToken0RealReserves;
430
- debtReserveOut = debtToken1RealReserves;
431
- debtIReserveIn = debtToken0ImaginaryReserves;
432
- debtIReserveOut = debtToken1ImaginaryReserves;
433
- } else {
434
- colReserveIn = token1RealReserves;
435
- colReserveOut = token0RealReserves;
436
- colIReserveIn = token1ImaginaryReserves;
437
- colIReserveOut = token0ImaginaryReserves;
438
- debtReserveIn = debtToken1RealReserves;
439
- debtReserveOut = debtToken0RealReserves;
440
- debtIReserveIn = debtToken1ImaginaryReserves;
441
- debtIReserveOut = debtToken0ImaginaryReserves;
442
- }
443
-
444
- let a: bigint;
445
- if (colPoolEnabled && debtPoolEnabled) {
446
- a = this.swapRoutingIn(
447
- amountToSwap,
448
- colIReserveOut,
449
- colIReserveIn,
450
- debtIReserveOut,
451
- debtIReserveIn,
452
- );
453
- } else if (debtPoolEnabled) {
454
- a = BigInt(-1); // Route from debt pool
455
- } else if (colPoolEnabled) {
456
- a = amountToSwap + BigInt(1); // Route from collateral pool
457
- } else {
458
- throw new Error('No pools are enabled');
459
- }
460
-
461
- let amountOutCollateral = BigInt(0);
462
- let amountOutDebt = BigInt(0);
463
-
464
- if (a <= BigInt(0)) {
465
- // Entire trade routes through debt pool
466
- amountOutDebt = this.getAmountOut(
467
- amountToSwap,
468
- debtIReserveIn,
469
- debtIReserveOut,
470
- );
471
- } else if (a >= amountToSwap) {
472
- // Entire trade routes through collateral pool
473
- amountOutCollateral = this.getAmountOut(
474
- amountToSwap,
475
- colIReserveIn,
476
- colIReserveOut,
477
- );
478
- } else {
479
- // Trade routes through both pools
480
- amountOutCollateral = this.getAmountOut(a, colIReserveIn, colIReserveOut);
481
- amountOutDebt = this.getAmountOut(
482
- amountToSwap - a,
483
- debtIReserveIn,
484
- debtIReserveOut,
485
- );
486
- }
487
-
488
- if (amountOutDebt > debtReserveOut) {
489
- return 0n;
490
- }
491
-
492
- if (amountOutCollateral > colReserveOut) {
493
- return 0n;
494
- }
495
- const totalAmountOut = amountOutCollateral + amountOutDebt;
496
-
497
- return totalAmountOut;
498
- }
499
-
500
- /**
501
- * Given an input amount of asset and pair reserves, returns the maximum output amount of the other asset.
502
- * @param amountIn - The amount of input asset.
503
- * @param iReserveIn - Imaginary token reserve with input amount.
504
- * @param iReserveOut - Imaginary token reserve of output amount.
505
- * @returns The maximum output amount of the other asset.
506
- */
507
- private getAmountOut(
508
- amountIn: bigint,
509
- iReserveIn: bigint,
510
- iReserveOut: bigint,
511
- ): bigint {
512
- // Both numerator and denominator are scaled to 1e6 to factor in fee scaling.
513
- const numerator = amountIn * iReserveOut;
514
- const denominator = iReserveIn + amountIn;
515
-
516
- // Using the swap formula: (AmountIn * iReserveY) / (iReserveX + AmountIn)
517
- // We use division with rounding down, which is the default for bigint division
518
-
519
- return numerator / denominator;
520
- }
521
-
522
- /**
523
- * Given an output amount of asset and pair reserves, returns the input amount of the other asset
524
- * @param amountOut - Desired output amount of the asset.
525
- * @param iReserveIn - Imaginary token reserve of input amount.
526
- * @param iReserveOut - Imaginary token reserve of output amount.
527
- * @returns The input amount of the other asset.
528
- */
529
- private getAmountIn(
530
- amountOut: bigint,
531
- iReserveIn: bigint,
532
- iReserveOut: bigint,
533
- ): bigint {
534
- // Both numerator and denominator are scaled to 1e6 to factor in fee scaling.
535
- const numerator = amountOut * iReserveIn;
536
- const denominator = iReserveOut - amountOut;
537
-
538
- // Using the swap formula: (AmountOut * iReserveX) / (iReserveY - AmountOut)
539
- return numerator / denominator;
540
- }
541
-
542
- /**
543
- * Calculates how much of a swap should go through the collateral pool for output amount.
544
- * @param t - Total amount out.
545
- * @param x - Imaginary reserves of token in of collateral.
546
- * @param y - Imaginary reserves of token out of collateral.
547
- * @param x2 - Imaginary reserves of token in of debt.
548
- * @param y2 - Imaginary reserves of token out of debt.
549
- * @returns How much swap should go through collateral pool. Remaining will go from debt.
550
- * @note If a < 0 then entire trade route through debt pool and debt pool arbitrage with col pool.
551
- * @note If a > t then entire trade route through col pool and col pool arbitrage with debt pool.
552
- * @note If a > 0 & a < t then swap will route through both pools.
553
- */
554
- private swapRoutingOut(
555
- t: bigint,
556
- x: bigint,
557
- y: bigint,
558
- x2: bigint,
559
- y2: bigint,
560
- ): bigint {
561
- // Adding 1e18 precision
562
- const xyRoot = BigInt(
563
- Math.floor(Math.sqrt(Number(x * y * BigInt(10n ** 18n)))),
564
- );
565
- const x2y2Root = BigInt(
566
- Math.floor(Math.sqrt(Number(x2 * y2 * BigInt(10n ** 18n)))),
567
- );
568
-
569
- // 1e18 precision gets cancelled out in division
570
- const numerator = t * xyRoot + y * x2y2Root - y2 * xyRoot;
571
- const denominator = xyRoot + x2y2Root;
572
-
573
- // Use integer division (rounds down)
574
- const a = numerator / denominator;
575
-
576
- return a;
577
- }
578
-
579
- /**
580
- * Calculates how much of a swap should go through the collateral pool.
581
- * @param t - Total amount in.
582
- * @param x - Imaginary reserves of token out of collateral.
583
- * @param y - Imaginary reserves of token in of collateral.
584
- * @param x2 - Imaginary reserves of token out of debt.
585
- * @param y2 - Imaginary reserves of token in of debt.
586
- * @returns How much swap should go through collateral pool. Remaining will go from debt.
587
- * @note If a < 0 then entire trade route through debt pool and debt pool arbitrage with col pool.
588
- * @note If a > t then entire trade route through col pool and col pool arbitrage with debt pool.
589
- * @note If a > 0 & a < t then swap will route through both pools.
590
- */
591
- private swapRoutingIn(
592
- t: bigint,
593
- x: bigint,
594
- y: bigint,
595
- x2: bigint,
596
- y2: bigint,
597
- ): bigint {
598
- // Adding 1e18 precision
599
-
600
- const xyRoot = BigInt(Math.floor(Math.sqrt(Number(x * y * BigInt(1e18)))));
601
- const x2y2Root = BigInt(
602
- Math.floor(Math.sqrt(Number(x2 * y2 * BigInt(1e18)))),
603
- );
604
-
605
- // Calculating 'a' using the given formula
606
- const a =
607
- (Number(y2) * Number(xyRoot) +
608
- Number(t) * Number(xyRoot) -
609
- Number(y) * Number(x2y2Root)) /
610
- (Number(xyRoot) + Number(x2y2Root));
611
- return BigInt(Math.floor(a));
612
- }
613
-
614
- /**
615
- * Calculates the input amount for a given output amount in a swap operation.
616
- * @param {boolean} swap0to1 - Direction of the swap. True if swapping token0 for token1, false otherwise.
617
- * @param {bigint} amountOut - The amount of output token to be swapped.
618
- * @param {Reserves} colReserves - The reserves of the collateral pool.
619
- * @param {Reserves} debtReserves - The reserves of the debt pool.
620
- * @param {number} inDecimals - The number of decimals for the input token.
621
- * @param {number} outDecimals - The number of decimals for the output token.
622
- * @param {number} fee - The fee for the swap. 1e4 = 1%
623
- * @returns {bigint} amountIn - The calculated input amount required for the swap.
624
- */
625
- private swapOut(
626
- swap0to1: boolean,
627
- amountOut: bigint,
628
- colReserves: CollateralReserves,
629
- debtReserves: DebtReserves,
630
- inDecimals: number,
631
- outDecimals: number,
632
- fee: bigint,
633
- ): bigint {
634
- const amountOutAdjusted =
635
- (amountOut * BigInt(10 ** 12)) / BigInt(10 ** outDecimals);
636
- const amountIn = this.swapOutAdjusted(
637
- swap0to1,
638
- amountOutAdjusted,
639
- colReserves,
640
- debtReserves,
641
- );
642
-
643
- const FEE_100_PERCENT = BigInt(1e6); // Assuming this constant is defined elsewhere
644
-
645
- const result =
646
- ((amountIn * FEE_100_PERCENT) / (FEE_100_PERCENT - fee)) *
647
- BigInt(10 ** (inDecimals - 12));
648
-
649
- return result;
650
- }
651
-
652
- /**
653
- * Calculates the input amount for a given output amount in a swap operation.
654
- * @param {boolean} swap0to1 - Direction of the swap. True if swapping token0 for token1, false otherwise.
655
- * @param {bigint} amountOut - The amount of output token to be swapped.
656
- * @param {CollateralReserves} colReserves - The reserves of the collateral pool.
657
- * @param {DebtReserves} debtReserves - The reserves of the debt pool.
658
- * @returns {bigint} The calculated input amount required for the swap.
659
- */
660
- private swapOutAdjusted(
661
- swap0to1: boolean,
662
- amountOut: bigint,
663
- colReserves: CollateralReserves,
664
- debtReserves: DebtReserves,
665
- ): bigint {
666
- const {
667
- token0RealReserves,
668
- token1RealReserves,
669
- token0ImaginaryReserves,
670
- token1ImaginaryReserves,
671
- } = colReserves;
672
-
673
- const {
674
- token0RealReserves: debtToken0RealReserves,
675
- token1RealReserves: debtToken1RealReserves,
676
- token0ImaginaryReserves: debtToken0ImaginaryReserves,
677
- token1ImaginaryReserves: debtToken1ImaginaryReserves,
678
- } = debtReserves;
679
-
680
- // Check if all reserves of collateral pool are greater than 0
681
- const colPoolEnabled =
682
- token0RealReserves > 0n &&
683
- token1RealReserves > 0n &&
684
- token0ImaginaryReserves > 0n &&
685
- token1ImaginaryReserves > 0n;
686
-
687
- // Check if all reserves of debt pool are greater than 0
688
- const debtPoolEnabled =
689
- debtToken0RealReserves > 0n &&
690
- debtToken1RealReserves > 0n &&
691
- debtToken0ImaginaryReserves > 0n &&
692
- debtToken1ImaginaryReserves > 0n;
693
-
694
- let colReserveIn: bigint,
695
- colReserveOut: bigint,
696
- debtReserveIn: bigint,
697
- debtReserveOut: bigint;
698
- let colIReserveIn: bigint,
699
- colIReserveOut: bigint,
700
- debtIReserveIn: bigint,
701
- debtIReserveOut: bigint;
702
-
703
- if (swap0to1) {
704
- colReserveIn = token0RealReserves;
705
- colReserveOut = token1RealReserves;
706
- colIReserveIn = token0ImaginaryReserves;
707
- colIReserveOut = token1ImaginaryReserves;
708
- debtReserveIn = debtToken0RealReserves;
709
- debtReserveOut = debtToken1RealReserves;
710
- debtIReserveIn = debtToken0ImaginaryReserves;
711
- debtIReserveOut = debtToken1ImaginaryReserves;
712
- } else {
713
- colReserveIn = token1RealReserves;
714
- colReserveOut = token0RealReserves;
715
- colIReserveIn = token1ImaginaryReserves;
716
- colIReserveOut = token0ImaginaryReserves;
717
- debtReserveIn = debtToken1RealReserves;
718
- debtReserveOut = debtToken0RealReserves;
719
- debtIReserveIn = debtToken1ImaginaryReserves;
720
- debtIReserveOut = debtToken0ImaginaryReserves;
721
- }
722
-
723
- let a: bigint;
724
- if (colPoolEnabled && debtPoolEnabled) {
725
- a = this.swapRoutingOut(
726
- amountOut,
727
- colIReserveIn,
728
- colIReserveOut,
729
- debtIReserveIn,
730
- debtIReserveOut,
731
- );
732
- } else if (debtPoolEnabled) {
733
- a = -1n; // Route from debt pool
734
- } else if (colPoolEnabled) {
735
- a = amountOut + 1n; // Route from collateral pool
736
- } else {
737
- throw new Error('No pools are enabled');
738
- }
739
-
740
- let amountInCollateral = 0n;
741
- let amountInDebt = 0n;
742
-
743
- if (a <= 0n) {
744
- // Entire trade routes through debt pool
745
- amountInDebt = this.getAmountIn(
746
- amountOut,
747
- debtIReserveIn,
748
- debtIReserveOut,
749
- );
750
- if (amountOut > debtReserveOut) {
751
- return 2n ** 64n - 1n; // BigInt max value
752
- }
753
- } else if (a >= amountOut) {
754
- // Entire trade routes through collateral pool
755
- amountInCollateral = this.getAmountIn(
756
- amountOut,
757
- colIReserveIn,
758
- colIReserveOut,
759
- );
760
- if (amountOut > colReserveOut) {
761
- return 2n ** 64n - 1n; // BigInt max value
762
- }
763
- } else {
764
- // Trade routes through both pools
765
- amountInCollateral = this.getAmountIn(a, colIReserveIn, colIReserveOut);
766
- amountInDebt = this.getAmountIn(
767
- amountOut - a,
768
- debtIReserveIn,
769
- debtIReserveOut,
770
- );
771
- if (amountOut - a > debtReserveOut || a > debtReserveOut) {
772
- return 2n ** 64n - 1n; // BigInt max value
773
- }
774
- }
775
-
776
- const totalAmountIn = amountInCollateral + amountInDebt;
777
-
778
- return totalAmountIn;
779
- }
780
- }