@paraswap/dex-lib 3.8.21-traderjoe-multihop.2 → 3.8.21

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 (256) hide show
  1. package/build/abi/AaveV1_lending_pool.json +27 -0
  2. package/build/abi/inception/inception-ineth-pool.json +1062 -0
  3. package/build/abi/inception/inception-ineth.json +609 -0
  4. package/build/abi/inception/inception-ratio-feed.json +93 -0
  5. package/build/abi/inception/inception-vault.json +991 -0
  6. package/build/abi/infusion/InfusionFactory.json +147 -0
  7. package/build/abi/infusion/InfusionPair.json +658 -0
  8. package/build/abi/infusion/InfusionRouter.json +442 -0
  9. package/build/abi/lite-psm/dai.json +333 -0
  10. package/build/abi/lite-psm/psm.json +468 -0
  11. package/build/abi/lite-psm/usdc.json +814 -0
  12. package/build/abi/oswap/oswap.abi.json +452 -0
  13. package/build/abi/platypus/asset.json +490 -0
  14. package/build/abi/platypus/avax-pool.json +820 -0
  15. package/build/abi/platypus/oracle.json +119 -0
  16. package/build/abi/platypus/pool.json +677 -0
  17. package/build/abi/smardex/all/smardex-router.json +648 -0
  18. package/build/abi/smardex/layer-1/smardex-factory.json +242 -0
  19. package/build/abi/smardex/layer-1/smardex-pool.json +506 -0
  20. package/build/abi/smardex/layer-2/smardex-factory.json +185 -0
  21. package/build/abi/smardex/layer-2/smardex-pool.json +578 -0
  22. package/build/abi/swaap-v1/pool.json +1346 -0
  23. package/build/dex/aave-v1/aave-v1.d.ts +34 -0
  24. package/build/dex/aave-v1/aave-v1.js +180 -0
  25. package/build/dex/aave-v1/aave-v1.js.map +1 -0
  26. package/build/dex/aave-v1/config.d.ts +11 -0
  27. package/build/dex/aave-v1/config.js +20 -0
  28. package/build/dex/aave-v1/config.js.map +1 -0
  29. package/build/dex/aave-v1/tokens-mainnet.json +134 -0
  30. package/build/dex/aave-v1/tokens.d.ts +3 -0
  31. package/build/dex/aave-v1/tokens.js +50 -0
  32. package/build/dex/aave-v1/tokens.js.map +1 -0
  33. package/build/dex/aave-v1/types.d.ts +12 -0
  34. package/build/dex/aave-v1/types.js +3 -0
  35. package/build/dex/aave-v1/types.js.map +1 -0
  36. package/build/dex/idle-dao/idle-dao-pool.d.ts +56 -0
  37. package/build/dex/idle-dao/idle-dao-pool.js +176 -0
  38. package/build/dex/idle-dao/idle-dao-pool.js.map +1 -0
  39. package/build/dex/idle-dao/idle-dao-pooling-pool.d.ts +16 -0
  40. package/build/dex/idle-dao/idle-dao-pooling-pool.js +57 -0
  41. package/build/dex/idle-dao/idle-dao-pooling-pool.js.map +1 -0
  42. package/build/dex/idle-dao/scripts.d.ts +9 -0
  43. package/build/dex/idle-dao/scripts.js +552 -0
  44. package/build/dex/idle-dao/scripts.js.map +1 -0
  45. package/build/dex/inception/config.d.ts +5 -0
  46. package/build/dex/inception/config.js +112 -0
  47. package/build/dex/inception/config.js.map +1 -0
  48. package/build/dex/inception/inception-event-pool.d.ts +23 -0
  49. package/build/dex/inception/inception-event-pool.js +34 -0
  50. package/build/dex/inception/inception-event-pool.js.map +1 -0
  51. package/build/dex/inception/inception-native.d.ts +43 -0
  52. package/build/dex/inception/inception-native.js +131 -0
  53. package/build/dex/inception/inception-native.js.map +1 -0
  54. package/build/dex/inception/inception-pool.d.ts +18 -0
  55. package/build/dex/inception/inception-pool.js +32 -0
  56. package/build/dex/inception/inception-pool.js.map +1 -0
  57. package/build/dex/inception/inception-price-feed.d.ts +19 -0
  58. package/build/dex/inception/inception-price-feed.js +51 -0
  59. package/build/dex/inception/inception-price-feed.js.map +1 -0
  60. package/build/dex/inception/inception.d.ts +44 -0
  61. package/build/dex/inception/inception.js +162 -0
  62. package/build/dex/inception/inception.js.map +1 -0
  63. package/build/dex/inception/tokens.d.ts +9 -0
  64. package/build/dex/inception/tokens.js +28 -0
  65. package/build/dex/inception/tokens.js.map +1 -0
  66. package/build/dex/inception/types.d.ts +22 -0
  67. package/build/dex/inception/types.js +3 -0
  68. package/build/dex/inception/types.js.map +1 -0
  69. package/build/dex/inception/utils.d.ts +7 -0
  70. package/build/dex/inception/utils.js +58 -0
  71. package/build/dex/inception/utils.js.map +1 -0
  72. package/build/dex/index.js +2 -0
  73. package/build/dex/index.js.map +1 -1
  74. package/build/dex/infusion/config.d.ts +4 -0
  75. package/build/dex/infusion/config.js +25 -0
  76. package/build/dex/infusion/config.js.map +1 -0
  77. package/build/dex/infusion/infusion-stable-pool.d.ts +4 -0
  78. package/build/dex/infusion/infusion-stable-pool.js +74 -0
  79. package/build/dex/infusion/infusion-stable-pool.js.map +1 -0
  80. package/build/dex/infusion/infusion.d.ts +54 -0
  81. package/build/dex/infusion/infusion.js +510 -0
  82. package/build/dex/infusion/infusion.js.map +1 -0
  83. package/build/dex/infusion/types.d.ts +47 -0
  84. package/build/dex/infusion/types.js +3 -0
  85. package/build/dex/infusion/types.js.map +1 -0
  86. package/build/dex/infusion/utils/isStablePair.d.ts +2 -0
  87. package/build/dex/infusion/utils/isStablePair.js +18 -0
  88. package/build/dex/infusion/utils/isStablePair.js.map +1 -0
  89. package/build/dex/lite-psm/config.d.ts +3 -0
  90. package/build/dex/lite-psm/config.js +29 -0
  91. package/build/dex/lite-psm/config.js.map +1 -0
  92. package/build/dex/lite-psm/lite-psm-event-pool.d.ts +29 -0
  93. package/build/dex/lite-psm/lite-psm-event-pool.js +164 -0
  94. package/build/dex/lite-psm/lite-psm-event-pool.js.map +1 -0
  95. package/build/dex/lite-psm/lite-psm.d.ts +52 -0
  96. package/build/dex/lite-psm/lite-psm.js +341 -0
  97. package/build/dex/lite-psm/lite-psm.js.map +1 -0
  98. package/build/dex/lite-psm/types.d.ts +40 -0
  99. package/build/dex/lite-psm/types.js +3 -0
  100. package/build/dex/lite-psm/types.js.map +1 -0
  101. package/build/dex/maker-psm/maker-psm.js +4 -1
  102. package/build/dex/maker-psm/maker-psm.js.map +1 -1
  103. package/build/dex/oswap/config.d.ts +5 -0
  104. package/build/dex/oswap/config.js +31 -0
  105. package/build/dex/oswap/config.js.map +1 -0
  106. package/build/dex/oswap/oswap-pool.d.ts +51 -0
  107. package/build/dex/oswap/oswap-pool.js +150 -0
  108. package/build/dex/oswap/oswap-pool.js.map +1 -0
  109. package/build/dex/oswap/oswap.d.ts +46 -0
  110. package/build/dex/oswap/oswap.js +273 -0
  111. package/build/dex/oswap/oswap.js.map +1 -0
  112. package/build/dex/oswap/types.d.ts +20 -0
  113. package/build/dex/oswap/types.js +3 -0
  114. package/build/dex/oswap/types.js.map +1 -0
  115. package/build/dex/platypus/asset.d.ts +14 -0
  116. package/build/dex/platypus/asset.js +97 -0
  117. package/build/dex/platypus/asset.js.map +1 -0
  118. package/build/dex/platypus/config.d.ts +11 -0
  119. package/build/dex/platypus/config.js +69 -0
  120. package/build/dex/platypus/config.js.map +1 -0
  121. package/build/dex/platypus/platypus.d.ts +63 -0
  122. package/build/dex/platypus/platypus.js +473 -0
  123. package/build/dex/platypus/platypus.js.map +1 -0
  124. package/build/dex/platypus/pool-avax.d.ts +8 -0
  125. package/build/dex/platypus/pool-avax.js +54 -0
  126. package/build/dex/platypus/pool-avax.js.map +1 -0
  127. package/build/dex/platypus/pool-base.d.ts +19 -0
  128. package/build/dex/platypus/pool-base.js +86 -0
  129. package/build/dex/platypus/pool-base.js.map +1 -0
  130. package/build/dex/platypus/pool-pure.d.ts +8 -0
  131. package/build/dex/platypus/pool-pure.js +37 -0
  132. package/build/dex/platypus/pool-pure.js.map +1 -0
  133. package/build/dex/platypus/pool-subscriber.d.ts +14 -0
  134. package/build/dex/platypus/pool-subscriber.js +167 -0
  135. package/build/dex/platypus/pool-subscriber.js.map +1 -0
  136. package/build/dex/platypus/pool.d.ts +8 -0
  137. package/build/dex/platypus/pool.js +58 -0
  138. package/build/dex/platypus/pool.js.map +1 -0
  139. package/build/dex/platypus/types.d.ts +92 -0
  140. package/build/dex/platypus/types.js +10 -0
  141. package/build/dex/platypus/types.js.map +1 -0
  142. package/build/dex/quickswap/spiritswap-v3.d.ts +6 -0
  143. package/build/dex/quickswap/spiritswap-v3.js +19 -0
  144. package/build/dex/quickswap/spiritswap-v3.js.map +1 -0
  145. package/build/dex/smardex/config.d.ts +4 -0
  146. package/build/dex/smardex/config.js +64 -0
  147. package/build/dex/smardex/config.js.map +1 -0
  148. package/build/dex/smardex/constants.d.ts +18 -0
  149. package/build/dex/smardex/constants.js +33 -0
  150. package/build/dex/smardex/constants.js.map +1 -0
  151. package/build/dex/smardex/sdk/constants.d.ts +6 -0
  152. package/build/dex/smardex/sdk/constants.js +13 -0
  153. package/build/dex/smardex/sdk/constants.js.map +1 -0
  154. package/build/dex/smardex/sdk/core.d.ts +113 -0
  155. package/build/dex/smardex/sdk/core.js +499 -0
  156. package/build/dex/smardex/sdk/core.js.map +1 -0
  157. package/build/dex/smardex/sdk/errors.d.ts +15 -0
  158. package/build/dex/smardex/sdk/errors.js +22 -0
  159. package/build/dex/smardex/sdk/errors.js.map +1 -0
  160. package/build/dex/smardex/sdk/types.d.ts +62 -0
  161. package/build/dex/smardex/sdk/types.js +3 -0
  162. package/build/dex/smardex/sdk/types.js.map +1 -0
  163. package/build/dex/smardex/sdk/utils.d.ts +44 -0
  164. package/build/dex/smardex/sdk/utils.js +133 -0
  165. package/build/dex/smardex/sdk/utils.js.map +1 -0
  166. package/build/dex/smardex/smardex-event-pool.d.ts +28 -0
  167. package/build/dex/smardex/smardex-event-pool.js +119 -0
  168. package/build/dex/smardex/smardex-event-pool.js.map +1 -0
  169. package/build/dex/smardex/smardex.d.ts +65 -0
  170. package/build/dex/smardex/smardex.js +519 -0
  171. package/build/dex/smardex/smardex.js.map +1 -0
  172. package/build/dex/smardex/types.d.ts +55 -0
  173. package/build/dex/smardex/types.js +3 -0
  174. package/build/dex/smardex/types.js.map +1 -0
  175. package/build/dex/solidly/forks-override/cone.d.ts +22 -0
  176. package/build/dex/solidly/forks-override/cone.js +53 -0
  177. package/build/dex/solidly/forks-override/cone.js.map +1 -0
  178. package/build/dex/solidly/forks-override/solisnek.d.ts +8 -0
  179. package/build/dex/solidly/forks-override/solisnek.js +15 -0
  180. package/build/dex/solidly/forks-override/solisnek.js.map +1 -0
  181. package/build/dex/swaap-v1/config.d.ts +17 -0
  182. package/build/dex/swaap-v1/config.js +56 -0
  183. package/build/dex/swaap-v1/config.js.map +1 -0
  184. package/build/dex/swaap-v1/constants.d.ts +6 -0
  185. package/build/dex/swaap-v1/constants.js +17 -0
  186. package/build/dex/swaap-v1/constants.js.map +1 -0
  187. package/build/dex/swaap-v1/libraries/ChainlinkUtils.d.ts +25 -0
  188. package/build/dex/swaap-v1/libraries/ChainlinkUtils.js +62 -0
  189. package/build/dex/swaap-v1/libraries/ChainlinkUtils.js.map +1 -0
  190. package/build/dex/swaap-v1/libraries/Const.d.ts +18 -0
  191. package/build/dex/swaap-v1/libraries/Const.js +26 -0
  192. package/build/dex/swaap-v1/libraries/Const.js.map +1 -0
  193. package/build/dex/swaap-v1/libraries/GeometricBrownianMotion.d.ts +77 -0
  194. package/build/dex/swaap-v1/libraries/GeometricBrownianMotion.js +316 -0
  195. package/build/dex/swaap-v1/libraries/GeometricBrownianMotion.js.map +1 -0
  196. package/build/dex/swaap-v1/libraries/LogExpMath.d.ts +75 -0
  197. package/build/dex/swaap-v1/libraries/LogExpMath.js +418 -0
  198. package/build/dex/swaap-v1/libraries/LogExpMath.js.map +1 -0
  199. package/build/dex/swaap-v1/libraries/MathMMM.d.ts +185 -0
  200. package/build/dex/swaap-v1/libraries/MathMMM.js +453 -0
  201. package/build/dex/swaap-v1/libraries/MathMMM.js.map +1 -0
  202. package/build/dex/swaap-v1/libraries/Num.d.ts +31 -0
  203. package/build/dex/swaap-v1/libraries/Num.js +167 -0
  204. package/build/dex/swaap-v1/libraries/Num.js.map +1 -0
  205. package/build/dex/swaap-v1/libraries/PoolQuotations.d.ts +23 -0
  206. package/build/dex/swaap-v1/libraries/PoolQuotations.js +131 -0
  207. package/build/dex/swaap-v1/libraries/PoolQuotations.js.map +1 -0
  208. package/build/dex/swaap-v1/libraries/Struct.d.ts +56 -0
  209. package/build/dex/swaap-v1/libraries/Struct.js +3 -0
  210. package/build/dex/swaap-v1/libraries/Struct.js.map +1 -0
  211. package/build/dex/swaap-v1/swaap-v1-pool.d.ts +45 -0
  212. package/build/dex/swaap-v1/swaap-v1-pool.js +656 -0
  213. package/build/dex/swaap-v1/swaap-v1-pool.js.map +1 -0
  214. package/build/dex/swaap-v1/swaap-v1.d.ts +62 -0
  215. package/build/dex/swaap-v1/swaap-v1.js +379 -0
  216. package/build/dex/swaap-v1/swaap-v1.js.map +1 -0
  217. package/build/dex/swaap-v1/types.d.ts +106 -0
  218. package/build/dex/swaap-v1/types.js +9 -0
  219. package/build/dex/swaap-v1/types.js.map +1 -0
  220. package/build/dex/trader-joe-v2.1/base.js +16 -5
  221. package/build/dex/trader-joe-v2.1/base.js.map +1 -1
  222. package/build/dex/trader-joe-v2.1/types.d.ts +3 -3
  223. package/build/dex-helper/dummy-dex-helper.js +1 -1
  224. package/build/dex-helper/dummy-dex-helper.js.map +1 -1
  225. package/package.json +1 -1
  226. package/src/abi/lite-psm/dai.json +333 -0
  227. package/src/abi/lite-psm/psm.json +468 -0
  228. package/src/abi/lite-psm/usdc.json +814 -0
  229. package/src/dex/index.ts +2 -0
  230. package/src/dex/lite-psm/config.ts +29 -0
  231. package/src/dex/lite-psm/lite-psm-e2e.test.ts +79 -0
  232. package/src/dex/lite-psm/lite-psm-event-pool.ts +216 -0
  233. package/src/dex/lite-psm/lite-psm-events.test.ts +61 -0
  234. package/src/dex/lite-psm/lite-psm-integration.test.ts +99 -0
  235. package/src/dex/lite-psm/lite-psm.ts +523 -0
  236. package/src/dex/lite-psm/types.ts +46 -0
  237. package/src/dex/maker-psm/maker-psm.ts +4 -1
  238. package/src/dex/trader-joe-v2.1/base.ts +16 -5
  239. package/src/dex/trader-joe-v2.1/types.ts +3 -3
  240. package/src/dex-helper/dummy-dex-helper.ts +1 -1
  241. package/.idea/aws.xml +0 -17
  242. package/.idea/codeStyles/Project.xml +0 -19
  243. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  244. package/.idea/misc.xml +0 -6
  245. package/.idea/modules.xml +0 -8
  246. package/.idea/paraswap-dex-lib.iml +0 -9
  247. package/.idea/prettier.xml +0 -7
  248. package/.idea/vcs.xml +0 -6
  249. package/build/abi/maverick-v2/MaverickV2Router.json +0 -778
  250. package/build/dex/trader-joe-v2.1/optimizer.d.ts +0 -2
  251. package/build/dex/trader-joe-v2.1/optimizer.js +0 -47
  252. package/build/dex/trader-joe-v2.1/optimizer.js.map +0 -1
  253. package/build/dex/trader-joe-v2.1/trader-joe-v2.1-new.d.ts +0 -7
  254. package/build/dex/trader-joe-v2.1/trader-joe-v2.1-new.js +0 -20
  255. package/build/dex/trader-joe-v2.1/trader-joe-v2.1-new.js.map +0 -1
  256. package/src/dex/trader-joe-v2.1/optimizer.ts +0 -60
