2021-07-02 10:56:32 +00:00
|
|
|
import assert from 'assert';
|
|
|
|
import debug from 'debug';
|
2021-07-06 11:25:11 +00:00
|
|
|
import { Client as UniClient } from '@vulcanize/uni-watcher';
|
2021-07-22 04:32:06 +00:00
|
|
|
import { JobQueue } from '../../util';
|
|
|
|
import { Indexer } from './indexer';
|
2021-07-02 10:56:32 +00:00
|
|
|
|
|
|
|
const log = debug('vulcanize:events');
|
|
|
|
|
2021-07-22 04:32:06 +00:00
|
|
|
export interface PoolCreatedEvent {
|
2021-07-13 12:02:57 +00:00
|
|
|
__typename: 'PoolCreatedEvent';
|
2021-07-06 11:25:11 +00:00
|
|
|
token0: string;
|
|
|
|
token1: string;
|
|
|
|
fee: bigint;
|
|
|
|
tickSpacing: bigint;
|
|
|
|
pool: string;
|
|
|
|
}
|
|
|
|
|
2021-07-22 04:32:06 +00:00
|
|
|
export interface InitializeEvent {
|
2021-07-13 12:02:57 +00:00
|
|
|
__typename: 'InitializeEvent';
|
2021-07-09 07:08:25 +00:00
|
|
|
sqrtPriceX96: bigint;
|
|
|
|
tick: bigint;
|
|
|
|
}
|
|
|
|
|
2021-07-22 04:32:06 +00:00
|
|
|
export interface MintEvent {
|
2021-07-13 12:02:57 +00:00
|
|
|
__typename: 'MintEvent';
|
2021-07-13 06:31:54 +00:00
|
|
|
sender: string;
|
|
|
|
owner: string;
|
|
|
|
tickLower: bigint;
|
|
|
|
tickUpper: bigint;
|
|
|
|
amount: bigint;
|
|
|
|
amount0: bigint;
|
|
|
|
amount1: bigint;
|
|
|
|
}
|
|
|
|
|
2021-07-22 04:32:06 +00:00
|
|
|
export interface BurnEvent {
|
2021-07-13 12:02:57 +00:00
|
|
|
__typename: 'BurnEvent';
|
2021-07-13 07:06:10 +00:00
|
|
|
owner: string;
|
|
|
|
tickLower: bigint;
|
|
|
|
tickUpper: bigint;
|
|
|
|
amount: bigint;
|
|
|
|
amount0: bigint;
|
|
|
|
amount1: bigint;
|
|
|
|
}
|
|
|
|
|
2021-07-22 04:32:06 +00:00
|
|
|
export interface SwapEvent {
|
2021-07-15 05:15:35 +00:00
|
|
|
__typename: 'SwapEvent';
|
|
|
|
sender: string;
|
|
|
|
recipient: string;
|
|
|
|
amount0: bigint;
|
|
|
|
amount1: bigint;
|
|
|
|
sqrtPriceX96: bigint;
|
|
|
|
liquidity: bigint;
|
|
|
|
tick: bigint;
|
|
|
|
}
|
|
|
|
|
2021-07-22 04:32:06 +00:00
|
|
|
export interface IncreaseLiquidityEvent {
|
2021-07-16 13:04:51 +00:00
|
|
|
__typename: 'IncreaseLiquidityEvent';
|
|
|
|
tokenId: bigint;
|
|
|
|
liquidity: bigint;
|
|
|
|
amount0: bigint;
|
|
|
|
amount1: bigint;
|
|
|
|
}
|
|
|
|
|
2021-07-22 04:32:06 +00:00
|
|
|
export interface DecreaseLiquidityEvent {
|
2021-07-19 05:13:29 +00:00
|
|
|
__typename: 'DecreaseLiquidityEvent';
|
|
|
|
tokenId: bigint;
|
|
|
|
liquidity: bigint;
|
|
|
|
amount0: bigint;
|
|
|
|
amount1: bigint;
|
|
|
|
}
|
|
|
|
|
2021-07-22 06:15:21 +00:00
|
|
|
export interface CollectEvent {
|
|
|
|
__typename: 'CollectEvent';
|
|
|
|
tokenId: bigint;
|
|
|
|
recipient: string;
|
|
|
|
amount0: bigint;
|
|
|
|
amount1: bigint;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface TransferEvent {
|
|
|
|
__typename: 'TransferEvent';
|
|
|
|
from: string;
|
|
|
|
to: string;
|
|
|
|
tokenId: bigint;
|
|
|
|
}
|
|
|
|
|
2021-07-22 04:32:06 +00:00
|
|
|
export interface Block {
|
2021-07-13 12:02:57 +00:00
|
|
|
number: number;
|
|
|
|
hash: string;
|
|
|
|
timestamp: number;
|
2021-07-22 04:32:06 +00:00
|
|
|
parentHash: string;
|
2021-07-13 12:02:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 04:32:06 +00:00
|
|
|
export interface Transaction {
|
2021-07-13 12:02:57 +00:00
|
|
|
hash: string;
|
2021-07-22 04:32:06 +00:00
|
|
|
from?: string;
|
2021-07-13 12:02:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 04:32:06 +00:00
|
|
|
export interface ResultEvent {
|
2021-07-13 12:02:57 +00:00
|
|
|
block: Block;
|
|
|
|
tx: Transaction;
|
|
|
|
contract: string;
|
2021-07-22 04:32:06 +00:00
|
|
|
eventIndex: number;
|
2021-07-22 06:15:21 +00:00
|
|
|
event: PoolCreatedEvent | InitializeEvent | MintEvent | BurnEvent | SwapEvent | IncreaseLiquidityEvent | DecreaseLiquidityEvent | CollectEvent | TransferEvent;
|
2021-07-06 11:25:11 +00:00
|
|
|
proof: {
|
2021-07-13 12:02:57 +00:00
|
|
|
data: string;
|
2021-07-06 11:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 04:32:06 +00:00
|
|
|
export const QUEUE_EVENT_PROCESSING = 'event-processing';
|
|
|
|
export const QUEUE_BLOCK_PROCESSING = 'block-processing';
|
|
|
|
|
2021-07-02 10:56:32 +00:00
|
|
|
export class EventWatcher {
|
2021-07-06 11:25:11 +00:00
|
|
|
_subscription?: ZenObservable.Subscription
|
|
|
|
_uniClient: UniClient
|
2021-07-22 04:32:06 +00:00
|
|
|
_jobQueue: JobQueue
|
|
|
|
_indexer: Indexer
|
2021-07-02 10:56:32 +00:00
|
|
|
|
2021-07-22 04:32:06 +00:00
|
|
|
constructor (indexer: Indexer, uniClient: UniClient, jobQueue: JobQueue) {
|
2021-07-06 11:25:11 +00:00
|
|
|
this._uniClient = uniClient;
|
2021-07-22 04:32:06 +00:00
|
|
|
this._jobQueue = jobQueue;
|
|
|
|
this._indexer = indexer;
|
2021-07-02 10:56:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async start (): Promise<void> {
|
|
|
|
assert(!this._subscription, 'subscription already started');
|
2021-07-06 11:25:11 +00:00
|
|
|
log('Started watching upstream events...');
|
2021-07-09 07:08:25 +00:00
|
|
|
|
2021-07-22 04:32:06 +00:00
|
|
|
this._jobQueue.onComplete(QUEUE_BLOCK_PROCESSING, async (job) => {
|
|
|
|
const { data: { request: { data: { block } } } } = job;
|
|
|
|
log(`Job onComplete block ${block.hash} ${block.number}`);
|
2021-07-13 06:31:54 +00:00
|
|
|
});
|
|
|
|
|
2021-07-22 04:32:06 +00:00
|
|
|
this._jobQueue.onComplete(QUEUE_EVENT_PROCESSING, async (job) => {
|
|
|
|
const { data: { request } } = job;
|
2021-07-13 07:06:10 +00:00
|
|
|
|
2021-07-22 04:32:06 +00:00
|
|
|
log(`Job onComplete event ${request.data.id}`);
|
2021-07-13 07:06:10 +00:00
|
|
|
});
|
|
|
|
|
2021-07-22 04:32:06 +00:00
|
|
|
this._subscription = await this._uniClient.watchEvents(async ({ block }: ResultEvent) => {
|
|
|
|
log('watchEvent', block.hash, block.number);
|
|
|
|
return this._jobQueue.pushJob(QUEUE_BLOCK_PROCESSING, { block });
|
2021-07-15 05:15:35 +00:00
|
|
|
});
|
2021-07-16 13:04:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 04:32:06 +00:00
|
|
|
async stop (): Promise<void> {
|
|
|
|
if (this._subscription) {
|
|
|
|
log('Stopped watching upstream events');
|
|
|
|
this._subscription.unsubscribe();
|
2021-07-16 13:04:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-02 10:56:32 +00:00
|
|
|
}
|