@paraswap/dex-lib 2.42.13 → 2.42.14-algebra-bugfix-missed-created-pools.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 (206) hide show
  1. package/build/abi/algebra/AlgebraPool.abi.json +727 -0
  2. package/build/abi/uniswap-v3/UniswapV3Quoter.abi.json +193 -0
  3. package/build/dex/{baseswap-v3/baseswap-v3-pool.d.ts → algebra/algebra-pool.d.ts} +16 -16
  4. package/build/dex/{baseswap-v3/baseswap-v3-pool.js → algebra/algebra-pool.js} +161 -137
  5. package/build/dex/algebra/algebra-pool.js.map +1 -0
  6. package/build/dex/algebra/algebra.d.ts +1 -0
  7. package/build/dex/algebra/algebra.js +45 -17
  8. package/build/dex/algebra/algebra.js.map +1 -1
  9. package/build/dex/quickswap/camelot-v3.d.ts +6 -0
  10. package/build/dex/quickswap/camelot-v3.js +19 -0
  11. package/build/dex/quickswap/camelot-v3.js.map +1 -0
  12. package/build/dex/quickswap/zyberswap-v3.d.ts +6 -0
  13. package/build/dex/quickswap/zyberswap-v3.js +19 -0
  14. package/build/dex/quickswap/zyberswap-v3.js.map +1 -0
  15. package/package.json +1 -1
  16. package/src/dex/algebra/algebra.ts +58 -33
  17. package/build/abi/kyberswap-elastic/IAntiSnipAttackPositionManager.json +0 -1276
  18. package/build/abi/kyberswap-elastic/IFactory.json +0 -550
  19. package/build/abi/kyberswap-elastic/IPool.json +0 -1042
  20. package/build/abi/kyberswap-elastic/IQuoterV2.json +0 -226
  21. package/build/abi/kyberswap-elastic/IRouter.json +0 -239
  22. package/build/abi/kyberswap-elastic/TicksFeesReader.json +0 -142
  23. package/build/abi/wombat/asset.json +0 -769
  24. package/build/abi/wombat/bmw.json +0 -1247
  25. package/build/abi/wombat/pool.json +0 -1364
  26. package/build/dex/baseswap-v3/baseswap-v3-factory.d.ts +0 -26
  27. package/build/dex/baseswap-v3/baseswap-v3-factory.js +0 -44
  28. package/build/dex/baseswap-v3/baseswap-v3-factory.js.map +0 -1
  29. package/build/dex/baseswap-v3/baseswap-v3-pool.js.map +0 -1
  30. package/build/dex/baseswap-v3/baseswap-v3.d.ts +0 -71
  31. package/build/dex/baseswap-v3/baseswap-v3.js +0 -752
  32. package/build/dex/baseswap-v3/baseswap-v3.js.map +0 -1
  33. package/build/dex/baseswap-v3/config.d.ts +0 -9
  34. package/build/dex/baseswap-v3/config.js +0 -48
  35. package/build/dex/baseswap-v3/config.js.map +0 -1
  36. package/build/dex/baseswap-v3/constants.d.ts +0 -28
  37. package/build/dex/baseswap-v3/constants.js +0 -35
  38. package/build/dex/baseswap-v3/constants.js.map +0 -1
  39. package/build/dex/baseswap-v3/contract-math/BitMath.d.ts +0 -4
  40. package/build/dex/baseswap-v3/contract-math/BitMath.js +0 -93
  41. package/build/dex/baseswap-v3/contract-math/BitMath.js.map +0 -1
  42. package/build/dex/baseswap-v3/contract-math/FixedPoint128.d.ts +0 -3
  43. package/build/dex/baseswap-v3/contract-math/FixedPoint128.js +0 -8
  44. package/build/dex/baseswap-v3/contract-math/FixedPoint128.js.map +0 -1
  45. package/build/dex/baseswap-v3/contract-math/FixedPoint96.d.ts +0 -4
  46. package/build/dex/baseswap-v3/contract-math/FixedPoint96.js +0 -9
  47. package/build/dex/baseswap-v3/contract-math/FixedPoint96.js.map +0 -1
  48. package/build/dex/baseswap-v3/contract-math/FullMath.d.ts +0 -4
  49. package/build/dex/baseswap-v3/contract-math/FullMath.js +0 -19
  50. package/build/dex/baseswap-v3/contract-math/FullMath.js.map +0 -1
  51. package/build/dex/baseswap-v3/contract-math/LiquidityMath.d.ts +0 -3
  52. package/build/dex/baseswap-v3/contract-math/LiquidityMath.js +0 -22
  53. package/build/dex/baseswap-v3/contract-math/LiquidityMath.js.map +0 -1
  54. package/build/dex/baseswap-v3/contract-math/Oracle.d.ts +0 -10
  55. package/build/dex/baseswap-v3/contract-math/Oracle.js +0 -133
  56. package/build/dex/baseswap-v3/contract-math/Oracle.js.map +0 -1
  57. package/build/dex/baseswap-v3/contract-math/SqrtPriceMath.d.ts +0 -10
  58. package/build/dex/baseswap-v3/contract-math/SqrtPriceMath.js +0 -91
  59. package/build/dex/baseswap-v3/contract-math/SqrtPriceMath.js.map +0 -1
  60. package/build/dex/baseswap-v3/contract-math/SwapMath.d.ts +0 -8
  61. package/build/dex/baseswap-v3/contract-math/SwapMath.js +0 -70
  62. package/build/dex/baseswap-v3/contract-math/SwapMath.js.map +0 -1
  63. package/build/dex/baseswap-v3/contract-math/Tick.d.ts +0 -7
  64. package/build/dex/baseswap-v3/contract-math/Tick.js +0 -45
  65. package/build/dex/baseswap-v3/contract-math/Tick.js.map +0 -1
  66. package/build/dex/baseswap-v3/contract-math/TickBitMap.d.ts +0 -7
  67. package/build/dex/baseswap-v3/contract-math/TickBitMap.js +0 -84
  68. package/build/dex/baseswap-v3/contract-math/TickBitMap.js.map +0 -1
  69. package/build/dex/baseswap-v3/contract-math/TickMath.d.ts +0 -8
  70. package/build/dex/baseswap-v3/contract-math/TickMath.js +0 -162
  71. package/build/dex/baseswap-v3/contract-math/TickMath.js.map +0 -1
  72. package/build/dex/baseswap-v3/contract-math/UnsafeMath.d.ts +0 -3
  73. package/build/dex/baseswap-v3/contract-math/UnsafeMath.js +0 -10
  74. package/build/dex/baseswap-v3/contract-math/UnsafeMath.js.map +0 -1
  75. package/build/dex/baseswap-v3/contract-math/uniswap-v3-math.d.ts +0 -37
  76. package/build/dex/baseswap-v3/contract-math/uniswap-v3-math.js +0 -393
  77. package/build/dex/baseswap-v3/contract-math/uniswap-v3-math.js.map +0 -1
  78. package/build/dex/baseswap-v3/contract-math/utils.d.ts +0 -7
  79. package/build/dex/baseswap-v3/contract-math/utils.js +0 -42
  80. package/build/dex/baseswap-v3/contract-math/utils.js.map +0 -1
  81. package/build/dex/baseswap-v3/types.d.ts +0 -164
  82. package/build/dex/baseswap-v3/types.js +0 -9
  83. package/build/dex/baseswap-v3/types.js.map +0 -1
  84. package/build/dex/baseswap-v3/utils.d.ts +0 -6
  85. package/build/dex/baseswap-v3/utils.js +0 -65
  86. package/build/dex/baseswap-v3/utils.js.map +0 -1
  87. package/build/dex/hello/config.d.ts +0 -4
  88. package/build/dex/hello/config.js +0 -15
  89. package/build/dex/hello/config.js.map +0 -1
  90. package/build/dex/hello/hello-pool.d.ts +0 -39
  91. package/build/dex/hello/hello-pool.js +0 -64
  92. package/build/dex/hello/hello-pool.js.map +0 -1
  93. package/build/dex/hello/hello.d.ts +0 -37
  94. package/build/dex/hello/hello.js +0 -126
  95. package/build/dex/hello/hello.js.map +0 -1
  96. package/build/dex/hello/types.d.ts +0 -6
  97. package/build/dex/hello/types.js +0 -3
  98. package/build/dex/hello/types.js.map +0 -1
  99. package/build/dex/kyberswap-elastic/config.d.ts +0 -4
  100. package/build/dex/kyberswap-elastic/config.js +0 -80
  101. package/build/dex/kyberswap-elastic/config.js.map +0 -1
  102. package/build/dex/kyberswap-elastic/constants.d.ts +0 -17
  103. package/build/dex/kyberswap-elastic/constants.js +0 -42
  104. package/build/dex/kyberswap-elastic/constants.js.map +0 -1
  105. package/build/dex/kyberswap-elastic/contract-math/FixedPoint96.d.ts +0 -4
  106. package/build/dex/kyberswap-elastic/contract-math/FixedPoint96.js +0 -9
  107. package/build/dex/kyberswap-elastic/contract-math/FixedPoint96.js.map +0 -1
  108. package/build/dex/kyberswap-elastic/contract-math/FullMath.d.ts +0 -5
  109. package/build/dex/kyberswap-elastic/contract-math/FullMath.js +0 -22
  110. package/build/dex/kyberswap-elastic/contract-math/FullMath.js.map +0 -1
  111. package/build/dex/kyberswap-elastic/contract-math/LiqDeltaMath.d.ts +0 -3
  112. package/build/dex/kyberswap-elastic/contract-math/LiqDeltaMath.js +0 -12
  113. package/build/dex/kyberswap-elastic/contract-math/LiqDeltaMath.js.map +0 -1
  114. package/build/dex/kyberswap-elastic/contract-math/LiquidityMath.d.ts +0 -3
  115. package/build/dex/kyberswap-elastic/contract-math/LiquidityMath.js +0 -22
  116. package/build/dex/kyberswap-elastic/contract-math/LiquidityMath.js.map +0 -1
  117. package/build/dex/kyberswap-elastic/contract-math/QtyDeltaMath.d.ts +0 -5
  118. package/build/dex/kyberswap-elastic/contract-math/QtyDeltaMath.js +0 -25
  119. package/build/dex/kyberswap-elastic/contract-math/QtyDeltaMath.js.map +0 -1
  120. package/build/dex/kyberswap-elastic/contract-math/QuadMath.d.ts +0 -4
  121. package/build/dex/kyberswap-elastic/contract-math/QuadMath.js +0 -27
  122. package/build/dex/kyberswap-elastic/contract-math/QuadMath.js.map +0 -1
  123. package/build/dex/kyberswap-elastic/contract-math/ReinvestmentMath.d.ts +0 -3
  124. package/build/dex/kyberswap-elastic/contract-math/ReinvestmentMath.js +0 -14
  125. package/build/dex/kyberswap-elastic/contract-math/ReinvestmentMath.js.map +0 -1
  126. package/build/dex/kyberswap-elastic/contract-math/SafeCast.d.ts +0 -8
  127. package/build/dex/kyberswap-elastic/contract-math/SafeCast.js +0 -30
  128. package/build/dex/kyberswap-elastic/contract-math/SafeCast.js.map +0 -1
  129. package/build/dex/kyberswap-elastic/contract-math/SqrtPriceMath.d.ts +0 -10
  130. package/build/dex/kyberswap-elastic/contract-math/SqrtPriceMath.js +0 -91
  131. package/build/dex/kyberswap-elastic/contract-math/SqrtPriceMath.js.map +0 -1
  132. package/build/dex/kyberswap-elastic/contract-math/SwapMath.d.ts +0 -13
  133. package/build/dex/kyberswap-elastic/contract-math/SwapMath.js +0 -188
  134. package/build/dex/kyberswap-elastic/contract-math/SwapMath.js.map +0 -1
  135. package/build/dex/kyberswap-elastic/contract-math/TickLinkedList.d.ts +0 -6
  136. package/build/dex/kyberswap-elastic/contract-math/TickLinkedList.js +0 -41
  137. package/build/dex/kyberswap-elastic/contract-math/TickLinkedList.js.map +0 -1
  138. package/build/dex/kyberswap-elastic/contract-math/TickMath.d.ts +0 -9
  139. package/build/dex/kyberswap-elastic/contract-math/TickMath.js +0 -165
  140. package/build/dex/kyberswap-elastic/contract-math/TickMath.js.map +0 -1
  141. package/build/dex/kyberswap-elastic/contract-math/UnsafeMath.d.ts +0 -3
  142. package/build/dex/kyberswap-elastic/contract-math/UnsafeMath.js +0 -10
  143. package/build/dex/kyberswap-elastic/contract-math/UnsafeMath.js.map +0 -1
  144. package/build/dex/kyberswap-elastic/contract-math/kyberswap-elastic-math.d.ts +0 -28
  145. package/build/dex/kyberswap-elastic/contract-math/kyberswap-elastic-math.js +0 -502
  146. package/build/dex/kyberswap-elastic/contract-math/kyberswap-elastic-math.js.map +0 -1
  147. package/build/dex/kyberswap-elastic/contract-math/utils.d.ts +0 -3
  148. package/build/dex/kyberswap-elastic/contract-math/utils.js +0 -16
  149. package/build/dex/kyberswap-elastic/contract-math/utils.js.map +0 -1
  150. package/build/dex/kyberswap-elastic/errors.d.ts +0 -2
  151. package/build/dex/kyberswap-elastic/errors.js +0 -6
  152. package/build/dex/kyberswap-elastic/errors.js.map +0 -1
  153. package/build/dex/kyberswap-elastic/kyberswap-elastic-pool.d.ts +0 -80
  154. package/build/dex/kyberswap-elastic/kyberswap-elastic-pool.js +0 -458
  155. package/build/dex/kyberswap-elastic/kyberswap-elastic-pool.js.map +0 -1
  156. package/build/dex/kyberswap-elastic/kyberswap-elastic.d.ts +0 -53
  157. package/build/dex/kyberswap-elastic/kyberswap-elastic.js +0 -610
  158. package/build/dex/kyberswap-elastic/kyberswap-elastic.js.map +0 -1
  159. package/build/dex/kyberswap-elastic/types.d.ts +0 -134
  160. package/build/dex/kyberswap-elastic/types.js +0 -9
  161. package/build/dex/kyberswap-elastic/types.js.map +0 -1
  162. package/build/dex/kyberswap-elastic/utils/decoders.d.ts +0 -9
  163. package/build/dex/kyberswap-elastic/utils/decoders.js +0 -78
  164. package/build/dex/kyberswap-elastic/utils/decoders.js.map +0 -1
  165. package/build/dex/morphex/config.d.ts +0 -11
  166. package/build/dex/morphex/config.js +0 -43
  167. package/build/dex/morphex/config.js.map +0 -1
  168. package/build/dex/morphex/morphex.d.ts +0 -25
  169. package/build/dex/morphex/morphex.js +0 -19
  170. package/build/dex/morphex/morphex.js.map +0 -1
  171. package/build/dex/my-test-dex/config.d.ts +0 -4
  172. package/build/dex/my-test-dex/config.js +0 -15
  173. package/build/dex/my-test-dex/config.js.map +0 -1
  174. package/build/dex/my-test-dex/my-test-dex-pool.d.ts +0 -39
  175. package/build/dex/my-test-dex/my-test-dex-pool.js +0 -64
  176. package/build/dex/my-test-dex/my-test-dex-pool.js.map +0 -1
  177. package/build/dex/my-test-dex/my-test-dex.d.ts +0 -37
  178. package/build/dex/my-test-dex/my-test-dex.js +0 -126
  179. package/build/dex/my-test-dex/my-test-dex.js.map +0 -1
  180. package/build/dex/my-test-dex/types.d.ts +0 -6
  181. package/build/dex/my-test-dex/types.js +0 -3
  182. package/build/dex/my-test-dex/types.js.map +0 -1
  183. package/build/dex/solidly/forks-override/fvm.d.ts +0 -20
  184. package/build/dex/solidly/forks-override/fvm.js +0 -42
  185. package/build/dex/solidly/forks-override/fvm.js.map +0 -1
  186. package/build/dex/wombat/config.d.ts +0 -5
  187. package/build/dex/wombat/config.js +0 -50
  188. package/build/dex/wombat/config.js.map +0 -1
  189. package/build/dex/wombat/types.d.ts +0 -33
  190. package/build/dex/wombat/types.js +0 -3
  191. package/build/dex/wombat/types.js.map +0 -1
  192. package/build/dex/wombat/utils.d.ts +0 -9
  193. package/build/dex/wombat/utils.js +0 -66
  194. package/build/dex/wombat/utils.js.map +0 -1
  195. package/build/dex/wombat/wombat-bmw.d.ts +0 -40
  196. package/build/dex/wombat/wombat-bmw.js +0 -145
  197. package/build/dex/wombat/wombat-bmw.js.map +0 -1
  198. package/build/dex/wombat/wombat-pool.d.ts +0 -22
  199. package/build/dex/wombat/wombat-pool.js +0 -161
  200. package/build/dex/wombat/wombat-pool.js.map +0 -1
  201. package/build/dex/wombat/wombat-quoter.d.ts +0 -16
  202. package/build/dex/wombat/wombat-quoter.js +0 -176
  203. package/build/dex/wombat/wombat-quoter.js.map +0 -1
  204. package/build/dex/wombat/wombat.d.ts +0 -55
  205. package/build/dex/wombat/wombat.js +0 -292
  206. package/build/dex/wombat/wombat.js.map +0 -1
