Ethers.js – Fix Strange Errors When Listening to Events with NodeJS

ethereumjethers.jsnodejs

I am trying to listen to blockchain events using ethers on NodeJS. I've looked around for few tutorials and copied the code from one of them. Currently the code i have is:

const ethers = require("ethers");
const usdtABI = require("./abi/usdtAbi.json");

async function main() {
  const usdtAddress = "0xdAC17F958D2ee523a2206206994597C13D831ec7";
  const provider = new ethers.providers.WebSocketProvider(
    "wss://eth-mainnet.alchemyapi.io/v2/api-key"
  );
  const contract = new ethers.Contract(usdtAddress, usdtABI, provider);
  contract.on("Transfer", (from, to, value, event) => {
    let info = {
      from: from,
      to: to,
      value: ethers.utils.formatUnits(value, 6),
      data: event,
    };
    console.log(JSON.stringify(info, null, 4));
  });
}
main();

Link to Youtube tutorial: https://www.youtube.com/watch?v=7GT_-jvSZIA&t=1s

The strange thing is that when i run this code on React project, it runs absolutely fine. However, on NodeJS, i am getting the following error:

const provider = new ethers.providers.WebSocketProvider(
                                        ^

TypeError: Cannot read properties of undefined (reading 'WebSocketProvider')

Seems like on NodeJS, there is no providers class for ethers. But the WebSocketProvider Class is immediately exposed on ethers with ethers.WebSocketProvider. So i tried to use this instead of ethers.providers.WebSocketProvider.

After running the code, i fell like it is actually registering the listener but after few seconds, i am getting new error:

error = new TypeError(message);
                    ^

TypeError: unknown ProviderEvent (argument="event", value="{\"topics\":[\"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef\"],\"address\":[\"0xdac17f958d2ee523a2206206994597c13d831ec7\"]}", code=INVALID_ARGUMENT, version=6.0.2)

Please note that the same code is working on React. I am wondering why it is not working on nodejs server side? Why is ethers acting differently?

Bellow i am attaching the steps i've used to create the node app:

mkdir Nodeapp && cd Nodeapp
npx init -y
npm i ethers
touch app.js
<<Fill the app.js file with the code above>>
node app.js

I have also looked at few github projects doing this, without any success. Any help will be appreciated.

Best Answer

Instead of:

const provider = new ethers.providers.WebSocketProvider(
    "wss://eth-mainnet.alchemyapi.io/v2/api-key"
  );

Try:

const provider = new ethers.WebSocketProvider(
    "wss://eth-mainnet.alchemyapi.io/v2/api-key"
  );

The first was the usage in version 5.4. Currently the ethers Object have no providers element.

EDIT:

Version 6 is beta, it is posible that using the curremt 5.4 can help you to avoid errors. Please try to edit the file package.json:

"dependencies": {
    "ethers": "^5.4"
  }

re-execute npm install

use again:

const provider = new ethers.providers.WebSocketProvider(
        "wss://eth-mainnet.alchemyapi.io/v2/api-key"
      );