What I would like to know is if there is a way to continuously listen for a single contract and fetch all the events emitted in real time. What I was able to find right now are ways of listening for only one event at a time in specific contract. To be more precise: I have a smart contract deployed in polygon testnet (mumbai), this is an upgradable contract that is connected to other contracts and as usual it emits events that I would like to capture. I use Alchemy as provider. Another fact that is not clear to me is why in polyscan the method in the event is listed like 0x40c10f19 and not with the name.

I also try to use this code before asking you guys of how to do this: `

filter = {
    address: CONTRACT_ADDRESS,


But when I actually interact with the contract I can't capture the events (the console.log does not display anything) but I can see the event in polyscan. I also try with by filtering only with the address without specifying the topic and I also try withproveder.once` but no way.

What am I doing wrong? or instead, What am I missing? every hints or contributions are appreciate :)

  • Hi, I know you asked about ethers.js, but would you be open to see an option with web3.js? It makes it pretty easy to do what you need I think. Sep 9, 2022 at 10:35
  • Oh yeah sure it would be the same
    – colo
    Sep 9, 2022 at 11:16
  • cool I'll place it in the answers! Sep 9, 2022 at 12:29

2 Answers 2


Hi developer advocate at Chainstack here!

Web3.js has a subscriprion method to easily to that!

const Web3 = require("web3");

const node_url = "CHAINSTACK_WSS_URL";
const web3 = new Web3(node_url)

var logs = web3.eth.subscribe("logs", {
        address: "CONTRACT_ADDRESS",
        topics: []
    }, function(error, result) {
        if (!error)
    .on("connected", function(subscriptionId) {
    .on("data", function(log) {
    .on("changed", function(log) {});

Remember that you need a web-socket endpoint to use subscriptions in web3.js.

You can find more examples in the Chainstack docs API reference, and in the web3.js docs.

I hope this helps you!

  • Ok perfect it works properly! thank you :)
    – colo
    Sep 12, 2022 at 9:56
  • yeah but the OP has specifically asked for ethers, not web3 ;S
    – scibuff
    Sep 2, 2023 at 7:46

You can use couple of methods in ether.js to achieve the result.

I wrote a sample code to listen to the events from a particular contract by listening to the block mining event.

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

const { ALCHEMY_API_KEY } = process.env;

const provider = new ethers.providers.JsonRpcProvider(
const abi = [
  "event Transfer(address indexed from, address indexed to, uint amount)", //Saved the erc20 transfer event ABI in an array form. Which is a great feature etherjs provides.

const address = "0x326C977E6efc84E512bB9C30f76E30c160eD06FB"; // Smartcontract address

const contract = new ethers.Contract(address, abi, provider); //creating a new contract instance by provding address, ABI, and provider.

const init = async () => {
  await provider.on("block", async (blockNumber) => {
    // This line of code listens to the block mining and every time a block is mined, it will return blocknumber.
    const transferEvent = await contract.queryFilter(
      //Simply used the queryfilter to listen to the transfer event everytime a block is mined.
      blockNumber - 1,


init().catch((err) => {

I wrote for goerli testnet but you can use the same for polygon too. All you need to do is to change the provider address and also create .env file and save your node provider api key there.

You can run the programing by running the command

node < name >.js

Finally, you can add as many event as possible in the ABI and write a queryfilter for the event.

This code is applicable to all ERC standards.

Tell me if it helps!

  • Yes works and it's super useful thank you! do you know how to create the topic list given the ABI? I mean if there is something in ethers that is able to, given the ABI, to create all the topics (so the events) automatically?
    – colo
    Sep 12, 2022 at 9:59

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.