Web3.js – Fixing Empty Array Issue on getPastEvents for Polygon, Optimism, and Arbitrum

ethers.jslayer-2polygonsolidityweb3js

I have a problem getting past events of a contract on polygon, Optimism and Arbitrum.
the same code works perfectly on ethereum but for some reason i cant get it to work on
polygon, OP and ARB.
I have tried different methods, using web3js, and ethers but no luck.

events = await Contract.getPastEvents("Borrow", {
        filter: {},
        fromBlock: startBlock,
        toBlock: "latest",
      });

start block is current block minus some blocks, I have tried even from block 1, block 0, block -100, "earliest" you name it, but every single time, it returns an empty array.

this is the code for ethers that i tried and works on ethereum network but not polygon, OP and ARB.

const filter = Contract.filters.Borrow(null, null, null);
// const filter = Contract.filters.Borrow(); also this
events = await Contract.queryFilter(
          filter,
          startBlock,
          "latest"
        );

P.S: the contract has that event, alot of it as the matter of fact and shows on those network scan. also my provider is alchemy and it works correctly (i can call view functions and it works on all mentioned networks, just event is the problem)

appreciate any help.

Best Answer

@Ismael points out to change the provider and when I have changed the provider, the code works correctly.

Related Topic