@paraswap/dex-lib 3.8.21-traderjoe-multihop.3 → 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 (258) 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/config.js +2 -2
  223. package/build/dex/trader-joe-v2.1/types.d.ts +3 -3
  224. package/build/dex-helper/dummy-dex-helper.js +1 -1
  225. package/build/dex-helper/dummy-dex-helper.js.map +1 -1
  226. package/package.json +1 -1
  227. package/src/abi/lite-psm/dai.json +333 -0
  228. package/src/abi/lite-psm/psm.json +468 -0
  229. package/src/abi/lite-psm/usdc.json +814 -0
  230. package/src/dex/index.ts +2 -0
  231. package/src/dex/lite-psm/config.ts +29 -0
  232. package/src/dex/lite-psm/lite-psm-e2e.test.ts +79 -0
  233. package/src/dex/lite-psm/lite-psm-event-pool.ts +216 -0
  234. package/src/dex/lite-psm/lite-psm-events.test.ts +61 -0
  235. package/src/dex/lite-psm/lite-psm-integration.test.ts +99 -0
  236. package/src/dex/lite-psm/lite-psm.ts +523 -0
  237. package/src/dex/lite-psm/types.ts +46 -0
  238. package/src/dex/maker-psm/maker-psm.ts +4 -1
  239. package/src/dex/trader-joe-v2.1/base.ts +16 -5
  240. package/src/dex/trader-joe-v2.1/config.ts +2 -2
  241. package/src/dex/trader-joe-v2.1/types.ts +3 -3
  242. package/src/dex-helper/dummy-dex-helper.ts +1 -1
  243. package/.idea/aws.xml +0 -17
  244. package/.idea/codeStyles/Project.xml +0 -19
  245. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  246. package/.idea/misc.xml +0 -6
  247. package/.idea/modules.xml +0 -8
  248. package/.idea/paraswap-dex-lib.iml +0 -9
  249. package/.idea/prettier.xml +0 -7
  250. package/.idea/vcs.xml +0 -6
  251. package/build/abi/maverick-v2/MaverickV2Router.json +0 -778
  252. package/build/dex/trader-joe-v2.1/optimizer.d.ts +0 -2
  253. package/build/dex/trader-joe-v2.1/optimizer.js +0 -47
  254. package/build/dex/trader-joe-v2.1/optimizer.js.map +0 -1
  255. package/build/dex/trader-joe-v2.1/trader-joe-v2.1-new.d.ts +0 -7
  256. package/build/dex/trader-joe-v2.1/trader-joe-v2.1-new.js +0 -20
  257. package/build/dex/trader-joe-v2.1/trader-joe-v2.1-new.js.map +0 -1
  258. package/src/dex/trader-joe-v2.1/optimizer.ts +0 -60
package/src/dex/index.ts CHANGED
@@ -84,6 +84,7 @@ import { VelodromeSlipstream } from './uniswap-v3/forks/velodrome-slipstream/vel
84
84
  import { AaveV3Stata } from './aave-v3-stata/aave-v3-stata';
85
85
  import { ConcentratorArusd } from './concentrator-arusd/concentrator-arusd';
86
86
  import { FxProtocolRusd } from './fx-protocol-rusd/fx-protocol-rusd';
87
+ import { LitePsm } from './lite-psm/lite-psm';
87
88
 
