0

I have the following js script that i want to use to read events from a smart contract below, but when i run the script I get the following error message

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

My js script

require('dotenv').config();
const ethers = require('ethers');

const addr = "0x751E47217f1e587B65Df8587B6D277bC73d19207";
const abi = require('./abi.json');

async function main() {
  const provider = new ethers.providers.JsonRpcProvider(process.env.TESTNET_URL)
  const wallet = new ethers.Wallet(process.env.KEY, provider)
  const signer = wallet.provider.getSigner(wallet.address);
  const Contract = new ethers.Contract(addr, abi, provider);

  Contract.on('transferEvent', (to, value, event) =>{
    let info ={
      acc: to,
      value: ethers.utils.formatUnits(value),
      data: event,
    };
    console.log(JSON.stringify(info));
  })
}
main();

1 Answer 1

0

Check your process.env.KEY . It must be giving undefined . It should be a hex string .
Incorrect process.env.KEY is causing const wallet = new ethers.Wallet(process.env.KEY, provider) to throw an error .

Also it's Transfer not transferEvent

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.