Implement event watchers for NonFungiblePositionManager events. (#141)

Co-authored-by: nabarun <nabarun@deepstacksoft.com>
This commit is contained in:
Ashwin Phatak 2021-07-15 17:27:10 +05:30 committed by GitHub
parent 70e88b1004
commit 29c8c1f80f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 81 additions and 20 deletions

View File

@ -51,6 +51,14 @@ Example:
$ yarn watch:contract --address 0xfE0034a874c2707c23F91D7409E9036F5e08ac34 --kind factory --startingBlock 100
```
Start watching the NonFungiblePositionManager contract:
Example:
```bash
$ yarn watch:contract --address 0xB171168C0df9457Ff3E3D795aE25Bf4f41e2FFE3 --kind nfpm --startingBlock 100
```
## Scripts
* `yarn server`

View File

@ -26,7 +26,7 @@ import { Database } from '../database';
type: 'string',
require: true,
demandOption: true,
describe: 'Kind of contract (factory|pool)'
describe: 'Kind of contract (factory|pool|nfpm)'
},
startingBlock: {
type: 'number',

View File

@ -2,6 +2,7 @@ import { Entity, PrimaryGeneratedColumn, Column, Index } from 'typeorm';
export const KIND_FACTORY = 'factory';
export const KIND_POOL = 'pool';
export const KIND_NFPM = 'nfpm';
@Entity()
@Index(['address'], { unique: true })

View File

@ -10,10 +10,11 @@ import { Config } from '@vulcanize/util';
import { Database } from './database';
import { Event, UNKNOWN_EVENT_NAME } from './entity/Event';
import { BlockProgress } from './entity/BlockProgress';
import { Contract, KIND_FACTORY, KIND_POOL } from './entity/Contract';
import { Contract, KIND_FACTORY, KIND_POOL, KIND_NFPM } from './entity/Contract';
import factoryABI from './artifacts/factory.json';
import poolABI from './artifacts/pool.json';
import nfpmABI from './artifacts/NonfungiblePositionManager.json';
// TODO: Move to config.
const MAX_EVENTS_BLOCK_RANGE = 1000;
@ -40,6 +41,7 @@ export class Indexer {
_factoryContract: ethers.utils.Interface
_poolContract: ethers.utils.Interface
_nfpmContract: ethers.utils.Interface
constructor (config: Config, db: Database, ethClient: EthClient) {
this._config = config;
@ -49,6 +51,7 @@ export class Indexer {
this._factoryContract = new ethers.utils.Interface(factoryABI);
this._poolContract = new ethers.utils.Interface(poolABI);
this._nfpmContract = new ethers.utils.Interface(nfpmABI);
}
getResultEvent (event: Event): ResultEvent {
@ -212,6 +215,64 @@ export class Indexer {
}
}
break;
}
case KIND_NFPM: {
const logDescription = this._nfpmContract.parseLog({ data, topics });
switch (logDescription.name) {
case 'IncreaseLiquidity': {
eventName = logDescription.name;
const { tokenId, liquidity, amount0, amount1 } = logDescription.args;
eventInfo = {
tokenId: tokenId.toString(),
liquidity: liquidity.toString(),
amount0: amount0.toString(),
amount1: amount1.toString()
};
break;
}
case 'DecreaseLiquidity': {
eventName = logDescription.name;
const { tokenId, liquidity, amount0, amount1 } = logDescription.args;
eventInfo = {
tokenId: tokenId.toString(),
liquidity: liquidity.toString(),
amount0: amount0.toString(),
amount1: amount1.toString()
};
break;
}
case 'Collect': {
eventName = logDescription.name;
const { tokenId, recipient, amount0, amount1 } = logDescription.args;
eventInfo = {
tokenId: tokenId.toString(),
recipient,
amount0: amount0.toString(),
amount1: amount1.toString()
};
break;
}
case 'Transfer': {
eventName = logDescription.name;
const { from, to, tokenId } = logDescription.args;
eventInfo = {
from,
to,
tokenId: tokenId.toString()
};
break;
}
}
break;
}
}

View File

@ -11,12 +11,6 @@ export const createResolvers = async (indexer: Indexer, eventWatcher: EventWatch
return {
BigInt: new BigInt('bigInt'),
ERC20Event: {
__resolveType () {
return null;
}
},
FactoryEvent: {
__resolveType () {
return null;

View File

@ -20,17 +20,6 @@ type ResultUInt256 {
proof: Proof
}
# ERC20 Events
# event Transfer(address indexed from, address indexed to, uint256 value);
type TransferEvent {
from: String!
to: String!
value: BigInt!
}
union ERC20Event = TransferEvent
# Factory Types
type ResultGetPool {
@ -104,8 +93,16 @@ type CollectEvent {
amount1: BigInt!
}
# ERC721 Event
# event Transfer(address indexed from, address indexed to, uint256 tokenId);
type TransferEvent {
from: String!
to: String!
tokenId: BigInt!
}
# All events emitted by the NonfungiblePositionManager contract.
union NonFungiblePositionManagerEvent = IncreaseLiquidityEvent | DecreaseLiquidityEvent | CollectEvent
union NonFungiblePositionManagerEvent = IncreaseLiquidityEvent | DecreaseLiquidityEvent | CollectEvent | TransferEvent
# Pool Events