@@ -0,0 +1,523 @@
1
+ import { Interface } from '@ethersproject/abi';
2
+ import {
3
+ Token,
4
+ Address,
5
+ ExchangePrices,
6
+ PoolPrices,
7
+ Log,
8
+ AdapterExchangeParam,
9
+ SimpleExchangeParam,
10
+ PoolLiquidity,
11
+ Logger,
12
+ NumberAsString,
13
+ DexExchangeParam,
14
+ ExchangeTxInfo,
15
+ PreprocessTransactionOptions,
16
+ } from '../../types';
17
+ import { SwapSide, Network, NULL_ADDRESS } from '../../constants';
18
+ import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
19
+ import {
20
+ getDexKeysWithNetwork,
21
+ getBigIntPow,
22
+ uuidToBytes16,
23
+ } from '../../utils';
24
+ import { IDex } from '../../dex/idex';
25
+ import { IDexHelper } from '../../dex-helper/idex-helper';
26
+ import {
27
+ LitePsmData,
28
+ PoolState,
29
+ PoolConfig,
30
+ LitePsmParams,
31
+ LitePsmDirectPayload,
32
+ } from './types';
33
+ import { SimpleExchange } from '../simple-exchange';
34
+ import { LitePsmConfig } from './config';
35
+ import PsmABI from '../../abi/lite-psm/psm.json';
36
+ import { BI_POWS } from '../../bigint-constants';
37
+ import { SpecialDex } from '../../executor/types';
38
+ import { hexConcat, hexZeroPad, hexlify } from '@ethersproject/bytes';
39
+ import {
40
+ ContractMethodV6,
41
+ OptimalSwapExchange,
42
+ ParaSwapVersion,
43
+ } from '@paraswap/core';
44
+ import { BigNumber } from 'ethers';
45
+ import { LitePsmEventPool, getOnChainState } from './lite-psm-event-pool';
46
+ import { extractReturnAmountPosition } from '../../executor/utils';
47
+
48
+ const psmInterface = new Interface(PsmABI);
49
+ const WAD = BI_POWS[18];
50
+
51
+ const ceilDiv = (a: bigint, b: bigint) => (a + b - 1n) / b;
52
+
53
+ export class LitePsm
54
+ extends SimpleExchange
55
+ implements IDex<LitePsmData, LitePsmDirectPayload>
56
+ {
57
+ protected eventPools: { [gemAddress: string]: LitePsmEventPool };
58
+
59
+ // warning: There is limit on swap
60
+ readonly hasConstantPriceLargeAmounts = true;
61
+ readonly isFeeOnTransferSupported = false;
62
+
63
+ public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
64
+ getDexKeysWithNetwork(LitePsmConfig);
65
+
66
+ logger: Logger;
67
+
68
+ constructor(
69
+ protected network: Network,
70
+ dexKey: string,
71
+ protected dexHelper: IDexHelper,
72
+ protected dai: Token = LitePsmConfig[dexKey][network].dai,
73
+ protected vatAddress: Address = LitePsmConfig[dexKey][network].vatAddress,
74
+ protected poolConfigs: PoolConfig[] = LitePsmConfig[dexKey][network].pools,
75
+ ) {
76
+ super(dexHelper, dexKey);
77
+ this.logger = dexHelper.getLogger(dexKey);
78
+ this.eventPools = {};
79
+ poolConfigs.forEach(
80
+ p =>
81
+ (this.eventPools[p.gem.address.toLowerCase()] = new LitePsmEventPool(
82
+ dexKey,
83
+ network,
84
+ dexHelper,
85
+ this.logger,
86
+ p,
87
+ this.vatAddress,
88
+ )),
89
+ );
90
+ }
91
+
92
+ static getDirectFunctionNameV6(): string[] {
93
+ return [ContractMethodV6.swapExactAmountInOutOnMakerPSM];
94
+ }
95
+
96
+ async initializePricing(blockNumber: number) {
97
+ const poolStates = await getOnChainState(
98
+ this.dexHelper.multiContract,
99
+ this.poolConfigs,
100
+ this.vatAddress,
101
+ blockNumber,
102
+ LitePsmConfig[this.dexKey][this.network].dai.address,
103
+ );
104
+ await Promise.all(
105
+ this.poolConfigs.map(async (p, i) => {
106
+ const eventPool = this.eventPools[p.gem.address.toLowerCase()];
107
+ await eventPool.initialize(blockNumber, {
108
+ state: poolStates[i],
109
+ });
110
+ }),
111
+ );
112
+ }
113
+
114
+ // Returns the list of contract adapters (name and index)
115
+ // for a buy/sell. Return null if there are no adapters.
116
+ getAdapters(side: SwapSide): { name: string; index: number }[] | null {
117
+ return null;
118
+ }
119
+
120
+ getEventPool(srcToken: Token, destToken: Token): LitePsmEventPool | null {
121
+ const srcAddress = srcToken.address.toLowerCase();
122
+ const destAddress = destToken.address.toLowerCase();
123
+ return (
124
+ (srcAddress === this.dai.address && this.eventPools[destAddress]) ||
125
+ (destAddress === this.dai.address && this.eventPools[srcAddress]) ||
126
+ null
127
+ );
128
+ }
129
+
130
+ // Returns list of pool identifiers that can be used
131
+ // for a given swap. poolIdentifiers must be unique
132
+ // across DEXes.
133
+ async getPoolIdentifiers(
134
+ srcToken: Token,
135
+ destToken: Token,
136
+ side: SwapSide,
137
+ blockNumber: number,
138
+ ): Promise<string[]> {
139
+ const eventPool = this.getEventPool(srcToken, destToken);
140
+ if (!eventPool) return [];
141
+ return [eventPool.getIdentifier()];
142
+ }
143
+
144
+ async getPoolState(
145
+ pool: LitePsmEventPool,
146
+ blockNumber: number,
147
+ ): Promise<PoolState> {
148
+ const eventState = pool.getState(blockNumber);
149
+ if (eventState) return eventState;
150
+ const onChainState = await pool.generateState(blockNumber);
151
+ pool.setState(onChainState, blockNumber);
152
+ return onChainState;
153
+ }
154
+
155
+ computePrices(
156
+ isSrcDai: boolean,
157
+ to18ConversionFactor: bigint,
158
+ side: SwapSide,
159
+ amounts: bigint[],
160
+ poolState: PoolState,
161
+ ): bigint[] {
162
+ const sellGemCheck = (dart: bigint) => {
163
+ return dart <= poolState.daiBalance;
164
+ };
165
+ const buyGemCheck = (dart: bigint) => {
166
+ return dart <= poolState.gemBalance * to18ConversionFactor;
167
+ };
168
+
169
+ return amounts.map(a => {
170
+ if (side === SwapSide.SELL) {
171
+ if (isSrcDai) {
172
+ const gemAmt18 = (a * WAD) / (WAD + poolState.tout);
173
+ if (buyGemCheck(gemAmt18)) return gemAmt18 / to18ConversionFactor;
174
+ } else {
175
+ const gemAmt18 = to18ConversionFactor * a;
176
+ if (sellGemCheck(gemAmt18))
177
+ return gemAmt18 - (gemAmt18 * poolState.tin) / WAD;
178
+ }
179
+ } else {
180
+ if (isSrcDai) {
181
+ const gemAmt18 = to18ConversionFactor * a;
182
+ if (buyGemCheck(gemAmt18))
183
+ return gemAmt18 + (gemAmt18 * poolState.tout) / WAD;
184
+ } else {
185
+ const gemAmt18 = (a * WAD) / (WAD - poolState.tin);
186
+ if (sellGemCheck(gemAmt18)) return gemAmt18 / to18ConversionFactor;
187
+ }
188
+ }
189
+ return 0n;
190
+ });
191
+ }
192
+
193
+ // Returns pool prices for amounts.
194
+ // If limitPools is defined only pools in limitPools
195
+ // should be used. If limitPools is undefined then
196
+ // any pools can be used.
197
+ async getPricesVolume(
198
+ srcToken: Token,
199
+ destToken: Token,
200
+ amounts: bigint[],
201
+ side: SwapSide,
202
+ blockNumber: number,
203
+ limitPools?: string[],
204
+ ): Promise<null | ExchangePrices<LitePsmData>> {
205
+ const eventPool = this.getEventPool(srcToken, destToken);
206
+ if (!eventPool) return null;
207
+
208
+ const poolIdentifier = eventPool.getIdentifier();
209
+ if (limitPools && !limitPools.includes(poolIdentifier)) return null;
210
+
211
+ const poolState = await this.getPoolState(eventPool, blockNumber);
212
+
213
+ const unitVolume = getBigIntPow(
214
+ (side === SwapSide.SELL ? srcToken : destToken).decimals,
215
+ );
216
+
217
+ const isSrcDai = srcToken.address.toLowerCase() === this.dai.address;
218
+ const gem = isSrcDai ? destToken : srcToken;
219
+ const toll =
220
+ (side === SwapSide.SELL && isSrcDai) ||
221
+ (side === SwapSide.BUY && !isSrcDai)
222
+ ? poolState.tout
223
+ : poolState.tin;
224
+
225
+ const [unit, ...prices] = this.computePrices(
226
+ isSrcDai,
227
+ eventPool.to18ConversionFactor,
228
+ side,
229
+ [unitVolume, ...amounts],
230
+ poolState,
231
+ );
232
+
233
+ return [
234
+ {
235
+ prices,
236
+ unit,
237
+ data: {
238
+ toll: toll.toString(),
239
+ psmAddress: eventPool.poolConfig.psmAddress,
240
+ gemJoinAddress: eventPool.poolConfig.gemJoinAddress,
241
+ gemDecimals: gem.decimals,
242
+ },
243
+ poolAddresses: [eventPool.poolConfig.psmAddress],
244
+ exchange: this.dexKey,
245
+ gasCost: isSrcDai ? 80_000 : 100_000,
246
+ poolIdentifier,
247
+ },
248
+ ];
249
+ }
250
+
251
+ // Returns estimated gas cost of calldata for this DEX in multiSwap
252
+ getCalldataGasCost(poolPrices: PoolPrices<LitePsmData>): number | number[] {
253
+ return (
254
+ CALLDATA_GAS_COST.DEX_OVERHEAD +
255
+ CALLDATA_GAS_COST.LENGTH_SMALL +
256
+ CALLDATA_GAS_COST.ADDRESS +
257
+ // TODO: pools have toll as zero currently but may change
258
+ CALLDATA_GAS_COST.ZERO +
259
+ // Either 1 (18 decimals) or 1e12 = 0xe8d4a51000 (6 decimals)
260
+ CALLDATA_GAS_COST.wordNonZeroBytes(
261
+ poolPrices.data.gemDecimals === 6 ? 4 : 1,
262
+ )
263
+ );
264
+ }
265
+
266
+ getPsmParams(
267
+ srcToken: string,
268
+ srcAmount: string,
269
+ destAmount: string,
270
+ data: LitePsmData,
271
+ side: SwapSide,
272
+ ): { isGemSell: boolean; gemAmount: string } {
273
+ const isSrcDai = srcToken.toLowerCase() === this.dai.address;
274
+ const to18ConversionFactor = getBigIntPow(18 - data.gemDecimals);
275
+ if (side === SwapSide.SELL) {
276
+ if (isSrcDai) {
277
+ const gemAmt18 = (BigInt(srcAmount) * WAD) / (WAD + BigInt(data.toll));
278
+ return {
279
+ isGemSell: false,
280
+ gemAmount: (gemAmt18 / to18ConversionFactor).toString(),
281
+ };
282
+ } else {
283
+ return { isGemSell: true, gemAmount: srcAmount };
284
+ }
285
+ } else {
286
+ if (isSrcDai) {
287
+ return { isGemSell: false, gemAmount: destAmount };
288
+ } else {
289
+ const gemAmt = ceilDiv(
290
+ BigInt(destAmount) * WAD,
291
+ (WAD - BigInt(data.toll)) * to18ConversionFactor,
292
+ );
293
+ return {
294
+ isGemSell: true,
295
+ gemAmount: gemAmt.toString(),
296
+ };
297
+ }
298
+ }
299
+ }
300
+
301
+ getAdapterParam(
302
+ srcToken: string,
303
+ destToken: string,
304
+ srcAmount: string,
305
+ destAmount: string,
306
+ data: LitePsmData,
307
+ side: SwapSide,
308
+ ): AdapterExchangeParam {
309
+ return {
310
+ targetExchange: '0x',
311
+ networkFee: '0',
312
+ payload: '0x',
313
+ };
314
+ }
315
+
316
+ getTokenFromAddress(address: Address): Token {
317
+ return { address, decimals: 0 };
318
+ }
319
+
320
+ async preProcessTransaction?(
321
+ optimalSwapExchange: OptimalSwapExchange<LitePsmData>,
322
+ srcToken: Token,
323
+ destToken: Token,
324
+ side: SwapSide,
325
+ options: PreprocessTransactionOptions,
326
+ ): Promise<[OptimalSwapExchange<LitePsmData>, ExchangeTxInfo]> {
327
+ if (!optimalSwapExchange.data) {
328
+ throw new Error(
329
+ `Error_${this.dexKey}_preProcessTransaction payload is not received`,
330
+ );
331
+ }
332
+
333
+ let isApproved = false;
334
+
335
+ // isApproved is only used in direct method and available only for v6, then no need to check approve for v5
336
+ // because it's either done in getSimpleParam or approve call in the adapter smart contract
337
+ if (options.version === ParaSwapVersion.V6 && options.isDirectMethod) {
338
+ isApproved = await this.dexHelper.augustusApprovals.hasApproval(
339
+ options.executionContractAddress,
340
+ srcToken.address,
341
+ srcToken.address.toLowerCase() === this.dai.address.toLowerCase()
342
+ ? optimalSwapExchange.data.psmAddress
343
+ : optimalSwapExchange.data.gemJoinAddress,
344
+ );
345
+ }
346
+
347
+ return [
348
+ {
349
+ ...optimalSwapExchange,
350
+ data: { ...optimalSwapExchange.data, isApproved },
351
+ },
352
+ {},
353
+ ];
354
+ }
355
+
356
+ getDexParam(
357
+ srcToken: Address,
358
+ destToken: Address,
359
+ srcAmount: NumberAsString,
360
+ destAmount: NumberAsString,
361
+ recipient: Address,
362
+ data: LitePsmData,
363
+ side: SwapSide,
364
+ ): DexExchangeParam {
365
+ const { isGemSell, gemAmount } = this.getPsmParams(
366
+ srcToken,
367
+ srcAmount,
368
+ destAmount,
369
+ data,
370
+ side,
371
+ );
372
+
373
+ let exchangeData = psmInterface.encodeFunctionData(
374
+ isGemSell ? 'sellGem' : 'buyGem',
375
+ [recipient, gemAmount],
376
+ );
377
+
378
+ // append toll and to18ConversionFactor & set specialDexFlag = SWAP_ON_MAKER_PSM to
379
+ // - `buyGem` on Ex1 & Ex2
380
+ // - `sellGem` on Ex3
381
+ let specialDexFlag = SpecialDex.DEFAULT;
382
+ if (
383
+ (side === SwapSide.SELL && !isGemSell) ||
384
+ (side === SwapSide.BUY && isGemSell)
385
+ ) {
386
+ exchangeData = hexConcat([
387
+ exchangeData,
388
+ hexZeroPad(hexlify(BigInt(data.toll)), 32),
389
+ hexZeroPad(hexlify(getBigIntPow(18 - data.gemDecimals)), 32),
390
+ ]);
391
+ specialDexFlag = SpecialDex.SWAP_ON_MAKER_PSM;
392
+ }
393
+
394
+ return {
395
+ needWrapNative: this.needWrapNative,
396
+ dexFuncHasRecipient: true,
397
+ exchangeData,
398
+ targetExchange: data.psmAddress,
399
+ specialDexFlag,
400
+ spender: isGemSell ? data.gemJoinAddress : data.psmAddress,
401
+ returnAmountPos: extractReturnAmountPosition(
402
+ psmInterface,
403
+ isGemSell ? 'sellGem' : 'buyGem',
404
+ isGemSell ? 'daiOutWad' : 'daiInWad',
405
+ ),
406
+ };
407
+ }
408
+
409
+ getDirectParamV6(
410
+ srcToken: Address,
411
+ destToken: Address,
412
+ fromAmount: NumberAsString,
413
+ toAmount: NumberAsString,
414
+ quotedAmount: NumberAsString,
415
+ data: LitePsmData,
416
+ side: SwapSide,
417
+ permit: string,
418
+ uuid: string,
419
+ partnerAndFee: string,
420
+ beneficiary: string,
421
+ blockNumber: number,
422
+ contractMethod: string,
423
+ ) {
424
+ if (!contractMethod) throw new Error(`contractMethod need to be passed`);
425
+ if (!LitePsm.getDirectFunctionNameV6().includes(contractMethod!)) {
426
+ throw new Error(`Invalid contract method ${contractMethod}`);
427
+ }
428
+
429
+ const beneficiaryParam = BigNumber.from(beneficiary);
430
+
431
+ const approveParam = !data.isApproved
432
+ ? BigNumber.from(1).shl(255)
433
+ : BigNumber.from(0);
434
+ const directionParam =
435
+ side === SwapSide.SELL ? BigNumber.from(0) : BigNumber.from(1).shl(254);
436
+
437
+ const beneficiaryDirectionApproveFlag = beneficiaryParam
438
+ .or(directionParam)
439
+ .or(approveParam);
440
+
441
+ const to18ConversionFactor = getBigIntPow(18 - data.gemDecimals);
442
+
443
+ const metadata = hexConcat([
444
+ hexZeroPad(uuidToBytes16(uuid), 16),
445
+ hexZeroPad(hexlify(blockNumber), 16),
446
+ ]);
447
+
448
+ const params: LitePsmParams = [
449
+ srcToken,
450
+ // not used on the contract, but used for analytics
451
+ destToken,
452
+ fromAmount,
453
+ // TODO: Investigate `toAmount` vs `quotedAmount`
454
+ side === SwapSide.BUY && srcToken.toLowerCase() === this.dai.address
455
+ ? toAmount
456
+ : quotedAmount,
457
+ data.toll,
458
+ to18ConversionFactor.toString(),
459
+ data.psmAddress,
460
+ data.gemJoinAddress,
461
+ metadata,
462
+ beneficiaryDirectionApproveFlag.toString(),
463
+ ];
464
+
465
+ const payload: LitePsmDirectPayload = [params, permit];
466
+
467
+ const encoder = (...params: (string | LitePsmDirectPayload)[]) => {
468
+ return this.augustusV6Interface.encodeFunctionData(
469
+ ContractMethodV6.swapExactAmountInOutOnMakerPSM,
470
+ [...params],
471
+ );
472
+ };
473
+
474
+ return { params: payload, encoder, networkFee: '0' };
475
+ }
476
+
477
+ // Returns list of top pools based on liquidity. Max
478
+ // limit number pools should be returned.
479
+ async getTopPoolsForToken(
480
+ tokenAddress: Address,
481
+ limit: number,
482
+ ): Promise<PoolLiquidity[]> {
483
+ const _tokenAddress = tokenAddress.toLowerCase();
484
+ // Liquidity depends on the swapping side hence we simply use the min
485
+ // Its always in terms of stable coin hence liquidityUSD = liquidity
486
+ const minLiq = (poolState: PoolState, decimals: number) => {
487
+ const buyLimit = poolState.gemBalance * getBigIntPow(18 - decimals);
488
+ const sellLimit = poolState.daiBalance;
489
+ return (
490
+ 2 *
491
+ parseInt(
492
+ (
493
+ (buyLimit > sellLimit ? sellLimit : buyLimit) / BI_POWS[18]
494
+ ).toString(),
495
+ )
496
+ );
497
+ };
498
+
499
+ const isDai = _tokenAddress === this.dai.address;
500
+
501
+ const validPoolConfigs = isDai
502
+ ? this.poolConfigs
503
+ : this.eventPools[_tokenAddress]
504
+ ? [this.eventPools[_tokenAddress].poolConfig]
505
+ : [];
506
+ if (!validPoolConfigs.length) return [];
507
+
508
+ const poolStates = await getOnChainState(
509
+ this.dexHelper.multiContract,
510
+ validPoolConfigs,
511
+ this.vatAddress,
512
+ 'latest',
513
+ LitePsmConfig[this.dexKey][this.network].dai.address,
514
+ );
515
+
516
+ return validPoolConfigs.map((p, i) => ({
517
+ exchange: this.dexKey,
518
+ address: p.psmAddress,
519
+ liquidityUSD: minLiq(poolStates[i], p.gem.decimals),
520
+ connectorTokens: [isDai ? p.gem : this.dai],
521
+ }));
522
+ }
523
+ }
@@ -0,0 +1,46 @@
1
+ import { Address, NumberAsString, Token } from '../../types';
2
+
3
+ export type PoolState = {
4
+ tin: bigint; // toll in
5
+ tout: bigint; // toll out
6
+ rate: bigint;
7
+ daiBalance: bigint; // `sellGem` ceiling
8
+ gemBalance: bigint; // `buyGem` ceiling
9
+ };
10
+
11
+ export type LitePsmData = {
12
+ psmAddress: Address;
13
+ gemJoinAddress: Address;
14
+ gemDecimals: number;
15
+ toll: string;
16
+ isApproved?: boolean;
17
+ };
18
+
19
+ export type PoolConfig = {
20
+ gem: Token;
21
+ gemJoinAddress: Address; // dai liquidity
22
+ pocketAddress: Address; // gem liquidity
23
+ psmAddress: Address;
24
+ identifier: string; // bytes32 of pool identifier (Eg. bytes32("PSM-USDC-A"))
25
+ };
26
+
27
+ export type DexParams = {
28
+ dai: Token;
29
+ vatAddress: Address;
30
+ pools: PoolConfig[];
31
+ };
32
+
33
+ export type LitePsmParams = [
34
+ srcToken: Address,
35
+ destToken: Address,
36
+ fromAmount: NumberAsString,
37
+ toAmount: NumberAsString,
38
+ toll: NumberAsString,
39
+ to18ConversionFactor: NumberAsString,
40
+ exchange: Address,
41
+ gemJoinAddress: Address,
42
+ metadata: string,
43
+ beneficiaryDirectionApproveFlag: NumberAsString,
44
+ ];
45
+
46
+ export type LitePsmDirectPayload = [params: LitePsmParams, permit: string];
@@ -654,7 +654,10 @@ export class MakerPsm
654
654
  destToken,
