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

Pagination of events query #558

Merged
merged 3 commits into from Aug 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -122,7 +122,7 @@
"start:dev": "NODE_ENV=development npm run watch",
"start:e2e": "MODE=e2e npm run watch",
"watch": "nodemon",
"build": "tsc",
"build": "perl -i -pe\"s|timeout: 20000|timeout: 60000|\" node_modules/@trufflesuite/web3-provider-engine/subproviders/rpc.js && tsc",
"pkg": "pkg .",
"pkg:win": "pkg --targets node16-win-x64 --output bin/pointnetwork-win .",
"pkg:linux": "pkg --targets node16-linux-x64 --output bin/pointnetwork-linux .",
Expand Down
25 changes: 24 additions & 1 deletion src/network/providers/ethereum.js
Expand Up @@ -337,7 +337,30 @@ ethereum.getPastEvents = async (
options.filter.identityOwner = ethereum.toChecksumAddress(options.filter.identityOwner);
}

let events = await contract.getPastEvents(event, options);
const eventBlocksPageSize = 10000;
let latestBlock = 0;
if (options.toBlock === 'latest'){
latestBlock = await getWeb3().eth.getBlockNumber();
} else {
latestBlock = options.toBlock;
}

const ranges = [];
for (let start = options.fromBlock; start < latestBlock; start += eventBlocksPageSize){
ranges.push(
{
fromBlock: start,
toBlock: (start + eventBlocksPageSize < latestBlock
? start + eventBlocksPageSize
: latestBlock)
});
}
const eventsParts = await Promise.all(ranges.map(({fromBlock, toBlock}) => contract.getPastEvents(event, {...options, fromBlock, toBlock})));
let events = [];
for (const evPart of eventsParts){
events = events.concat(evPart);
}
//let events = await contract.getPastEvents(event, options);

//filter non-indexed properties from return value for convenience
if (options.hasOwnProperty('filter') && Object.keys(options.filter).length > 0) {
Expand Down