Skip to content

Commit

Permalink
Add indexing of token properties in erc20-watcher and fix smoke-tests (
Browse files Browse the repository at this point in the history
…#4)

* Add indexing of token properties in erc20-watcher

* Fix uni-info-watcher smoke-test
  • Loading branch information
prathamesh0 authored Feb 11, 2022
1 parent 6c9cce4 commit 6b8c73d
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 35 deletions.
47 changes: 47 additions & 0 deletions packages/erc20-watcher/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { Database as BaseDatabase, QueryOptions, Where } from '@vulcanize/util';

import { Allowance } from './entity/Allowance';
import { Balance } from './entity/Balance';
import { Name } from './entity/Name';
import { Symbol } from './entity/Symbol';
import { Decimals } from './entity/Decimals';
import { Contract } from './entity/Contract';
import { Event } from './entity/Event';
import { SyncStatus } from './entity/SyncStatus';
Expand Down Expand Up @@ -62,6 +65,31 @@ export class Database {
.getOne();
}

async getName ({ blockHash, token }: { blockHash: string, token: string }): Promise<Name | undefined> {
return this._conn.getRepository(Name)
.findOne({
blockHash,
token
});
}

// eslint-disable-next-line @typescript-eslint/ban-types
async getSymbol ({ blockHash, token }: { blockHash: string, token: string }): Promise<Symbol | undefined> {
return this._conn.getRepository(Symbol)
.findOne({
blockHash,
token
});
}

async getDecimals ({ blockHash, token }: { blockHash: string, token: string }): Promise<Decimals | undefined> {
return this._conn.getRepository(Decimals)
.findOne({
blockHash,
token
});
}

async saveBalance ({ blockHash, blockNumber, token, owner, value, proof }: DeepPartial<Balance>): Promise<Balance> {
const repo = this._conn.getRepository(Balance);
const entity = repo.create({ blockHash, blockNumber, token, owner, value, proof });
Expand All @@ -74,6 +102,25 @@ export class Database {
return repo.save(entity);
}

async saveName ({ blockHash, blockNumber, token, value, proof }: DeepPartial<Name>): Promise<Name> {
const repo = this._conn.getRepository(Name);
const entity = repo.create({ blockHash, blockNumber, token, value, proof });
return repo.save(entity);
}

// eslint-disable-next-line @typescript-eslint/ban-types
async saveSymbol ({ blockHash, blockNumber, token, value, proof }: DeepPartial<Symbol>): Promise<Symbol> {
const repo = this._conn.getRepository(Symbol);
const entity = repo.create({ blockHash, blockNumber, token, value, proof });
return repo.save(entity);
}

async saveDecimals ({ blockHash, blockNumber, token, value, proof }: DeepPartial<Decimals>): Promise<Decimals> {
const repo = this._conn.getRepository(Decimals);
const entity = repo.create({ blockHash, blockNumber, token, value, proof });
return repo.save(entity);
}

async getContracts (): Promise<Contract[]> {
const repo = this._conn.getRepository(Contract);

Expand Down
2 changes: 1 addition & 1 deletion packages/erc20-watcher/src/entity/Allowance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Entity, PrimaryGeneratedColumn, Column, Index } from 'typeorm';
import { bigintTransformer } from '@vulcanize/util';

@Entity()
@Index(['blockHash', 'blockNumber', 'token', 'owner', 'spender'], { unique: true })
@Index(['blockHash', 'token', 'owner', 'spender'], { unique: true })
export class Allowance {
@PrimaryGeneratedColumn()
id!: number;
Expand Down
2 changes: 1 addition & 1 deletion packages/erc20-watcher/src/entity/Balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Entity, PrimaryGeneratedColumn, Column, Index } from 'typeorm';
import { bigintTransformer } from '@vulcanize/util';

@Entity()
@Index(['blockHash', 'blockNumber', 'token', 'owner'], { unique: true })
@Index(['blockHash', 'token', 'owner'], { unique: true })
export class Balance {
@PrimaryGeneratedColumn()
id!: number;
Expand Down
27 changes: 27 additions & 0 deletions packages/erc20-watcher/src/entity/Decimals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Copyright 2022 Vulcanize, Inc.
//

import { Entity, PrimaryGeneratedColumn, Column, Index } from 'typeorm';

@Entity()
@Index(['blockHash', 'token'], { unique: true })
export class Decimals {
@PrimaryGeneratedColumn()
id!: number;

@Column('varchar', { length: 66 })
blockHash!: string;

@Column('integer')
blockNumber!: number;

@Column('varchar', { length: 42 })
token!: string;

@Column('integer')
value!: number;

@Column('text', { nullable: true })
proof!: string;
}
27 changes: 27 additions & 0 deletions packages/erc20-watcher/src/entity/Name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Copyright 2022 Vulcanize, Inc.
//

import { Entity, PrimaryGeneratedColumn, Column, Index } from 'typeorm';

@Entity()
@Index(['blockHash', 'token'], { unique: true })
export class Name {
@PrimaryGeneratedColumn()
id!: number;

@Column('varchar', { length: 66 })
blockHash!: string;

@Column('integer')
blockNumber!: number;

@Column('varchar', { length: 42 })
token!: string;

@Column('varchar')
value!: string;

@Column('text', { nullable: true })
proof!: string;
}
27 changes: 27 additions & 0 deletions packages/erc20-watcher/src/entity/Symbol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Copyright 2022 Vulcanize, Inc.
//

import { Entity, PrimaryGeneratedColumn, Column, Index } from 'typeorm';

@Entity()
@Index(['blockHash', 'token'], { unique: true })
export class Symbol {
@PrimaryGeneratedColumn()
id!: number;

@Column('varchar', { length: 66 })
blockHash!: string;

@Column('integer')
blockNumber!: number;

@Column('varchar', { length: 42 })
token!: string;

@Column('varchar')
value!: string;

@Column('text', { nullable: true })
proof!: string;
}
45 changes: 43 additions & 2 deletions packages/erc20-watcher/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,21 @@ export class Indexer {
}

async name (blockHash: string, token: string): Promise<ValueResult> {
const entity = await this._db.getName({ blockHash, token });
if (entity) {
log('name: db hit.');

return {
value: entity.value,
proof: JSON.parse(entity.proof)
};
}

log('name: db miss, fetching from upstream server');
let result: ValueResult;

const { block: { number: blockNumber } } = await this._ethClient.getBlockByHash(blockHash);

if (this._serverMode === ETH_CALL_MODE) {
const value = await fetchTokenName(this._ethProvider, blockHash, token);

Expand All @@ -190,14 +203,27 @@ export class Indexer {
result = await this._baseIndexer.getStorageValue(this._storageLayout, blockHash, token, '_name');
}

// log(JSONbig.stringify(result, null, 2));
await this._db.saveName({ blockHash, blockNumber, token, value: result.value, proof: JSONbig.stringify(result.proof) });

return result;
}

async symbol (blockHash: string, token: string): Promise<ValueResult> {
const entity = await this._db.getSymbol({ blockHash, token });
if (entity) {
log('symbol: db hit.');

return {
value: entity.value,
proof: JSON.parse(entity.proof)
};
}

log('symbol: db miss, fetching from upstream server');
let result: ValueResult;

const { block: { number: blockNumber } } = await this._ethClient.getBlockByHash(blockHash);

if (this._serverMode === ETH_CALL_MODE) {
const value = await fetchTokenSymbol(this._ethProvider, blockHash, token);

Expand All @@ -206,14 +232,27 @@ export class Indexer {
result = await this._baseIndexer.getStorageValue(this._storageLayout, blockHash, token, '_symbol');
}

// log(JSONbig.stringify(result, null, 2));
await this._db.saveSymbol({ blockHash, blockNumber, token, value: result.value, proof: JSONbig.stringify(result.proof) });

return result;
}

async decimals (blockHash: string, token: string): Promise<ValueResult> {
const entity = await this._db.getDecimals({ blockHash, token });
if (entity) {
log('decimals: db hit.');

return {
value: entity.value,
proof: JSON.parse(entity.proof)
};
}

log('decimals: db miss, fetching from upstream server');
let result: ValueResult;

const { block: { number: blockNumber } } = await this._ethClient.getBlockByHash(blockHash);

if (this._serverMode === ETH_CALL_MODE) {
const value = await fetchTokenDecimals(this._ethProvider, blockHash, token);

Expand All @@ -224,6 +263,8 @@ export class Indexer {
throw new Error('Not implemented.');
}

await this._db.saveDecimals({ blockHash, blockNumber, token, value: result.value, proof: JSONbig.stringify(result.proof) });

return result;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/uni-info-watcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"test": "mocha src/**/*.test.ts",
"test:gpev": "mocha src/get-prev-entity.test.ts",
"test:server": "mocha src/server.test.ts",
"test:init": "ts-node test/init.ts",
"build": "tsc",
"server": "DEBUG=vulcanize:* node --enable-source-maps dist/server.js",
"server:prof": "DEBUG=vulcanize:* node --require pprof --enable-source-maps dist/server.js",
Expand All @@ -36,7 +37,7 @@
"job-runner": "DEBUG=vulcanize:* node --enable-source-maps dist/job-runner.js",
"job-runner:prof": "DEBUG=vulcanize:* node --require pprof --enable-source-maps dist/job-runner.js",
"job-runner:dev": "DEBUG=vulcanize:* nodemon --watch src src/job-runner.ts",
"smoke-test": "mocha src/smoke.test.ts",
"smoke-test": "yarn test:init && mocha src/smoke.test.ts",
"fill": "DEBUG=vulcanize:* node --enable-source-maps dist/fill.js",
"fill:prof": "DEBUG=vulcanize:* node --require pprof --enable-source-maps dist/fill.js",
"fill:dev": "DEBUG=vulcanize:* ts-node src/fill.ts",
Expand Down
4 changes: 3 additions & 1 deletion packages/uni-info-watcher/src/job-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ export const main = async (): Promise<any> => {
const indexer = new Indexer(db, uniClient, erc20Client, ethClient, postgraphileClient, ethProvider, jobQueue, mode);
await indexer.init();

await indexer.addContracts();
if (mode !== 'demo') {
await indexer.addContracts();
}

const jobRunner = new JobRunner(jobQueueConfig, indexer, jobQueue);
await jobRunner.start();
Expand Down
Loading

0 comments on commit 6b8c73d

Please sign in to comment.