655
655
  fromAmount,
656
656
  // as there's no slippage, use instead of toAmount
657
- quotedAmount,
657
+ // TODO: Investigate `toAmount` vs `quotedAmount`
658
+ side === SwapSide.BUY && srcToken.toLowerCase() === this.dai.address
659
+ ? toAmount
660
+ : quotedAmount,
658
661
  data.toll,
659
662
  to18ConversionFactor.toString(),
660
663
  data.psmAddress,
@@ -53,7 +53,18 @@ export class BaseTraderJoeV2
53
53
  ['tuple(tuple(uint256[],uint8[],address[]),uint256)'],
54
54
  [
55
55
  [
56
- [data.binSteps, data.versions, data.tokenPath],
56
+ [
57
+ [
58
+ data.binStep, // _pairBinSteps: uint256[]
59
+ ],
60
+ [
61
+ this.versionIndex, // _versions: uint8[]
62
+ ],
63
+ [
64
+ data.tokenIn,
65
+ data.tokenOut, // _tokenPath: address[]
66
+ ],
67
+ ],
57
68
  getLocalDeadlineAsFriendlyPlaceholder(), // _deadline: uint256
58
69
  ],
59
70
  ],
@@ -84,14 +95,14 @@ export class BaseTraderJoeV2
84
95
  ? [
85
96
  srcAmount,
86
97
  destAmount,
87
- [data.binSteps, data.versions, data.tokenPath],
98
+ [[data.binStep], [this.versionIndex], [srcToken, destToken]],
88
99
  this.augustusAddress,
89
100
  getLocalDeadlineAsFriendlyPlaceholder(),
90
101
  ]
