3
const hre = require("hardhat");

// Returns the ether balance of a given address.
async function getBalance(address) {
  const balanceBigInt = await hre.ethers.provider.getBalance(address);
  return hre.ethers.utils.formatEther(balanceBigInt);
}

// Logs the Ether balances for a list of addresses.
async function printBalances(addresses) {
  let index = 0;
  for (const address of addresses) {
    console.log(`Addresses ${index} balance: `, await getBalance(address));
    index++;
  }
}

// Logs the memos stored on-chain from coffee purchases.
async function printMemos(memos) {
  for (const memo of memos) {
    const timestamp = memo.timestamp;
    const tipper = memo.name;
    const tipperAddress = memo.from;
    const message = memo.message;
    console.log(`At ${timestamp}, ${tipper} (${tipperAddress}) said: "${message}"`);
  }
}

async function main() {
  const [owner, tipper, tipper2, tipper3] = await hre.ethers.getSigners();
  // const BuyMeACoffee = await hre.ethers.getContractFactory("BuyMeACoffee");
  const buyMeACoffee = await ethers.deployContract("BuyMeACoffee");
  // Deploy the contract.
  await buyMeACoffee.waitForDeployment();
  console.log("BuyMeACoffee deployed to:", buyMeACoffee.target);
  console.log("5");
  // Check balances before the coffee purchase.
  const addresses = [owner.address, tipper.address, buyMeACoffee.address];
  console.log("== start ==");
  await printBalances(addresses);

  // Buy the owner a few coffees.
  const tip = { value: hre.ethers.utils.parseEther("1") };
  await buyMeACoffee.connect(tipper).buyCoffee("Rahul", "You're a good coder", tip);
  await buyMeACoffee.connect(tipper2).buyCoffee("Suprit", "You're a good coder", tip);
  await buyMeACoffee.connect(tipper3).buyCoffee("Prasad", "You're a good coder", tip);

  // Check balances after the coffee purchase.
  console.log("== bought coffee ==");
  await printBalances(addresses);

  // Withdraw.
  await buyMeACoffee.connect(owner).withdrawTips();

  // Check balances after withdrawal.
  console.log("== withdrawTips ==");
  await printBalances(addresses);

  // Check out the memos.
  console.log("== memos ==");
  const memos = await buyMeACoffee.getMemos();
  printMemos(memos);
}

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

error->
BuyMeACoffee deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
5
== start ==
TypeError: Cannot read properties of undefined (reading 'formatEther')
    at getBalance (C:\Users\rohan\OneDrive\Documents\CODES\BuyMeACoffee\scripts\deploy.js:6:27)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at printBalances (C:\Users\rohan\OneDrive\Documents\CODES\BuyMeACoffee\scripts\deploy.js:13:49)
    at main (C:\Users\rohan\OneDrive\Documents\CODES\BuyMeACoffee\scripts\deploy.js:40:3)

1 Answer 1

3

If you are using version 6 or higher of ethers, you no longer need the "utils" keyword.

const val = hre.ethers.utils.formatEther(100000000000000)
console.log("val:", val)

Check your package.json to find the version of ethers you're using, on version ^6.6.2 I get this error when I try to run the code above.

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

But when I remove the .utils

const val = hre.ethers.formatEther(100000000000000)
console.log("val:", val)

I get this returned in the terminal:

val: 0.0001

Another way you could write this would be by first importing ethers directly from hardhat:

const { ethers } = require("hardhat")

and this will also return 0.0001 correctly

const val = ethers.formatEther(100000000000000)
console.log("val:", val)

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.