1

I'm building an API to scrape events from a contract suite deployed to Rinkeby. Here's an example function with some context:

const network = getNetwork(parseInt(`${process.env.CHAIN_ID}`));
const provider = network.name === 'unknown' ?
      new ethers.providers.JsonRpcProvider('http://localhost:8545/') :
      getDefaultProvider(network);

const tokenInstanceContract = new Contract(`${process.env.TOKEN_ADDRESS}`,
      TokenABI.abi,
      provider);

export async function getAllTxs(address: string = "", total: number = 50, userRole: any = null) {
    const event = tokenInstanceContract.interface.events.Transfer
    const logs = await provider.getLogs({
      fromBlock: process.env.DEPLOYMENT_BLOCK,
      toBlock: 'latest',
      topic: event
    }).catch((err: string) => console.error("BACKEND CATASTROPHE: ", err))
    console.log("YO ITS THE ALL TX LOGS: ", logs);

    return logs
}

The contracts were deployed recently. Still when I trigger this function, I get the following back:

code: -32005,
  data: undefined,
  url: 'https://rinkeby.infura.io/v3/{redacted}',
  body: '{"method":"eth_getLogs","params":[{"fromBlock":"0x6a20f3","toBlock":"latest"}],"id":408,"jsonrpc":"2.0"}',
  responseText: '{"jsonrpc":"2.0","id":408,"error":{"code":-32005,"message":"query returned more than 10000 results"}}'
}

I can tell that the function is hitting the Infura API, and seems to be scraping from the parameter passed into fromBlock. If that's the case, though, why are there so many results? I mean, even ignoring that the contract doesn't have even close to 10K interactions.

I assume that I'm filtering wrong, but can't figure out how to make this better. Any ideas?

0

Yes, I was filtering the logs very wrong. Let's look at this amazing oopsie from the code block above:

const logs = await provider.getLogs({
      fromBlock: process.env.DEPLOYMENT_BLOCK,
      toBlock: 'latest',
      topic: event
    })

Note that the getLogs is called through the provider, not the contract, so in the code above the getLogs doesn't "know" to only get Transfer events from my specific contract, so instead it tries to return every ERC20 'Transfer' event since the deployment block of my contract. getLogs can also accept an address filter, so I should've been doing something like:

const logs = await provider.getLogs({
      fromBlock: process.env.DEPLOYMENT_BLOCK,
      toBlock: 'latest',
      address: process.env.MY_CONTRACT_ADDRESS,
      topic: event
    })

Which will only return 'Transfer' events from the address.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.