@paraswap/dex-lib 2.16.0 → 2.16.1-generate-state-latest.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 (220) hide show
  1. package/build/composed-event-subscriber.d.ts +2 -2
  2. package/build/composed-event-subscriber.js +9 -4
  3. package/build/composed-event-subscriber.js.map +1 -1
  4. package/build/dex/aave-v1/aave-v1.d.ts +1 -1
  5. package/build/dex/aave-v1/aave-v1.js +1 -1
  6. package/build/dex/aave-v1/aave-v1.js.map +1 -1
  7. package/build/dex/aave-v2/aave-v2.d.ts +1 -1
  8. package/build/dex/aave-v2/aave-v2.js +1 -1
  9. package/build/dex/aave-v2/aave-v2.js.map +1 -1
  10. package/build/dex/aave-v3/aave-v3.d.ts +1 -1
  11. package/build/dex/aave-v3/aave-v3.js +1 -1
  12. package/build/dex/aave-v3/aave-v3.js.map +1 -1
  13. package/build/dex/aave-v3/tokens/arbitrum.json +50 -0
  14. package/build/dex/aave-v3/tokens/avalanche.json +74 -0
  15. package/build/dex/aave-v3/tokens/fantom.json +62 -0
  16. package/build/dex/aave-v3/tokens/optimism.json +50 -0
  17. package/build/dex/aave-v3/tokens/polygon.json +116 -0
  18. package/build/dex/aave-v3/utils.d.ts +1 -1
  19. package/build/dex/aave-v3/utils.js.map +1 -1
  20. package/build/dex/balancer-v1/balancer-v1-pool.d.ts +2 -2
  21. package/build/dex/balancer-v1/balancer-v1-pool.js +5 -1
  22. package/build/dex/balancer-v1/balancer-v1-pool.js.map +1 -1
  23. package/build/dex/balancer-v1/balancer-v1.d.ts +1 -1
  24. package/build/dex/balancer-v1/balancer-v1.js +7 -4
  25. package/build/dex/balancer-v1/balancer-v1.js.map +1 -1
  26. package/build/dex/balancer-v1/utils.d.ts +3 -1
  27. package/build/dex/balancer-v1/utils.js +18 -5
  28. package/build/dex/balancer-v1/utils.js.map +1 -1
  29. package/build/dex/balancer-v2/balancer-v2.d.ts +10 -5
  30. package/build/dex/balancer-v2/balancer-v2.js +15 -10
  31. package/build/dex/balancer-v2/balancer-v2.js.map +1 -1
  32. package/build/dex/camelot/camelot.d.ts +2 -2
  33. package/build/dex/camelot/camelot.js +15 -6
  34. package/build/dex/camelot/camelot.js.map +1 -1
  35. package/build/dex/curve-v1/curve-v1.d.ts +1 -1
  36. package/build/dex/curve-v1/curve-v1.js +5 -8
  37. package/build/dex/curve-v1/curve-v1.js.map +1 -1
  38. package/build/dex/curve-v1/pools/curve-metapool.d.ts +2 -2
  39. package/build/dex/curve-v1/pools/curve-metapool.js +12 -4
  40. package/build/dex/curve-v1/pools/curve-metapool.js.map +1 -1
  41. package/build/dex/curve-v1/pools/curve-pool.d.ts +3 -3
  42. package/build/dex/curve-v1/pools/curve-pool.js +13 -4
  43. package/build/dex/curve-v1/pools/curve-pool.js.map +1 -1
  44. package/build/dex/curve-v1/pools/getstate-multicall.d.ts +6 -1
  45. package/build/dex/curve-v1/pools/getstate-multicall.js +32 -25
  46. package/build/dex/curve-v1/pools/getstate-multicall.js.map +1 -1
  47. package/build/dex/curve-v1-factory/curve-v1-factory.d.ts +3 -3
  48. package/build/dex/curve-v1-factory/curve-v1-factory.js +4 -4
  49. package/build/dex/curve-v1-factory/curve-v1-factory.js.map +1 -1
  50. package/build/dex/curve-v1-factory/curve-v1-pool-manager.d.ts +1 -1
  51. package/build/dex/curve-v1-factory/curve-v1-pool-manager.js +1 -1
  52. package/build/dex/curve-v1-factory/curve-v1-pool-manager.js.map +1 -1
  53. package/build/dex/curve-v1-factory/state-polling-pools/custom-pool-polling.d.ts +1 -1
  54. package/build/dex/curve-v1-factory/state-polling-pools/custom-pool-polling.js.map +1 -1
  55. package/build/dex/curve-v1-factory/state-polling-pools/factory-pool-polling.d.ts +1 -1
  56. package/build/dex/curve-v1-factory/state-polling-pools/factory-pool-polling.js.map +1 -1
  57. package/build/dex/curve-v1-factory/state-polling-pools/polling-manager.d.ts +4 -4
  58. package/build/dex/curve-v1-factory/state-polling-pools/polling-manager.js +5 -15
  59. package/build/dex/curve-v1-factory/state-polling-pools/polling-manager.js.map +1 -1
  60. package/build/dex/curve-v1-factory/state-polling-pools/pool-polling-base.d.ts +1 -1
  61. package/build/dex/curve-v1-factory/types.d.ts +1 -1
  62. package/build/dex/generic-rfq/generic-rfq.d.ts +1 -1
  63. package/build/dex/generic-rfq/generic-rfq.js +1 -1
  64. package/build/dex/generic-rfq/generic-rfq.js.map +1 -1
  65. package/build/dex/generic-rfq/utils.js +1 -1
  66. package/build/dex/generic-rfq/utils.js.map +1 -1
  67. package/build/dex/gmx/gmx.d.ts +1 -1
  68. package/build/dex/gmx/gmx.js +1 -1
  69. package/build/dex/gmx/gmx.js.map +1 -1
  70. package/build/dex/gmx/pool.js +5 -4
  71. package/build/dex/gmx/pool.js.map +1 -1
  72. package/build/dex/idex.d.ts +1 -1
  73. package/build/dex/jarvis-v6/jarvis-v6-events.d.ts +4 -3
  74. package/build/dex/jarvis-v6/jarvis-v6-events.js +8 -4
  75. package/build/dex/jarvis-v6/jarvis-v6-events.js.map +1 -1
  76. package/build/dex/jarvis-v6/jarvis-v6.d.ts +1 -1
  77. package/build/dex/jarvis-v6/jarvis-v6.js +12 -8
  78. package/build/dex/jarvis-v6/jarvis-v6.js.map +1 -1
  79. package/build/dex/jarvis-v6/utils.d.ts +2 -1
  80. package/build/dex/jarvis-v6/utils.js +15 -6
  81. package/build/dex/jarvis-v6/utils.js.map +1 -1
  82. package/build/dex/kyberdmm/kyberdmm.js +4 -1
  83. package/build/dex/kyberdmm/kyberdmm.js.map +1 -1
  84. package/build/dex/kyberdmm/pool.d.ts +2 -2
  85. package/build/dex/kyberdmm/pool.js +21 -20
  86. package/build/dex/kyberdmm/pool.js.map +1 -1
  87. package/build/dex/maker-psm/maker-psm.d.ts +3 -3
  88. package/build/dex/maker-psm/maker-psm.js +35 -27
  89. package/build/dex/maker-psm/maker-psm.js.map +1 -1
  90. package/build/dex/maverick-v1/maverick-v1-pool.d.ts +3 -5
  91. package/build/dex/maverick-v1/maverick-v1-pool.js +27 -38
  92. package/build/dex/maverick-v1/maverick-v1-pool.js.map +1 -1
  93. package/build/dex/maverick-v1/maverick-v1.d.ts +2 -2
  94. package/build/dex/maverick-v1/maverick-v1.js +8 -8
  95. package/build/dex/maverick-v1/maverick-v1.js.map +1 -1
  96. package/build/dex/nerve/nerve-pool.d.ts +2 -2
  97. package/build/dex/nerve/nerve-pool.js +28 -25
  98. package/build/dex/nerve/nerve-pool.js.map +1 -1
  99. package/build/dex/nerve/nerve.d.ts +2 -2
  100. package/build/dex/nerve/nerve.js +5 -5
  101. package/build/dex/nerve/nerve.js.map +1 -1
  102. package/build/dex/platypus/platypus.d.ts +3 -3
  103. package/build/dex/platypus/platypus.js +6 -5
  104. package/build/dex/platypus/platypus.js.map +1 -1
  105. package/build/dex/swaap-v1/config.d.ts +1 -1
  106. package/build/dex/swaap-v1/config.js +2 -2
  107. package/build/dex/swaap-v1/swaap-v1-pool.d.ts +3 -3
  108. package/build/dex/swaap-v1/swaap-v1-pool.js +15 -9
  109. package/build/dex/swaap-v1/swaap-v1-pool.js.map +1 -1
  110. package/build/dex/swaap-v1/swaap-v1.d.ts +2 -2
  111. package/build/dex/swaap-v1/swaap-v1.js +11 -8
  112. package/build/dex/swaap-v1/swaap-v1.js.map +1 -1
  113. package/build/dex/synthetix/synthetix-state.js +1 -1
  114. package/build/dex/synthetix/synthetix-state.js.map +1 -1
  115. package/build/dex/synthetix/synthetix.d.ts +1 -1
  116. package/build/dex/synthetix/synthetix.js +5 -4
  117. package/build/dex/synthetix/synthetix.js.map +1 -1
  118. package/build/dex/test-dex/config.d.ts +4 -0
  119. package/build/dex/test-dex/config.js +15 -0
  120. package/build/dex/test-dex/config.js.map +1 -0
  121. package/build/dex/test-dex/test-dex-pool.d.ts +39 -0
  122. package/build/dex/test-dex/test-dex-pool.js +63 -0
  123. package/build/dex/test-dex/test-dex-pool.js.map +1 -0
  124. package/build/dex/test-dex/test-dex.d.ts +37 -0
  125. package/build/dex/test-dex/test-dex.js +126 -0
  126. package/build/dex/test-dex/test-dex.js.map +1 -0
  127. package/build/dex/test-dex/types.d.ts +6 -0
  128. package/build/dex/test-dex/types.js +3 -0
  129. package/build/dex/test-dex/types.js.map +1 -0
  130. package/build/dex/uniswap-v2/uniswap-v2.d.ts +2 -2
  131. package/build/dex/uniswap-v2/uniswap-v2.js +15 -10
  132. package/build/dex/uniswap-v2/uniswap-v2.js.map +1 -1
  133. package/build/dex/uniswap-v3/uniswap-v3-pool.d.ts +3 -3
  134. package/build/dex/uniswap-v3/uniswap-v3-pool.js +33 -27
  135. package/build/dex/uniswap-v3/uniswap-v3-pool.js.map +1 -1
  136. package/build/dex/uniswap-v3/uniswap-v3.d.ts +2 -2
  137. package/build/dex/uniswap-v3/uniswap-v3.js +4 -3
  138. package/build/dex/uniswap-v3/uniswap-v3.js.map +1 -1
  139. package/build/dex/woo-fi-v2/woo-fi-math.d.ts +16 -0
  140. package/build/dex/woo-fi-v2/woo-fi-math.js +137 -0
  141. package/build/dex/woo-fi-v2/woo-fi-math.js.map +1 -0
  142. package/build/dex/woo-fi-v2/woo-fi-v2.d.ts +1 -1
  143. package/build/dex/woo-fi-v2/woo-fi-v2.js +1 -1
  144. package/build/dex/woo-fi-v2/woo-fi-v2.js.map +1 -1
  145. package/build/implementations/local-paraswap-sdk.js +1 -2
  146. package/build/implementations/local-paraswap-sdk.js.map +1 -1
  147. package/build/lib/generics-events-subscribers/erc20-event-subscriber.d.ts +2 -2
  148. package/build/lib/generics-events-subscribers/erc20-event-subscriber.js +17 -12
  149. package/build/lib/generics-events-subscribers/erc20-event-subscriber.js.map +1 -1
  150. package/build/lib/multi-wrapper.d.ts +5 -1
  151. package/build/lib/multi-wrapper.js +21 -2
  152. package/build/lib/multi-wrapper.js.map +1 -1
  153. package/build/lib/tokens/balancer-fetcher.d.ts +5 -1
  154. package/build/lib/tokens/balancer-fetcher.js +7 -9
  155. package/build/lib/tokens/balancer-fetcher.js.map +1 -1
  156. package/build/pricing-helper.d.ts +1 -1
  157. package/build/pricing-helper.js +1 -1
  158. package/build/pricing-helper.js.map +1 -1
  159. package/build/stateful-event-subscriber.d.ts +7 -3
  160. package/build/stateful-event-subscriber.js +25 -22
  161. package/build/stateful-event-subscriber.js.map +1 -1
  162. package/build/utils.d.ts +2 -2
  163. package/build/utils.js +1 -1
  164. package/build/utils.js.map +1 -1
  165. package/dex-template/__DexName__.ts.template +3 -1
  166. package/package.json +1 -1
  167. package/src/composed-event-subscriber.ts +24 -13
  168. package/src/dex/aave-v1/aave-v1.ts +1 -1
  169. package/src/dex/aave-v2/aave-v2.ts +1 -1
  170. package/src/dex/aave-v3/aave-v3.ts +3 -1
  171. package/src/dex/aave-v3/utils.ts +3 -3
  172. package/src/dex/balancer-v1/balancer-v1-pool.ts +18 -9
  173. package/src/dex/balancer-v1/balancer-v1.ts +13 -8
  174. package/src/dex/balancer-v1/utils.ts +36 -7
  175. package/src/dex/balancer-v2/balancer-v2.ts +42 -25
  176. package/src/dex/camelot/camelot.ts +29 -10
  177. package/src/dex/curve-v1/curve-v1.ts +11 -11
  178. package/src/dex/curve-v1/pools/curve-metapool.ts +23 -7
  179. package/src/dex/curve-v1/pools/curve-pool.ts +29 -8
  180. package/src/dex/curve-v1/pools/getstate-multicall.ts +83 -56
  181. package/src/dex/curve-v1-factory/curve-v1-factory.ts +4 -4
  182. package/src/dex/curve-v1-factory/curve-v1-pool-manager.ts +1 -1
  183. package/src/dex/curve-v1-factory/state-polling-pools/custom-pool-polling.ts +1 -1
  184. package/src/dex/curve-v1-factory/state-polling-pools/factory-pool-polling.ts +1 -1
  185. package/src/dex/curve-v1-factory/state-polling-pools/polling-manager.ts +5 -16
  186. package/src/dex/curve-v1-factory/state-polling-pools/pool-polling-base.ts +1 -1
  187. package/src/dex/curve-v1-factory/types.ts +1 -1
  188. package/src/dex/generic-rfq/generic-rfq.ts +3 -1
  189. package/src/dex/generic-rfq/utils.ts +1 -1
  190. package/src/dex/gmx/gmx.ts +1 -1
  191. package/src/dex/gmx/pool.ts +9 -4
  192. package/src/dex/idex.ts +1 -1
  193. package/src/dex/jarvis-v6/jarvis-v6-events.ts +17 -6
  194. package/src/dex/jarvis-v6/jarvis-v6.ts +17 -15
  195. package/src/dex/jarvis-v6/utils.ts +21 -9
  196. package/src/dex/kyberdmm/kyberdmm.ts +4 -1
  197. package/src/dex/kyberdmm/pool.ts +32 -24
  198. package/src/dex/maker-psm/maker-psm.ts +69 -47
  199. package/src/dex/maverick-v1/maverick-v1-pool.ts +50 -38
  200. package/src/dex/maverick-v1/maverick-v1.ts +12 -8
  201. package/src/dex/nerve/nerve-pool.ts +42 -30
  202. package/src/dex/nerve/nerve.ts +9 -5
  203. package/src/dex/platypus/platypus.ts +11 -7
  204. package/src/dex/swaap-v1/config.ts +1 -1
  205. package/src/dex/swaap-v1/swaap-v1-pool.ts +23 -14
  206. package/src/dex/swaap-v1/swaap-v1.ts +21 -14
  207. package/src/dex/synthetix/synthetix-state.ts +3 -3
  208. package/src/dex/synthetix/synthetix.ts +5 -4
  209. package/src/dex/uniswap-v2/uniswap-v2.ts +27 -13
  210. package/src/dex/uniswap-v3/uniswap-v3-pool.ts +43 -39
  211. package/src/dex/uniswap-v3/uniswap-v3.ts +5 -4
  212. package/src/dex/woo-fi-v2/woo-fi-v2.ts +3 -1
  213. package/src/implementations/local-paraswap-sdk.ts +1 -2
  214. package/src/lib/generics-events-subscribers/erc20-event-subscriber.ts +29 -18
  215. package/src/lib/multi-wrapper.ts +41 -2
  216. package/src/lib/tokens/balancer-fetcher.ts +23 -19
  217. package/src/pricing-helper.ts +5 -2
  218. package/src/stateful-event-subscriber.ts +35 -28
  219. package/src/utils.ts +3 -4
  220. package/tests/utils.test.ts +44 -0
