Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't subscribe to events via ethers.js and contract.on(event, handler) #9

Open
jsdw opened this issue Nov 15, 2024 · 0 comments
Open

Comments

@jsdw
Copy link

jsdw commented Nov 15, 2024

The relevant code:

import * as ethers from 'ethers';

const CONTRACT_ADDRESS = "0x0d73950cfe29ed1e4e21923307d12a06ee20d3c8"
const CONTRACT_ABI = [
  "event MessageChanged(string message)",
  "function message() view returns (string)",
  "function setMessage(string calldata m) external payable",
]

const provider = new ethers.BrowserProvider((window as any).ethereum)

provider.getSigner().then(signer => {
    const contract = new ethers.Contract(CONTRACT_ADDRESS, CONTRACT_ABI, signer)
    contract.on("MessageChanged",() => { /* handle event */ })
})

The main bit of interest here is the contract.on to subscribe to events from my simple smart contract.

Running this in the browser leads to the error:

Error: could not coalesce error (error={ "code": -32603, "data": { "cause": null, "code": -32601, "message": "Method not found" }, "message": "Internal JSON-RPC error." }, payload={ "id": 4, "jsonrpc": "2.0", "method": "eth_newFilter", "params": [ { "address": [ "0x0d73950cfe29ed1e4e21923307d12a06ee20d3c8" ], "topics": [ "0xbb4847942d98bb5bb249692c72ce235605e41502e705831e609875320ef2cac7" ] } ] }, code=UNKNOWN_ERROR, version=6.13.4)

Looks like we don't have an eth_newFilter RPC yet.

A workaround suggested elsewhere was to set polling to true in the provider, ie const provider = new ethers.BrowserProvider((window as any).ethereum, undefined, { polling: true }). However, this exposes a different missing RPC: eth_getLogs.

Thus, I don't seem able to subscribe to events at the mo :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant