For context, I'm simply trying to display the balance of a USDC pool on my UI.

These are the relevant parts of my js:

const [usdcBalance, setusdcBalance]= useState(0);

const checkEvents = async() => {
  const provider = new ethers.providers.Web3Provider(window.ethereum)
  let juniorPool = new ethers.Contract(juniorPoolAddress, JuniorPool.abi, provider)

  juniorPool.on("DepositMade", (capitalProvider, amount)=>{
    setusdcBalance(parseInt(`${usdcBalance}`) + parseInt(amount));

This is the output from my console, when I run the deposit function 3 times. The deposit function is the function that emits the DepositMade event. Each deposit I deposited 110 usdc:

enter image description here

The behavior I want is for setusdcBalance to take whatever the usdcBalance there is right now and add the amount added from a new deposit.

I realize that I could change the smart contract event to include blockNumber and use an if Statement on the event handler. But wouldn't that be really inefficient assuming that there might be thousands of past events? Is there a better way to do it?

1 Answer 1


You can try to use event filters:

contract.filters.EVENT_NAME( ...args )Filter, which return a filter for EVENT_NAME, optionally filtering by additional constraints.

Only indexed event parameters may be filtered. If a parameter is null (or not provided) then any value in that field matches.

See: https://docs.ethers.io/v5/api/contract/contract/#Contract--filters

  • Oh, it uses a Bloom filter, I see. Thank you very much for this. I'll try this approach.
    – HeeeeSark
    Commented Oct 12, 2022 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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