Search code examples
node.jsblockchainpolygonweb3js

Web3.js - getPastEvents('transfer') events from last 10000 blocks


I'm retrieving transfer events for a solidity contract. Calling getPastEvents method along with parameter fromBlock 0 works for localhost blockchain, but on Polygon main net, there is 10K blocks scan limit, thus I need to scan only the last 10K blocks.

I'm calling the methods like this ⬇️


const scanFromBlock = (
      await this.web3.eth.getBlock(
        (await this.web3.eth.getBlock('latest')).number - 10000
      )
    ).number

    const transactions = await contract.getPastEvents('Transfer', {
      filter: {
        fromBlock: scanFromBlock,
        from: options.from as string,
        to: options.to as string
      }
    })

But I'm getting this error, so I assume there is other way to properly call it, I've read the ethers docs, Web3.js docs but either I'm blind or dumb. 😀

Have you experienced this?

Error: Returned error: Errors encountered in param 0: Invalid value "-0x26d7" supplied to : (QUANTITY | "earliest" | "latest" | "pending")/0: QUANTITY, Invalid value "-0x26d7" supplied to : (QUANTITY | "earliest" | "latest" | "pending")/1: "earliest" | "latest" | "pending"

Solution

  • the solution is to use "earliest", this will automatically detect the earliest possible block where the scan can start from.

    but, still I don't know if there is any other way to start from a specific block number.