Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate watcher with DB caching for multiple return type #16

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/azimuth-watcher/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,9 @@ export class Database implements DatabaseInterface {
return repo.save(entity);
}

async saveGetKeys ({ blockHash, blockNumber, contractAddress, _point, value, proof }: DeepPartial<GetKeys>): Promise<GetKeys> {
async saveGetKeys ({ blockHash, blockNumber, contractAddress, _point, value0, value1, value2, value3, proof }: DeepPartial<GetKeys>): Promise<GetKeys> {
const repo = this._conn.getRepository(GetKeys);
const entity = repo.create({ blockHash, blockNumber, contractAddress, _point, value, proof });
const entity = repo.create({ blockHash, blockNumber, contractAddress, _point, value0, value1, value2, value3, proof });
return repo.save(entity);
}

Expand Down
11 changes: 10 additions & 1 deletion packages/azimuth-watcher/src/entity/GetKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@ export class GetKeys {
_point!: bigint;

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

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

@Column('numeric', { transformer: bigintTransformer })
value2!: bigint;

@Column('numeric', { transformer: bigintTransformer })
value3!: bigint;

@Column('text', { nullable: true })
proof!: string;
Expand Down
20 changes: 20 additions & 0 deletions packages/azimuth-watcher/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@ export class Indexer implements IndexerInterface {
}

async getKeys (blockHash: string, contractAddress: string, _point: bigint): Promise<ValueResult> {
const entity = await this._db.getGetKeys({ blockHash, contractAddress, _point });
if (entity) {
log('getKeys: db hit.');

return {
value: {
value0: entity.value0,
value1: entity.value1,
value2: entity.value2,
value3: entity.value3
},
proof: JSON.parse(entity.proof)
};
}

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

log('getKeys: db miss, fetching from upstream server');

const abi = this._abiMap.get(KIND_AZIMUTH);
Expand All @@ -141,6 +159,8 @@ export class Indexer implements IndexerInterface {

const result: ValueResult = { value };

await this._db.saveGetKeys({ blockHash, blockNumber, contractAddress, _point, value0: value.value0, value1: value.value1, value2: value.value2, value3: value.value3, proof: JSONbigNative.stringify(result.proof) });

return result;
}

Expand Down