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-07 05:40:10 +00:00
|
|
|
import { BigNumber } from 'ethers';
|
2021-07-02 10:56:32 +00:00
|
|
|
|
2021-07-06 11:25:11 +00:00
|
|
|
import { Database } from './database';
|
2021-07-09 07:08:25 +00:00
|
|
|
import { findEthPerToken, getEthPriceInUSD, WHITELIST_TOKENS } from './utils/pricing';
|
|
|
|
import { updatePoolDayData, updatePoolHourData } from './utils/intervalUpdates';
|
|
|
|
import { Token } from './entity/Token';
|
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;
|
|
|
|
}
|
|
|
|
|
2021-07-09 07:08:25 +00:00
|
|
|
interface InitializeEvent {
|
|
|
|
sqrtPriceX96: bigint;
|
|
|
|
tick: bigint;
|
|
|
|
}
|
|
|
|
|
2021-07-06 11:25:11 +00:00
|
|
|
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':
|
2021-07-09 07:08:25 +00:00
|
|
|
log('Factory PoolCreated event', contract);
|
2021-07-06 11:25:11 +00:00
|
|
|
this._handlePoolCreated(blockHash, blockNumber, contract, eventValues as PoolCreatedEvent);
|
|
|
|
break;
|
|
|
|
|
2021-07-09 07:08:25 +00:00
|
|
|
case 'InitializeEvent':
|
|
|
|
log('Pool Initialize event', contract);
|
|
|
|
this._handleInitialize(blockHash, blockNumber, contract, eventValues as InitializeEvent);
|
|
|
|
break;
|
|
|
|
|
2021-07-06 11:25:11 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _handlePoolCreated (blockHash: string, blockNumber: number, contractAddress: string, poolCreatedEvent: PoolCreatedEvent): Promise<void> {
|
2021-07-07 05:40:10 +00:00
|
|
|
const { token0: token0Address, token1: token1Address, fee, pool: poolAddress } = poolCreatedEvent;
|
2021-07-06 11:25:11 +00:00
|
|
|
|
|
|
|
// Load factory.
|
|
|
|
const factory = await this._db.loadFactory({ blockNumber, id: contractAddress });
|
|
|
|
|
2021-07-07 05:40:10 +00:00
|
|
|
// Update Factory.
|
|
|
|
let factoryPoolCount = BigNumber.from(factory.poolCount);
|
|
|
|
factoryPoolCount = factoryPoolCount.add(1);
|
|
|
|
factory.poolCount = BigInt(factoryPoolCount.toHexString());
|
|
|
|
|
|
|
|
// Get Tokens.
|
|
|
|
let [token0, token1] = await Promise.all([
|
|
|
|
this._db.getToken({ blockNumber, id: token0Address }),
|
|
|
|
this._db.getToken({ blockNumber, id: token1Address })
|
|
|
|
]);
|
2021-07-06 11:25:11 +00:00
|
|
|
|
2021-07-07 05:40:10 +00:00
|
|
|
// Create Tokens if not present.
|
|
|
|
if (!token0) {
|
2021-07-09 07:08:25 +00:00
|
|
|
token0 = await this._createToken(blockHash, blockNumber, token0Address);
|
2021-07-07 05:40:10 +00:00
|
|
|
}
|
2021-07-06 11:25:11 +00:00
|
|
|
|
2021-07-07 05:40:10 +00:00
|
|
|
if (!token1) {
|
2021-07-09 07:08:25 +00:00
|
|
|
token1 = await this._createToken(blockHash, blockNumber, token1Address);
|
2021-07-07 05:40:10 +00:00
|
|
|
}
|
2021-07-06 11:25:11 +00:00
|
|
|
|
2021-07-07 05:40:10 +00:00
|
|
|
// Create new Pool entity.
|
|
|
|
// Skipping adding createdAtTimestamp field as it is not queried in frontend subgraph.
|
2021-07-09 07:08:25 +00:00
|
|
|
const pool = await this._db.loadPool({
|
2021-07-07 05:40:10 +00:00
|
|
|
blockNumber,
|
|
|
|
id: poolAddress,
|
|
|
|
token0: token0,
|
|
|
|
token1: token1,
|
|
|
|
feeTier: BigInt(fee)
|
|
|
|
});
|
|
|
|
|
2021-07-09 07:08:25 +00:00
|
|
|
// Update white listed pools.
|
|
|
|
if (WHITELIST_TOKENS.includes(token0.id)) {
|
|
|
|
token1.whitelistPools.push(pool);
|
|
|
|
await this._db.saveToken(token1, blockNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WHITELIST_TOKENS.includes(token1.id)) {
|
|
|
|
token0.whitelistPools.push(pool);
|
|
|
|
await this._db.saveToken(token0, blockNumber);
|
|
|
|
}
|
2021-07-07 05:40:10 +00:00
|
|
|
|
|
|
|
// Save entities to DB.
|
|
|
|
await this._db.saveFactory(factory, blockNumber);
|
2021-07-06 11:25:11 +00:00
|
|
|
}
|
2021-07-09 07:08:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create new Token.
|
|
|
|
* @param tokenAddress
|
|
|
|
*/
|
|
|
|
async _createToken (blockHash: string, blockNumber: number, tokenAddress: string): Promise<Token> {
|
|
|
|
const { value: symbol } = await this._erc20Client.getSymbol(blockHash, tokenAddress);
|
|
|
|
const { value: name } = await this._erc20Client.getName(blockHash, tokenAddress);
|
|
|
|
const { value: totalSupply } = await this._erc20Client.getTotalSupply(blockHash, tokenAddress);
|
|
|
|
|
|
|
|
// TODO: Decimals not implemented by erc20-watcher.
|
|
|
|
// const { value: decimals } = await this._erc20Client.getDecimals(blockHash, tokenAddress);
|
|
|
|
|
|
|
|
return this._db.loadToken({
|
|
|
|
blockNumber,
|
|
|
|
id: tokenAddress,
|
|
|
|
symbol,
|
|
|
|
name,
|
|
|
|
totalSupply
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async _handleInitialize (blockHash: string, blockNumber: number, contractAddress: string, initializeEvent: InitializeEvent): Promise<void> {
|
|
|
|
const { sqrtPriceX96, tick } = initializeEvent;
|
|
|
|
const pool = await this._db.getPool({ id: contractAddress, blockNumber });
|
|
|
|
assert(pool, `Pool ${contractAddress} not found.`);
|
|
|
|
|
|
|
|
// Update Pool.
|
|
|
|
pool.sqrtPrice = BigInt(sqrtPriceX96);
|
|
|
|
pool.tick = BigInt(tick);
|
|
|
|
this._db.savePool(pool, blockNumber);
|
|
|
|
|
|
|
|
// Update ETH price now that prices could have changed.
|
|
|
|
const bundle = await this._db.loadBundle({ id: '1', blockNumber });
|
|
|
|
bundle.ethPriceUSD = await getEthPriceInUSD(this._db);
|
|
|
|
this._db.saveBundle(bundle, blockNumber);
|
|
|
|
|
|
|
|
await updatePoolDayData(this._db, { contractAddress, blockNumber });
|
|
|
|
await updatePoolHourData(this._db, { contractAddress, blockNumber });
|
|
|
|
|
|
|
|
const [token0, token1] = await Promise.all([
|
|
|
|
this._db.getToken({ id: pool.token0.id, blockNumber }),
|
|
|
|
this._db.getToken({ id: pool.token1.id, blockNumber })
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert(token0 && token1, 'Pool tokens not found.');
|
|
|
|
|
|
|
|
// Update token prices.
|
|
|
|
token0.derivedETH = await findEthPerToken(token0);
|
|
|
|
token1.derivedETH = await findEthPerToken(token1);
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
this._db.saveToken(token0, blockNumber),
|
|
|
|
this._db.saveToken(token1, blockNumber)
|
|
|
|
]);
|
|
|
|
}
|
2021-07-02 10:56:32 +00:00
|
|
|
}
|