-
Notifications
You must be signed in to change notification settings - Fork 307
/
uniswap-v3-pool.ts
554 lines (494 loc) · 17 KB
/
uniswap-v3-pool.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
import _ from 'lodash';
import { Contract } from 'web3-eth-contract';
import { Interface } from '@ethersproject/abi';
import { ethers } from 'ethers';
import { assert, DeepReadonly } from 'ts-essentials';
import { Log, Logger, BlockHeader, Address } from '../../types';
import {
InitializeStateOptions,
StatefulEventSubscriber,
} from '../../stateful-event-subscriber';
import { IDexHelper } from '../../dex-helper/idex-helper';
import {
PoolState,
DecodedStateMultiCallResultWithRelativeBitmaps,
DecodeStateMultiCallFunc,
} from './types';
import UniswapV3PoolABI from '../../abi/uniswap-v3/UniswapV3Pool.abi.json';
import { bigIntify, catchParseLogError, isSampled } from '../../utils';
import { uniswapV3Math } from './contract-math/uniswap-v3-math';
import { MultiCallParams } from '../../lib/multi-wrapper';
import {
OUT_OF_RANGE_ERROR_POSTFIX,
TICK_BITMAP_BUFFER,
TICK_BITMAP_TO_USE,
} from './constants';
import { TickBitMap } from './contract-math/TickBitMap';
import { uint256ToBigInt } from '../../lib/decoders';
import { decodeStateMultiCallResultWithRelativeBitmaps } from './utils';
import { _reduceTickBitmap, _reduceTicks } from './contract-math/utils';
export class UniswapV3EventPool extends StatefulEventSubscriber<PoolState> {
handlers: {
[event: string]: (
event: any,
pool: PoolState,
log: Log,
blockHeader: Readonly<BlockHeader>,
) => PoolState;
} = {};
logDecoder: (log: Log) => any;
readonly token0: Address;
readonly token1: Address;
private _poolAddress?: Address;
protected _stateRequestCallData?: MultiCallParams<
bigint | DecodedStateMultiCallResultWithRelativeBitmaps
>[];
public readonly poolIface = new Interface(UniswapV3PoolABI);
public initFailed = false;
public initRetryAttemptCount = 0;
public feeCodeAsString;
constructor(
readonly dexHelper: IDexHelper,
parentName: string,
readonly stateMultiContract: Contract,
readonly decodeStateMultiCallResultWithRelativeBitmaps:
| DecodeStateMultiCallFunc
| undefined,
readonly erc20Interface: Interface,
protected readonly factoryAddress: Address,
public feeCode: bigint,
token0: Address,
token1: Address,
logger: Logger,
mapKey: string = '',
readonly poolInitCodeHash: string,
public readonly tickSpacing?: bigint,
) {
let poolKey = `${token0}_${token1}_${feeCode}`;
if (tickSpacing !== undefined) {
poolKey = `${poolKey}_${tickSpacing}`;
}
super(parentName, poolKey, dexHelper, logger, true, mapKey);
this.feeCodeAsString = feeCode.toString();
this.token0 = token0.toLowerCase();
this.token1 = token1.toLowerCase();
this.logDecoder = (log: Log) => this.poolIface.parseLog(log);
this.addressesSubscribed = new Array<Address>(1);
// Add handlers
this.handlers['Swap'] = this.handleSwapEvent.bind(this);
this.handlers['Burn'] = this.handleBurnEvent.bind(this);
this.handlers['Mint'] = this.handleMintEvent.bind(this);
this.handlers['SetFeeProtocol'] = this.handleSetFeeProtocolEvent.bind(this);
this.handlers['IncreaseObservationCardinalityNext'] =
this.handleIncreaseObservationCardinalityNextEvent.bind(this);
// Wen need them to keep balance of the pool up to date
this.handlers['Collect'] = this.handleCollectEvent.bind(this);
// Almost the same as Collect, but for pool owners
this.handlers['CollectProtocol'] = this.handleCollectEvent.bind(this);
this.handlers['Flash'] = this.handleFlashEvent.bind(this);
}
get poolAddress() {
if (this._poolAddress === undefined) {
this._poolAddress = this._computePoolAddress(
this.token0,
this.token1,
this.feeCode,
);
}
return this._poolAddress;
}
set poolAddress(address: Address) {
this._poolAddress = address.toLowerCase();
}
async initialize(
blockNumber: number,
options?: InitializeStateOptions<PoolState>,
) {
await super.initialize(blockNumber, options);
}
protected getPoolIdentifierData() {
return {
token0: this.token0,
token1: this.token1,
fee: this.feeCode,
tickSpacing: this.tickSpacing,
};
}
protected async processBlockLogs(
state: DeepReadonly<PoolState>,
logs: Readonly<Log>[],
blockHeader: Readonly<BlockHeader>,
): Promise<DeepReadonly<PoolState> | null> {
const newState = await super.processBlockLogs(state, logs, blockHeader);
if (newState && !newState.isValid) {
return await this.generateState(blockHeader.number);
}
return newState;
}
protected processLog(
state: DeepReadonly<PoolState>,
log: Readonly<Log>,
blockHeader: Readonly<BlockHeader>,
): DeepReadonly<PoolState> | null {
try {
const event = this.logDecoder(log);
const uniswapV3EventLoggingSampleRate =
this.dexHelper.config.data.uniswapV3EventLoggingSampleRate;
if (
!this.dexHelper.config.isSlave &&
uniswapV3EventLoggingSampleRate &&
isSampled(uniswapV3EventLoggingSampleRate)
) {
this.logger.info(
`event=${event.name} - block=${
blockHeader.number
}. Log sampled at rate ${uniswapV3EventLoggingSampleRate * 100}%`,
);
}
if (event.name in this.handlers) {
// Because we have observations in array which is mutable by nature, there is a
// ts compile error: https://stackoverflow.com/questions/53412934/disable-allowing-assigning-readonly-types-to-non-readonly-types
// And there is no good workaround, so turn off the type checker for this line
const _state = _.cloneDeep(state) as PoolState;
try {
return this.handlers[event.name](event, _state, log, blockHeader);
} catch (e) {
if (
e instanceof Error &&
e.message.endsWith(OUT_OF_RANGE_ERROR_POSTFIX)
) {
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,
);
} else {
this.logger.error(
`${this.parentName}: Pool ${this.poolAddress}, ` +
`network=${this.dexHelper.config.data.network}: Unexpected ` +
`error while handling event on blockNumber=${blockHeader.number}, ` +
`blockHash=${blockHeader.hash} and parentHash=${
blockHeader.parentHash
} for UniswapV3, ${JSON.stringify(event)}`,
e,
);
}
_state.isValid = false;
return _state;
}
}
} catch (e) {
catchParseLogError(e, this.logger);
}
return null; // ignore unrecognized event
}
protected _getStateRequestCallData() {
if (!this._stateRequestCallData) {
const callData: MultiCallParams<
bigint | DecodedStateMultiCallResultWithRelativeBitmaps
>[] = [
{
target: this.token0,
callData: this.erc20Interface.encodeFunctionData('balanceOf', [
this.poolAddress,
]),
decodeFunction: uint256ToBigInt,
},
{
target: this.token1,
callData: this.erc20Interface.encodeFunctionData('balanceOf', [
this.poolAddress,
]),
decodeFunction: uint256ToBigInt,
},
{
target: this.stateMultiContract.options.address,
callData: this.stateMultiContract.methods
.getFullStateWithRelativeBitmaps(
this.factoryAddress,
this.token0,
this.token1,
this.feeCode,
this.getBitmapRangeToRequest(),
this.getBitmapRangeToRequest(),
)
.encodeABI(),
decodeFunction:
this.decodeStateMultiCallResultWithRelativeBitmaps !== undefined
? this.decodeStateMultiCallResultWithRelativeBitmaps
: decodeStateMultiCallResultWithRelativeBitmaps,
},
];
this._stateRequestCallData = callData;
}
return this._stateRequestCallData;
}
getBitmapRangeToRequest() {
return TICK_BITMAP_TO_USE + TICK_BITMAP_BUFFER;
}
async checkState(
blockNumber: number,
): Promise<DeepReadonly<PoolState> | null> {
const state = this.getState(blockNumber);
if (state) {
return state;
}
this.logger.error(
`UniV3: No state found for ${this.name} ${this.addressesSubscribed[0]} for bn: ${blockNumber}`,
);
return null;
}
_setState(state: any, blockNumber: number, reason?: string): void {
if (this.parentName === 'UniswapV3') {
this.logger.info(
`UniV3: Setting state: '${!!state ? 'non-empty' : 'empty'}' for '${
this.name
}' for bn: '${blockNumber}' due to reason: '${
reason ?? 'outside_of_event_subscriber'
}'`,
);
}
super._setState(state, blockNumber);
}
async generateState(blockNumber: number): Promise<Readonly<PoolState>> {
const callData = this._getStateRequestCallData();
const [resBalance0, resBalance1, resState] =
await this.dexHelper.multiWrapper.tryAggregate<
bigint | DecodedStateMultiCallResultWithRelativeBitmaps
>(
false,
callData,
blockNumber,
this.dexHelper.multiWrapper.defaultBatchSize,
false,
);
// Quite ugly solution, but this is the one that fits to current flow.
// I think UniswapV3 callbacks subscriptions are complexified for no reason.
// Need to be revisited later
assert(resState.success, 'Pool does not exist');
const [balance0, balance1, _state] = [
resBalance0.returnData,
resBalance1.returnData,
resState.returnData,
] as [bigint, bigint, DecodedStateMultiCallResultWithRelativeBitmaps];
const tickBitmap = {};
const ticks = {};
_reduceTickBitmap(tickBitmap, _state.tickBitmap);
_reduceTicks(ticks, _state.ticks);
const observations = {
[_state.slot0.observationIndex]: {
blockTimestamp: bigIntify(_state.observation.blockTimestamp),
tickCumulative: bigIntify(_state.observation.tickCumulative),
secondsPerLiquidityCumulativeX128: bigIntify(
_state.observation.secondsPerLiquidityCumulativeX128,
),
initialized: _state.observation.initialized,
},
};
const currentTick = bigIntify(_state.slot0.tick);
const tickSpacing = bigIntify(_state.tickSpacing);
const startTickBitmap = TickBitMap.position(currentTick / tickSpacing)[0];
const requestedRange = this.getBitmapRangeToRequest();
return {
pool: _state.pool,
blockTimestamp: bigIntify(_state.blockTimestamp),
slot0: {
sqrtPriceX96: bigIntify(_state.slot0.sqrtPriceX96),
tick: currentTick,
observationIndex: +_state.slot0.observationIndex,
observationCardinality: +_state.slot0.observationCardinality,
observationCardinalityNext: +_state.slot0.observationCardinalityNext,
feeProtocol: bigIntify(_state.slot0.feeProtocol),
},
liquidity: bigIntify(_state.liquidity),
fee: this.feeCode,
tickSpacing,
maxLiquidityPerTick: bigIntify(_state.maxLiquidityPerTick),
tickBitmap,
ticks,
observations,
isValid: true,
startTickBitmap,
lowestKnownTick:
(BigInt.asIntN(24, startTickBitmap - requestedRange) << 8n) *
tickSpacing,
highestKnownTick:
((BigInt.asIntN(24, startTickBitmap + requestedRange) << 8n) +
BigInt.asIntN(24, 255n)) *
tickSpacing,
balance0,
balance1,
};
}
handleSwapEvent(
event: any,
pool: PoolState,
log: Log,
blockHeader: BlockHeader,
) {
const newSqrtPriceX96 = bigIntify(event.args.sqrtPriceX96);
const amount0 = bigIntify(event.args.amount0);
const amount1 = bigIntify(event.args.amount1);
const newTick = bigIntify(event.args.tick);
const newLiquidity = bigIntify(event.args.liquidity);
pool.blockTimestamp = bigIntify(blockHeader.timestamp);
if (amount0 <= 0n && amount1 <= 0n) {
this.logger.error(
`${this.parentName}: amount0 <= 0n && amount1 <= 0n for ` +
`${this.poolAddress} and ${blockHeader.number}. Check why it happened`,
);
pool.isValid = false;
return pool;
} else {
const zeroForOne = amount0 > 0n;
uniswapV3Math.swapFromEvent(
pool,
newSqrtPriceX96,
newTick,
newLiquidity,
zeroForOne,
);
if (zeroForOne) {
if (amount1 < 0n) {
pool.balance1 -= BigInt.asUintN(256, -amount1);
} else {
this.logger.error(
`In swapEvent for pool ${pool.pool} received incorrect values ${zeroForOne} and ${amount1}`,
);
pool.isValid = false;
}
// This is not correct fully, because pool may get more tokens then it needs, but
// it is not accounted in internal state, it should be good enough
pool.balance0 += BigInt.asUintN(256, amount0);
} else {
if (amount0 < 0n) {
pool.balance0 -= BigInt.asUintN(256, -amount0);
} else {
this.logger.error(
`In swapEvent for pool ${pool.pool} received incorrect values ${zeroForOne} and ${amount0}`,
);
pool.isValid = false;
}
pool.balance1 += BigInt.asUintN(256, amount1);
}
return pool;
}
}
handleBurnEvent(
event: any,
pool: PoolState,
log: Log,
blockHeader: BlockHeader,
) {
const amount = bigIntify(event.args.amount);
const tickLower = bigIntify(event.args.tickLower);
const tickUpper = bigIntify(event.args.tickUpper);
pool.blockTimestamp = bigIntify(blockHeader.timestamp);
uniswapV3Math._modifyPosition(pool, {
tickLower,
tickUpper,
liquidityDelta: -BigInt.asIntN(128, BigInt.asIntN(256, amount)),
});
// From this transaction I conclude that there is no balance change from
// Burn event: https://dashboard.tenderly.co/tx/mainnet/0xfccf5341147ac3ad0e66452273d12dfc3219e81f8fb369a6cdecfb24b9b9d078/logs
// And it aligns with UniswapV3 doc:
// https://github.com/Uniswap/v3-core/blob/05c10bf6d547d6121622ac51c457f93775e1df09/contracts/interfaces/pool/IUniswapV3PoolActions.sol#L59
// It just updates positions and tokensOwed which may be requested calling collect
// So, we don't need to update pool.balances0 and pool.balances1 here
return pool;
}
handleMintEvent(
event: any,
pool: PoolState,
log: Log,
blockHeader: BlockHeader,
) {
const amount = bigIntify(event.args.amount);
const tickLower = bigIntify(event.args.tickLower);
const tickUpper = bigIntify(event.args.tickUpper);
const amount0 = bigIntify(event.args.amount0);
const amount1 = bigIntify(event.args.amount1);
pool.blockTimestamp = bigIntify(blockHeader.timestamp);
uniswapV3Math._modifyPosition(pool, {
tickLower,
tickUpper,
liquidityDelta: amount,
});
pool.balance0 += amount0;
pool.balance1 += amount1;
return pool;
}
handleSetFeeProtocolEvent(
event: any,
pool: PoolState,
log: Log,
blockHeader: BlockHeader,
) {
const feeProtocol0 = bigIntify(event.args.feeProtocol0New);
const feeProtocol1 = bigIntify(event.args.feeProtocol1New);
pool.slot0.feeProtocol = feeProtocol0 + (feeProtocol1 << 4n);
pool.blockTimestamp = bigIntify(blockHeader.timestamp);
return pool;
}
handleCollectEvent(
event: any,
pool: PoolState,
log: Log,
blockHeader: BlockHeader,
) {
const amount0 = bigIntify(event.args.amount0);
const amount1 = bigIntify(event.args.amount1);
pool.balance0 -= amount0;
pool.balance1 -= amount1;
pool.blockTimestamp = bigIntify(blockHeader.timestamp);
return pool;
}
handleFlashEvent(
event: any,
pool: PoolState,
log: Log,
blockHeader: BlockHeader,
) {
const paid0 = bigIntify(event.args.paid0);
const paid1 = bigIntify(event.args.paid1);
pool.balance0 += paid0;
pool.balance1 += paid1;
pool.blockTimestamp = bigIntify(blockHeader.timestamp);
return pool;
}
handleIncreaseObservationCardinalityNextEvent(
event: any,
pool: PoolState,
log: Log,
blockHeader: BlockHeader,
) {
pool.slot0.observationCardinalityNext = parseInt(
event.args.observationCardinalityNextNew,
10,
);
pool.blockTimestamp = bigIntify(blockHeader.timestamp);
return pool;
}
protected _computePoolAddress(
token0: Address,
token1: Address,
fee: bigint,
): Address {
// https://github.com/Uniswap/v3-periphery/blob/main/contracts/libraries/PoolAddress.sol
if (token0 > token1) [token0, token1] = [token1, token0];
const encodedKey = ethers.utils.keccak256(
ethers.utils.defaultAbiCoder.encode(
['address', 'address', 'uint24'],
[token0, token1, BigInt.asUintN(24, fee)],
),
);
return ethers.utils.getCreate2Address(
this.factoryAddress,
encodedKey,
this.poolInitCodeHash,
);
}
}