Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ethers.js: ReferenceError: utils is not defined

I’m trying to create a listener for incoming transactions with ethers.js (v5.6). According to the docs, to listen to incoming transactions you need to create this filter:

// List all token transfers  *to*  myAddress:
filter = {
    address: tokenAddress,
    topics: [
        utils.id("Transfer(address,address,uint256)"),
        null,
        hexZeroPad(myAddress, 32)
    ]
};

Having this in my script gives me an error saying utils.id("Transfer(address,address,uint256)"), ReferenceError: utils is not defined. I can’t find anything in the docs about importing an utils package. Can anyone sort me out?

My full code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

async function incomingTransactions() {
    if (loadedUser) {
        console.log("User loaded", loadedUser)
        let myAddress = loadedUser.publicKey
        let filter = {
            address: myAddress,
            topics: [
                utils.id("Transfer(address,address,uint256)"),
                null,
                hexZeroPad(myAddress, 32)
            ]
        };

        // let foo = await provider.getLogs(filter)
        // console.log(foo)
    } 
    console.log("No user loaded")
}

const interval = setInterval(function() {
    incomingTransactions();
}, 5000);
 

>Solution :

Looks like utils is part of the ethers object, and hexZeroPad and id are part of utils so you can use them like so:

const { ethers } = require("ethers"); // assuming commonjs

ethers.utils.id("Transfer(address,address,uint256)");
ethers.utils.hexZeroPad(myAddress, 32);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading