Skip to content

Commit

Permalink
Update getLogs API usage (#195)
Browse files Browse the repository at this point in the history
* Update client query to get logs

* Update calls to get logs for contracts
  • Loading branch information
prathamesh0 authored Oct 5, 2022
1 parent 18861ea commit 978f0bb
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 68 deletions.
20 changes: 7 additions & 13 deletions packages/codegen/src/templates/indexer-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> => 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 }));
}
Expand Down
20 changes: 7 additions & 13 deletions packages/eden-watcher/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> => 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 }));
}
Expand Down
20 changes: 7 additions & 13 deletions packages/erc721-watcher/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> => 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 }));
}
Expand Down
20 changes: 7 additions & 13 deletions packages/graph-test-watcher/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> => 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 }));
}
Expand Down
1 change: 1 addition & 0 deletions packages/ipld-eth-client/src/eth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface Vars {
blockHash: string;
contract?: string;
slot?: string;
addresses?: string[];
}

export class EthClient {
Expand Down
4 changes: 2 additions & 2 deletions packages/ipld-eth-client/src/eth-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion packages/lighthouse-watcher/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]);

Expand Down
20 changes: 7 additions & 13 deletions packages/mobymask-watcher/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> => 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 }));
}
Expand Down

0 comments on commit 978f0bb

Please sign in to comment.