1

I am getting this error TypeError: Cannot read properties of undefined (reading 'provider')

Here is my code for buy-coffee.js inside of scripts.

// We require the Hardhat Runtime Environment explicitly here. This is optional
// but useful for running the script in a standalone fashion through `node <script>`.
//
// You can also run a script with `npx hardhat run <script>`. If you do that, Hardhat
// will compile your contracts, add the Hardhat Runtime Environment's members to the
// global scope, and execute the script.
const {ethers,waffle} = require("hardhat");

async function getBalance(address) {
  const provider = waffle.provider;
  const balanceBigInt = await provider.getBalance(address);
  return ethers.utils.formatEther(balanceBigInt);
}

async function printBalance(addresses) {
  let index = 0;
  for (const address of addresses) {
    console.log(`Address ${index} balance: `, await getBalance(address));
    index++;
  }
}

async function printMemos(memos) {
  for (const memo of memos) {
    const timeStamp = memo.timeStamp;
    const name = memo.name;
    const address = memo.from;
    const message = memo.message;
    console.log(`At ${timeStamp}, ${name}, ${address}, said: "${message}" `);
  }
}

async function main() {
  // Get Example Accounts.
  const [owner, tipper, tipper2, tipper3] = await ethers.getSigners();

  // Get the Contract and deploy.
  const BuyMeACoffee = await ethers.getContractFactory("BuyMeACoffee");
  const buyMeACoffee = await BuyMeACoffee.deploy();
  await buyMeACoffee.deployed();
  console.log(
    `BuyMeACoffee Contract Deployed at Address: ${buyMeACoffee.address}`
  )

  // Check Balances before Coffee Purchase.
  const addresses = [owner.address, tipper.address, buyMeACoffee.address];
  console.log("Addresses: ");
  await printBalance(addresses);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

1 Answer 1

0

I believe you are missing await keyword.

const provider = await waffle.provider;

Other than this, you may use ethers for this. You can get signer as:

const signer = await ethers.provider.getSigner(0);

Zero gets the 0th index. getSigners() gives you an array.

Then,

const address = await signer.getAddress();
3
  • 1
    I tried using await but still it's not working. Jan 19, 2023 at 2:49
  • Do the ethers one. It works.
    – vampireAb
    Jan 19, 2023 at 13:43
  • 1
    I got the error my hardhat.config.js file was missing the statement require("@nomiclabs/hardhat-waffle"); Now its working! Thanks though. Jan 19, 2023 at 14:02

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.