Skip to content

Instantly share code, notes, and snippets.

@wschwab
Created August 24, 2020 14:57
Show Gist options
  • Save wschwab/6e89eeb29a8e24203ee954c042e47a6f to your computer and use it in GitHub Desktop.
Save wschwab/6e89eeb29a8e24203ee954c042e47a6f to your computer and use it in GitHub Desktop.
an attempt at implementing some kind of pagination to an Ethereum event filter
const eventFilterv5WithPagination = (contractAddress, erc20abi, _provider, numberOfResponses) => {
// creating the interface of the ABI
const iface = new ethers.utils.Interface(erc20abi.abi);
// intialize array for the logs
let logs = [];
// get latest block number
const latest = await provider.getBlockNumber();
// intialize a counter for which block we're scraping starting at the most recent block
let blockNumberIndex = latest;
// while loop runs until there are as many responses as desired
while (logs.length < numberOfResponses) {
const tempLogs = await provider.getLogs({
address: contractAddress,
// both fromBlock and toBlock are the index, meaning only one block's logs are pulled
fromBlock: blockNumberIndex,
toBlock: blockNumberIndex,
})
// an added console.log to help see what's going on
console.log("BLOCK: ", blockNumberIndex, " NUMBER OF LOGS: ", tempLogs.length);
blockNumberIndex -= 1;
logs = logs && logs.length > 0 ? [...logs, ...tempLogs] : [...tempLogs]
};
// this will return an array with the decoded events
const decodedEvents = logs.map(log => {
iface.decodeEventLog("Transfer", log.data)
});
// let's pull out the to and from addresses and amounts
const toAddresses = decodedEvents.map(event => event["values"]["to"]);
const fromAddresses = decodedEvents.map(event => event["values"]["from"]);
const amounts = decodedEvents.map(event => event["values"]["amount"]);
return [fromAddresses, toAddresses, amounts]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment