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';
|
|
|
|
import { Client as ERC20Client } from '@vulcanize/erc20-watcher';
|
2021-07-02 10:56:32 +00:00
|
|
|
|
2021-07-06 11:25:11 +00:00
|
|
|
import { Database } from './database';
|
2021-07-02 10:56:32 +00:00
|
|
|
|
|
|
|
const log = debug('vulcanize:events');
|
|
|
|
|
2021-07-06 11:25:11 +00:00
|
|
|
interface PoolCreatedEvent {
|
|
|
|
token0: string;
|
|
|
|
token1: string;
|
|
|
|
fee: bigint;
|
|
|
|
tickSpacing: bigint;
|
|
|
|
pool: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ResultEvent {
|
|
|
|
proof: {
|
|
|
|
data: string
|
|
|
|
}
|
|
|
|
event: {
|
|
|
|
__typename: string;
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-02 10:56:32 +00:00
|
|
|
export class EventWatcher {
|
2021-07-06 11:25:11 +00:00
|
|
|
_db: Database
|
|
|
|
_subscription?: ZenObservable.Subscription
|
|
|
|
_uniClient: UniClient
|
|
|
|
_erc20Client: ERC20Client
|
2021-07-02 10:56:32 +00:00
|
|
|
|
2021-07-06 11:25:11 +00:00
|
|
|
constructor (db: Database, uniClient: UniClient, erc20Client: ERC20Client) {
|
|
|
|
assert(db);
|
2021-07-02 10:56:32 +00:00
|
|
|
|
2021-07-06 11:25:11 +00:00
|
|
|
this._db = db;
|
|
|
|
this._uniClient = uniClient;
|
|
|
|
this._erc20Client = erc20Client;
|
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...');
|
|
|
|
this._subscription = await this._uniClient.watchEvents(this._handleEvents.bind(this));
|
2021-07-02 10:56:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async stop (): Promise<void> {
|
|
|
|
if (this._subscription) {
|
2021-07-06 11:25:11 +00:00
|
|
|
log('Stopped watching upstream events');
|
2021-07-02 10:56:32 +00:00
|
|
|
this._subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
2021-07-06 11:25:11 +00:00
|
|
|
|
|
|
|
async _handleEvents ({ blockHash, blockNumber, contract, event }: { blockHash: string, blockNumber: number, contract: string, event: ResultEvent}): Promise<void> {
|
|
|
|
// TODO: Process proof (proof.data) in event.
|
|
|
|
const { event: { __typename: eventType, ...eventValues } } = event;
|
|
|
|
|
|
|
|
switch (eventType) {
|
|
|
|
case 'PoolCreatedEvent':
|
|
|
|
this._handlePoolCreated(blockHash, blockNumber, contract, eventValues as PoolCreatedEvent);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _handlePoolCreated (blockHash: string, blockNumber: number, contractAddress: string, poolCreatedEvent: PoolCreatedEvent): Promise<void> {
|
|
|
|
const { token0: token0Address, token1: token1Address, fee, tickSpacing, pool: poolAddress } = poolCreatedEvent;
|
|
|
|
|
|
|
|
// Load factory.
|
|
|
|
const factory = await this._db.loadFactory({ blockNumber, id: contractAddress });
|
|
|
|
factory.poolCount = factory.poolCount + 1;
|
|
|
|
|
|
|
|
// Create new Pool entity.
|
|
|
|
const pool = this._db.loadPool({ blockNumber, id: poolAddress });
|
|
|
|
|
|
|
|
// TODO: Load Token entities.
|
|
|
|
const getTokenValues = async (tokenAddress: string) => {
|
|
|
|
const { value: symbol } = await this._erc20Client.getSymbol(blockHash, tokenAddress);
|
|
|
|
return { symbol };
|
|
|
|
};
|
|
|
|
|
|
|
|
const token0 = this._db.loadToken({ blockNumber, id: token0Address }, () => getTokenValues(token0Address));
|
|
|
|
const token1 = this._db.loadToken({ blockNumber, id: token1Address }, () => getTokenValues(token1Address));
|
|
|
|
|
|
|
|
// TODO: Update Token entities.
|
|
|
|
|
|
|
|
// TODO: Update Pool entity.
|
|
|
|
|
|
|
|
// TODO: Save entities to DB.
|
|
|
|
}
|
2021-07-02 10:56:32 +00:00
|
|
|
}
|