@paraswap/dex-lib 2.49.2 → 2.50.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 (151) hide show
  1. package/build/abi/algebra/AlgebraPool.abi.json +727 -0
  2. package/build/abi/uniswap-v3/AlienBaseV3Router.abi.json +52 -0
  3. package/build/abi/uniswap-v3/UniswapV3Quoter.abi.json +193 -0
  4. package/build/dex/algebra/algebra-pool-v1_1.d.ts +3 -2
  5. package/build/dex/algebra/algebra-pool-v1_1.js +3 -2
  6. package/build/dex/algebra/algebra-pool-v1_1.js.map +1 -1
  7. package/build/dex/algebra/algebra-pool-v1_9_bidirectional_fee.d.ts +16 -0
  8. package/build/dex/algebra/algebra-pool-v1_9_bidirectional_fee.js +26 -0
  9. package/build/dex/algebra/algebra-pool-v1_9_bidirectional_fee.js.map +1 -0
  10. package/build/dex/algebra/algebra-pool-v1_9_directional_fee.d.ts +54 -0
  11. package/build/dex/algebra/algebra-pool-v1_9_directional_fee.js +356 -0
  12. package/build/dex/algebra/algebra-pool-v1_9_directional_fee.js.map +1 -0
  13. package/build/dex/algebra/algebra-pool.d.ts +46 -0
  14. package/build/dex/algebra/algebra-pool.js +336 -0
  15. package/build/dex/algebra/algebra-pool.js.map +1 -0
  16. package/build/dex/algebra/algebra.d.ts +2 -1
  17. package/build/dex/algebra/algebra.js +6 -1
  18. package/build/dex/algebra/algebra.js.map +1 -1
  19. package/build/dex/algebra/config.js +16 -0
  20. package/build/dex/algebra/config.js.map +1 -1
  21. package/build/dex/algebra/types.d.ts +1 -1
  22. package/build/dex/quickswap/camelot-v3.d.ts +6 -0
  23. package/build/dex/quickswap/camelot-v3.js +19 -0
  24. package/build/dex/quickswap/camelot-v3.js.map +1 -0
  25. package/build/dex/quickswap/zyberswap-v3.d.ts +6 -0
  26. package/build/dex/quickswap/zyberswap-v3.js +19 -0
  27. package/build/dex/quickswap/zyberswap-v3.js.map +1 -0
  28. package/build/dex/uniswap-v2/rebase-tokens.json +7 -0
  29. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.d.ts +17 -0
  30. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js +64 -0
  31. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js.map +1 -0
  32. package/package.json +1 -1
  33. package/src/dex/algebra/algebra-e2e.test.ts +22 -0
  34. package/src/dex/algebra/algebra-pool-v1_1.ts +3 -2
  35. package/src/dex/algebra/algebra-pool-v1_9_bidirectional_fee.ts +55 -0
  36. package/src/dex/algebra/algebra.ts +12 -3
  37. package/src/dex/algebra/config.ts +18 -0
  38. package/src/dex/algebra/types.ts +1 -1
  39. package/build/abi/Redstone.json +0 -739
  40. package/build/abi/angle/stagToken.json +0 -963
  41. package/build/abi/angle-transmuter/MorphoOracle.json +0 -193
  42. package/build/abi/angle-transmuter/MorphoVault.json +0 -2506
  43. package/build/abi/angle-transmuter/Pyth.json +0 -1534
  44. package/build/abi/angle-transmuter/Transmuter.json +0 -1711
  45. package/build/abi/pharaoh/PharaohPool.abi.json +0 -1582
  46. package/build/abi/smardex/smardex-factory.json +0 -185
  47. package/build/abi/smardex/smardex-pool.json +0 -578
  48. package/build/abi/smardex/smardex-router.json +0 -648
  49. package/build/abi/trader-joe-v2_1/FactoryABI.json +0 -1208
  50. package/build/abi/trader-joe-v2_1/PairABI.json +0 -1493
  51. package/build/abi/trader-joe-v2_1/RouterABI.json +0 -1489
  52. package/build/abi/trader-joe-v2_1/StateMulticall.json +0 -193
  53. package/build/dex/angle-staked-stable/angle-staked-stable-pool.d.ts +0 -55
  54. package/build/dex/angle-staked-stable/angle-staked-stable-pool.js +0 -183
  55. package/build/dex/angle-staked-stable/angle-staked-stable-pool.js.map +0 -1
  56. package/build/dex/angle-staked-stable/angle-staked-stable.d.ts +0 -50
  57. package/build/dex/angle-staked-stable/angle-staked-stable.js +0 -280
  58. package/build/dex/angle-staked-stable/angle-staked-stable.js.map +0 -1
  59. package/build/dex/angle-staked-stable/config.d.ts +0 -4
  60. package/build/dex/angle-staked-stable/config.js +0 -46
  61. package/build/dex/angle-staked-stable/config.js.map +0 -1
  62. package/build/dex/angle-staked-stable/types.d.ts +0 -17
  63. package/build/dex/angle-staked-stable/types.js +0 -3
  64. package/build/dex/angle-staked-stable/types.js.map +0 -1
  65. package/build/dex/angle-transmuter/angle-transmuter-pool.d.ts +0 -61
  66. package/build/dex/angle-transmuter/angle-transmuter-pool.js +0 -415
  67. package/build/dex/angle-transmuter/angle-transmuter-pool.js.map +0 -1
  68. package/build/dex/angle-transmuter/angle-transmuter.d.ts +0 -59
  69. package/build/dex/angle-transmuter/angle-transmuter.js +0 -262
  70. package/build/dex/angle-transmuter/angle-transmuter.js.map +0 -1
  71. package/build/dex/angle-transmuter/backedOracle.d.ts +0 -19
  72. package/build/dex/angle-transmuter/backedOracle.js +0 -56
  73. package/build/dex/angle-transmuter/backedOracle.js.map +0 -1
  74. package/build/dex/angle-transmuter/config.d.ts +0 -4
  75. package/build/dex/angle-transmuter/config.js +0 -30
  76. package/build/dex/angle-transmuter/config.js.map +0 -1
  77. package/build/dex/angle-transmuter/constants.d.ts +0 -77
  78. package/build/dex/angle-transmuter/constants.js +0 -94
  79. package/build/dex/angle-transmuter/constants.js.map +0 -1
  80. package/build/dex/angle-transmuter/morphoOracle.d.ts +0 -42
  81. package/build/dex/angle-transmuter/morphoOracle.js +0 -179
  82. package/build/dex/angle-transmuter/morphoOracle.js.map +0 -1
  83. package/build/dex/angle-transmuter/morphoVault.d.ts +0 -19
  84. package/build/dex/angle-transmuter/morphoVault.js +0 -78
  85. package/build/dex/angle-transmuter/morphoVault.js.map +0 -1
  86. package/build/dex/angle-transmuter/pyth.d.ts +0 -19
  87. package/build/dex/angle-transmuter/pyth.js +0 -70
  88. package/build/dex/angle-transmuter/pyth.js.map +0 -1
  89. package/build/dex/angle-transmuter/redstone.d.ts +0 -20
  90. package/build/dex/angle-transmuter/redstone.js +0 -73
  91. package/build/dex/angle-transmuter/redstone.js.map +0 -1
  92. package/build/dex/angle-transmuter/transmuter.d.ts +0 -52
  93. package/build/dex/angle-transmuter/transmuter.js +0 -374
  94. package/build/dex/angle-transmuter/transmuter.js.map +0 -1
  95. package/build/dex/angle-transmuter/types.d.ts +0 -188
  96. package/build/dex/angle-transmuter/types.js +0 -33
  97. package/build/dex/angle-transmuter/types.js.map +0 -1
  98. package/build/dex/angle-transmuter/utils.d.ts +0 -9
  99. package/build/dex/angle-transmuter/utils.js +0 -252
  100. package/build/dex/angle-transmuter/utils.js.map +0 -1
  101. package/build/dex/smardex/subgraph-queries.d.ts +0 -1
  102. package/build/dex/smardex/subgraph-queries.js +0 -17
  103. package/build/dex/smardex/subgraph-queries.js.map +0 -1
  104. package/build/dex/smardex/usd-reserves.d.ts +0 -23
  105. package/build/dex/smardex/usd-reserves.js +0 -86
  106. package/build/dex/smardex/usd-reserves.js.map +0 -1
  107. package/build/dex/trader-joe-v2-1-2/config.d.ts +0 -4
  108. package/build/dex/trader-joe-v2-1-2/config.js +0 -47
  109. package/build/dex/trader-joe-v2-1-2/config.js.map +0 -1
  110. package/build/dex/trader-joe-v2-1-2/constants.d.ts +0 -20
  111. package/build/dex/trader-joe-v2-1-2/constants.js +0 -22
  112. package/build/dex/trader-joe-v2-1-2/constants.js.map +0 -1
  113. package/build/dex/trader-joe-v2-1-2/math/index.d.ts +0 -37
  114. package/build/dex/trader-joe-v2-1-2/math/index.js +0 -291
  115. package/build/dex/trader-joe-v2-1-2/math/index.js.map +0 -1
  116. package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2-pool.d.ts +0 -60
  117. package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2-pool.js +0 -248
  118. package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2-pool.js.map +0 -1
  119. package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2.d.ts +0 -53
  120. package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2.js +0 -307
  121. package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2.js.map +0 -1
  122. package/build/dex/trader-joe-v2-1-2/types.d.ts +0 -82
  123. package/build/dex/trader-joe-v2-1-2/types.js +0 -9
  124. package/build/dex/trader-joe-v2-1-2/types.js.map +0 -1
  125. package/build/dex/trader-joe-v2_1/config.d.ts +0 -4
  126. package/build/dex/trader-joe-v2_1/config.js +0 -47
  127. package/build/dex/trader-joe-v2_1/config.js.map +0 -1
  128. package/build/dex/trader-joe-v2_1/constants.d.ts +0 -20
  129. package/build/dex/trader-joe-v2_1/constants.js +0 -22
  130. package/build/dex/trader-joe-v2_1/constants.js.map +0 -1
  131. package/build/dex/trader-joe-v2_1/math/index.d.ts +0 -37
  132. package/build/dex/trader-joe-v2_1/math/index.js +0 -291
  133. package/build/dex/trader-joe-v2_1/math/index.js.map +0 -1
  134. package/build/dex/trader-joe-v2_1/trader-joe-v2.1.d.ts +0 -0
  135. package/build/dex/trader-joe-v2_1/trader-joe-v2.1.js +0 -384
  136. package/build/dex/trader-joe-v2_1/trader-joe-v2.1.js.map +0 -1
  137. package/build/dex/trader-joe-v2_1/trader-joe-v2_1-pool.d.ts +0 -60
  138. package/build/dex/trader-joe-v2_1/trader-joe-v2_1-pool.js +0 -247
  139. package/build/dex/trader-joe-v2_1/trader-joe-v2_1-pool.js.map +0 -1
  140. package/build/dex/trader-joe-v2_1/trader-joe-v2_1.d.ts +0 -53
  141. package/build/dex/trader-joe-v2_1/trader-joe-v2_1.js +0 -307
  142. package/build/dex/trader-joe-v2_1/trader-joe-v2_1.js.map +0 -1
  143. package/build/dex/trader-joe-v2_1/types.d.ts +0 -82
  144. package/build/dex/trader-joe-v2_1/types.js +0 -9
  145. package/build/dex/trader-joe-v2_1/types.js.map +0 -1
  146. package/build/dex/wombat/wombat-pool-poller.d.ts +0 -22
  147. package/build/dex/wombat/wombat-pool-poller.js +0 -164
  148. package/build/dex/wombat/wombat-pool-poller.js.map +0 -1
  149. package/build/dex-helper/RedisBatcher.d.ts +0 -20
  150. package/build/dex-helper/RedisBatcher.js +0 -75
  151. package/build/dex-helper/RedisBatcher.js.map +0 -1
