Skip to content

Commit

Permalink
Handle Position Manager DecreaseLiquidity event. (#148)
Browse files Browse the repository at this point in the history
Co-authored-by: nabarun <[email protected]>
  • Loading branch information
ashwinphatak and nikugogoi authored Jul 19, 2021
1 parent 3477366 commit c1ef96b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
47 changes: 45 additions & 2 deletions packages/uni-info-watcher/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -753,6 +766,36 @@ export class EventWatcher {
await this._savePositionSnapshot(position, block, tx);
}

async _handleDecreaseLiquidity (block: Block, contractAddress: string, tx: Transaction, event: DecreaseLiquidityEvent): Promise<void> {
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<Position | null> {
const { number: blockNumber, hash: blockHash, timestamp: blockTimestamp } = block;
const { hash: txHash } = tx;
Expand Down
7 changes: 7 additions & 0 deletions packages/uni-watcher/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ export class Client {
amount0
amount1
}
... on DecreaseLiquidityEvent {
tokenId
liquidity
amount0
amount1
}
}
}
}
Expand Down

0 comments on commit c1ef96b

Please sign in to comment.