@@ -3,55 +3,50 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.BaseswapV3EventPool = void 0;
6
+ exports.AlgebraEventPool = void 0;
7
7
  const lodash_1 = __importDefault(require("lodash"));
8
8
  const abi_1 = require("@ethersproject/abi");
9
- const ethers_1 = require("ethers");
10
9
  const ts_essentials_1 = require("ts-essentials");
11
- const stateful_event_subscriber_1 = require("../../stateful-event-subscriber");
12
- const UniswapV3Pool_abi_json_1 = __importDefault(require("../../abi/uniswap-v3/UniswapV3Pool.abi.json"));
13
10
  const utils_1 = require("../../utils");
14
- const uniswap_v3_math_1 = require("./contract-math/uniswap-v3-math");
15
- const constants_1 = require("./constants");
16
- const TickBitMap_1 = require("./contract-math/TickBitMap");
11
+ const stateful_event_subscriber_1 = require("../../stateful-event-subscriber");
12
+ const ethers_1 = require("ethers");
13
+ const AlgebraPool_abi_json_1 = __importDefault(require("../../abi/algebra/AlgebraPool.abi.json"));
14
+ const constants_1 = require("../uniswap-v3/constants");
17
15
  const decoders_1 = require("../../lib/decoders");
18
16
  const utils_2 = require("./utils");
