Handle collect event (#158)

* Handle NFPM Collect event.

* Handle NFPM Transfer event.

Co-authored-by: nabarun <nabarun@deepstacksoft.com>
This commit is contained in:
Ashwin Phatak 2021-07-22 11:45:21 +05:30 committed by GitHub
parent 0d39363467
commit 34de3d51b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 9 deletions

View File

@ -69,6 +69,21 @@ export interface DecreaseLiquidityEvent {
amount1: bigint;
}
export interface CollectEvent {
__typename: 'CollectEvent';
tokenId: bigint;
recipient: string;
amount0: bigint;
amount1: bigint;
}
export interface TransferEvent {
__typename: 'TransferEvent';
from: string;
to: string;
tokenId: bigint;
}
export interface Block {
number: number;
hash: string;
@ -86,7 +101,7 @@ export interface ResultEvent {
tx: Transaction;
contract: string;
eventIndex: number;
event: PoolCreatedEvent | InitializeEvent | MintEvent | BurnEvent | SwapEvent | IncreaseLiquidityEvent | DecreaseLiquidityEvent;
event: PoolCreatedEvent | InitializeEvent | MintEvent | BurnEvent | SwapEvent | IncreaseLiquidityEvent | DecreaseLiquidityEvent | CollectEvent | TransferEvent;
proof: {
data: string;
}

View File

@ -15,7 +15,7 @@ import Decimal from 'decimal.js';
import { Position } from './entity/Position';
import { Database } from './database';
import { Event } from './entity/Event';
import { ResultEvent, Block, Transaction, PoolCreatedEvent, InitializeEvent, MintEvent, BurnEvent, SwapEvent, IncreaseLiquidityEvent, DecreaseLiquidityEvent } from './events';
import { ResultEvent, Block, Transaction, PoolCreatedEvent, InitializeEvent, MintEvent, BurnEvent, SwapEvent, IncreaseLiquidityEvent, DecreaseLiquidityEvent, CollectEvent, TransferEvent } from './events';
const log = debug('vulcanize:indexer');
@ -94,37 +94,47 @@ export class Indexer {
switch (eventType) {
case 'PoolCreatedEvent':
log('Factory PoolCreated event', contract);
this._handlePoolCreated(block, contract, tx, event as PoolCreatedEvent);
await this._handlePoolCreated(block, contract, tx, event as PoolCreatedEvent);
break;
case 'InitializeEvent':
log('Pool Initialize event', contract);
this._handleInitialize(block, contract, tx, event as InitializeEvent);
await this._handleInitialize(block, contract, tx, event as InitializeEvent);
break;
case 'MintEvent':
log('Pool Mint event', contract);
this._handleMint(block, contract, tx, event as MintEvent);
await this._handleMint(block, contract, tx, event as MintEvent);
break;
case 'BurnEvent':
log('Pool Burn event', contract);
this._handleBurn(block, contract, tx, event as BurnEvent);
await this._handleBurn(block, contract, tx, event as BurnEvent);
break;
case 'SwapEvent':
log('Pool Swap event', contract);
this._handleSwap(block, contract, tx, event as SwapEvent);
await this._handleSwap(block, contract, tx, event as SwapEvent);
break;
case 'IncreaseLiquidityEvent':
log('NFPM IncreaseLiquidity event', contract);
this._handleIncreaseLiquidity(block, contract, tx, event as IncreaseLiquidityEvent);
await this._handleIncreaseLiquidity(block, contract, tx, event as IncreaseLiquidityEvent);
break;
case 'DecreaseLiquidityEvent':
log('NFPM DecreaseLiquidity event', contract);
this._handleDecreaseLiquidity(block, contract, tx, event as DecreaseLiquidityEvent);
await this._handleDecreaseLiquidity(block, contract, tx, event as DecreaseLiquidityEvent);
break;
case 'CollectEvent':
log('NFPM Collect event', contract);
await this._handleCollect(block, contract, tx, event as CollectEvent);
break;
case 'TransferEvent':
log('NFPM Transfer event', contract);
await this._handleTransfer(block, contract, tx, event as TransferEvent);
break;
default:
@ -793,6 +803,47 @@ export class Indexer {
await this._savePositionSnapshot(position, block, tx);
}
async _handleCollect (block: Block, contractAddress: string, tx: Transaction, event: CollectEvent): Promise<void> {
const { number: blockNumber } = block;
let 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.collectedFeesToken0 = position.collectedFeesToken0.plus(amount0);
position.collectedFeesToken1 = position.collectedFeesToken1.plus(amount1);
position = await this._updateFeeVars(position, block, contractAddress, BigInt(event.tokenId));
await this._db.savePosition(position, blockNumber);
await this._savePositionSnapshot(position, block, tx);
}
async _handleTransfer (block: Block, contractAddress: string, tx: Transaction, event: TransferEvent): Promise<void> {
const position = await this._getPosition(block, contractAddress, tx, BigInt(event.tokenId));
// Position was not able to be fetched.
if (position === null) {
return;
}
position.owner = event.to;
await this._db.savePosition(position, block.number);
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;

View File

@ -72,6 +72,19 @@ const resultEvent = `
amount0
amount1
}
... on CollectEvent {
tokenId
recipient
amount0
amount1
}
... on TransferEvent {
from
to
tokenId
}
}
proof {