Skip to content

Commit

Permalink
Generate watcher with caching for multiple return type (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikugogoi committed Apr 27, 2023
1 parent 0bce143 commit f02afc0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
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

0 comments on commit f02afc0

Please sign in to comment.