91
102
  : [
92
103
  destAmount,
93
104
  srcAmount,
94
- [data.binSteps, data.versions, data.tokenPath],
105
+ [[data.binStep], [this.versionIndex], [srcToken, destToken]],
95
106
  this.augustusAddress,
96
107
  getLocalDeadlineAsFriendlyPlaceholder(),
97
108
  ];
@@ -132,14 +143,14 @@ export class BaseTraderJoeV2
132
143
  ? [
133
144
  srcAmount,
134
145
  destAmount,
135
- [data.binSteps, data.versions, data.tokenPath],
146
+ [[data.binStep], [this.versionIndex], [srcToken, destToken]],
136
147
  recipient,
137
148
  placeholder,
138
149
  ]
139
150
  : [
140
151
  destAmount,
141
152
  srcAmount,
142
- [data.binSteps, data.versions, data.tokenPath],
153
+ [[data.binStep], [this.versionIndex], [srcToken, destToken]],
143
154
  recipient,
144
155
  placeholder,
145
156
  ];
@@ -27,9 +27,9 @@ export type TraderJoeV2RouterParam =
27
27
  | TraderJoeV2RouterBuyParams;
28
28
 
29
29
  export type TraderJoeV2Data = {
30
- tokenPath: string[];
31
- binSteps: string[];
32
- versions: string[];
30
+ tokenIn: string; // redundant
31
+ tokenOut: string; // redundant
32
+ binStep: string;
33
33
  };
