From 978f0bb4564bdffd4a382a7adcf72781d23dacc1 Mon Sep 17 00:00:00 2001 From: prathamesh0 <42446521+prathamesh0@users.noreply.github.com> Date: Wed, 5 Oct 2022 15:15:00 +0530 Subject: [PATCH] Update `getLogs` API usage (#195) * Update client query to get logs * Update calls to get logs for contracts --- .../src/templates/indexer-template.handlebars | 20 +++++++------------ packages/eden-watcher/src/indexer.ts | 20 +++++++------------ packages/erc721-watcher/src/indexer.ts | 20 +++++++------------ packages/graph-test-watcher/src/indexer.ts | 20 +++++++------------ packages/ipld-eth-client/src/eth-client.ts | 1 + packages/ipld-eth-client/src/eth-queries.ts | 4 ++-- packages/lighthouse-watcher/src/indexer.ts | 2 +- packages/mobymask-watcher/src/indexer.ts | 20 +++++++------------ 8 files changed, 39 insertions(+), 68 deletions(-) diff --git a/packages/codegen/src/templates/indexer-template.handlebars b/packages/codegen/src/templates/indexer-template.handlebars index e0d9ebcfe..a4eb1c723 100644 --- a/packages/codegen/src/templates/indexer-template.handlebars +++ b/packages/codegen/src/templates/indexer-template.handlebars @@ -760,22 +760,16 @@ export class Indexer implements IPLDIndexerInterface { console.time('time:indexer#_fetchAndSaveEvents-fetch-logs'); if (this._serverConfig.filterLogs) { const watchedContracts = this._baseIndexer.getWatchedContracts(); + const addresses = watchedContracts.map((watchedContract): string => { + return watchedContract.address; + }); - // TODO: Query logs by multiple contracts. - const contractlogsPromises = watchedContracts.map((watchedContract): Promise => this._ethClient.getLogs({ + const logsResult = await this._ethClient.getLogs({ blockHash, - contract: watchedContract.address - })); - - const contractlogs = await Promise.all(contractlogsPromises); + addresses + }); - // Flatten logs by contract and sort by index. - logs = contractlogs.map(data => { - return data.logs; - }).flat() - .sort((a, b) => { - return a.index - b.index; - }); + logs = logsResult.logs; } else { ({ logs } = await this._ethClient.getLogs({ blockHash })); } diff --git a/packages/eden-watcher/src/indexer.ts b/packages/eden-watcher/src/indexer.ts index 965cec467..1383abb9a 100644 --- a/packages/eden-watcher/src/indexer.ts +++ b/packages/eden-watcher/src/indexer.ts @@ -1014,22 +1014,16 @@ export class Indexer implements IPLDIndexerInterface { console.time('time:indexer#_fetchAndSaveEvents-fetch-logs'); if (this._serverConfig.filterLogs) { const watchedContracts = this._baseIndexer.getWatchedContracts(); + const addresses = watchedContracts.map((watchedContract): string => { + return watchedContract.address; + }); - // TODO: Query logs by multiple contracts. - const contractlogsPromises = watchedContracts.map((watchedContract): Promise => this._ethClient.getLogs({ + const logsResult = await this._ethClient.getLogs({ blockHash, - contract: watchedContract.address - })); - - const contractlogs = await Promise.all(contractlogsPromises); + addresses + }); - // Flatten logs by contract and sort by index. - logs = contractlogs.map(data => { - return data.logs; - }).flat() - .sort((a, b) => { - return a.index - b.index; - }); + logs = logsResult.logs; } else { ({ logs } = await this._ethClient.getLogs({ blockHash })); } diff --git a/packages/erc721-watcher/src/indexer.ts b/packages/erc721-watcher/src/indexer.ts index bd370bbc6..eaa5daa16 100644 --- a/packages/erc721-watcher/src/indexer.ts +++ b/packages/erc721-watcher/src/indexer.ts @@ -992,22 +992,16 @@ export class Indexer implements IPLDIndexerInterface { console.time('time:indexer#_fetchAndSaveEvents-fetch-logs'); if (this._serverConfig.filterLogs) { const watchedContracts = this._baseIndexer.getWatchedContracts(); + const addresses = watchedContracts.map((watchedContract): string => { + return watchedContract.address; + }); - // TODO: Query logs by multiple contracts. - const contractlogsPromises = watchedContracts.map((watchedContract): Promise => this._ethClient.getLogs({ + const logsResult = await this._ethClient.getLogs({ blockHash, - contract: watchedContract.address - })); + addresses + }); - const contractlogs = await Promise.all(contractlogsPromises); - - // Flatten logs by contract and sort by index. - logs = contractlogs.map(data => { - return data.logs; - }).flat() - .sort((a, b) => { - return a.index - b.index; - }); + logs = logsResult.logs; } else { ({ logs } = await this._ethClient.getLogs({ blockHash })); } diff --git a/packages/graph-test-watcher/src/indexer.ts b/packages/graph-test-watcher/src/indexer.ts index 509ee1ee0..6782d2982 100644 --- a/packages/graph-test-watcher/src/indexer.ts +++ b/packages/graph-test-watcher/src/indexer.ts @@ -714,22 +714,16 @@ export class Indexer implements IPLDIndexerInterface { console.time('time:indexer#_fetchAndSaveEvents-fetch-logs'); if (this._serverConfig.filterLogs) { const watchedContracts = this._baseIndexer.getWatchedContracts(); + const addresses = watchedContracts.map((watchedContract): string => { + return watchedContract.address; + }); - // TODO: Query logs by multiple contracts. - const contractlogsPromises = watchedContracts.map((watchedContract): Promise => this._ethClient.getLogs({ + const logsResult = await this._ethClient.getLogs({ blockHash, - contract: watchedContract.address - })); - - const contractlogs = await Promise.all(contractlogsPromises); + addresses + }); - // Flatten logs by contract and sort by index. - logs = contractlogs.map(data => { - return data.logs; - }).flat() - .sort((a, b) => { - return a.index - b.index; - }); + logs = logsResult.logs; } else { ({ logs } = await this._ethClient.getLogs({ blockHash })); } diff --git a/packages/ipld-eth-client/src/eth-client.ts b/packages/ipld-eth-client/src/eth-client.ts index 502a62abe..a3c047af0 100644 --- a/packages/ipld-eth-client/src/eth-client.ts +++ b/packages/ipld-eth-client/src/eth-client.ts @@ -18,6 +18,7 @@ interface Vars { blockHash: string; contract?: string; slot?: string; + addresses?: string[]; } export class EthClient { diff --git a/packages/ipld-eth-client/src/eth-queries.ts b/packages/ipld-eth-client/src/eth-queries.ts index 257fcb76b..334ae56ee 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!, $contract: Address) { - getLogs(blockHash: $blockHash, contract: $contract) { +query getLogs($blockHash: Bytes32!, $addresses: [Address!]) { + getLogs(blockHash: $blockHash, addresses: $addresses) { account { address } diff --git a/packages/lighthouse-watcher/src/indexer.ts b/packages/lighthouse-watcher/src/indexer.ts index 2f3330194..212fef5e5 100644 --- a/packages/lighthouse-watcher/src/indexer.ts +++ b/packages/lighthouse-watcher/src/indexer.ts @@ -85,7 +85,7 @@ export class Indexer { const contract = this._config.watch.lighthouse; const [{ logs }, { block }] = await Promise.all([ - this._ethClient.getLogs({ blockHash, contract }), + this._ethClient.getLogs({ blockHash, addresses: [contract] }), this._ethClient.getBlockByHash(blockHash) ]); diff --git a/packages/mobymask-watcher/src/indexer.ts b/packages/mobymask-watcher/src/indexer.ts index a753d0ab4..6ea575e19 100644 --- a/packages/mobymask-watcher/src/indexer.ts +++ b/packages/mobymask-watcher/src/indexer.ts @@ -736,22 +736,16 @@ export class Indexer implements IPLDIndexerInterface { console.time('time:indexer#_fetchAndSaveEvents-fetch-logs'); if (this._serverConfig.filterLogs) { const watchedContracts = this._baseIndexer.getWatchedContracts(); + const addresses = watchedContracts.map((watchedContract): string => { + return watchedContract.address; + }); - // TODO: Query logs by multiple contracts. - const contractlogsPromises = watchedContracts.map((watchedContract): Promise => this._ethClient.getLogs({ + const logsResult = await this._ethClient.getLogs({ blockHash, - contract: watchedContract.address - })); + addresses + }); - const contractlogs = await Promise.all(contractlogsPromises); - - // Flatten logs by contract and sort by index. - logs = contractlogs.map(data => { - return data.logs; - }).flat() - .sort((a, b) => { - return a.index - b.index; - }); + logs = logsResult.logs; } else { ({ logs } = await this._ethClient.getLogs({ blockHash })); }