@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
@@ -3,13 +3,17 @@ import { Interface, JsonFragment } from '@ethersproject/abi';
3
3
  import { DeepReadonly } from 'ts-essentials';
4
4
  import erc20ABI from '../../abi/erc20.json';
5
5
  import { Log, Logger } from '../../types';
6
- import { StatefulEventSubscriber } from '../../stateful-event-subscriber';
6
+ import {
7
+ StatefulEventSubscriber,
8
+ StateWithBlock,
9
+ } from '../../stateful-event-subscriber';
7
10
  import { IDexHelper } from '../../dex-helper/idex-helper';
8
11
  import { EventHandler, NervePoolConfig, PoolState } from './types';
9
12
  import { NerveConfig } from './config';
10
13
  import { BlockHeader } from 'web3-eth';
11
14
  import { bigIntify, typeCastPoolState } from './utils';
12
15
  import { NervePoolMath } from './nerve-math';
16
+ import { blockAndTryAggregate } from '../../utils';
13
17
 
14
18
  export class NerveEventPool extends StatefulEventSubscriber<PoolState> {
15
19
  readonly math: NervePoolMath;
@@ -125,7 +129,7 @@ export class NerveEventPool extends StatefulEventSubscriber<PoolState> {
125
129
 
126
130
  async generateState(
127
131
  blockNumber: number | 'latest' = 'latest',
128
- ): Promise<DeepReadonly<PoolState>> {
132
+ ): Promise<StateWithBlock<PoolState>> {
129
133
  const calldata = _.flattenDeep([
130
134
  {
131
135
  target: this.address,
@@ -147,25 +151,30 @@ export class NerveEventPool extends StatefulEventSubscriber<PoolState> {
147
151
  })),
148
152
  ]);
149
153
 
150
- const data = await this.dexHelper.multiContract.methods
151
- .aggregate(calldata)
152
- .call({}, blockNumber);
154
+ const results = await blockAndTryAggregate(
155
+ true,
156
+ this.dexHelper.multiContract,
157
+ calldata,
158
+ blockNumber,
159
+ );
160
+
161
+ const data = results.results;
153
162
 
154
163
  const [swapStorage, lpToken_supply, paused, balances] = [
155
- this.poolIface.decodeFunctionResult('swapStorage', data.returnData[0]),
164
+ this.poolIface.decodeFunctionResult('swapStorage', data[0].returnData),
156
165
  bigIntify(
157
166
  this.lpTokenIface.decodeFunctionResult(
158
167
  'totalSupply',
159
- data.returnData[1],
168
+ data[1].returnData,
160
169
  )[0]._hex,
161
170
  ),
162
- this.poolIface.decodeFunctionResult('paused', data.returnData[2]),
171
+ this.poolIface.decodeFunctionResult('paused', data[2].returnData),
163
172
  _.flatten(
164
173
  _.range(3, this.numTokens + 3).map(i =>
165
174
  bigIntify(
166
175
  this.poolIface.decodeFunctionResult(
167
176
  'getTokenBalance',
168
- data.returnData[i],
177
+ data[i].returnData,
169
178
  )[0]._hex,
170
179
  ),
171
180
  ),
@@ -173,27 +182,30 @@ export class NerveEventPool extends StatefulEventSubscriber<PoolState> {
173
182
  ];
174
183
 
175
184
  return {
176
- initialA: bigIntify(swapStorage.initialA._hex),
177
- futureA: bigIntify(swapStorage.futureA._hex),
178
- initialATime: bigIntify(swapStorage.initialATime._hex),
179
- futureATime: bigIntify(swapStorage.futureATime._hex),
180
- // If we have swapFee or fee, use these values, otherwise set to zero
181
- swapFee:
182
- ((swapStorage.swapFee || swapStorage.fee) &&
183
- bigIntify((swapStorage.swapFee || swapStorage.fee)._hex)) ||
184
- 0n,
185
- adminFee: bigIntify(swapStorage.adminFee._hex),
186
- defaultDepositFee:
187
- swapStorage.defaultDepositFee &&
188
- bigIntify(swapStorage.defaultDepositFee._hex),
189
- defaultWithdrawFee:
190
- swapStorage.defaultWithdrawFee &&
191
- bigIntify(swapStorage.defaultWithdrawFee._hex),
192
- lpToken_supply,
193
- balances,
194
- tokenPrecisionMultipliers: this.tokenPrecisionMultipliers,
195
- isValid: true,
196
- paused: paused[0],
185
+ blockNumber: results.blockNumber,
186
+ state: {
187
+ initialA: bigIntify(swapStorage.initialA._hex),
188
+ futureA: bigIntify(swapStorage.futureA._hex),
189
+ initialATime: bigIntify(swapStorage.initialATime._hex),
190
+ futureATime: bigIntify(swapStorage.futureATime._hex),
191
+ // If we have swapFee or fee, use these values, otherwise set to zero
192
+ swapFee:
193
+ ((swapStorage.swapFee || swapStorage.fee) &&
194
+ bigIntify((swapStorage.swapFee || swapStorage.fee)._hex)) ||
195
+ 0n,
196
+ adminFee: bigIntify(swapStorage.adminFee._hex),
197
+ defaultDepositFee:
198
+ swapStorage.defaultDepositFee &&
199
+ bigIntify(swapStorage.defaultDepositFee._hex),
200
+ defaultWithdrawFee:
201
+ swapStorage.defaultWithdrawFee &&
202
+ bigIntify(swapStorage.defaultWithdrawFee._hex),
203
+ lpToken_supply,
204
+ balances,
205
+ tokenPrecisionMultipliers: this.tokenPrecisionMultipliers,
206
+ isValid: true,
207
+ paused: paused[0],
208
+ },
197
209
  };
198
210
  }
199
211
 
@@ -70,7 +70,10 @@ export class Nerve
70
70
  return Object.values(this.eventPools);
71
71
  }
72
72
 
73
- async setupEventPool(poolConfig: NervePoolConfig, blockNumber: number) {
73
+ async setupEventPool(
74
+ poolConfig: NervePoolConfig,
75
+ blockNumber: number | 'latest' = 'latest',
76
+ ) {
74
77
  const poolIdentifier = Nerve.getIdentifier(this.dexKey, poolConfig.address);
75
78
 
76
79
  if (!poolConfig.isMetapool) {
@@ -93,7 +96,7 @@ export class Nerve
93
96
  }
94
97
  }
95
98
 
96
- async initializePricing(blockNumber: number) {
99
+ async initializePricing(blockNumber: number | 'latest' = 'latest') {
97
100
  await Promise.all(
98
101
  Object.values(this.poolConfigs).map(
99
102
  async poolConfig => await this.setupEventPool(poolConfig, blockNumber),
@@ -124,9 +127,10 @@ export class Nerve
124
127
  this.logger.info(
125
128
  `State for ${this.dexKey} pool ${eventPool.name} on ${this.network} is stale or invalid on block ${_blockNumber}. Generating new one`,
126
129
  );
127
- const newState = await eventPool.generateState(_blockNumber);
128
- eventPool.setState(newState, _blockNumber);
129
- return { state: newState, pool: eventPool };
130
+ const newStateWithBn = await eventPool.generateState(_blockNumber);
131
+
132
+ eventPool.setState(newStateWithBn.state, newStateWithBn.blockNumber);
133
+ return { state: newStateWithBn.state, pool: eventPool };
130
134
  } else {
131
135
  return { state, pool: eventPool };
132
136
  }
@@ -71,7 +71,9 @@ export class Platypus extends SimpleExchange implements IDex<PlatypusData> {
71
71
  this.logger = dexHelper.getLogger(`${dexKey}-${network}`);
72
72
  }
73
73
 
74
- async generateConfigInfo(blockNumber: number): Promise<PlatypusConfigInfo> {
74
+ async generateConfigInfo(
75
+ blockNumber: number | 'latest',
76
+ ): Promise<PlatypusConfigInfo> {
75
77
  const cfgInfo: PlatypusConfigInfo = {
76
78
  poolAddresses: [],
77
79
  pools: {},
@@ -244,7 +246,7 @@ export class Platypus extends SimpleExchange implements IDex<PlatypusData> {
244
246
  return cfgInfo;
245
247
  }
246
248
 
247
- async init(blockNumber: number) {
249
+ async init(blockNumber: number | 'latest') {
248
250
  if (this.cfgInfo) return;
249
251
  this.cfgInfo = await this.generateConfigInfo(blockNumber);
250
252
  }
@@ -253,7 +255,7 @@ export class Platypus extends SimpleExchange implements IDex<PlatypusData> {
253
255
  // pricing service. It is intended to setup the integration
254
256
  // for pricing requests. It is optional for a DEX to
255
257
  // implement this function
256
- async initializePricing(blockNumber: number) {
258
+ async initializePricing(blockNumber: number | 'latest' = 'latest') {
257
259
  await this.init(blockNumber);
258
260
  if (!this.cfgInfo)
259
261
  throw new Error(
@@ -329,7 +331,7 @@ export class Platypus extends SimpleExchange implements IDex<PlatypusData> {
329
331
  }
330
332
 
331
333
  // Returns list of pool identifiers that can be used
332
- // for a given swap. poolIdentifers must be unique
334
+ // for a given swap. poolIdentifiers must be unique
333
335
  // across DEXes. It is recommended to use
334
336
  // ${dexKey}_${poolAddress} as a poolIdentifier
335
337
  async getPoolIdentifiers(
@@ -385,8 +387,10 @@ export class Platypus extends SimpleExchange implements IDex<PlatypusData> {
385
387
  ) => {
386
388
  let state = pool.getState(blockNumber);
387
389
  if (!state) {
388
- state = await pool.generateState(blockNumber);
389
- pool.setState(state, blockNumber);
390
+ const newState = await pool.generateState(blockNumber);
391
+
392
+ pool.setState(newState.state, newState.blockNumber);
393
+ state = newState.state;
390
394
  }
391
395
  if (state.params.paused) return null;
392
396
  const [unit, ...prices] = pool.computePrices(
@@ -426,7 +430,7 @@ export class Platypus extends SimpleExchange implements IDex<PlatypusData> {
426
430
 
427
431
  // Encode params required by the exchange adapter
428
432
  // Used for multiSwap, buy & megaSwap
429
- // Hint: abiCoder.encodeParameter() couls be useful
433
+ // Hint: abiCoder.encodeParameter() could be useful
430
434
  getAdapterParam(
431
435
  srcToken: string,
432
436
  destToken: string,
@@ -6,7 +6,7 @@ export const MAX_GAS_COST_ESTIMATION = 375 * 1000;
6
6
  export const SUBGRAPH_TIMEOUT = 1000 * 10;
7
7
  export const MAX_POOL_CNT = 1000;
8
8
  export const POOL_CACHE_TTL = 60 * 60; // 1hr
9
- export const RECCURING_POOL_FETCH_INTERVAL_MS = 12 * 3600 * 1000; // 12h (in ms)
9
+ export const RECURRING_POOL_FETCH_INTERVAL_MS = 12 * 3600 * 1000; // 12h (in ms)
10
10
 
11
11
  export const SwaapV1Config: DexConfigMap<DexParams> = {
12
12
  SwaapV1: {
@@ -11,7 +11,10 @@ import {
11
11
  OracleInitialData,
12
12
  ChainLinkData,
13
13
  } from './types';
14
- import { StatefulEventSubscriber } from '../../stateful-event-subscriber';
14
+ import {
15
+ StatefulEventSubscriber,
16
+ StateWithBlock,
17
+ } from '../../stateful-event-subscriber';
15
18
  import { Log, MultiCallInput, MultiCallOutput } from '../../types';
16
19
  import PoolABI from '../../abi/swaap-v1/pool.json';
17
20
  import ProxyABI from '../../abi/chainlink.json';
@@ -533,7 +536,7 @@ export class SwaapV1Pool extends StatefulEventSubscriber<SwaapV1PoolState> {
533
536
  }
534
537
 
535
538
  public async getTokensAndParameters(
536
- blockNumber: number,
539
+ blockNumber: number | 'latest',
537
540
  ): Promise<[Address[], SwaapV1PoolParameters]> {
538
541
  const multiCallInputs: MultiCallInput[] = [
539
542
  {
@@ -886,28 +889,34 @@ export class SwaapV1Pool extends StatefulEventSubscriber<SwaapV1PoolState> {
886
889
  //function should not use any previous states to derive a new state, it should
887
890
  //generate one from scratch.
888
891
  public async generateState(
889
- blockNumber?: number | 'latest',
890
- ): Promise<DeepReadonly<SwaapV1PoolState>> {
891
- if (blockNumber === 'latest' || blockNumber === undefined) {
892
- blockNumber = await this.dexHelper.provider.getBlockNumber();
893
- }
894
-
895
- const [tokens, parameters] = await this.getTokensAndParameters(blockNumber);
892
+ blockNumber: number | 'latest',
893
+ ): Promise<StateWithBlock<SwaapV1PoolState>> {
894
+ const [_blockNumber, tokensAndParameters] = await Promise.all([
895
+ blockNumber === 'latest'
896
+ ? this.dexHelper.provider.getBlockNumber()
897
+ : blockNumber,
898
+ this.getTokensAndParameters(blockNumber),
899
+ ]);
900
+
901
+ const [tokens, parameters] = tokensAndParameters;
896
902
  const [proxies, liquidities] = await this.getProxiesAndLiquidities(
897
903
  tokens,
898
- blockNumber,
904
+ _blockNumber,
899
905
  );
900
906
  const oracles = await this.getOraclesData(
901
907
  tokens,
902
908
  proxies,
903
909
  parameters,
904
- blockNumber,
910
+ _blockNumber,
905
911
  );
906
912
 
907
913
  return {
908
- parameters: parameters,
909
- liquidities: liquidities,
910
- oracles: oracles,
914
+ blockNumber: _blockNumber,
915
+ state: {
916
+ parameters: parameters,
917
+ liquidities: liquidities,
918
+ oracles: oracles,
919
+ },
911
920
  };
912
921
  }
913
922
 
@@ -8,7 +8,6 @@ import {
8
8
  SimpleExchangeParam,
9
9
  PoolLiquidity,
10
10
  Logger,
11
- MultiCallInput,
12
11
  } from '../../types';
13
12
  import { SwapSide, Network, MAX_INT, MAX_UINT } from '../../constants';
14
13
  import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
@@ -21,9 +20,7 @@ import {
21
20
  SwaapV1Data,
22
21
  SwaapV1PoolState,
23
22
  SubgraphPoolBase,
24
- SwaapV1ProxySwapArguments,
25
23
  SwaapV1PoolSwapArguments,
26
- SwaapV1Swap,
27
24
  } from './types';
28
25
  import { SimpleExchange } from '../simple-exchange';
29
26
  import {
@@ -34,7 +31,7 @@ import {
34
31
  MAX_POOL_CNT,
35
32
  POOL_CACHE_TTL,
36
33
  MAX_GAS_COST_ESTIMATION,
37
- RECCURING_POOL_FETCH_INTERVAL_MS,
34
+ RECURRING_POOL_FETCH_INTERVAL_MS,
38
35
  } from './config';
39
36
  import { SwaapV1Pool } from './swaap-v1-pool';
40
37
  import PoolABI from '../../abi/swaap-v1/pool.json';
@@ -74,18 +71,20 @@ export class SwaapV1 extends SimpleExchange implements IDex<SwaapV1Data> {
74
71
  // pricing service. It is intended to setup the integration
75
72
  // for pricing requests. It is optional for a DEX to
76
73
  // implement this function
77
- async initializePricing(blockNumber: number) {
74
+ async initializePricing(blockNumber: number | 'latest' = 'latest') {
78
75
  await this.getPools(blockNumber, true);
79
76
  setInterval(
80
- () => this.getPools(null, false),
81
- RECCURING_POOL_FETCH_INTERVAL_MS,
77
+ () => this.getPools(blockNumber, false),
78
+ RECURRING_POOL_FETCH_INTERVAL_MS,
82
79
  );
83
80
  }
84
81
 
85
- async getPools(_blockNumber: number | null, init: boolean) {
86
- const blockNumber = _blockNumber
87
- ? _blockNumber
88
- : await this.dexHelper.provider.getBlockNumber();
82
+ async getPools(_blockNumber: number | 'latest', init: boolean) {
83
+ const blockNumber =
84
+ _blockNumber === 'latest'
85
+ ? await this.dexHelper.provider.getBlockNumber()
86
+ : _blockNumber;
87
+
89
88
  const allPools = await this.fetchAllSubgraphPools(blockNumber);
90
89
 
91
90
  await SwaapV1.initPools(
@@ -93,12 +92,14 @@ export class SwaapV1 extends SimpleExchange implements IDex<SwaapV1Data> {
93
92
  blockNumber,
94
93
  Object.values(allPools),
95
94
  );
95
+
96
96
  if (!allPools)
97
97
  if (init) {
98
98
  throw new Error(
99
99
  'initializePricing: SwaapV1 cfgInfo still null after init',
100
100
  );
101
101
  }
102
+
102
103
  for (const poolConfig of allPools.values()) {
103
104
  if (
104
105
  init ||
@@ -155,7 +156,7 @@ export class SwaapV1 extends SimpleExchange implements IDex<SwaapV1Data> {
155
156
  }
156
157
 
157
158
  // Returns list of pool identifiers that can be used
158
- // for a given swap. poolIdentifers must be unique
159
+ // for a given swap. poolIdentifiers must be unique
159
160
  // across DEXes. It is recommended to use
160
161
  // ${dexKey}_${poolAddress} as a poolIdentifier
161
162
  async getPoolIdentifiers(
@@ -172,7 +173,7 @@ export class SwaapV1 extends SimpleExchange implements IDex<SwaapV1Data> {
172
173
 
173
174
  // Encode params required by the exchange adapter
174
175
  // Used for multiSwap, buy & megaSwap
175
- // Hint: abiCoder.encodeParameter() couls be useful
176
+ // Hint: abiCoder.encodeParameter() could be useful
176
177
  getAdapterParam(
177
178
  srcToken: string,
178
179
  destToken: string,
@@ -585,7 +586,13 @@ export class SwaapV1 extends SimpleExchange implements IDex<SwaapV1Data> {
585
586
  allowedPools.map(async poolAddress => {
586
587
  let state = this.eventPools[poolAddress].getState(blockNumber);
587
588
  if (!state) {
588
- state = await this.eventPools[poolAddress].generateState(blockNumber);
589
+ const newStateWithBn = await this.eventPools[
590
+ poolAddress
591
+ ].generateState(blockNumber);
592
+ state = newStateWithBn.state;
593
+ blockNumber = newStateWithBn.blockNumber;
594
+
595
+ this.eventPools[poolAddress].setState(state, blockNumber);
589
596
  }
590
597
  let [unit, prices] = this.getPricesPool(
591
598
  from.address,
@@ -400,14 +400,14 @@ export class SynthetixState {
400
400
  await this.multiWrapper.tryAggregate<string | bigint>(
401
401
  true,
402
402
  this._buildInitialStateCallData(),
403
- blockNumber,
403
+ blockNumber === undefined ? 'latest' : blockNumber,
404
404
  )
405
405
  ).map(d => d.returnData) as [
406
406
  Address,
407
407
  Address,
408
408
  bigint,
409
409
  bigint,
410
- ...Address[]
410
+ ...Address[],
411
411
  ];
412
412
 
413
413
  const [
@@ -437,7 +437,7 @@ export class SynthetixState {
437
437
  Address,
438
438
  Address,
439
439
  bigint,
440
- ...string[]
440
+ ...string[],
441
441
  ];
442
442
 
443
443
  _require(
@@ -87,15 +87,16 @@ export class Synthetix extends SimpleExchange implements IDex<SynthetixData> {
87
87
  return this.synthetixState.onchainConfigValues;
88
88
  }
89
89
 
90
- async initializePricing(blockNumber: number) {
90
+ async initializePricing(blockNumber: number | 'latest' = 'latest') {
91
+ const _blockNumber = blockNumber === 'latest' ? undefined : blockNumber;
91
92
  if (this.synthetixState.onchainConfigValuesWithUndefined === undefined) {
92
- await this.synthetixState.updateOnchainConfigValues(blockNumber);
93
+ await this.synthetixState.updateOnchainConfigValues(_blockNumber);
93
94
  }
94
95
  if (!this.statePollingTimer) {
95
- await this.synthetixState.updateOnchainState();
96
+ await this.synthetixState.updateOnchainState(_blockNumber);
96
97
  this.statePollingTimer = setInterval(async () => {
97
98
  try {
98
- await this.synthetixState.updateOnchainState();
99
+ await this.synthetixState.updateOnchainState(_blockNumber);
99
100
  this.logger.info(
100
101
  `${this.dexKey}: onchain state was updated for network=${this.network}`,
101
102
  );
@@ -2,7 +2,10 @@ import { AbiCoder, Interface } from '@ethersproject/abi';
2
2
  import _ from 'lodash';
3
3
  import { AsyncOrSync, DeepReadonly } from 'ts-essentials';
4
4
  import erc20ABI from '../../abi/erc20.json';
5
- import { StatefulEventSubscriber } from '../../stateful-event-subscriber';
5
+ import {
6
+ StateWithBlock,
7
+ StatefulEventSubscriber,
8
+ } from '../../stateful-event-subscriber';
6
9
  import {
7
10
  AdapterExchangeParam,
8
11
  Address,
@@ -43,6 +46,7 @@ import {
43
46
  isETHAddress,
44
47
  prependWithOx,
45
48
  getBigIntPow,
49
+ blockAndTryAggregate,
46
50
  } from '../../utils';
47
51
  import uniswapV2ABI from '../../abi/uniswap-v2/uniswap-v2-pool.json';
48
52
  import uniswapV2factoryABI from '../../abi/uniswap-v2/uniswap-v2-factory.json';
@@ -137,7 +141,7 @@ export class UniswapV2EventPool extends StatefulEventSubscriber<UniswapV2PoolSta
137
141
 
138
142
  async generateState(
139
143
  blockNumber: number | 'latest' = 'latest',
140
- ): Promise<DeepReadonly<UniswapV2PoolState>> {
144
+ ): Promise<StateWithBlock<UniswapV2PoolState>> {
141
145
  let calldata = [
142
146
  {
143
147
  target: this.poolAddress,
@@ -149,22 +153,29 @@ export class UniswapV2EventPool extends StatefulEventSubscriber<UniswapV2PoolSta
149
153
  calldata.push(this.feesMultiCallEntry!);
150
154
  }
151
155
 
152
- const data: { returnData: any[] } =
153
- await this.dexHelper.multiContract.methods
154
- .aggregate(calldata)
155
- .call({}, blockNumber);
156
+ const results = await blockAndTryAggregate(
157
+ true,
158
+ this.dexHelper.multiContract,
159
+ calldata,
160
+ blockNumber,
161
+ );
162
+
163
+ const data = results.results;
156
164
 
157
165
  const decodedData = coder.decode(
158
166
  ['uint112', 'uint112', 'uint32'],
159
- data.returnData[0],
167
+ data[0].returnData,
160
168
  );
161
169
 
162
170
  return {
163
- reserves0: decodedData[0].toString(),
164
- reserves1: decodedData[1].toString(),
165
- feeCode: this.dynamicFees
166
- ? this.feesMultiCallDecoder!(data.returnData[1])
167
- : this.feeCode,
171
+ blockNumber: results.blockNumber,
172
+ state: {
173
+ reserves0: decodedData[0].toString(),
174
+ reserves1: decodedData[1].toString(),
175
+ feeCode: this.dynamicFees
176
+ ? this.feesMultiCallDecoder!(data[1].returnData)
177
+ : this.feeCode,
178
+ },
168
179
  };
169
180
  }
170
181
  }
@@ -274,7 +285,10 @@ export class UniswapV2
274
285
  pair.pool.addressesSubscribed.push(pair.exchange!);
275
286
 
276
287
  await pair.pool.initialize(blockNumber, {
277
- state: { reserves0, reserves1, feeCode },
288
+ stateWithBn: {
289
+ blockNumber: blockNumber,
290
+ state: { reserves0, reserves1, feeCode },
291
+ },
278
292
  });
279
293
  }
280
294
 
@@ -5,6 +5,7 @@ import { ethers } from 'ethers';
5
5
  import { assert, DeepReadonly } from 'ts-essentials';
6
6
  import { Log, Logger, BlockHeader, Address } from '../../types';
7
7
  import {
8
+ StateWithBlock,
8
9
  InitializeStateOptions,
9
10
  StatefulEventSubscriber,
10
11
  } from '../../stateful-event-subscriber';
@@ -115,7 +116,7 @@ export class UniswapV3EventPool extends StatefulEventSubscriber<PoolState> {
115
116
  }
116
117
 
117
118
  async initialize(
118
- blockNumber: number,
119
+ blockNumber: number | 'latest',
119
120
  options?: InitializeStateOptions<PoolState>,
120
121
  ) {
121
122
  await super.initialize(blockNumber, options);
@@ -128,7 +129,9 @@ export class UniswapV3EventPool extends StatefulEventSubscriber<PoolState> {
128
129
  ): Promise<DeepReadonly<PoolState> | null> {
129
130
  const newState = await super.processBlockLogs(state, logs, blockHeader);
130
131
  if (newState && !newState.isValid) {
131
- return await this.generateState(blockHeader.number);
132
+ const newStateWithBn = await this.generateState('latest');
133
+ this.setState(newStateWithBn.state, newStateWithBn.blockNumber);
134
+ return newStateWithBn.state;
132
135
  }
133
136
  return newState;
134
137
  }
@@ -239,19 +242,17 @@ export class UniswapV3EventPool extends StatefulEventSubscriber<PoolState> {
239
242
  return TICK_BITMAP_TO_USE + TICK_BITMAP_BUFFER;
240
243
  }
241
244
 
242
- async generateState(blockNumber: number): Promise<Readonly<PoolState>> {
245
+ async generateState(
246
+ blockNumber: number | 'latest',
247
+ ): Promise<StateWithBlock<PoolState>> {
243
248
  const callData = this._getStateRequestCallData();
244
249
 
245
- const [resBalance0, resBalance1, resState] =
246
- await this.dexHelper.multiWrapper.tryAggregate<
250
+ const { results, blockNumber: _blockNumber } =
251
+ await this.dexHelper.multiWrapper.blockTryAggregateWithoutBatching<
247
252
  bigint | DecodedStateMultiCallResultWithRelativeBitmaps
248
- >(
249
- false,
250
- callData,
251
- blockNumber,
252
- this.dexHelper.multiWrapper.defaultBatchSize,
253
- false,
254
- );
253
+ >(false, callData, blockNumber, false);
254
+
255
+ const [resBalance0, resBalance1, resState] = results;
255
256
 
256
257
  // Quite ugly solution, but this is the one that fits to current flow.
257
258
  // I think UniswapV3 callbacks subscriptions are complexified for no reason.
@@ -288,34 +289,37 @@ export class UniswapV3EventPool extends StatefulEventSubscriber<PoolState> {
288
289
  const requestedRange = this.getBitmapRangeToRequest();
289
290
 
290
291
  return {
291
- pool: _state.pool,
292
- blockTimestamp: bigIntify(_state.blockTimestamp),
293
- slot0: {
294
- sqrtPriceX96: bigIntify(_state.slot0.sqrtPriceX96),
295
- tick: currentTick,
296
- observationIndex: +_state.slot0.observationIndex,
297
- observationCardinality: +_state.slot0.observationCardinality,
298
- observationCardinalityNext: +_state.slot0.observationCardinalityNext,
299
- feeProtocol: bigIntify(_state.slot0.feeProtocol),
300
- },
301
- liquidity: bigIntify(_state.liquidity),
302
- fee: this.feeCode,
303
- tickSpacing,
304
- maxLiquidityPerTick: bigIntify(_state.maxLiquidityPerTick),
305
- tickBitmap,
306
- ticks,
307
- observations,
308
- isValid: true,
309
- startTickBitmap,
310
- lowestKnownTick:
311
- (BigInt.asIntN(24, startTickBitmap - requestedRange) << 8n) *
312
- tickSpacing,
313
- highestKnownTick:
314
- ((BigInt.asIntN(24, startTickBitmap + requestedRange) << 8n) +
315
- BigInt.asIntN(24, 255n)) *
292
+ blockNumber: _blockNumber,
293
+ state: {
294
+ pool: _state.pool,
295
+ blockTimestamp: bigIntify(_state.blockTimestamp),
296
+ slot0: {
297
+ sqrtPriceX96: bigIntify(_state.slot0.sqrtPriceX96),
298
+ tick: currentTick,
299
+ observationIndex: +_state.slot0.observationIndex,
300
+ observationCardinality: +_state.slot0.observationCardinality,
301
+ observationCardinalityNext: +_state.slot0.observationCardinalityNext,
302
+ feeProtocol: bigIntify(_state.slot0.feeProtocol),
303
+ },
304
+ liquidity: bigIntify(_state.liquidity),
305
+ fee: this.feeCode,
316
306
  tickSpacing,
317
- balance0,
318
- balance1,
307
+ maxLiquidityPerTick: bigIntify(_state.maxLiquidityPerTick),
308
+ tickBitmap,
309
+ ticks,
310
+ observations,
311
+ isValid: true,
312
+ startTickBitmap,
313
+ lowestKnownTick:
314
+ (BigInt.asIntN(24, startTickBitmap - requestedRange) << 8n) *
315
+ tickSpacing,
316
+ highestKnownTick:
317
+ ((BigInt.asIntN(24, startTickBitmap + requestedRange) << 8n) +
318
+ BigInt.asIntN(24, 255n)) *
319
+ tickSpacing,
320
+ balance0,
321
+ balance1,
322
+ },
319
323
  };
320
324
  }
321
325