3

I'm trying to listen to all emitted events from a contract with Ethers.js and Websockets.

Any idea why this is not working?

var wsProvider = new ethers.providers.WebSocketProvider("wss://rinkeby.infura.io/ws/v3/idhere");

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

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

The websockets connection seems to work because this returns all new blocks:

wsProvider.on('block', (blockNumber) => {
  console.log('New Block: ' + blockNumber);
});

2 Answers 2

2

Its because you have to specify the network type, otherwise it will take mainnet as default if not provided

also make sure your infura provider websocket is for rinkeby/supports rinkeby

var wsProvider = new ethers.providers.WebSocketProvider("wss://rinkeby.infura.io/ws/v3/idhere", "rinkeby");
0

The following code works though:

contract.on("*", (event) => {
  console.log("event: ", event);
});

Apparently, etherJS's event listener only returns one response, it is a JSON object.

Modified answer

This works too:

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

But it only works for transfer events, for other events the event object is undefined. I guess the return object has to match the event's arguements.

source code:

const { ethers } = require("ethers");
const wsurl = "your ws URL"

var wsProvider = new ethers.providers.WebSocketProvider(wsurl);
var contractAddress = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
var CONTRACT_ABI = '[{"constant":false,"inputs":[{"name":"newImplementation","type":"address"}],"name":"upgradeTo","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"newImplementation","type":"address"},{"name":"data","type":"bytes"}],"name":"upgradeToAndCall","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[],"name":"implementation","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"newAdmin","type":"address"}],"name":"changeAdmin","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"admin","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_implementation","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"previousAdmin","type":"address"},{"indexed":false,"name":"newAdmin","type":"address"}],"name":"AdminChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"implementation","type":"address"}],"name":"Upgraded","type":"event"}]'

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

contract.on("*", (resp) => {
    console.log("response: ", resp);
});
1
  • BTW I am a Chainstack developer advocate, I tested the script using a Chainstack endpoint. If Infura doesn't work, then you can probably try our service. The developer plan is free and it comes with 3M free requests per month. Mar 9, 2023 at 9:13

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.