@@ -0,0 +1,356 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.AlgebraEventPoolV1_9_directional_fee = void 0;
7
+ const lodash_1 = __importDefault(require("lodash"));
8
+ const abi_1 = require("@ethersproject/abi");
9
+ const ts_essentials_1 = require("ts-essentials");
10
+ const utils_1 = require("../../utils");
11
+ const stateful_event_subscriber_1 = require("../../stateful-event-subscriber");
12
+ const ethers_1 = require("ethers");
13
+ const AlgebraPool_v1_9_abi_json_1 = __importDefault(require("../../abi/algebra/AlgebraPool-v1_9.abi.json"));
14
+ const constants_1 = require("../uniswap-v3/constants");
15
+ const decoders_1 = require("../../lib/decoders");
16
+ const utils_2 = require("./utils");
17
+ const AlgebraMath_1 = require("./lib/AlgebraMath");
18
+ const utils_3 = require("../uniswap-v3/contract-math/utils");
19
+ const constants_2 = require("../../constants");
20
+ const TickTable_1 = require("./lib/TickTable");
21
+ const constants_3 = require("./constants");
22
+ class AlgebraEventPoolV1_9_directional_fee 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);
25
+ this.dexHelper = dexHelper;
26
+ this.stateMultiContract = stateMultiContract;
27
+ this.erc20Interface = erc20Interface;
28
+ this.factoryAddress = factoryAddress;
29
+ this.poolInitCodeHash = poolInitCodeHash;
30
+ this.poolDeployer = poolDeployer;
31
+ this.handlers = {};
32
+ this.poolIface = new abi_1.Interface(AlgebraPool_v1_9_abi_json_1.default);
33
+ this.initFailed = false;
34
+ this.initRetryAttemptCount = 0;
35
+ this.token0 = token0.toLowerCase();
36
+ this.token1 = token1.toLowerCase();
37
+ this.logDecoder = (log) => this.poolIface.parseLog(log);
38
+ this.addressesSubscribed = new Array(1);
39
+ this.handlers['Fee'] = this.handleNewFee.bind(this);
40
+ this.handlers['Swap'] = this.handleSwapEvent.bind(this);
41
+ this.handlers['Mint'] = this.handleMintEvent.bind(this);
42
+ this.handlers['Burn'] = this.handleBurnEvent.bind(this);
43
+ this.handlers['Flash'] = this.handleFlashEvent.bind(this);
44
+ this.handlers['Collect'] = this.handleCollectEvent.bind(this);
45
+ this.handlers['CommunityFee'] = this.handleCommunityFee.bind(this);
46
+ this.handlers['TickSpacing'] = this.handleTickSpacing.bind(this);
47
+ }
48
+ get poolAddress() {
49
+ if (this._poolAddress === undefined) {
50
+ this._poolAddress = this._computePoolAddress(this.token0, this.token1);
51
+ }
52
+ return this._poolAddress;
53
+ }
54
+ set poolAddress(address) {
55
+ this._poolAddress = address.toLowerCase();
56
+ }
57
+ async initialize(blockNumber, options) {
58
+ await super.initialize(blockNumber, options);
59
+ }
60
+ getPoolIdentifierData() {
61
+ return {
62
+ token0: this.token0,
63
+ token1: this.token1,
64
+ };
65
+ }
66
+ async processBlockLogs(state, logs, blockHeader) {
67
+ const newState = await super.processBlockLogs(state, logs, blockHeader);
68
+ if (newState && !newState.isValid) {
69
+ return await this.generateState(blockHeader.number);
70
+ }
71
+ return newState;
72
+ }
73
+ processLog(state, log, blockHeader) {
74
+ try {
75
+ const event = this.logDecoder(log);
76
+ if (event.name in this.handlers) {
77
+ // Because we have observations in array which is mutable by nature, there is a
78
+ // ts compile error: https://stackoverflow.com/questions/53412934/disable-allowing-assigning-readonly-types-to-non-readonly-types
79
+ // And there is no good workaround, so turn off the type checker for this line
80
+ const _state = lodash_1.default.cloneDeep(state);
81
+ try {
82
+ const newState = this.handlers[event.name](event, _state, log, blockHeader);
83
+ return newState;
84
+ }
85
+ catch (e) {
86
+ if (e instanceof Error &&
87
+ e.message.endsWith(constants_1.OUT_OF_RANGE_ERROR_POSTFIX)) {
88
+ this.logger.warn(`${this.parentName}: Pool ${this.poolAddress} on ${this.dexHelper.config.data.network} is out of TickBitmap requested range. Re-query the state. ${JSON.stringify(event)}`, e);
89
+ }
90
+ else {
91
+ this.logger.error(`${this.parentName}: Pool ${this.poolAddress}, ` +
92
+ `network=${this.dexHelper.config.data.network}: Unexpected ` +
93
+ `error while handling event on blockNumber=${blockHeader.number}, ` +
94
+ `blockHash=${blockHeader.hash} and parentHash=${blockHeader.parentHash} for QuickSwapV3, ${JSON.stringify(event)}`, e);
95
+ }
96
+ _state.isValid = false;
97
+ return _state;
98
+ }
99
+ }
100
+ }
101
+ catch (e) {
102
+ (0, utils_1.catchParseLogError)(e, this.logger);
103
+ }
104
+ return null; // ignore unrecognized event
105
+ }
106
+ getBitmapRangeToRequest() {
107
+ const networkId = this.dexHelper.config.data.network;
108
+ const tickBitmapToUse = constants_3.TICK_BITMAP_TO_USE_BY_CHAIN[networkId] ?? constants_3.TICK_BITMAP_TO_USE;
109
+ const tickBitmapBuffer = constants_3.TICK_BITMAP_BUFFER_BY_CHAIN[networkId] ?? constants_3.TICK_BITMAP_BUFFER;
110
+ return tickBitmapToUse + tickBitmapBuffer;
111
+ }
112
+ async _fetchPoolState_v1_9SingleStep(blockNumber) {
113
+ const callData = [
114
+ {
115
+ target: this.token0,
116
+ callData: this.erc20Interface.encodeFunctionData('balanceOf', [
117
+ this.poolAddress,
118
+ ]),
119
+ decodeFunction: decoders_1.uint256ToBigInt,
120
+ },
121
+ {
122
+ target: this.token1,
123
+ callData: this.erc20Interface.encodeFunctionData('balanceOf', [
124
+ this.poolAddress,
125
+ ]),
126
+ decodeFunction: decoders_1.uint256ToBigInt,
127
+ },
128
+ {
129
+ target: this.stateMultiContract.options.address,
130
+ callData: this.stateMultiContract.methods
131
+ .getFullStateWithRelativeBitmaps(this.factoryAddress, this.token0, this.token1, this.getBitmapRangeToRequest(), this.getBitmapRangeToRequest())
132
+ .encodeABI(),
133
+ decodeFunction: utils_2.decodeStateMultiCallResultWithRelativeBitmapsV1_9,
134
+ },
135
+ ];
136
+ const [resBalance0, resBalance1, resState] = await this.dexHelper.multiWrapper.tryAggregate(false, callData, blockNumber, this.dexHelper.multiWrapper.defaultBatchSize, false);
137
+ (0, ts_essentials_1.assert)(resState.success, 'Pool does not exist');
138
+ const [balance0, balance1, _state] = [
139
+ resBalance0.returnData,
140
+ resBalance1.returnData,
141
+ resState.returnData,
142
+ ];
143
+ return [balance0, balance1, _state];
144
+ }
145
+ async _fetchPoolState_v1_9MultiStep(blockNumber) {
146
+ const balancesAndGlobalStateCalldata = [
147
+ {
148
+ target: this.token0,
149
+ callData: this.erc20Interface.encodeFunctionData('balanceOf', [
150
+ this.poolAddress,
151
+ ]),
152
+ decodeFunction: decoders_1.uint256ToBigInt,
153
+ },
154
+ {
155
+ target: this.token1,
156
+ callData: this.erc20Interface.encodeFunctionData('balanceOf', [
157
+ this.poolAddress,
158
+ ]),
159
+ decodeFunction: decoders_1.uint256ToBigInt,
160
+ },
161
+ {
162
+ target: this.stateMultiContract.options.address,
163
+ callData: this.stateMultiContract.methods
164
+ .getFullStateWithoutTicks(this.factoryAddress, this.token0, this.token1, 0, 0)
165
+ .encodeABI(),
166
+ decodeFunction: utils_2.decodeStateMultiCallResultWithRelativeBitmapsV1_9,
167
+ },
168
+ ];
169
+ const [resBalance0, resBalance1, stateWithoutTicksAndTickBitmap] = await this.dexHelper.multiWrapper.tryAggregate(false, balancesAndGlobalStateCalldata, blockNumber, this.dexHelper.multiWrapper.defaultBatchSize, false);
170
+ (0, ts_essentials_1.assert)(stateWithoutTicksAndTickBitmap.success, 'Pool does not exist');
171
+ const [balance0, balance1, _stateWithoutTicksAndTickBitmap] = [
172
+ resBalance0.returnData,
173
+ resBalance1.returnData,
174
+ stateWithoutTicksAndTickBitmap.returnData,
175
+ ];
176
+ const { globalState: { tick }, } = _stateWithoutTicksAndTickBitmap;
177
+ const currentBitmapIndex = (0, utils_1.int16)(BigInt(tick) >> 8n);
178
+ const buffer = this.getBitmapRangeToRequest();
179
+ const startBitMapIndex = currentBitmapIndex - buffer;
180
+ const endBitMapIndex = currentBitmapIndex + buffer;
181
+ const allBitMapIndices = Array.from({ length: 2 * Number(buffer) + 1 }, () => startBitMapIndex + endBitMapIndex);
182
+ const ticksAndBitMaps = await Promise.all(allBitMapIndices.map(relativeTick => {
183
+ return this.stateMultiContract.methods
184
+ .getAdditionalBitmapWithTicks(this.factoryAddress, this.token0, this.token1, relativeTick, relativeTick)
185
+ .call();
186
+ }));
187
+ const _state = {
188
+ ..._stateWithoutTicksAndTickBitmap,
189
+ tickBitmap: ticksAndBitMaps.flatMap(v => v[0]),
190
+ ticks: ticksAndBitMaps.flatMap(v => v[1]),
191
+ };
192
+ return [balance0, balance1, _state];
193
+ }
194
+ async _fetchInitStateMultiStrategies(blockNumber) {
195
+ try {
196
+ return await this._fetchPoolState_v1_9SingleStep(blockNumber);
197
+ }
198
+ catch (e) {
199
+ if (e instanceof Error && e.message.includes('Pool does not exist'))
200
+ throw e;
201
+ if (this.dexHelper.config.data.network != constants_2.Network.ZKEVM)
202
+ throw e;
203
+ return this._fetchPoolState_v1_9MultiStep(blockNumber);
204
+ }
205
+ }
206
+ async generateState(blockNumber) {
207
+ const [balance0, balance1, _state] = await this._fetchInitStateMultiStrategies(blockNumber);
208
+ const tickBitmap = {};
209
+ const ticks = {};
210
+ (0, utils_3._reduceTickBitmap)(tickBitmap, _state.tickBitmap);
211
+ (0, utils_3._reduceTicks)(ticks, _state.ticks);
212
+ const globalState = {
213
+ communityFeeToken0: (0, utils_1.bigIntify)(_state.globalState.communityFeeToken0),
214
+ communityFeeToken1: (0, utils_1.bigIntify)(_state.globalState.communityFeeToken1),
215
+ feeZto: (0, utils_1.bigIntify)(_state.globalState.feeZto),
216
+ feeOtz: (0, utils_1.bigIntify)(_state.globalState.feeOtz),
217
+ price: (0, utils_1.bigIntify)(_state.globalState.price),
218
+ tick: (0, utils_1.bigIntify)(_state.globalState.tick),
219
+ };
220
+ const currentTick = globalState.tick;
221
+ const startTickBitmap = TickTable_1.TickTable.position(BigInt(currentTick))[0];
222
+ return {
223
+ pool: _state.pool,
224
+ blockTimestamp: (0, utils_1.bigIntify)(_state.blockTimestamp),
225
+ globalState,
226
+ liquidity: (0, utils_1.bigIntify)(_state.liquidity),
227
+ tickSpacing: (0, utils_1.bigIntify)(_state.tickSpacing),
228
+ maxLiquidityPerTick: (0, utils_1.bigIntify)(_state.maxLiquidityPerTick),
229
+ tickBitmap,
230
+ ticks,
231
+ startTickBitmap,
232
+ isValid: true,
233
+ balance0,
234
+ balance1,
235
+ areTicksCompressed: false,
236
+ };
237
+ }
238
+ handleSwapEvent(event, pool, log, blockHeader) {
239
+ const newSqrtPriceX96 = (0, utils_1.bigIntify)(event.args.price);
240
+ const amount0 = (0, utils_1.bigIntify)(event.args.amount0);
241
+ const amount1 = (0, utils_1.bigIntify)(event.args.amount1);
242
+ const newTick = (0, utils_1.bigIntify)(event.args.tick);
243
+ const newLiquidity = (0, utils_1.bigIntify)(event.args.liquidity);
244
+ pool.blockTimestamp = (0, utils_1.bigIntify)(blockHeader.timestamp);
245
+ if (amount0 <= 0n && amount1 <= 0n) {
246
+ this.logger.error(`${this.parentName}: amount0 <= 0n && amount1 <= 0n for ` +
247
+ `${this.poolAddress} and ${blockHeader.number}. Check why it happened`);
248
+ pool.isValid = false;
249
+ return pool;
250
+ }
251
+ else {
252
+ const zeroForOne = amount0 > 0n;
253
+ const [, , , , , communityFee] = AlgebraMath_1.AlgebraMath._calculateSwapAndLock(this.dexHelper.config.data.network, pool, zeroForOne, newSqrtPriceX96, newTick, newLiquidity);
254
+ if (zeroForOne) {
255
+ if (amount1 < 0n) {
256
+ pool.balance1 -= BigInt.asUintN(256, -amount1);
257
+ }
258
+ else {
259
+ this.logger.error(`In swapEvent for pool ${pool.pool} received incorrect values ${zeroForOne} and ${amount1}`);
260
+ pool.isValid = false;
261
+ }
262
+ // This is not correct fully, because pool may get more tokens then it needs, but
263
+ // it is not accounted in internal state, it should be good enough
264
+ pool.balance0 += BigInt.asUintN(256, amount0);
265
+ }
266
+ else {
267
+ if (amount0 < 0n) {
268
+ pool.balance0 -= BigInt.asUintN(256, -amount0);
269
+ }
270
+ else {
271
+ this.logger.error(`In swapEvent for pool ${pool.pool} received incorrect values ${zeroForOne} and ${amount0}`);
272
+ pool.isValid = false;
273
+ }
274
+ pool.balance1 += BigInt.asUintN(256, amount1);
275
+ }
276
+ if (communityFee > 0n) {
277
+ // _payCommunityFee
278
+ if (zeroForOne) {
279
+ pool.balance0 -= communityFee;
280
+ }
281
+ else {
282
+ pool.balance1 -= communityFee;
283
+ }
284
+ }
285
+ return pool;
286
+ }
287
+ }
288
+ handleMintEvent(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 liquidityActual = (0, utils_1.bigIntify)(event.args.liquidityAmount);
292
+ const amount0 = (0, utils_1.bigIntify)(event.args.amount0);
293
+ const amount1 = (0, utils_1.bigIntify)(event.args.amount1);
294
+ pool.blockTimestamp = (0, utils_1.bigIntify)(blockHeader.timestamp);
295
+ AlgebraMath_1.AlgebraMath._updatePositionTicksAndFees(this.dexHelper.config.data.network, pool, bottomTick, topTick, liquidityActual);
296
+ pool.balance0 += amount0;
297
+ pool.balance1 += amount1;
298
+ return pool;
299
+ }
300
+ handleBurnEvent(event, pool, log, blockHeader) {
301
+ const bottomTick = (0, utils_1.bigIntify)(event.args.bottomTick);
302
+ const topTick = (0, utils_1.bigIntify)(event.args.topTick);
303
+ const amount = (0, utils_1.bigIntify)(event.args.liquidityAmount);
304
+ pool.blockTimestamp = (0, utils_1.bigIntify)(blockHeader.timestamp);
305
+ AlgebraMath_1.AlgebraMath._updatePositionTicksAndFees(this.dexHelper.config.data.network, pool, bottomTick, topTick, -BigInt.asIntN(128, BigInt.asIntN(256, amount)));
306
+ // no balance change
307
+ return pool;
308
+ }
309
+ handleNewFee(event, pool, log, blockHeader) {
310
+ const feeZto = (0, utils_1.bigIntify)(event.args.feeZto);
311
+ const feeOtz = (0, utils_1.bigIntify)(event.args.feeOtz);
312
+ pool.globalState.feeZto = feeZto;
313
+ pool.globalState.feeOtz = feeOtz;
314
+ pool.blockTimestamp = (0, utils_1.bigIntify)(blockHeader.timestamp);
315
+ return pool;
316
+ }
317
+ handleCollectEvent(event, pool, log, blockHeader) {
318
+ const amount0 = (0, utils_1.bigIntify)(event.args.amount0);
319
+ const amount1 = (0, utils_1.bigIntify)(event.args.amount1);
320
+ pool.balance0 -= amount0;
321
+ pool.balance1 -= amount1;
322
+ pool.blockTimestamp = (0, utils_1.bigIntify)(blockHeader.timestamp);
323
+ return pool;
324
+ }
325
+ handleFlashEvent(event, pool, log, blockHeader) {
326
+ const paid0 = (0, utils_1.bigIntify)(event.args.paid0);
327
+ const paid1 = (0, utils_1.bigIntify)(event.args.paid1);
328
+ pool.balance0 += paid0;
329
+ pool.balance1 += paid1;
330
+ pool.blockTimestamp = (0, utils_1.bigIntify)(blockHeader.timestamp);
331
+ return pool;
332
+ }
333
+ handleCommunityFee(event, pool, log, blockHeader) {
334
+ const communityFeeToken0 = (0, utils_1.bigIntify)(event.ags.communityFee0New);
335
+ const communityFeeToken1 = (0, utils_1.bigIntify)(event.ags.communityFee1New);
336
+ pool.globalState.communityFeeToken0 = communityFeeToken0;
337
+ pool.globalState.communityFeeToken1 = communityFeeToken1;
338
+ pool.blockTimestamp = (0, utils_1.bigIntify)(blockHeader.timestamp);
339
+ return pool;
340
+ }
341
+ handleTickSpacing(event, pool, log, blockHeader) {
342
+ const newTickSpacing = (0, utils_1.bigIntify)(event.args.newTickSpacing);
343
+ pool.tickSpacing = newTickSpacing;
344
+ pool.blockTimestamp = (0, utils_1.bigIntify)(blockHeader.timestamp);
345
+ return pool;
346
+ }
347
+ _computePoolAddress(token0, token1) {
348
+ // https://github.com/Uniswap/v3-periphery/blob/main/contracts/libraries/PoolAddress.sol
349
+ if (token0 > token1)
350
+ [token0, token1] = [token1, token0];
351
+ const encodedKey = ethers_1.ethers.utils.keccak256(ethers_1.ethers.utils.defaultAbiCoder.encode(['address', 'address'], [token0, token1]));
352
+ return ethers_1.ethers.utils.getCreate2Address(this.poolDeployer, encodedKey, this.poolInitCodeHash);
353
+ }
354
+ }
355
+ exports.AlgebraEventPoolV1_9_directional_fee = AlgebraEventPoolV1_9_directional_fee;
356
+ //# sourceMappingURL=algebra-pool-v1_9_directional_fee.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"algebra-pool-v1_9_directional_fee.js","sourceRoot":"","sources":["../../../src/dex/algebra/algebra-pool-v1_9_directional_fee.ts"],"names":[],"mappings":";;;;;;AAAA,oDAAuB;AACvB,4CAA+C;AAC/C,iDAAqD;AAErD,uCAAmE;AACnE,+EAGyC;AAQzC,mCAAgC;AAEhC,4GAAyE;AACzE,uDAAqE;AACrE,iDAAqD;AAErD,mCAA4E;AAC5E,mDAAgD;AAChD,6DAG2C;AAC3C,+CAA0C;AAC1C,+CAA4C;AAC5C,2CAKqB;AAErB,MAAa,oCAAqC,SAAQ,mDAAuC;IAuB/F,YACW,SAAqB,EAC9B,UAAkB,EACT,kBAA4B,EAC5B,cAAyB,EACf,cAAuB,EAC1C,MAAe,EACf,MAAe,EACf,MAAc,EACd,SAAiB,EAAE,EACV,gBAAwB,EACxB,YAAoB;QAE7B,KAAK,CAAC,UAAU,EAAE,GAAG,MAAM,IAAI,MAAM,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;QAZjE,cAAS,GAAT,SAAS,CAAY;QAErB,uBAAkB,GAAlB,kBAAkB,CAAU;QAC5B,mBAAc,GAAd,cAAc,CAAW;QACf,mBAAc,GAAd,cAAc,CAAS;QAKjC,qBAAgB,GAAhB,gBAAgB,CAAQ;QACxB,iBAAY,GAAZ,YAAY,CAAQ;QAjC/B,aAAQ,GAOJ,EAAE,CAAC;QAUS,cAAS,GAAG,IAAI,eAAS,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BAAkB,CAAC;QACzD,IAAI,CAAC,WAAW,CAAC,kBAAkB,GAAG,kBAAkB,CAAC;QAEzD,IAAI,CAAC,cAAc,GAAG,IAAA,iBAAS,EAAC,WAAW,CAAC,SAAS,CAAC,CAAC;QAEvD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,iBAAiB,CACf,KAAU,EACV,IAAoB,EACpB,GAAQ,EACR,WAAwB;QAExB,MAAM,cAAc,GAAG,IAAA,iBAAS,EAAC,KAAK,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;QAE5D,IAAI,CAAC,WAAW,GAAG,cAAc,CAAC;QAClC,IAAI,CAAC,cAAc,GAAG,IAAA,iBAAS,EAAC,WAAW,CAAC,SAAS,CAAC,CAAC;QAEvD,OAAO,IAAI,CAAC;IACd,CAAC;IAEO,mBAAmB,CAAC,MAAe,EAAE,MAAe;QAC1D,wFAAwF;QACxF,IAAI,MAAM,GAAG,MAAM;YAAE,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAEzD,MAAM,UAAU,GAAG,eAAM,CAAC,KAAK,CAAC,SAAS,CACvC,eAAM,CAAC,KAAK,CAAC,eAAe,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;AAhkBD,oFAgkBC"}
@@ -0,0 +1,46 @@
1
+ import { Interface } from '@ethersproject/abi';
2
+ import { DeepReadonly } from 'ts-essentials';
3
+ import { Address, BlockHeader, Log, Logger } from '../../types';
4
+ import { InitializeStateOptions, StatefulEventSubscriber } from '../../stateful-event-subscriber';
5
+ import { IDexHelper } from '../../dex-helper/idex-helper';
6
+ import { PoolState } from './types';
7
+ import { Contract } from 'web3-eth-contract';
8
+ import { DecodedStateMultiCallResultWithRelativeBitmaps } from './types';
9
+ export declare class AlgebraEventPool extends StatefulEventSubscriber<PoolState> {
10
+ readonly dexHelper: IDexHelper;
11
+ readonly stateMultiContract: Contract;
12
+ readonly erc20Interface: Interface;
13
+ protected readonly factoryAddress: Address;
14
+ readonly poolInitCodeHash: string;
15
+ readonly poolDeployer: string;
16
+ handlers: {
17
+ [event: string]: (event: any, state: DeepReadonly<PoolState>, log: Readonly<Log>, blockHeader: BlockHeader) => DeepReadonly<PoolState> | null;
18
+ };
19
+ logDecoder: (log: Log) => any;
20
+ addressesSubscribed: string[];
21
+ private _poolAddress?;
22
+ readonly token0: Address;
23
+ readonly token1: Address;
24
+ readonly poolIface: Interface;
25
+ initFailed: boolean;
26
+ initRetryAttemptCount: number;
27
+ constructor(dexHelper: IDexHelper, parentName: string, stateMultiContract: Contract, erc20Interface: Interface, factoryAddress: Address, token0: Address, token1: Address, logger: Logger, mapKey: string | undefined, poolInitCodeHash: string, poolDeployer: string);
28
+ get poolAddress(): Address;
29
+ set poolAddress(address: Address);
30
+ initialize(blockNumber: number, options?: InitializeStateOptions<PoolState>): Promise<void>;
31
+ protected processBlockLogs(state: DeepReadonly<PoolState>, logs: Readonly<Log>[], blockHeader: Readonly<BlockHeader>): Promise<DeepReadonly<PoolState> | null>;
32
+ protected processLog(state: DeepReadonly<PoolState>, log: Readonly<Log>, blockHeader: Readonly<BlockHeader>): DeepReadonly<PoolState> | null;
33
+ getBitmapRangeToRequest(): bigint;
34
+ private _fetchPoolStateSingleStep;
35
+ private _fetchPoolStateMultiStep;
36
+ _fetchInitStateMultiStrategies(blockNumber: number): Promise<[bigint, bigint, DecodedStateMultiCallResultWithRelativeBitmaps]>;
37
+ generateState(blockNumber: number): Promise<Readonly<PoolState>>;
38
+ handleSwapEvent(event: any, pool: PoolState, log: Log, blockHeader: BlockHeader): PoolState;
39
+ handleMintEvent(event: any, pool: PoolState, log: Log, blockHeader: BlockHeader): PoolState;
40
+ handleBurnEvent(event: any, pool: PoolState, log: Log, blockHeader: BlockHeader): PoolState;
41
+ handleNewFee(event: any, pool: PoolState, log: Log, blockHeader: BlockHeader): PoolState;
42
+ handleCollectEvent(event: any, pool: PoolState, log: Log, blockHeader: BlockHeader): PoolState;
43
+ handleFlashEvent(event: any, pool: PoolState, log: Log, blockHeader: BlockHeader): PoolState;
44
+ handleCommunityFee(event: any, pool: PoolState, log: Log, blockHeader: BlockHeader): PoolState;
45
+ private _computePoolAddress;
46
+ }