19
- const utils_3 = require("./contract-math/utils");
20
- class BaseswapV3EventPool extends stateful_event_subscriber_1.StatefulEventSubscriber {
21
- constructor(dexHelper, parentName, stateMultiContract, decodeStateMultiCallResultWithRelativeBitmaps, erc20Interface, factoryAddress, feeCode, token0, token1, logger, mapKey = '', poolInitCodeHash) {
22
- super(parentName, `${token0}_${token1}_${feeCode}`, dexHelper, logger, true, mapKey);
17
+ const AlgebraMath_1 = require("./lib/AlgebraMath");
18
+ const TickBitMap_1 = require("../uniswap-v3/contract-math/TickBitMap");
19
+ const utils_3 = require("../uniswap-v3/contract-math/utils");
20
+ const Constants_1 = require("./lib/Constants");
21
+ const constants_2 = require("../../constants");
22
+ class AlgebraEventPool extends stateful_event_subscriber_1.StatefulEventSubscriber {
23
+ constructor(dexHelper, parentName, stateMultiContract, erc20Interface, factoryAddress, token0, token1, logger, mapKey = '', poolInitCodeHash, poolDeployer) {
24
+ super(parentName, `${token0}_${token1}`, dexHelper, logger, true, mapKey);
23
25
  this.dexHelper = dexHelper;
24
26
  this.stateMultiContract = stateMultiContract;
25
- this.decodeStateMultiCallResultWithRelativeBitmaps = decodeStateMultiCallResultWithRelativeBitmaps;
26
27
  this.erc20Interface = erc20Interface;
27
28
  this.factoryAddress = factoryAddress;
28
- this.feeCode = feeCode;
29
29
  this.poolInitCodeHash = poolInitCodeHash;
30
+ this.poolDeployer = poolDeployer;
30
31
  this.handlers = {};
31
- this.poolIface = new abi_1.Interface(UniswapV3Pool_abi_json_1.default);
32
+ this.poolIface = new abi_1.Interface(AlgebraPool_abi_json_1.default);
32
33
  this.initFailed = false;
33
34
  this.initRetryAttemptCount = 0;
34
- this.feeCodeAsString = feeCode.toString();
35
35
  this.token0 = token0.toLowerCase();
36
36
  this.token1 = token1.toLowerCase();
37
37
  this.logDecoder = (log) => this.poolIface.parseLog(log);
38
38
  this.addressesSubscribed = new Array(1);
39
- // Add handlers
39
+ this.handlers['Fee'] = this.handleNewFee.bind(this);
40
40
  this.handlers['Swap'] = this.handleSwapEvent.bind(this);
41
- this.handlers['Burn'] = this.handleBurnEvent.bind(this);
42
41
  this.handlers['Mint'] = this.handleMintEvent.bind(this);
43
- this.handlers['SetFeeProtocol'] = this.handleSetFeeProtocolEvent.bind(this);
44
- this.handlers['IncreaseObservationCardinalityNext'] =
45
- this.handleIncreaseObservationCardinalityNextEvent.bind(this);
46
- // Wen need them to keep balance of the pool up to date
47
- this.handlers['Collect'] = this.handleCollectEvent.bind(this);
48
- // Almost the same as Collect, but for pool owners
49
- this.handlers['CollectProtocol'] = this.handleCollectEvent.bind(this);
42
+ this.handlers['Burn'] = this.handleBurnEvent.bind(this);
50
43
  this.handlers['Flash'] = this.handleFlashEvent.bind(this);
44
+ this.handlers['Collect'] = this.handleCollectEvent.bind(this);
45
+ this.handlers['CommunityFee'] = this.handleCommunityFee.bind(this);
51
46
  }
52
47
  get poolAddress() {
53
48
  if (this._poolAddress === undefined) {
54
- this._poolAddress = this._computePoolAddress(this.token0, this.token1, this.feeCode);
49
+ this._poolAddress = this._computePoolAddress(this.token0, this.token1);
55
50
  }
56
51
  return this._poolAddress;
57
52
  }
@@ -71,19 +66,14 @@ class BaseswapV3EventPool extends stateful_event_subscriber_1.StatefulEventSubsc
71
66
  processLog(state, log, blockHeader) {
72
67
  try {
73
68
  const event = this.logDecoder(log);
74
- const uniswapV3EventLoggingSampleRate = this.dexHelper.config.data.uniswapV3EventLoggingSampleRate;
75
- if (!this.dexHelper.config.isSlave &&
76
- uniswapV3EventLoggingSampleRate &&
77
- (0, utils_1.isSampled)(uniswapV3EventLoggingSampleRate)) {
78
- this.logger.info(`event=${event.name} - block=${blockHeader.number}. Log sampled at rate ${uniswapV3EventLoggingSampleRate * 100}%`);
79
- }
80
69
  if (event.name in this.handlers) {
81
70
  // Because we have observations in array which is mutable by nature, there is a
82
71
  // ts compile error: https://stackoverflow.com/questions/53412934/disable-allowing-assigning-readonly-types-to-non-readonly-types
83
72
  // And there is no good workaround, so turn off the type checker for this line
84
73
  const _state = lodash_1.default.cloneDeep(state);
85
74
  try {
86
- return this.handlers[event.name](event, _state, log, blockHeader);
75
+ const newState = this.handlers[event.name](event, _state, log, blockHeader);
76
+ return newState;
87
77
  }
88
78
  catch (e) {
89
79
  if (e instanceof Error &&
@@ -94,7 +84,7 @@ class BaseswapV3EventPool extends stateful_event_subscriber_1.StatefulEventSubsc
94
84
  this.logger.error(`${this.parentName}: Pool ${this.poolAddress}, ` +
95
85
  `network=${this.dexHelper.config.data.network}: Unexpected ` +
96
86
  `error while handling event on blockNumber=${blockHeader.number}, ` +
97
- `blockHash=${blockHeader.hash} and parentHash=${blockHeader.parentHash} for UniswapV3, ${JSON.stringify(event)}`, e);
87
+ `blockHash=${blockHeader.hash} and parentHash=${blockHeader.parentHash} for QuickSwapV3, ${JSON.stringify(event)}`, e);
98
88
  }
99
89
  _state.isValid = false;
100
90
  return _state;
@@ -106,99 +96,135 @@ class BaseswapV3EventPool extends stateful_event_subscriber_1.StatefulEventSubsc
106
96
  }
107
97
  return null; // ignore unrecognized event
108
98
  }
109
- _getStateRequestCallData() {
110
- if (!this._stateRequestCallData) {
111
- const callData = [
112
- {
113
- target: this.token0,
114
- callData: this.erc20Interface.encodeFunctionData('balanceOf', [
115
- this.poolAddress,
116
- ]),
117
- decodeFunction: decoders_1.uint256ToBigInt,
118
- },
119
- {
120
- target: this.token1,
121
- callData: this.erc20Interface.encodeFunctionData('balanceOf', [
122
- this.poolAddress,
123
- ]),
124
- decodeFunction: decoders_1.uint256ToBigInt,
125
- },
126
- {
127
- target: this.stateMultiContract.options.address,
128
- callData: this.stateMultiContract.methods
129
- .getFullStateWithRelativeBitmaps(this.factoryAddress, this.token0, this.token1, this.feeCode, this.getBitmapRangeToRequest(), this.getBitmapRangeToRequest())
130
- .encodeABI(),
131
- decodeFunction: this.decodeStateMultiCallResultWithRelativeBitmaps !== undefined
132
- ? this.decodeStateMultiCallResultWithRelativeBitmaps
133
- : utils_2.decodeStateMultiCallResultWithRelativeBitmaps,
134
- },
135
- ];
136
- this._stateRequestCallData = callData;
137
- }
138
- return this._stateRequestCallData;
139
- }
140
99
  getBitmapRangeToRequest() {
141
100
  return constants_1.TICK_BITMAP_TO_USE + constants_1.TICK_BITMAP_BUFFER;
142
101
  }
143
- async generateState(blockNumber) {
144
- const callData = this._getStateRequestCallData();
102
+ async _fetchPoolStateSingleStep(blockNumber) {
103
+ const callData = [
104
+ {
105
+ target: this.token0,
106
+ callData: this.erc20Interface.encodeFunctionData('balanceOf', [
107
+ this.poolAddress,
108
+ ]),
109
+ decodeFunction: decoders_1.uint256ToBigInt,
110
+ },
111
+ {
112
+ target: this.token1,
113
+ callData: this.erc20Interface.encodeFunctionData('balanceOf', [
114
+ this.poolAddress,
115
+ ]),
116
+ decodeFunction: decoders_1.uint256ToBigInt,
117
+ },
118
+ {
119
+ target: this.stateMultiContract.options.address,
120
+ callData: this.stateMultiContract.methods
121
+ .getFullStateWithRelativeBitmaps(this.factoryAddress, this.token0, this.token1, this.getBitmapRangeToRequest(), this.getBitmapRangeToRequest())
122
+ .encodeABI(),
123
+ decodeFunction: utils_2.decodeStateMultiCallResultWithRelativeBitmaps,
124
+ },
125
+ ];
145
126
  const [resBalance0, resBalance1, resState] = await this.dexHelper.multiWrapper.tryAggregate(false, callData, blockNumber, this.dexHelper.multiWrapper.defaultBatchSize, false);
146
- // Quite ugly solution, but this is the one that fits to current flow.
147
- // I think UniswapV3 callbacks subscriptions are complexified for no reason.
148
- // Need to be revisited later
149
127
  (0, ts_essentials_1.assert)(resState.success, 'Pool does not exist');
150
128
  const [balance0, balance1, _state] = [
151
129
  resBalance0.returnData,
152
130
  resBalance1.returnData,
153
131
  resState.returnData,
154
132
  ];
133
+ return [balance0, balance1, _state];
134
+ }
135
+ async _fetchPoolStateMultiStep(blockNumber) {
136
+ const balancesAndGlobalStateCalldata = [
137
+ {
138
+ target: this.token0,
139
+ callData: this.erc20Interface.encodeFunctionData('balanceOf', [
140
+ this.poolAddress,
141
+ ]),
142
+ decodeFunction: decoders_1.uint256ToBigInt,
143
+ },
144
+ {
145
+ target: this.token1,
146
+ callData: this.erc20Interface.encodeFunctionData('balanceOf', [
147
+ this.poolAddress,
148
+ ]),
149
+ decodeFunction: decoders_1.uint256ToBigInt,
150
+ },
151
+ {
152
+ target: this.stateMultiContract.options.address,
153
+ callData: this.stateMultiContract.methods
154
+ .getFullStateWithoutTicks(this.factoryAddress, this.token0, this.token1, 0, 0)
155
+ .encodeABI(),
156
+ decodeFunction: utils_2.decodeStateMultiCallResultWithRelativeBitmaps,
157
+ },
158
+ ];
159
+ const [resBalance0, resBalance1, stateWithoutTicksAndTickBitmap] = await this.dexHelper.multiWrapper.tryAggregate(false, balancesAndGlobalStateCalldata, blockNumber, this.dexHelper.multiWrapper.defaultBatchSize, false);
160
+ (0, ts_essentials_1.assert)(stateWithoutTicksAndTickBitmap.success, 'Pool does not exist');
161
+ const [balance0, balance1, _stateWithoutTicksAndTickBitmap] = [
162
+ resBalance0.returnData,
163
+ resBalance1.returnData,
164
+ stateWithoutTicksAndTickBitmap.returnData,
165
+ ];
166
+ const { globalState: { tick }, } = _stateWithoutTicksAndTickBitmap;
167
+ const currentBitmapIndex = (0, utils_1.int16)((BigInt(tick) / Constants_1.Constants.TICK_SPACING) >> 8n);
168
+ const buffer = this.getBitmapRangeToRequest();
169
+ const startBitMapIndex = currentBitmapIndex - buffer;
170
+ const endBitMapIndex = currentBitmapIndex + buffer;
171
+ const allBitMapIndices = Array.from({ length: 2 * Number(buffer) + 1 }, () => startBitMapIndex + endBitMapIndex);
172
+ const ticksAndBitMaps = await Promise.all(allBitMapIndices.map(relativeTick => {
173
+ return this.stateMultiContract.methods
174
+ .getAdditionalBitmapWithTicks(this.factoryAddress, this.token0, this.token1, relativeTick, relativeTick)
175
+ .call();
176
+ }));
177
+ const _state = {
178
+ ..._stateWithoutTicksAndTickBitmap,
179
+ tickBitmap: ticksAndBitMaps.flatMap(v => v[0]),
180
+ ticks: ticksAndBitMaps.flatMap(v => v[1]),
181
+ };
182
+ return [balance0, balance1, _state];
183
+ }
184
+ async _fetchInitStateMultiStrategies(blockNumber) {
185
+ try {
186
+ return await this._fetchPoolStateSingleStep(blockNumber);
187
+ }
188
+ catch (e) {
189
+ if (e instanceof Error && e.message.includes('Pool does not exist'))
190
+ throw e;
191
+ if (this.dexHelper.config.data.network != constants_2.Network.ZKEVM)
192
+ throw e;
193
+ return this._fetchPoolStateMultiStep(blockNumber);
194
+ }
195
+ }
196
+ async generateState(blockNumber) {
197
+ const [balance0, balance1, _state] = await this._fetchInitStateMultiStrategies(blockNumber);
155
198
  const tickBitmap = {};
156
199
  const ticks = {};
157
200
  (0, utils_3._reduceTickBitmap)(tickBitmap, _state.tickBitmap);
158
201
  (0, utils_3._reduceTicks)(ticks, _state.ticks);
159
- const observations = {
160
- [_state.slot0.observationIndex]: {
161
- blockTimestamp: (0, utils_1.bigIntify)(_state.observation.blockTimestamp),
162
- tickCumulative: (0, utils_1.bigIntify)(_state.observation.tickCumulative),
163
- secondsPerLiquidityCumulativeX128: (0, utils_1.bigIntify)(_state.observation.secondsPerLiquidityCumulativeX128),
164
- initialized: _state.observation.initialized,
165
- },
202
+ const globalState = {
203
+ communityFeeToken0: (0, utils_1.bigIntify)(_state.globalState.communityFeeToken0),
204
+ communityFeeToken1: (0, utils_1.bigIntify)(_state.globalState.communityFeeToken1),
205
+ fee: (0, utils_1.bigIntify)(_state.globalState.fee),
206
+ price: (0, utils_1.bigIntify)(_state.globalState.price),
207
+ tick: (0, utils_1.bigIntify)(_state.globalState.tick),
166
208
  };
167
- const currentTick = (0, utils_1.bigIntify)(_state.slot0.tick);
168
- const tickSpacing = (0, utils_1.bigIntify)(_state.tickSpacing);
169
- const startTickBitmap = TickBitMap_1.TickBitMap.position(currentTick / tickSpacing)[0];
170
- const requestedRange = this.getBitmapRangeToRequest();
209
+ const currentTick = globalState.tick;
210
+ const startTickBitmap = TickBitMap_1.TickBitMap.position(BigInt(currentTick) / Constants_1.Constants.TICK_SPACING)[0];
171
211
  return {
172
212
  pool: _state.pool,
173
213
  blockTimestamp: (0, utils_1.bigIntify)(_state.blockTimestamp),
174
- slot0: {
175
- sqrtPriceX96: (0, utils_1.bigIntify)(_state.slot0.sqrtPriceX96),
176
- tick: currentTick,
177
- observationIndex: +_state.slot0.observationIndex,
178
- observationCardinality: +_state.slot0.observationCardinality,
179
- observationCardinalityNext: +_state.slot0.observationCardinalityNext,
180
- feeProtocol: (0, utils_1.bigIntify)(_state.slot0.feeProtocol),
181
- },
214
+ globalState,
182
215
  liquidity: (0, utils_1.bigIntify)(_state.liquidity),
183
- fee: this.feeCode,
184
- tickSpacing,
185
- maxLiquidityPerTick: (0, utils_1.bigIntify)(_state.maxLiquidityPerTick),
216
+ tickSpacing: Constants_1.Constants.TICK_SPACING,
217
+ maxLiquidityPerTick: Constants_1.Constants.MAX_LIQUIDITY_PER_TICK,
186
218
  tickBitmap,
187
219
  ticks,
188
- observations,
189
- isValid: true,
190
220
  startTickBitmap,
191
- lowestKnownTick: (BigInt.asIntN(24, startTickBitmap - requestedRange) << 8n) *
192
- tickSpacing,
193
- highestKnownTick: ((BigInt.asIntN(24, startTickBitmap + requestedRange) << 8n) +
194
- BigInt.asIntN(24, 255n)) *
195
- tickSpacing,
221
+ isValid: true,
196
222
  balance0,
197
223
  balance1,
198
224
  };
199
225
  }
200
226
  handleSwapEvent(event, pool, log, blockHeader) {
201
- const newSqrtPriceX96 = (0, utils_1.bigIntify)(event.args.sqrtPriceX96);
227
+ const newSqrtPriceX96 = (0, utils_1.bigIntify)(event.args.price);
202
228
  const amount0 = (0, utils_1.bigIntify)(event.args.amount0);
203
229
  const amount1 = (0, utils_1.bigIntify)(event.args.amount1);
204
230
  const newTick = (0, utils_1.bigIntify)(event.args.tick);
@@ -212,7 +238,7 @@ class BaseswapV3EventPool extends stateful_event_subscriber_1.StatefulEventSubsc
212
238
  }
213
239
  else {
214
240
  const zeroForOne = amount0 > 0n;
215
- uniswap_v3_math_1.uniswapV3Math.swapFromEvent(pool, newSqrtPriceX96, newTick, newLiquidity, zeroForOne);
241
+ const [, , , , , communityFee] = AlgebraMath_1.AlgebraMath._calculateSwapAndLock(pool, zeroForOne, newSqrtPriceX96, newTick, newLiquidity);
216
242
  if (zeroForOne) {
217
243
  if (amount1 < 0n) {
218
244
  pool.balance1 -= BigInt.asUintN(256, -amount1);
@@ -235,47 +261,42 @@ class BaseswapV3EventPool extends stateful_event_subscriber_1.StatefulEventSubsc
235
261
  }
236
262
  pool.balance1 += BigInt.asUintN(256, amount1);
237
263
  }
264
+ if (communityFee > 0n) {
265
+ // _payCommunityFee
266
+ if (zeroForOne) {
267
+ pool.balance0 -= communityFee;
268
+ }
269
+ else {
270
+ pool.balance1 -= communityFee;
271
+ }
272
+ }
238
273
  return pool;
239
274
  }
240
275
  }
241
- handleBurnEvent(event, pool, log, blockHeader) {
242
- const amount = (0, utils_1.bigIntify)(event.args.amount);
243
- const tickLower = (0, utils_1.bigIntify)(event.args.tickLower);
244
- const tickUpper = (0, utils_1.bigIntify)(event.args.tickUpper);
245
- pool.blockTimestamp = (0, utils_1.bigIntify)(blockHeader.timestamp);
246
- uniswap_v3_math_1.uniswapV3Math._modifyPosition(pool, {
247
- tickLower,
248
- tickUpper,
249
- liquidityDelta: -BigInt.asIntN(128, BigInt.asIntN(256, amount)),
250
- });
251
- // From this transaction I conclude that there is no balance change from
252
- // Burn event: https://dashboard.tenderly.co/tx/mainnet/0xfccf5341147ac3ad0e66452273d12dfc3219e81f8fb369a6cdecfb24b9b9d078/logs
253
- // And it aligns with UniswapV3 doc:
254
- // https://github.com/Uniswap/v3-core/blob/05c10bf6d547d6121622ac51c457f93775e1df09/contracts/interfaces/pool/IUniswapV3PoolActions.sol#L59
255
- // It just updates positions and tokensOwed which may be requested calling collect
256
- // So, we don't need to update pool.balances0 and pool.balances1 here
257
- return pool;
258
- }
259
276
  handleMintEvent(event, pool, log, blockHeader) {
260
- const amount = (0, utils_1.bigIntify)(event.args.amount);
261
- const tickLower = (0, utils_1.bigIntify)(event.args.tickLower);
262
- const tickUpper = (0, utils_1.bigIntify)(event.args.tickUpper);
277
+ const bottomTick = (0, utils_1.bigIntify)(event.args.bottomTick);
278
+ const topTick = (0, utils_1.bigIntify)(event.args.topTick);
279
+ const liquidityActual = (0, utils_1.bigIntify)(event.args.liquidityAmount);
263
280
  const amount0 = (0, utils_1.bigIntify)(event.args.amount0);
264
281
  const amount1 = (0, utils_1.bigIntify)(event.args.amount1);
265
282
  pool.blockTimestamp = (0, utils_1.bigIntify)(blockHeader.timestamp);
266
- uniswap_v3_math_1.uniswapV3Math._modifyPosition(pool, {
267
- tickLower,
268
- tickUpper,
269
- liquidityDelta: amount,
270
- });
283
+ AlgebraMath_1.AlgebraMath._updatePositionTicksAndFees(pool, bottomTick, topTick, liquidityActual);
271
284
  pool.balance0 += amount0;
272
285
  pool.balance1 += amount1;
273
286
  return pool;
274
287
  }
275
- handleSetFeeProtocolEvent(event, pool, log, blockHeader) {
276
- const feeProtocol0 = (0, utils_1.bigIntify)(event.args.feeProtocol0New);
277
- const feeProtocol1 = (0, utils_1.bigIntify)(event.args.feeProtocol1New);
278
- pool.slot0.feeProtocol = feeProtocol0 + (feeProtocol1 << 4n);
288
+ handleBurnEvent(event, pool, log, blockHeader) {
289
+ const bottomTick = (0, utils_1.bigIntify)(event.args.bottomTick);
290
+ const topTick = (0, utils_1.bigIntify)(event.args.topTick);
291
+ const amount = (0, utils_1.bigIntify)(event.args.liquidityAmount);
292
+ pool.blockTimestamp = (0, utils_1.bigIntify)(blockHeader.timestamp);
293
+ AlgebraMath_1.AlgebraMath._updatePositionTicksAndFees(pool, bottomTick, topTick, -BigInt.asIntN(128, BigInt.asIntN(256, amount)));
294
+ // no balance change
295
+ return pool;
296
+ }
297
+ handleNewFee(event, pool, log, blockHeader) {
298
+ const fee = (0, utils_1.bigIntify)(event.args.fee);
299
+ pool.globalState.fee = fee;
279
300
  pool.blockTimestamp = (0, utils_1.bigIntify)(blockHeader.timestamp);
280
301
  return pool;
281
302
  }
@@ -295,18 +316,21 @@ class BaseswapV3EventPool extends stateful_event_subscriber_1.StatefulEventSubsc
295
316
  pool.blockTimestamp = (0, utils_1.bigIntify)(blockHeader.timestamp);
296
317
  return pool;
297
318
  }
298
- handleIncreaseObservationCardinalityNextEvent(event, pool, log, blockHeader) {
299
- pool.slot0.observationCardinalityNext = parseInt(event.args.observationCardinalityNextNew, 10);
319
+ handleCommunityFee(event, pool, log, blockHeader) {
320
+ const communityFeeToken0 = (0, utils_1.bigIntify)(event.ags.communityFee0New);
321
+ const communityFeeToken1 = (0, utils_1.bigIntify)(event.ags.communityFee1New);
322
+ pool.globalState.communityFeeToken0 = communityFeeToken0;
323
+ pool.globalState.communityFeeToken1 = communityFeeToken1;
300
324
  pool.blockTimestamp = (0, utils_1.bigIntify)(blockHeader.timestamp);
301
325
  return pool;
302
326
  }
303
- _computePoolAddress(token0, token1, fee) {
327
+ _computePoolAddress(token0, token1) {
304
328
  // https://github.com/Uniswap/v3-periphery/blob/main/contracts/libraries/PoolAddress.sol
305
329
  if (token0 > token1)
306
330
  [token0, token1] = [token1, token0];
307
- const encodedKey = ethers_1.ethers.utils.keccak256(ethers_1.ethers.utils.defaultAbiCoder.encode(['address', 'address', 'uint24'], [token0, token1, BigInt.asUintN(24, fee)]));
308
- return ethers_1.ethers.utils.getCreate2Address(this.factoryAddress, encodedKey, this.poolInitCodeHash);
331
+ const encodedKey = ethers_1.ethers.utils.keccak256(ethers_1.ethers.utils.defaultAbiCoder.encode(['address', 'address'], [token0, token1]));
332
+ return ethers_1.ethers.utils.getCreate2Address(this.poolDeployer, encodedKey, this.poolInitCodeHash);
309
333
  }
310
334
  }
311
- exports.BaseswapV3EventPool = BaseswapV3EventPool;
312
- //# sourceMappingURL=baseswap-v3-pool.js.map
335
+ exports.AlgebraEventPool = AlgebraEventPool;
336
+ //# sourceMappingURL=algebra-pool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"algebra-pool.js","sourceRoot":"","sources":["../../../src/dex/algebra/algebra-pool.ts"],"names":[],"mappings":";;;;;;AAAA,oDAAuB;AACvB,4CAA+C;AAC/C,iDAAqD;AAErD,uCAAmE;AACnE,+EAGyC;AAOzC,mCAAgC;AAEhC,kGAAgE;AAEhE,uDAIiC;AACjC,iDAAqD;AAErD,mCAAwE;AACxE,mDAAgD;AAChD,uEAAoE;AACpE,6DAG2C;AAC3C,+CAA4C;AAC5C,+CAA0C;AAE1C,MAAa,gBAAiB,SAAQ,mDAAkC;IAuBtE,YACW,SAAqB,EAC9B,UAAkB,EACT,kBAA4B,EAC5B,cAAyB,EACf,cAAuB,EAC1C,MAAe,EACf,MAAe,EACf,MAAc,EAC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CAAC,MAAM,CACjC,CAAC,SAAS,EAAE,SAAS,CAAC,EACtB,CAAC,MAAM,EAAE,MAAM,CAAC,CACjB,CACF,CAAC;QAEF,OAAO,eAAM,CAAC,KAAK,CAAC,iBAAiB,CACnC,IAAI,CAAC,YAAY,EACjB,UAAU,EACV,IAAI,CAAC,gBAAgB,CACtB,CAAC;IACJ,CAAC;CACF;AA1hBD,4CA0hBC"}
@@ -20,6 +20,7 @@ export declare class Algebra extends SimpleExchange implements IDex<AlgebraData>
20
20
  private readonly factory;
21
21
  readonly isFeeOnTransferSupported: boolean;
22
22
  protected eventPools: Record<string, IAlgebraEventPool | null>;
23
+ private newlyCreatedPoolKeys;
23
24
  readonly hasConstantPriceLargeAmounts = false;
24
25
  readonly needWrapNative = true;
25
26
  intervalTask?: NodeJS.Timeout;
@@ -45,8 +45,8 @@ const AlgebraMath_1 = require("./lib/AlgebraMath");
45
45
  const algebra_pool_v1_1_1 = require("./algebra-pool-v1_1");
46
46
  const algebra_pool_v1_9_1 = require("./algebra-pool-v1_9");
47
47
  const algebra_factory_1 = require("./algebra-factory");
48
- const ALGEBRA_CLEAN_NOT_EXISTING_POOL_TTL_MS = 3 * 24 * 60 * 60 * 1000; // 3 days
49
- const ALGEBRA_CLEAN_NOT_EXISTING_POOL_INTERVAL_MS = 24 * 60 * 60 * 1000; // Once in a day
48
+ // const ALGEBRA_CLEAN_NOT_EXISTING_POOL_TTL_MS = 3 * 24 * 60 * 60 * 1000; // 3 days
49
+ // const ALGEBRA_CLEAN_NOT_EXISTING_POOL_INTERVAL_MS = 24 * 60 * 60 * 1000; // Once in a day
50
50
  const ALGEBRA_EFFICIENCY_FACTOR = 3;
51
51
  const ALGEBRA_TICK_GAS_COST = 24000; // Ceiled
52
52
  const ALGEBRA_TICK_BASE_OVERHEAD = 75000;
@@ -67,6 +67,7 @@ class Algebra extends simple_exchange_1.SimpleExchange {
67
67
  this.config = config;
68
68
  this.isFeeOnTransferSupported = false;
69
69
  this.eventPools = {};
70
+ this.newlyCreatedPoolKeys = new Set();
70
71
  this.hasConstantPriceLargeAmounts = false;
71
72
  this.needWrapNative = true;
72
73
  /*
@@ -76,7 +77,8 @@ class Algebra extends simple_exchange_1.SimpleExchange {
76
77
  this.onPoolCreatedDeleteFromNonExistingSet = async ({ token0, token1, }) => {
77
78
  const logPrefix = '[onPoolCreatedDeleteFromNonExistingSet]';
78
79
  const [_token0, _token1] = this._sortTokens(token0, token1);
79
- const poolKey = `${_token0}_${_token1}`;
80
+ const poolKey = `${_token0}_${_token1}`.toLowerCase();
81
+ this.newlyCreatedPoolKeys.add(poolKey);
80
82
  // consider doing it only from master pool for less calls to distant cache
81
83
  // delete entry locally to let local instance discover the pool
82
84
  delete this.eventPools[this.getPoolIdentifier(_token0, _token1)];
@@ -113,13 +115,22 @@ class Algebra extends simple_exchange_1.SimpleExchange {
113
115
  async initializePricing(blockNumber) {
114
116
  // Init listening to new pools creation
115
117
  await this.factory.initialize(blockNumber);
116
- if (!this.dexHelper.config.isSlave) {
117
- const cleanExpiredNotExistingPoolsKeys = async () => {
118
- const maxTimestamp = Date.now() - ALGEBRA_CLEAN_NOT_EXISTING_POOL_TTL_MS;
119
- await this.dexHelper.cache.zremrangebyscore(this.notExistingPoolSetKey, 0, maxTimestamp);
120
- };
121
- this.intervalTask = setInterval(cleanExpiredNotExistingPoolsKeys.bind(this), ALGEBRA_CLEAN_NOT_EXISTING_POOL_INTERVAL_MS);
122
- }
118
+ //// COMMENTING DEPRECATED LOGIC: as we now invalidate pools on creation this is not needed anymore
119
+ // if (!this.dexHelper.config.isSlave) {
120
+ // const cleanExpiredNotExistingPoolsKeys = async () => {
121
+ // const maxTimestamp =
122
+ // Date.now() - ALGEBRA_CLEAN_NOT_EXISTING_POOL_TTL_MS;
123
+ // await this.dexHelper.cache.zremrangebyscore(
124
+ // this.notExistingPoolSetKey,
125
+ // 0,
126
+ // maxTimestamp,
127
+ // );
128
+ // };
129
+ // this.intervalTask = setInterval(
130
+ // cleanExpiredNotExistingPoolsKeys.bind(this),
131
+ // ALGEBRA_CLEAN_NOT_EXISTING_POOL_INTERVAL_MS,
132
+ // );
133
+ // }
123
134
  }
124
135
  async getPool(srcAddress, destAddress, blockNumber) {
125
136
  let pool = this.eventPools[this.getPoolIdentifier(srcAddress, destAddress)];
@@ -178,20 +189,37 @@ class Algebra extends simple_exchange_1.SimpleExchange {
178
189
  pool.initRetryAttemptCount = 0;
179
190
  },
180
191
  });
192
+ if (this.newlyCreatedPoolKeys.has(key)) {
193
+ this.newlyCreatedPoolKeys.delete(key);
194
+ }
181
195
  }
182
196
  catch (e) {
183
197
  if (e instanceof Error && e.message.endsWith('Pool does not exist')) {
184
- // no need to await we want the set to have the pool key but it's not blocking
185
- this.dexHelper.cache.zadd(this.notExistingPoolSetKey, [Date.now(), key], 'NX');
186
- // Pool does not exist for this pair, so we can set it to null
187
- // to prevent more requests for this pool
188
- pool = null;
189
- this.logger.trace(`${this.dexHelper}: Pool: srcAddress=${srcAddress}, destAddress=${destAddress} not found`, e);
198
+ /*
199
+ protection against 2 race conditions
200
+ 1/ if pool.initialize() promise rejects after the Pool creation event got treated
201
+ 2/ if the rpc node we hit on the http request is lagging behind the one we got event from (websocket)
202
+ */
203
+ if (this.newlyCreatedPoolKeys.has(key)) {
204
+ this.logger.warn(`[block=${blockNumber}][Pool=${key}] newly created pool failed to initialise`);
205
+ this.newlyCreatedPoolKeys.delete(key);
206
+ }
207
+ else {
208
+ this.logger.info(`[block=${blockNumber}][Pool=${key}] pool failed to initialize so it's marked as non existing`, e);
209
+ // allowing only master/leader node to alter distant cache to reduce risk of race conditions
210
+ if (!this.dexHelper.config.isSlave) {
211
+ // no need to await we want the set to have the pool key but it's not blocking
212
+ this.dexHelper.cache.zadd(this.notExistingPoolSetKey, [Date.now(), key], 'NX');
213
+ }
214
+ // Pool does not exist for this pair, so we can set it to null
215
+ // to prevent more requests for this pool
216
+ pool = null;
217
+ }
190
218
  }
191
219
  else {
192
220
  // on unknown error mark as failed and increase retryCount for retry init strategy
193
221
  // note: state would be null by default which allows to fallback
194
- this.logger.warn(`${this.dexKey}: Can not generate pool state for srcAddress=${srcAddress}, destAddress=${destAddress} pool fallback to rpc and retry every ${this.config.initRetryFrequency} times, initRetryAttemptCount=${pool.initRetryAttemptCount}`, e);
222
+ this.logger.warn(`[block=${blockNumber}][Pool=${key}] Can not generate pool state for srcAddress=${srcAddress}, destAddress=${destAddress} pool fallback to rpc and retry every ${this.config.initRetryFrequency} times, initRetryAttemptCount=${pool.initRetryAttemptCount}`, e);
195
223
  pool.initFailed = true;
196
224
  }
197
225
  }