wevm / wagmi

React Hooks for Ethereum

Home Page:https://wagmi.sh

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

useWatchContractEvent doesn't work properly when using watched hooks

rin-st opened this issue · comments

commented

Describe the bug

I need to listen to events using useWatchContractEvent and it's working fine. At the same time I need to watch balance of some address. I'm using technique from v1 to v2 migration guide to watch balance. But when I'm adding it useWatchContractEvent doesn't work locally (hardhat, foundry) and works from time to time on sepolia (not every event). At the same time onError of useWatchContractEvent triggers often. Example from pinned app

InvalidInputRpcError: Missing or invalid parameters.
Double check you have provided the correct parameters.

URL: https://rpc.sepolia.org
Request body: {"method":"eth_getFilterChanges","params":["0xe113405727fb7f30c30ea40767b77639"]}

Details: filter not found
Version: viem@2.9.31
    at withRetry.delay.count.count (chunk-ADKPSZLC.js?v=ae0a275b:1698:19)
    at async attemptRetry (chunk-ADKPSZLC.js?v=ae0a275b:1662:22)

Error from hardhat network:

TypeError: Cannot read properties of null (reading '0')
    at getFilterChanges (getFilterChanges.js:100:20)
    at async unwatch.emitOnBegin (watchContractEvent.js:110:32)
    at async poll (poll.js:23:13)

Link to Minimal Reproducible Example

https://stackblitz.com/edit/new-wagmi-cauhct?file=src%2FApp.tsx

Steps To Reproduce

use useWatchContractEvent and at the same time other watched value

Wagmi Version

^2.5.12 (also tried last version)

Viem Version

^2.8.16 (also tried last version)

TypeScript Version

No response

Check existing issues

Anything else?

No response

filter are stateful, they don't work on public rpc providers that load balance between several nodes. i think the method needs to be updated to use eth_getLogs polling instead and maintain the filter state locally. i think there is already a fallback looking at viem documentation.