34
34
 
35
35
  export enum TraderJoeV2RouterFunctions {
@@ -261,7 +261,7 @@ export class DummyRequestWrapper implements IRequestWrapper {
261
261
  }
262
262
 
263
263
  class DummyBlockManager implements IBlockManager {
264
- constructor(public _blockNumber: number = 42) {}
264
+ constructor(public _blockNumber: number = 20569333) {}
265
265
 
266
266
  subscribeToLogs(
267
267
  subscriber: EventSubscriber,
package/.idea/aws.xml DELETED
@@ -1,17 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="accountSettings">
4
- <option name="activeProfile" value="profile:default" />
5
- <option name="activeRegion" value="us-east-1" />
6
- <option name="recentlyUsedProfiles">
7
- <list>
8
- <option value="profile:default" />
9
- </list>
10
- </option>
11
- <option name="recentlyUsedRegions">
12
- <list>
13
- <option value="us-east-1" />
14
- </list>
15
- </option>
16
- </component>
17
- </project>
@@ -1,19 +0,0 @@
1
- <component name="ProjectCodeStyleConfiguration">
2
- <code_scheme name="Project" version="173">
3
- <ScalaCodeStyleSettings>
4
- <option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" />
5
- </ScalaCodeStyleSettings>
6
- <TypeScriptCodeStyleSettings version="0">
7
- <option name="SPACE_WITHIN_ARRAY_INITIALIZER_BRACKETS" value="true" />
8
- <option name="USE_DOUBLE_QUOTES" value="false" />
9
- <option name="SPACES_WITHIN_IMPORTS" value="true" />
10
- </TypeScriptCodeStyleSettings>
11
- <codeStyleSettings language="TypeScript">
12
- <indentOptions>
13
- <option name="INDENT_SIZE" value="2" />
14
- <option name="CONTINUATION_INDENT_SIZE" value="2" />
15
- <option name="TAB_SIZE" value="2" />
16
- </indentOptions>
17
- </codeStyleSettings>
18
- </code_scheme>
19
- </component>
@@ -1,5 +0,0 @@
1
- <component name="ProjectCodeStyleConfiguration">
2
- <state>
3
- <option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
4
- </state>
5
- </component>