0

How to use ethersjs contract.on filter... does this connection dies when refresh ? should i start new filter again ??

i have event like NewEvent(address useraddress,uint somenumber,string somestring)

now on users side i want to get events that they created for example listen on for : NewEvent(useraddress)

i make specific only one argument to filter... but arguments can be 2 or 3

any idea how to make this work ?

2 Answers 2

1

Below is a working code. Currently it is listening to all transfer event for an ERC20 token, you just need to modify the null value in contract.filters.Transfer(null, null, null) to make the filter work.

const { ethers } = require("ethers");

const wsurl = "your websocket url"
var wsProvider = new ethers.providers.WebSocketProvider(wsurl);
var contractAddress = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"


var CONTRACT_ABI = [{
    "anonymous": false,
    "inputs": [{
            "indexed": true,
            "name": "from",
            "type": "address"
        },
        {
            "indexed": true,
            "name": "to",
            "type": "address"
        },
        {
            "indexed": false,
            "name": "value",
            "type": "uint256"
        }
    ],
    "name": "Transfer",
    "type": "event"
}]

let contract = new ethers.Contract(contractAddress, CONTRACT_ABI, wsProvider);

const filter = contract.filters.Transfer(null, null, null)

contract.on(filter, (from, to, value, event) => {
    console.log("event: ", event);
});

Hope it helps. Happy coding!

1
  • i got error filter is not defined
    – famfamfam
    Commented Aug 22 at 7:36
0

Ethers.js is a JavaScript library for interacting with the Ethereum blockchain that provides an API for working with smart contracts. You can use the contract.on function to filter events emitted by a smart contract.

When you use the contract.on function to filter events, the connection does not die when the page is refreshed. However, it's important to keep in mind that if the user navigates away from the page or closes the browser, the connection will be terminated and you will need to create a new filter.

To filter events based on a specific user address, you can use the contract.on function with a filter object that specifies the user address as the first argument of the event.

contract.on("NewEvent", { useraddress: userAddress }, (useraddress, somenumber, somestring) => { // handle event here });

You can also use the contract.on function with a filter object that specifies multiple arguments of the event, such as user address and somenumber.

contract.on("NewEvent", { useraddress: userAddress, somenumber: someNumber }, (useraddress, somenumber, somestring) => { // handle event here });

It's also worth noting that if you want to stop listening to the events, you can use the removeListener function to remove the listener from the contract. contract.removeListener(listener); or contract.removeAllListeners();

2
  • i get this error : item.listener.apply is not a function after trying your example.. Commented Jan 27, 2023 at 18:54
  • The error "item.listener.apply is not a function" occurs when the provided listener is not a function. In the code you provided, the listener is passed to the contract.on function and should be a function that takes the event arguments as parameters. Check that the listener you are passing to the contract.on function is a function and not any other type of value. If it's still not working, provide more context and the full stack trace of the error to better understand the issue.
    – vgonearth
    Commented Feb 1, 2023 at 23:01

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.