88
89
  const LegacyDexes = [
89
90
  CurveV2,
@@ -163,6 +164,7 @@ const Dexes = [
163
164
  AaveV3Stata,
164
165
  ConcentratorArusd,
165
166
  FxProtocolRusd,
167
+ LitePsm,
166
168
  ];
167
169
 
168
170
  export type LegacyDexConstructor = new (dexHelper: IDexHelper) => IDexTxBuilder<
@@ -0,0 +1,29 @@
1
+ import { DexParams } from './types';
2
+ import { DexConfigMap } from '../../types';
3
+ import { Network } from '../../constants';
4
+
5
+ // Warning: Addresses should be lowercased
6
+ export const LitePsmConfig: DexConfigMap<DexParams> = {
7
+ LitePsm: {
8
+ [Network.MAINNET]: {
9
+ dai: {
10
+ address: '0x6b175474e89094c44da98b954eedeac495271d0f',
11
+ decimals: 18,
12
+ },
13
+ vatAddress: '0x35d1b3f3d7966a1dfe207aa4514c12a259a0492b',
14
+ pools: [
15
+ {
16
+ gem: {
17
+ address: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
18
+ decimals: 6,
19
+ },
20
+ gemJoinAddress: '0xf6e72db5454dd049d0788e411b06cfaf16853042', // Same as psmAddress. LitePsm does not have an external gem join.
21
+ pocketAddress: '0x37305b1cd40574e4c5ce33f8e8306be057fd7341',
22
+ psmAddress: '0xf6e72db5454dd049d0788e411b06cfaf16853042',
23
+ identifier:
24
+ '0x4c4954452d50534d2d555344432d410000000000000000000000000000000000', // bytes32("LITE-PSM-USDC-A")
25
+ },
26
+ ],
27
+ },
28
+ },
29
+ };
@@ -0,0 +1,79 @@
1
+ import dotenv from 'dotenv';
2
+ dotenv.config();
3
+
4
+ import { testE2E } from '../../../tests/utils-e2e';
5
+ import { Tokens, Holders } from '../../../tests/constants-e2e';
6
+ import { Network, ContractMethod, SwapSide } from '../../constants';
7
+ import { StaticJsonRpcProvider } from '@ethersproject/providers';
8
+ import { generateConfig } from '../../config';
9
+
10
+ describe('LitePsm E2E', () => {
11
+ const dexKey = 'LitePsm';
12
+
13
+ describe('LitePsm MAINNET_V6', () => {
14
+ const network = Network.MAINNET;
15
+ const tokens = Tokens[network];
16
+ const holders = Holders[network];
17
+ const provider = new StaticJsonRpcProvider(
18
+ generateConfig(network).privateHttpProvider,
19
+ network,
20
+ );
21
+
22
+ const tokenASymbol: string = 'USDC';
23
+ const tokenBSymbol: string = 'DAI';
24
+
25
+ const tokenAAmount: string = '10000000';
26
+ const tokenBAmount: string = '10000000000000000';
27
+
28
+ const sideToContractMethods = new Map([
29
+ [
30
+ SwapSide.SELL,
31
+ [
32
+ ContractMethod.swapExactAmountIn,
33
+ ContractMethod.swapExactAmountInOutOnMakerPSM,
34
+ ],
35
+ ],
36
+ [
37
+ SwapSide.BUY,
38
+ [
39
+ ContractMethod.swapExactAmountOut,
40
+ ContractMethod.swapExactAmountInOutOnMakerPSM,
41
+ ],
42
+ ],
43
+ ]);
44
+
45
+ sideToContractMethods.forEach((contractMethods, side) =>
46
+ contractMethods.forEach((contractMethod: ContractMethod) => {
47
+ describe(`${contractMethod}`, () => {
48
+ it(`${side}:${tokenASymbol} -> ${tokenBSymbol}`, async () => {
49
+ await testE2E(
50
+ tokens[tokenASymbol],
51
+ tokens[tokenBSymbol],
52
+ holders[tokenASymbol],
53
+ side === SwapSide.SELL ? tokenAAmount : tokenBAmount,
54
+ side,
55
+ dexKey,
56
+ contractMethod,
57
+ network,
58
+ provider,
59
+ );
60
+ });
61
+
62
+ it(`${side}:${tokenBSymbol} -> ${tokenASymbol}`, async () => {
63
+ await testE2E(
64
+ tokens[tokenBSymbol],
65
+ tokens[tokenASymbol],
66
+ holders[tokenBSymbol],
67
+ side === SwapSide.SELL ? tokenBAmount : tokenAAmount,
68
+ side,
69
+ dexKey,
70
+ contractMethod,
71
+ network,
72
+ provider,
73
+ );
74
+ });
75
+ });
76
+ }),
77
+ );
78
+ });
79
+ });
@@ -0,0 +1,216 @@
1
+ import { Interface } from '@ethersproject/abi';
2
+ import { DeepReadonly } from 'ts-essentials';
3
+ import { Contract } from 'web3-eth-contract';
4
+ import { Address, Log, Logger } from '../../types';
5
+ import { StatefulEventSubscriber } from '../../stateful-event-subscriber';
6
+ import { getBigIntPow } from '../../utils';
7
+ import { IDexHelper } from '../../dex-helper/idex-helper';
8
+ import { PoolState, PoolConfig } from './types';
9
+ import { LitePsmConfig } from './config';
10
+ import PsmABI from '../../abi/maker-psm/psm.json';
11
+ import VatABI from '../../abi/maker-psm/vat.json';
12
+ import { erc20Iface } from '../../lib/tokens/utils';
13
+ import DaiABI from '../../abi/lite-psm/dai.json';
14
+ import UsdcABI from '../../abi/lite-psm/usdc.json';
15
+
16
+ const vatInterface = new Interface(VatABI);
17
+ const psmInterface = new Interface(PsmABI);
18
+ const daiInterface = new Interface(DaiABI);
19
+ const usdcInterface = new Interface(UsdcABI);
20
+
21
+ const bigIntify = (b: any) => BigInt(b.toString());
22
+
23
+ export async function getOnChainState(
24
+ multiContract: Contract,
25
+ poolConfigs: PoolConfig[],
26
+ vatAddress: Address,
27
+ blockNumber: number | 'latest',
28
+ daiAddress: Address,
29
+ ): Promise<PoolState[]> {
30
+ const callData = poolConfigs
31
+ .map(c => [
32
+ {
33
+ target: c.psmAddress,
34
+ callData: psmInterface.encodeFunctionData('tin', []),
35
+ },
36
+ {
37
+ target: c.psmAddress,
38
+ callData: psmInterface.encodeFunctionData('tout', []),
39
+ },
40
+ {
41
+ target: vatAddress,
42
+ callData: vatInterface.encodeFunctionData('ilks', [c.identifier]),
43
+ },
44
+ // dai liquidity
45
+ // ceiling for `sellGem`
46
+ {
47
+ target: daiAddress,
48
+ callData: erc20Iface.encodeFunctionData('balanceOf', [c.psmAddress]),
49
+ },
50
+ // gem liquidity
51
+ // ceiling for `buyGem`
52
+ {
53
+ target: c.gem.address,
54
+ callData: erc20Iface.encodeFunctionData('balanceOf', [c.pocketAddress]),
55
+ },
56
+ ])
57
+ .flat();
58
+
59
+ const res = await multiContract.methods
60
+ .aggregate(callData)
61
+ .call({}, blockNumber);
62
+
63
+ let i = 0;
64
+ const result = poolConfigs.map(c => {
65
+ const tin = bigIntify(
66
+ psmInterface.decodeFunctionResult('tin', res.returnData[i++])[0],
67
+ );
68
+ const tout = bigIntify(
69
+ psmInterface.decodeFunctionResult('tout', res.returnData[i++])[0],
70
+ );
71
+ const ilks = vatInterface.decodeFunctionResult('ilks', res.returnData[i++]);
72
+ const rate = bigIntify(ilks.rate);
73
+ const daiBalance = bigIntify(
74
+ erc20Iface.decodeFunctionResult('balanceOf', res.returnData[i++])[0],
75
+ );
76
+ const gemBalance = bigIntify(
77
+ erc20Iface.decodeFunctionResult('balanceOf', res.returnData[i++])[0],
78
+ );
79
+ const response = {
80
+ tin,
81
+ tout,
82
+ daiBalance,
83
+ gemBalance,
84
+ rate,
85
+ };
86
+
87
+ return response;
88
+ });
89
+
90
+ return result;
91
+ }
92
+
93
+ export class LitePsmEventPool extends StatefulEventSubscriber<PoolState> {
94
+ handlers: {
95
+ [event: string]: (event: any, pool: PoolState, log: Log) => PoolState;
96
+ } = {};
97
+
98
+ logDecoder: (log: Log) => any;
99
+
100
+ to18ConversionFactor: bigint;
101
+ bytes32Tout =
102
+ '0x746f757400000000000000000000000000000000000000000000000000000000'; // bytes32('tout')
103
+ bytes32Tin =
104
+ '0x74696e0000000000000000000000000000000000000000000000000000000000'; // bytes32('tin')
105
+
106
+ daiAddress: Address;
107
+
108
+ constructor(
109
+ parentName: string,
110
+ protected network: number,
111
+ protected dexHelper: IDexHelper,
112
+ logger: Logger,
113
+ public poolConfig: PoolConfig,
114
+ protected vatAddress: Address,
115
+ ) {
116
+ super(parentName, poolConfig.identifier, dexHelper, logger);
117
+
118
+ this.daiAddress =
119
+ LitePsmConfig[this.parentName][this.network].dai.address.toLowerCase();
120
+ this.logDecoder = (log: Log) => {
121
+ const logAddress = log.address.toLowerCase();
122
+ if (logAddress === this.daiAddress) {
123
+ return daiInterface.parseLog(log);
124
+ } else if (logAddress === this.poolConfig.gem.address) {
125
+ return usdcInterface.parseLog(log);
126
+ } else {
127
+ return psmInterface.parseLog(log);
128
+ }
129
+ };
130
+ this.addressesSubscribed = [
131
+ poolConfig.psmAddress,
132
+ this.daiAddress,
133
+ this.poolConfig.gem.address,
134
+ ];
135
+ this.to18ConversionFactor = getBigIntPow(18 - poolConfig.gem.decimals);
136
+
137
+ // Add handlers
138
+ this.handlers['File'] = this.handleFile.bind(this);
139
+ this.handlers['Transfer'] = this.handleTransfer.bind(this);
140
+ }
141
+
142
+ handleFile(event: any, pool: PoolState, log: Log): PoolState {
143
+ if (event.args.what === this.bytes32Tin) {
144
+ pool.tin = bigIntify(event.args.data);
145
+ } else if (event.args.what === this.bytes32Tout) {
146
+ pool.tout = bigIntify(event.args.data);
147
+ }
148
+ return pool;
149
+ }
150
+
151
+ handleTransfer(event: any, pool: PoolState, log: Log): PoolState {
152
+ if (log.address.toLowerCase() === this.daiAddress) {
153
+ return this.handleTransferDAI(event, pool, log);
154
+ } else if (log.address.toLowerCase() === this.poolConfig.gem.address) {
155
+ return this.handleTransferGEM(event, pool, log);
156
+ }
157
+ return pool;
158
+ }
159
+
160
+ handleTransferDAI(event: any, pool: PoolState, log: Log): PoolState {
161
+ if (event.args?.src?.toLowerCase() === this.poolConfig.psmAddress) {
162
+ pool.daiBalance -= bigIntify(event.args.wad);
163
+ } else if (event.args?.dst?.toLowerCase() === this.poolConfig.psmAddress) {
164
+ pool.daiBalance += bigIntify(event.args.wad);
165
+ }
166
+ return pool;
167
+ }
168
+
169
+ handleTransferGEM(event: any, pool: PoolState, log: Log): PoolState {
170
+ if (event.args?.from?.toLowerCase() === this.poolConfig.pocketAddress) {
171
+ pool.gemBalance -= bigIntify(event.args.value);
172
+ } else if (
173
+ event.args?.to?.toLowerCase() === this.poolConfig.pocketAddress
174
+ ) {
175
+ pool.gemBalance += bigIntify(event.args.value);
176
+ }
177
+ return pool;
178
+ }
179
+
180
+ getIdentifier(): string {
181
+ return `${this.parentName}_${this.poolConfig.psmAddress}`.toLowerCase();
182
+ }
183
+
184
+ protected processLog(
185
+ state: DeepReadonly<PoolState>,
186
+ log: Readonly<Log>,
187
+ ): DeepReadonly<PoolState> | null {
188
+ try {
189
+ const event = this.logDecoder(log);
190
+ if (event.name in this.handlers) {
191
+ return this.handlers[event.name](event, state, log);
192
+ }
193
+ return state;
194
+ } catch (e) {
195
+ this.logger.error(
196
+ `Error_${this.parentName}_processLog could not parse the log with topic ${log.topics}:`,
197
+ e,
198
+ );
199
+ return null;
200
+ }
201
+ }
202
+
203
+ async generateState(
204
+ blockNumber: number | 'latest',
205
+ ): Promise<Readonly<PoolState>> {
206
+ return (
207
+ await getOnChainState(
208
+ this.dexHelper.multiContract,
209
+ [this.poolConfig],
210
+ this.vatAddress,
211
+ blockNumber,
212
+ this.daiAddress,
213
+ )
214
+ )[0];
215
+ }
216
+ }
@@ -0,0 +1,61 @@
1
+ import dotenv from 'dotenv';
2
+ dotenv.config();
3
+
4
+ import { LitePsmEventPool } from './lite-psm-event-pool';
5
+ import { LitePsmConfig } from './config';
6
+ import { Network } from '../../constants';
7
+ import { DummyDexHelper } from '../../dex-helper/index';
8
+ import { testEventSubscriber } from '../../../tests/utils-events';
9
+ import { PoolState } from './types';
10
+
11
+ jest.setTimeout(50 * 1000);
12
+ const dexKey = 'LitePsm';
13
+ const network = Network.MAINNET;
14
+ const config = LitePsmConfig[dexKey][network];
15
+
16
+ function fetchPoolState(
17
+ makerPsmPool: LitePsmEventPool,
18
+ blockNumber: number,
19
+ ): Promise<PoolState> {
20
+ return makerPsmPool.generateState(blockNumber);
21
+ }
22
+
23
+ describe('LitePsm Event', function () {
24
+ const blockNumbers: { [eventName: string]: number[] } = {
25
+ // File: [], // never happened
26
+ SellGem: [20559136, 20559876, 20561086, 20563099, 20563672, 20563887],
27
+ BuyGem: [
28
+ 20554181, 20555354, 20555452, 20555880, 20556086, 20556431, 20558791,
29
+ ],
30
+ };
31
+
32
+ describe('LitePsmEventPool', function () {
33
+ Object.keys(blockNumbers).forEach((event: string) => {
34
+ blockNumbers[event].forEach((blockNumber: number) => {
35
+ it(`Should return the correct state after the ${blockNumber}:${event}`, async function () {
36
+ const dexHelper = new DummyDexHelper(network);
37
+ const logger = dexHelper.getLogger(dexKey);
38
+
39
+ const makerPsmPool = new LitePsmEventPool(
40
+ dexKey,
41
+ network,
42
+ dexHelper,
43
+ logger,
44
+ config.pools[0],
45
+ config.vatAddress,
46
+ );
47
+
48
+ await testEventSubscriber(
49
+ makerPsmPool,
50
+ makerPsmPool.addressesSubscribed,
51
+ (_blockNumber: number) =>
52
+ fetchPoolState(makerPsmPool, _blockNumber),
53
+ blockNumber,
54
+ `${dexKey}_${config.pools[0].psmAddress}`,
55
+ dexHelper.provider,
56
+ );
57
+ });
58
+ });
59
+ });
60
+ });
61
+ });
@@ -0,0 +1,99 @@
1
+ import dotenv from 'dotenv';
2
+ dotenv.config();
3
+
4
+ import { DummyDexHelper } from '../../dex-helper/index';
5
+ import { Network, SwapSide } from '../../constants';
6
+ import { LitePsm } from './lite-psm';
7
+ import { checkPoolPrices, checkPoolsLiquidity } from '../../../tests/utils';
8
+ import { Tokens } from '../../../tests/constants-e2e';
9
+ import { BI_POWS } from '../../bigint-constants';
10
+
11
+ const network = Network.MAINNET;
12
+ const TokenASymbol = 'USDC';
13
+ const TokenA = Tokens[network][TokenASymbol];
14
+
15
+ const TokenBSymbol = 'DAI';
16
+ const TokenB = Tokens[network][TokenBSymbol];
17
+
18
+ const tokenAAmounts = [0n, BI_POWS[8], 200000000n];
19
+
20
+ const tokenBAmounts = [0n, BI_POWS[18], 2000000000000000000n];
21
+
22
+ const dexKey = 'LitePsm';
23
+
24
+ describe('LitePsm', function () {
25
+ it('getPoolIdentifiers and getPricesVolume SELL', async function () {
26
+ const dexHelper = new DummyDexHelper(network);
27
+ const blocknumber = await dexHelper.web3Provider.eth.getBlockNumber();
28
+ const litePsm = new LitePsm(network, dexKey, dexHelper);
29
+
30
+ await litePsm.initializePricing(blocknumber);
31
+
32
+ const pools = await litePsm.getPoolIdentifiers(
33
+ TokenA,
34
+ TokenB,
35
+ SwapSide.SELL,
36
+ blocknumber,
37
+ );
38
+ console.log(`${TokenASymbol} <> ${TokenBSymbol} Pool Identifiers: `, pools);
39
+
40
+ expect(pools.length).toBeGreaterThan(0);
41
+
42
+ const poolPrices = await litePsm.getPricesVolume(
43
+ TokenA,
44
+ TokenB,
45
+ tokenAAmounts,
46
+ SwapSide.SELL,
47
+ blocknumber,
48
+ pools,
49
+ );
50
+ console.log(`${TokenASymbol} <> ${TokenBSymbol} Pool Prices: `, poolPrices);
51
+
52
+ expect(poolPrices).not.toBeNull();
53
+ checkPoolPrices(poolPrices!, tokenAAmounts, SwapSide.SELL, dexKey);
54
+ });
55
+
56
+ it('getPoolIdentifiers and getPricesVolume BUY', async function () {
57
+ const dexHelper = new DummyDexHelper(network);
58
+ const blocknumber = await dexHelper.web3Provider.eth.getBlockNumber();
59
+ const litePsm = new LitePsm(network, dexKey, dexHelper);
60
+
61
+ await litePsm.initializePricing(blocknumber);
62
+
63
+ const pools = await litePsm.getPoolIdentifiers(
64
+ TokenA,
65
+ TokenB,
66
+ SwapSide.BUY,
67
+ blocknumber,
68
+ );
69
+ console.log(`${TokenASymbol} <> ${TokenBSymbol} Pool Identifiers: `, pools);
70
+
71
+ expect(pools.length).toBeGreaterThan(0);
72
+
73
+ const poolPrices = await litePsm.getPricesVolume(
74
+ TokenA,
75
+ TokenB,
76
+ tokenBAmounts,
77
+ SwapSide.BUY,
78
+ blocknumber,
79
+ pools,
80
+ );
81
+ console.log(`${TokenASymbol} <> ${TokenBSymbol} Pool Prices: `, poolPrices);
82
+
83
+ expect(poolPrices).not.toBeNull();
84
+ checkPoolPrices(poolPrices!, tokenBAmounts, SwapSide.BUY, dexKey);
85
+ });
86
+
87
+ it('getTopPoolsForToken', async function () {
88
+ const dexHelper = new DummyDexHelper(network);
89
+ const litePsm = new LitePsm(network, dexKey, dexHelper);
90
+
91
+ const poolLiquidity = await litePsm.getTopPoolsForToken(TokenA.address, 10);
92
+ console.log(
93
+ `${TokenASymbol} Top Pools:`,
94
+ JSON.stringify(poolLiquidity, null, 2),
95
+ );
96
+
97
+ checkPoolsLiquidity(poolLiquidity, TokenA.address, dexKey);
98
+ });
99
+ });