1

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.

2 Answers 2

1

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"
      );
5
  • Hey. I tried this, unfortunately without success. I am getting the second error described in the post. Here's the whole error displayed in the terminal pastebin.com/w5NV13wJ
    – Akall
    Feb 6, 2023 at 14:34
  • it is weird, i copied your full code with my alchemy key and it is working here, i can see teh events, did you downgrade the version of ethers to 5.4?
    – Rafael
    Feb 6, 2023 at 14:41
  • at last, i'd try to change the ABI, the one i am using is pastebin.com/ifwy6cQZ
    – Rafael
    Feb 6, 2023 at 14:51
  • Hey, sorry for the late response. I've tried with completely removing the project and reinitialize it with ethers 5.4 and it worked as expected.
    – Akall
    Feb 6, 2023 at 19:00
  • Thanks so much! This works for me. Previously I was using ethers 6.0.8
    – LNQ
    Feb 27, 2023 at 3:34
0
  • and instead of : const provider = new ethers.providers.JsonRpcProvider("HTTP://0.0.0.0:7545");

do :

  • const provider = new ethers.JsonRpcProvider("HTTP://0.0.0.0:7545");
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Apr 1, 2023 at 8:52

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.