From c1ef96ba2e7524a511d1ffe5bb86570d8565b0d5 Mon Sep 17 00:00:00 2001 From: Ashwin Phatak Date: Mon, 19 Jul 2021 10:43:29 +0530 Subject: [PATCH] Handle Position Manager DecreaseLiquidity event. (#148) Co-authored-by: nabarun --- packages/uni-info-watcher/src/events.ts | 47 +++++++++++++++++++++++-- packages/uni-watcher/src/client.ts | 7 ++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/packages/uni-info-watcher/src/events.ts b/packages/uni-info-watcher/src/events.ts index 9cb7cfb7..ad94359d 100644 --- a/packages/uni-info-watcher/src/events.ts +++ b/packages/uni-info-watcher/src/events.ts @@ -70,6 +70,14 @@ interface IncreaseLiquidityEvent { amount1: bigint; } +interface DecreaseLiquidityEvent { + __typename: 'DecreaseLiquidityEvent'; + tokenId: bigint; + liquidity: bigint; + amount0: bigint; + amount1: bigint; +} + interface Block { number: number; hash: string; @@ -85,7 +93,7 @@ interface ResultEvent { block: Block; tx: Transaction; contract: string; - event: PoolCreatedEvent | InitializeEvent | MintEvent | BurnEvent | SwapEvent | IncreaseLiquidityEvent; + event: PoolCreatedEvent | InitializeEvent | MintEvent | BurnEvent | SwapEvent | IncreaseLiquidityEvent | DecreaseLiquidityEvent; proof: { data: string; } @@ -153,6 +161,11 @@ export class EventWatcher { this._handleIncreaseLiquidity(block, contract, tx, event as IncreaseLiquidityEvent); break; + case 'DecreaseLiquidityEvent': + log('NFPM DecreaseLiquidity event', contract); + this._handleDecreaseLiquidity(block, contract, tx, event as DecreaseLiquidityEvent); + break; + default: break; } @@ -731,7 +744,7 @@ export class EventWatcher { return; } - // Temp fix. + // Temp fix from Subgraph mapping code. if (utils.getAddress(position.pool.id) === utils.getAddress('0x8fe8d9bb8eeba3ed688069c3d6b556c9ca258248')) { return; } @@ -753,6 +766,36 @@ export class EventWatcher { await this._savePositionSnapshot(position, block, tx); } + async _handleDecreaseLiquidity (block: Block, contractAddress: string, tx: Transaction, event: DecreaseLiquidityEvent): Promise { + const { number: blockNumber } = block; + const position = await this._getPosition(block, contractAddress, tx, BigInt(event.tokenId)); + + // Position was not able to be fetched. + if (position == null) { + return; + } + + // Temp fix from Subgraph mapping code. + if (utils.getAddress(position.pool.id) === utils.getAddress('0x8fe8d9bb8eeba3ed688069c3d6b556c9ca258248')) { + return; + } + + const token0 = position.token0; + const token1 = position.token1; + const amount0 = convertTokenToDecimal(BigInt(event.amount0), BigInt(token0.decimals)); + const amount1 = convertTokenToDecimal(BigInt(event.amount1), BigInt(token1.decimals)); + + position.liquidity = BigInt(position.liquidity) - BigInt(event.liquidity); + position.depositedToken0 = position.depositedToken0.plus(amount0); + position.depositedToken1 = position.depositedToken1.plus(amount1); + + await this._updateFeeVars(position, block, contractAddress, BigInt(event.tokenId)); + + await this._db.savePosition(position, blockNumber); + + await this._savePositionSnapshot(position, block, tx); + } + async _getPosition (block: Block, contractAddress: string, tx: Transaction, tokenId: bigint): Promise { const { number: blockNumber, hash: blockHash, timestamp: blockTimestamp } = block; const { hash: txHash } = tx; diff --git a/packages/uni-watcher/src/client.ts b/packages/uni-watcher/src/client.ts index 26ab3306..14322d13 100644 --- a/packages/uni-watcher/src/client.ts +++ b/packages/uni-watcher/src/client.ts @@ -81,6 +81,13 @@ export class Client { amount0 amount1 } + + ... on DecreaseLiquidityEvent { + tokenId + liquidity + amount0 + amount1 + } } } }