From 2517f110eab75904d46e5a1351ce920c6f78f3ce Mon Sep 17 00:00:00 2001 From: prathamesh0 <42446521+prathamesh0@users.noreply.github.com> Date: Thu, 3 Nov 2022 03:44:58 -0500 Subject: [PATCH] Update GQL and getBlock API usage (#210) * Update getLogs and getBlockWithTransactions usage to add block number arg * Update getFullTransaction usage to add block number arg --- .../codegen/src/templates/indexer-template.handlebars | 3 ++- packages/eden-watcher/src/indexer.ts | 3 ++- packages/erc20-watcher/src/indexer.ts | 3 ++- packages/erc721-watcher/src/indexer.ts | 3 ++- packages/graph-node/src/watcher.ts | 8 ++++---- packages/graph-test-watcher/src/indexer.ts | 3 ++- packages/ipld-eth-client/src/eth-client.ts | 10 ++++++---- packages/ipld-eth-client/src/eth-queries.ts | 8 ++++---- packages/mobymask-watcher/src/hooks.ts | 2 +- packages/mobymask-watcher/src/indexer.ts | 7 ++++--- packages/util/src/indexer.ts | 7 ++++--- packages/util/src/misc.ts | 4 ++-- 12 files changed, 35 insertions(+), 26 deletions(-) diff --git a/packages/codegen/src/templates/indexer-template.handlebars b/packages/codegen/src/templates/indexer-template.handlebars index dd9857df..d8636a2f 100644 --- a/packages/codegen/src/templates/indexer-template.handlebars +++ b/packages/codegen/src/templates/indexer-template.handlebars @@ -656,8 +656,9 @@ export class Indexer implements IndexerInterface { parentHash }: DeepPartial): Promise<[BlockProgress, DeepPartial[]]> { assert(blockHash); + assert(blockNumber); - const dbEvents = await this._baseIndexer.fetchEvents(blockHash, this.parseEventNameAndArgs.bind(this)); + const dbEvents = await this._baseIndexer.fetchEvents(blockHash, blockNumber, this.parseEventNameAndArgs.bind(this)); const dbTx = await this._db.createTransactionRunner(); try { diff --git a/packages/eden-watcher/src/indexer.ts b/packages/eden-watcher/src/indexer.ts index e8f19f52..8ee2862a 100644 --- a/packages/eden-watcher/src/indexer.ts +++ b/packages/eden-watcher/src/indexer.ts @@ -893,8 +893,9 @@ export class Indexer implements IndexerInterface { parentHash }: DeepPartial): Promise<[BlockProgress, DeepPartial[]]> { assert(blockHash); + assert(blockNumber); - const dbEvents = await this._baseIndexer.fetchEvents(blockHash, this.parseEventNameAndArgs.bind(this)); + const dbEvents = await this._baseIndexer.fetchEvents(blockHash, blockNumber, this.parseEventNameAndArgs.bind(this)); const dbTx = await this._db.createTransactionRunner(); try { diff --git a/packages/erc20-watcher/src/indexer.ts b/packages/erc20-watcher/src/indexer.ts index d8769943..f2b0759b 100644 --- a/packages/erc20-watcher/src/indexer.ts +++ b/packages/erc20-watcher/src/indexer.ts @@ -442,8 +442,9 @@ export class Indexer implements IndexerInterface { parentHash }: DeepPartial): Promise<[BlockProgress, DeepPartial[]]> { assert(blockHash); + assert(blockNumber); - const dbEvents = await this._baseIndexer.fetchEvents(blockHash, this.parseEventNameAndArgs.bind(this)); + const dbEvents = await this._baseIndexer.fetchEvents(blockHash, blockNumber, this.parseEventNameAndArgs.bind(this)); const dbTx = await this._db.createTransactionRunner(); try { diff --git a/packages/erc721-watcher/src/indexer.ts b/packages/erc721-watcher/src/indexer.ts index 00e79411..d941dc4e 100644 --- a/packages/erc721-watcher/src/indexer.ts +++ b/packages/erc721-watcher/src/indexer.ts @@ -890,8 +890,9 @@ export class Indexer implements IndexerInterface { parentHash }: DeepPartial): Promise<[BlockProgress, DeepPartial[]]> { assert(blockHash); + assert(blockNumber); - const dbEvents = await this._baseIndexer.fetchEvents(blockHash, this.parseEventNameAndArgs.bind(this)); + const dbEvents = await this._baseIndexer.fetchEvents(blockHash, blockNumber, this.parseEventNameAndArgs.bind(this)); const dbTx = await this._db.createTransactionRunner(); try { diff --git a/packages/graph-node/src/watcher.ts b/packages/graph-node/src/watcher.ts index bc01c053..1309ec09 100644 --- a/packages/graph-node/src/watcher.ts +++ b/packages/graph-node/src/watcher.ts @@ -138,7 +138,7 @@ export class GraphWatcher { assert(blockData); assert(this._indexer && this._indexer.isWatchedContract); - const watchedContract = await this._indexer.isWatchedContract(contract); + const watchedContract = this._indexer.isWatchedContract(contract); assert(watchedContract); // Get dataSource in subgraph yaml based on contract address. @@ -174,7 +174,7 @@ export class GraphWatcher { const eventFragment = contractInterface.getEvent(eventSignature); - const tx = await this._getTransactionData(txHash); + const tx = await this._getTransactionData(txHash, Number(blockData.blockNumber)); const data = { block: blockData, @@ -409,14 +409,14 @@ export class GraphWatcher { } } - async _getTransactionData (txHash: string): Promise { + async _getTransactionData (txHash: string, blockNumber: number): Promise { let transaction = this._transactionsMap.get(txHash); if (transaction) { return transaction; } - transaction = await getFullTransaction(this._ethClient, txHash); + transaction = await getFullTransaction(this._ethClient, txHash, blockNumber); assert(transaction); this._transactionsMap.set(txHash, transaction); diff --git a/packages/graph-test-watcher/src/indexer.ts b/packages/graph-test-watcher/src/indexer.ts index fcf968ee..a2b551a9 100644 --- a/packages/graph-test-watcher/src/indexer.ts +++ b/packages/graph-test-watcher/src/indexer.ts @@ -598,8 +598,9 @@ export class Indexer implements IndexerInterface { parentHash }: DeepPartial): Promise<[BlockProgress, DeepPartial[]]> { assert(blockHash); + assert(blockNumber); - const dbEvents = await this._baseIndexer.fetchEvents(blockHash, this.parseEventNameAndArgs.bind(this)); + const dbEvents = await this._baseIndexer.fetchEvents(blockHash, blockNumber, this.parseEventNameAndArgs.bind(this)); const dbTx = await this._db.createTransactionRunner(); try { diff --git a/packages/ipld-eth-client/src/eth-client.ts b/packages/ipld-eth-client/src/eth-client.ts index a3c047af..e5006af3 100644 --- a/packages/ipld-eth-client/src/eth-client.ts +++ b/packages/ipld-eth-client/src/eth-client.ts @@ -16,6 +16,7 @@ export interface Config extends GraphQLConfig { interface Vars { blockHash: string; + blockNumber?: string; contract?: string; slot?: string; addresses?: string[]; @@ -106,15 +107,16 @@ export class EthClient { return result; } - async getFullTransaction (txHash: string): Promise { - console.time(`time:eth-client#getFullTransaction-${txHash}`); + async getFullTransaction (txHash: string, blockNumber?: number): Promise { + console.time(`time:eth-client#getFullTransaction-${JSON.stringify({ txHash, blockNumber })}`); const result = this._graphqlClient.query( ethQueries.getFullTransaction, { - txHash + txHash, + blockNumber: blockNumber?.toString() } ); - console.timeEnd(`time:eth-client#getFullTransaction-${txHash}`); + console.timeEnd(`time:eth-client#getFullTransaction-${JSON.stringify({ txHash, blockNumber })}`); return result; } diff --git a/packages/ipld-eth-client/src/eth-queries.ts b/packages/ipld-eth-client/src/eth-queries.ts index 334ae56e..a54b67ee 100644 --- a/packages/ipld-eth-client/src/eth-queries.ts +++ b/packages/ipld-eth-client/src/eth-queries.ts @@ -15,8 +15,8 @@ query getStorageAt($blockHash: Bytes32!, $contract: Address!, $slot: Bytes32!) { `; export const getLogs = gql` -query getLogs($blockHash: Bytes32!, $addresses: [Address!]) { - getLogs(blockHash: $blockHash, addresses: $addresses) { +query getLogs($blockHash: Bytes32!, $blockNumber: BigInt, $addresses: [Address!]) { + getLogs(blockHash: $blockHash, blockNumber: $blockNumber, addresses: $addresses) { account { address } @@ -100,8 +100,8 @@ query allEthHeaderCids($blockNumber: BigInt, $blockHash: String) { `; export const getFullTransaction = gql` -query ethTransactionCidByTxHash($txHash: String!) { - ethTransactionCidByTxHash(txHash: $txHash) { +query ethTransactionCidByTxHash($txHash: String!, $blockNumber: BigInt) { + ethTransactionCidByTxHash(txHash: $txHash, blockNumber: $blockNumber) { cid txHash index diff --git a/packages/mobymask-watcher/src/hooks.ts b/packages/mobymask-watcher/src/hooks.ts index 0d56df69..f2ce6c45 100644 --- a/packages/mobymask-watcher/src/hooks.ts +++ b/packages/mobymask-watcher/src/hooks.ts @@ -82,7 +82,7 @@ export async function handleEvent (indexer: Indexer, eventData: ResultEvent): Pr // Perform indexing for PhisherStatusUpdated and MemberStatusUpdated. if (['PhisherStatusUpdatedEvent', 'MemberStatusUpdatedEvent'].includes(eventData.event.__typename)) { - const txData = await indexer.getFullTransaction(eventData.tx.hash); + const txData = await indexer.getFullTransaction(eventData.tx.hash, eventData.block.number); const txDescription = getTxDescription(indexer, KIND_PHISHERREGISTRY, txData.input); let txDescriptions = [txDescription]; diff --git a/packages/mobymask-watcher/src/indexer.ts b/packages/mobymask-watcher/src/indexer.ts index 4e294ba7..cc3dccd9 100644 --- a/packages/mobymask-watcher/src/indexer.ts +++ b/packages/mobymask-watcher/src/indexer.ts @@ -598,8 +598,8 @@ export class Indexer implements IndexerInterface { } // Get full transaction data. - async getFullTransaction (txHash: string): Promise { - return getFullTransaction(this._ethClient, txHash); + async getFullTransaction (txHash: string, blockNumber: number): Promise { + return getFullTransaction(this._ethClient, txHash, blockNumber); } // Get contract interface for specified contract kind. @@ -620,8 +620,9 @@ export class Indexer implements IndexerInterface { parentHash }: DeepPartial): Promise<[BlockProgress, DeepPartial[]]> { assert(blockHash); + assert(blockNumber); - const dbEvents = await this._baseIndexer.fetchEvents(blockHash, this.parseEventNameAndArgs.bind(this)); + const dbEvents = await this._baseIndexer.fetchEvents(blockHash, blockNumber, this.parseEventNameAndArgs.bind(this)); const dbTx = await this._db.createTransactionRunner(); try { diff --git a/packages/util/src/indexer.ts b/packages/util/src/indexer.ts index 0013ad90..83375f31 100644 --- a/packages/util/src/indexer.ts +++ b/packages/util/src/indexer.ts @@ -273,7 +273,7 @@ export class Indexer { return [blockProgress, events]; } - async fetchEvents (blockHash: string, parseEventNameAndArgs: (kind: string, logObj: any) => any): Promise[]> { + async fetchEvents (blockHash: string, blockNumber: number, parseEventNameAndArgs: (kind: string, logObj: any) => any): Promise[]> { let logsPromise: Promise; if (this._serverConfig.filterLogs) { @@ -284,13 +284,14 @@ export class Indexer { logsPromise = this._ethClient.getLogs({ blockHash, + blockNumber: blockNumber.toString(), addresses }); } else { - logsPromise = this._ethClient.getLogs({ blockHash }); + logsPromise = this._ethClient.getLogs({ blockHash, blockNumber: blockNumber.toString() }); } - const transactionsPromise = this._ethClient.getBlockWithTransactions({ blockHash }); + const transactionsPromise = this._ethClient.getBlockWithTransactions({ blockHash, blockNumber }); const [ { logs }, diff --git a/packages/util/src/misc.ts b/packages/util/src/misc.ts index c40a92d5..5741222e 100644 --- a/packages/util/src/misc.ts +++ b/packages/util/src/misc.ts @@ -220,10 +220,10 @@ export const getFullBlock = async (ethClient: EthClient, ethProvider: providers. }; }; -export const getFullTransaction = async (ethClient: EthClient, txHash: string): Promise => { +export const getFullTransaction = async (ethClient: EthClient, txHash: string, blockNumber: number): Promise => { const { ethTransactionCidByTxHash: fullTx - } = await ethClient.getFullTransaction(txHash); + } = await ethClient.getFullTransaction(txHash, blockNumber); assert(fullTx.blockByMhKey);