@@ -93,22 +93,21 @@ export class JarvisV6
93
93
  // pricing service. It is intended to setup the integration
94
94
  // for pricing requests. It is optional for a DEX to
95
95
  // implement this function
96
- async initializePricing(blockNumber: number) {
97
- const poolStates = await getOnChainState(
98
- this.dexHelper,
99
- this.poolConfigs,
100
- blockNumber,
101
- {
96
+ async initializePricing(blockNumber: number | 'latest' = 'latest') {
97
+ const { blockNumber: _blockNumber, state: poolStates } =
98
+ await getOnChainState(this.dexHelper, this.poolConfigs, blockNumber, {
102
99
  poolInterface: this.poolInterface,
103
100
  priceFeedContract: this.priceFeedContract,
104
- },
105
- );
101
+ });
106
102
 
107
103
  await Promise.all(
108
104
  this.poolConfigs.map(async (pool, index) => {
109
105
  const eventPool = this.eventPools[pool.address.toLowerCase()];
110
- await eventPool.initialize(blockNumber, {
111
- state: poolStates[index],
106
+ await eventPool.initialize(_blockNumber, {
107
+ stateWithBn: {
108
+ state: poolStates[index],
109
+ blockNumber: _blockNumber,
110
+ },
112
111
  });
113
112
  }),
114
113
  );
@@ -145,10 +144,13 @@ export class JarvisV6
145
144
  blockNumber: number,
146
145
  ): Promise<PoolState> {
147
146
  const eventState = pool.getState(blockNumber);
148
- if (eventState) return eventState;
149
- const onChainState = await pool.generateState(blockNumber);
150
- pool.setState(onChainState, blockNumber);
151
- return onChainState;
147
+ if (eventState) {
148
+ return eventState;
149
+ }
150
+
151
+ const onChainStateWithBn = await pool.generateState(blockNumber);
152
+ pool.setState(onChainStateWithBn.state, onChainStateWithBn.blockNumber);
153
+ return onChainStateWithBn.state;
152
154
  }
153
155
 
154
156
  // Returns pool prices for amounts.
@@ -162,7 +164,7 @@ export class JarvisV6
162
164
  amounts: bigint[],
163
165
  side: SwapSide,
164
166
  blockNumber: number,
165
- limitPools?: string[], // unusued since DEX is constant price, safe to reuse pools
167
+ limitPools?: string[], // unused since DEX is constant price, safe to reuse pools
166
168
  ): Promise<null | ExchangePrices<JarvisV6Data>> {
167
169
  if (side !== SwapSide.SELL) return null;
168
170
 
@@ -6,6 +6,7 @@ import { getBigIntPow } from '../../utils';
6
6
  import { JarvisSwapFunctions, PoolConfig, PoolState } from './types';
7
7
  import { Interface } from '@ethersproject/abi';
8
8
  import { Contract } from 'web3-eth-contract';
9
+ import { StateWithBlock } from '../../stateful-event-subscriber';
9
10
 
10
11
  export const THIRTY_MINUTES = 60 * 30;
11
12
 
@@ -18,27 +19,38 @@ export async function getOnChainState(
18
19
  priceFeedContract,
19
20
  poolInterface,
20
21
  }: { priceFeedContract: Contract; poolInterface: Interface },
21
- ): Promise<PoolState[]> {
22
+ ): Promise<StateWithBlock<PoolState[]>> {
23
+ const _blockNumber =
24
+ blockNumber === 'latest'
25
+ ? await dexHelper.web3Provider.eth.getBlockNumber()
26
+ : blockNumber;
27
+
28
+ // These two calls can not be packed into multicall because Jarvis contract
29
+ // is callable either from Jarvis or EOA. If you try to call from Multicall,
30
+ // it will fail
22
31
  const [pricesFeedValues, poolFeePercentages] = await Promise.all([
23
32
  _getPricesFeedValues(
24
33
  dexHelper,
25
34
  poolConfigs,
26
- blockNumber,
35
+ _blockNumber,
27
36
  priceFeedContract,
28
37
  ),
29
- _getPoolFeePercentages(dexHelper, poolConfigs, blockNumber, poolInterface),
38
+ _getPoolFeePercentages(dexHelper, poolConfigs, _blockNumber, poolInterface),
30
39
  ]);
31
40
 
32
- return poolConfigs.map((_, index) => ({
33
- priceFeed: pricesFeedValues[index],
34
- pool: poolFeePercentages[index],
35
- }));
41
+ return {
42
+ blockNumber: _blockNumber,
43
+ state: poolConfigs.map((_, index) => ({
44
+ priceFeed: pricesFeedValues[index],
45
+ pool: poolFeePercentages[index],
46
+ })),
47
+ };
36
48
  }
37
49
 
38
50
  async function _getPoolFeePercentages(
39
51
  dexHelper: IDexHelper,
40
52
  poolConfigs: PoolConfig[],
41
- blockNumber: number | 'latest',
53
+ blockNumber: number,
42
54
  poolInterface: Interface,
43
55
  ) {
44
56
  const multiContract = dexHelper.multiContract;
@@ -65,7 +77,7 @@ async function _getPoolFeePercentages(
65
77
  async function _getPricesFeedValues(
66
78
  dexHelper: IDexHelper,
67
79
  poolConfigs: PoolConfig[],
68
- blockNumber: number | 'latest',
80
+ blockNumber: number,
69
81
  priceFeedContract: Contract,
70
82
  ) {
71
83
  const pairList = poolConfigs.map(pool =>
@@ -251,7 +251,10 @@ export class KyberDmm
251
251
  pair.pools[poolAddress] = pool;
252
252
  pool.addressesSubscribed.push(poolAddress);
253
253
  await pool.initialize(blockNumber, {
254
- state: poolData,
254
+ stateWithBn: {
255
+ state: poolData,
256
+ blockNumber,
257
+ },
255
258
  });
256
259
  }
257
260
  }
@@ -3,10 +3,13 @@ import { DeepReadonly } from 'ts-essentials';
3
3
  import kyberDmmPoolABI from '../../abi/kyberdmm/kyber-dmm-pool.abi.json';
4
4
  import { getFee, getRFactor } from './fee-formula';
5
5
  import { KyberDmmAbiEvents, TradeInfo } from './types';
6
- import { StatefulEventSubscriber } from '../../stateful-event-subscriber';
6
+ import {
7
+ StatefulEventSubscriber,
8
+ StateWithBlock,
9
+ } from '../../stateful-event-subscriber';
7
10
  import { Address, BlockHeader, Log, Logger, Token } from '../../types';
8
11
  import { IDexHelper } from '../../dex-helper/idex-helper';
9
- import { BI_POWS } from '../../bigint-constants';
12
+ import { blockAndTryAggregate } from '../../utils';
10
13
 
11
14
  export type KyberDmmPools = { [poolAddress: string]: KyberDmmPool };
12
15
 
@@ -113,7 +116,6 @@ export class KyberDmmPool extends StatefulEventSubscriber<KyberDmmPoolState> {
113
116
  },
114
117
  };
115
118
  }
116
- return null;
117
119
  }
118
120
 
119
121
  isAmpPool(): boolean {
@@ -122,10 +124,12 @@ export class KyberDmmPool extends StatefulEventSubscriber<KyberDmmPoolState> {
122
124
 
123
125
  async generateState(
124
126
  blockNumber: number | 'latest' = 'latest',
125
- ): Promise<DeepReadonly<KyberDmmPoolState>> {
126
- const data: { returnData: any[] } =
127
- await this.dexHelper.multiContract.methods
128
- .aggregate([
127
+ ): Promise<StateWithBlock<KyberDmmPoolState>> {
128
+ const { results: data, blockNumber: _blockNumber } =
129
+ await blockAndTryAggregate(
130
+ true,
131
+ this.dexHelper.multiContract,
132
+ [
129
133
  {
130
134
  target: this.poolAddress,
131
135
  callData: iface.encodeFunctionData('getTradeInfo', []),
@@ -134,18 +138,19 @@ export class KyberDmmPool extends StatefulEventSubscriber<KyberDmmPoolState> {
134
138
  target: this.poolAddress,
135
139
  callData: iface.encodeFunctionData('getVolumeTrendData', []),
136
140
  },
137
- ])
138
- .call({}, blockNumber);
141
+ ],
142
+ blockNumber,
143
+ );
139
144
 
140
145
  const [reserves0, reserves1, vReserves0, vReserves1] = coder
141
- .decode(['uint256', 'uint256', 'uint256', 'uint256'], data.returnData[0])
146
+ .decode(['uint256', 'uint256', 'uint256', 'uint256'], data[0].returnData)
142
147
  .map(a => BigInt(a.toString()));
143
148
 
144
149
  const [shortEMA, longEMA, , lastTradeBlock] = coder
145
- .decode(['uint256', 'uint256', 'uint128', 'uint256'], data.returnData[1])
150
+ .decode(['uint256', 'uint256', 'uint128', 'uint256'], data[1].returnData)
146
151
  .map(a => BigInt(a.toString()));
147
152
 
148
- if (blockNumber == 'latest')
153
+ if (blockNumber === 'latest')
149
154
  blockNumber = await this.dexHelper.web3Provider.eth.getBlockNumber();
150
155
 
151
156
  const prevBlockData: { returnData: any[] } =
@@ -166,19 +171,22 @@ export class KyberDmmPool extends StatefulEventSubscriber<KyberDmmPoolState> {
166
171
  .map(a => BigInt(a.toString()));
167
172
 
168
173
  return {
169
- trendData: {
170
- shortEMA,
171
- longEMA,
172
- lastBlockVolume,
173
- lastTradeBlock,
174
- },
175
- reserves: {
176
- reserves0,
177
- reserves1,
178
- vReserves0,
179
- vReserves1,
174
+ blockNumber,
175
+ state: {
176
+ trendData: {
177
+ shortEMA,
178
+ longEMA,
179
+ lastBlockVolume,
180
+ lastTradeBlock,
181
+ },
182
+ reserves: {
183
+ reserves0,
184
+ reserves1,
185
+ vReserves0,
186
+ vReserves1,
187
+ },
188
+ ampBps: this.ampBps,
180
189
  },
181
- ampBps: this.ampBps,
182
190
  };
183
191
  }
184
192
  }
@@ -14,8 +14,15 @@ import {
14
14
  } from '../../types';
15
15
  import { SwapSide, Network } from '../../constants';
16
16
  import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
17
- import { StatefulEventSubscriber } from '../../stateful-event-subscriber';
18
- import { getDexKeysWithNetwork, getBigIntPow } from '../../utils';
17
+ import {
18
+ StatefulEventSubscriber,
19
+ StateWithBlock,
20
+ } from '../../stateful-event-subscriber';
21
+ import {
22
+ getDexKeysWithNetwork,
23
+ getBigIntPow,
24
+ blockAndTryAggregate,
25
+ } from '../../utils';
19
26
  import { IDex } from '../../dex/idex';
20
27
  import { IDexHelper } from '../../dex-helper/idex-helper';
21
28
  import { MakerPsmData, PoolState, PoolConfig } from './types';
@@ -37,7 +44,7 @@ async function getOnChainState(
37
44
  poolConfigs: PoolConfig[],
38
45
  vatAddress: Address,
39
46
  blockNumber: number | 'latest',
40
- ): Promise<PoolState[]> {
47
+ ): Promise<StateWithBlock<PoolState[]>> {
41
48
  const callData = poolConfigs
42
49
  .map(c => [
43
50
  {
@@ -55,30 +62,35 @@ async function getOnChainState(
55
62
  ])
56
63
  .flat();
57
64
 
58
- const res = await multiContract.methods
59
- .aggregate(callData)
60
- .call({}, blockNumber);
65
+ const { blockNumber: _blockNumber, results: res } =
66
+ await blockAndTryAggregate(true, multiContract, callData, blockNumber);
61
67
 
62
68
  let i = 0;
63
- return poolConfigs.map(c => {
64
- const tin = bigIntify(
65
- psmInterface.decodeFunctionResult('tin', res.returnData[i++])[0],
66
- );
67
- const tout = bigIntify(
68
- psmInterface.decodeFunctionResult('tout', res.returnData[i++])[0],
69
- );
70
- const ilks = vatInterface.decodeFunctionResult('ilks', res.returnData[i++]);
71
- const Art = bigIntify(ilks.Art);
72
- const line = bigIntify(ilks.line);
73
- const rate = bigIntify(ilks.rate);
74
- return {
75
- tin,
76
- tout,
77
- Art,
78
- line,
79
- rate,
80
- };
81
- });
69
+ return {
70
+ blockNumber: _blockNumber,
71
+ state: poolConfigs.map(c => {
72
+ const tin = bigIntify(
73
+ psmInterface.decodeFunctionResult('tin', res[i++].returnData)[0],
74
+ );
75
+ const tout = bigIntify(
76
+ psmInterface.decodeFunctionResult('tout', res[i++].returnData)[0],
77
+ );
78
+ const ilks = vatInterface.decodeFunctionResult(
79
+ 'ilks',
80
+ res[i++].returnData,
81
+ );
82
+ const Art = bigIntify(ilks.Art);
83
+ const line = bigIntify(ilks.line);
84
+ const rate = bigIntify(ilks.rate);
85
+ return {
86
+ tin,
87
+ tout,
88
+ Art,
89
+ line,
90
+ rate,
91
+ };
92
+ }),
93
+ };
82
94
  }
83
95
 
84
96
  export class MakerPsmEventPool extends StatefulEventSubscriber<PoolState> {
@@ -174,15 +186,20 @@ export class MakerPsmEventPool extends StatefulEventSubscriber<PoolState> {
174
186
  * should be generated
175
187
  * @returns state of the event subscriber at blocknumber
176
188
  */
177
- async generateState(blockNumber: number): Promise<Readonly<PoolState>> {
178
- return (
179
- await getOnChainState(
180
- this.dexHelper.multiContract,
181
- [this.poolConfig],
182
- this.vatAddress,
183
- blockNumber,
184
- )
185
- )[0];
189
+ async generateState(
190
+ blockNumber: number | 'latest',
191
+ ): Promise<StateWithBlock<PoolState>> {
192
+ const { blockNumber: _blockNumber, state } = await getOnChainState(
193
+ this.dexHelper.multiContract,
194
+ [this.poolConfig],
195
+ this.vatAddress,
196
+ blockNumber,
197
+ );
198
+
199
+ return {
200
+ blockNumber: _blockNumber,
201
+ state: state[0],
202
+ };
186
203
  }
187
204
  }
188
205
 
@@ -223,18 +240,22 @@ export class MakerPsm extends SimpleExchange implements IDex<MakerPsmData> {
223
240
  );
224
241
  }
225
242
 
226
- async initializePricing(blockNumber: number) {
227
- const poolStates = await getOnChainState(
228
- this.dexHelper.multiContract,
229
- this.poolConfigs,
230
- this.vatAddress,
231
- blockNumber,
232
- );
243
+ async initializePricing(blockNumber: number | 'latest' = 'latest') {
244
+ const { blockNumber: _blockNumber, state: poolStates } =
245
+ await getOnChainState(
246
+ this.dexHelper.multiContract,
247
+ this.poolConfigs,
248
+ this.vatAddress,
249
+ blockNumber,
250
+ );
233
251
  await Promise.all(
234
252
  this.poolConfigs.map(async (p, i) => {
235
253
  const eventPool = this.eventPools[p.gem.address.toLowerCase()];
236
- await eventPool.initialize(blockNumber, {
237
- state: poolStates[i],
254
+ await eventPool.initialize(_blockNumber, {
255
+ stateWithBn: {
256
+ state: poolStates[i],
257
+ blockNumber: _blockNumber,
258
+ },
238
259
  });
239
260
  }),
240
261
  );
@@ -276,9 +297,10 @@ export class MakerPsm extends SimpleExchange implements IDex<MakerPsmData> {
276
297
  ): Promise<PoolState> {
277
298
  const eventState = pool.getState(blockNumber);
278
299
  if (eventState) return eventState;
279
- const onChainState = await pool.generateState(blockNumber);
280
- pool.setState(onChainState, blockNumber);
281
- return onChainState;
300
+ const onChainStateWithBn = await pool.generateState(blockNumber);
301
+ pool.setState(onChainStateWithBn.state, onChainStateWithBn.blockNumber);
302
+
303
+ return onChainStateWithBn.state;
282
304
  }
283
305
 
284
306
  computePrices(
@@ -523,7 +545,7 @@ export class MakerPsm extends SimpleExchange implements IDex<MakerPsmData> {
523
545
  : [];
524
546
  if (!validPoolConfigs.length) return [];
525
547
 
526
- const poolStates = await getOnChainState(
548
+ const { state: poolStates } = await getOnChainState(
527
549
  this.dexHelper.multiContract,
528
550
  validPoolConfigs,
529
551
  this.vatAddress,
@@ -1,24 +1,21 @@
1
- import { AbiCoder, Interface } from '@ethersproject/abi';
2
- import { assert, DeepReadonly } from 'ts-essentials';
1
+ import _ from 'lodash';
2
+ import { Interface } from '@ethersproject/abi';
3
+ import { DeepReadonly } from 'ts-essentials';
3
4
  import { Log, Logger, Address } from '../../types';
4
- import { StatefulEventSubscriber } from '../../stateful-event-subscriber';
5
+ import {
6
+ StatefulEventSubscriber,
7
+ StateWithBlock,
8
+ } from '../../stateful-event-subscriber';
5
9
  import { IDexHelper } from '../../dex-helper/idex-helper';
6
- import { Bin, MaverickV1Data, PoolState } from './types';
10
+ import { Bin, PoolState } from './types';
7
11
  import { Token } from '../../types';
8
-
9
- import { MaverickV1Config } from './config';
10
- import PoolABI from '../../abi/maverick-v1/pool.json';
11
- import PoolInspectorABI from '../../abi/maverick-v1/pool-inspector.json';
12
-
13
- import { Contract } from 'web3-eth-contract';
14
- import { AbiItem } from 'web3-utils';
15
12
  import { MaverickBinMap } from './maverick-math/maverick-bin-map';
16
13
  import { MaverickPoolMath } from './maverick-math/maverick-pool-math';
17
14
  import { MMath } from './maverick-math/maverick-basic-math';
18
-
19
- import * as _ from 'lodash';
20
15
  import { BI_POWS } from '../../bigint-constants';
21
- import { getBigIntPow } from '../../utils';
16
+ import { blockAndTryAggregate, getBigIntPow } from '../../utils';
17
+ import PoolABI from '../../abi/maverick-v1/pool.json';
18
+ import PoolInspectorABI from '../../abi/maverick-v1/pool-inspector.json';
22
19
 
23
20
  (BigInt.prototype as any).toJSON = function () {
24
21
  return this.toString();
@@ -32,8 +29,7 @@ export class MaverickV1EventPool extends StatefulEventSubscriber<PoolState> {
32
29
  logDecoder: (log: Log) => any;
33
30
 
34
31
  addressesSubscribed: string[];
35
- poolContract: Contract;
36
- poolInspectorContract: Contract;
32
+ poolInspectorInterface: Interface;
37
33
  poolMath: MaverickPoolMath;
38
34
 
39
35
  constructor(
@@ -68,15 +64,7 @@ export class MaverickV1EventPool extends StatefulEventSubscriber<PoolState> {
68
64
 
69
65
  this.addressesSubscribed = [address];
70
66
 
71
- this.poolInspectorContract = new this.dexHelper.web3Provider.eth.Contract(
72
- PoolInspectorABI as AbiItem[],
73
- this.inspectorAddress,
74
- );
75
-
76
- this.poolContract = new this.dexHelper.web3Provider.eth.Contract(
77
- PoolABI as AbiItem[],
78
- this.address,
79
- );
67
+ this.poolInspectorInterface = new Interface(PoolInspectorABI);
80
68
 
81
69
  this.poolMath = new MaverickPoolMath(
82
70
  parentName,
@@ -128,16 +116,37 @@ export class MaverickV1EventPool extends StatefulEventSubscriber<PoolState> {
128
116
  * should be generated
129
117
  * @returns state of the event subscriber at blocknumber
130
118
  */
131
- async generateState(blockNumber: number): Promise<Readonly<PoolState>> {
132
- const rawBins = await this.poolInspectorContract.methods['getActiveBins'](
133
- this.address,
134
- 0,
135
- 0,
136
- ).call({}, blockNumber);
137
- const rawState = await this.poolContract.methods['getState']().call(
138
- {},
119
+ async generateState(
120
+ blockNumber: number | 'latest',
121
+ ): Promise<Readonly<StateWithBlock<PoolState>>> {
122
+ const { blockNumber: _blockNumber, results } = await blockAndTryAggregate(
123
+ true,
124
+ this.dexHelper.multiContract,
125
+ [
126
+ {
127
+ target: this.inspectorAddress,
128
+ callData: this.poolInspectorInterface.encodeFunctionData(
129
+ 'getActiveBins',
130
+ [this.address, 0, 0],
131
+ ),
132
+ },
133
+ {
134
+ target: this.address,
135
+ callData: this.poolInterface.encodeFunctionData('getState', []),
136
+ },
137
+ ],
139
138
  blockNumber,
140
139
  );
140
+
141
+ const rawBins = this.poolInspectorInterface.decodeFunctionResult(
142
+ 'getActiveBins',
143
+ results[0].returnData,
144
+ );
145
+ const rawState = this.poolInterface.decodeFunctionResult(
146
+ 'getState',
147
+ results[1].returnData,
148
+ );
149
+
141
150
  let binPositions: { [tick: string]: { [kind: string]: bigint } } = {};
142
151
  let bins: { [id: string]: Bin } = {};
143
152
  let binMap: { [id: string]: bigint } = {};
@@ -164,11 +173,14 @@ export class MaverickV1EventPool extends StatefulEventSubscriber<PoolState> {
164
173
  });
165
174
 
166
175
  return {
167
- activeTick: BigInt(rawState.activeTick),
168
- binCounter: BigInt(rawState.binCounter),
169
- bins: bins,
170
- binPositions: binPositions,
171
- binMap: binMap,
176
+ blockNumber: await this.dexHelper.web3Provider.eth.getBlockNumber(),
177
+ state: {
178
+ activeTick: BigInt(rawState.activeTick),
179
+ binCounter: BigInt(rawState.binCounter),
180
+ bins: bins,
181
+ binPositions: binPositions,
182
+ binMap: binMap,
183
+ },
172
184
  };
173
185
  }
174
186
 
@@ -76,7 +76,7 @@ export class MaverickV1
76
76
  MaverickV1Config[this.dexKey][this.network].routerAddress;
77
77
  }
78
78
 
79
- async setupEventPools(blockNumber: number) {
79
+ async setupEventPools(blockNumber: number | 'latest' = 'latest') {
80
80
  const pools = await this.fetchAllSubgraphPools();
81
81
  await Promise.all(
82
82
  pools.map(async (pool: any) => {
@@ -102,13 +102,14 @@ export class MaverickV1
102
102
  MaverickV1Config[this.dexKey][this.network].poolInspectorAddress,
103
103
  this.logger,
104
104
  );
105
- const onChainState = await eventPool.generateState(blockNumber);
105
+ const { state: generatedState, blockNumber: generatedBlockNumber } =
106
+ await eventPool.generateState(blockNumber);
106
107
  if (blockNumber) {
107
- eventPool.setState(onChainState, blockNumber);
108
+ eventPool.setState(generatedState, generatedBlockNumber);
108
109
  this.dexHelper.blockManager.subscribeToLogs(
109
110
  eventPool,
110
111
  eventPool.addressesSubscribed,
111
- blockNumber,
112
+ generatedBlockNumber,
112
113
  );
113
114
  }
114
115
  this.pools[eventPool.address] = eventPool;
@@ -120,7 +121,7 @@ export class MaverickV1
120
121
  // pricing service. It is intended to setup the integration
121
122
  // for pricing requests. It is optional for a DEX to
122
123
  // implement this function
123
- async initializePricing(blockNumber: number) {
124
+ async initializePricing(blockNumber: number | 'latest' = 'latest') {
124
125
  await this.setupEventPools(blockNumber);
125
126
  }
126
127
 
@@ -224,8 +225,11 @@ export class MaverickV1
224
225
  try {
225
226
  let state = pool.getState(blockNumber);
226
227
  if (state === null) {
227
- state = await pool.generateState(blockNumber);
228
- pool.setState(state, blockNumber);
228
+ const {
229
+ state: generatedState,
230
+ blockNumber: generatedBlockNumber,
231
+ } = await pool.generateState(blockNumber);
232
+ pool.setState(generatedState, generatedBlockNumber);
229
233
  }
230
234
  if (state === null) {
231
235
  this.logger.debug(
@@ -266,7 +270,7 @@ export class MaverickV1
266
270
  let gasCosts: number[] = dataList.map(
267
271
  ([d, t]: [BigInt, number]) => {
268
272
  if (d == 0n) return 0;
269
- // I think it is reasonable estimation assuming "kind" gas cost is almost everytime around 1
273
+ // I think it is reasonable estimation assuming "kind" gas cost is almost every time around 1
270
274
  return (
271
275
  MAV_V1_BASE_GAS_COST +
272
276
  (MAV_V1_TICK_GAS_COST + MAV_V1_KIND_GAS_COST) * t