Lesson 7: TypeError: Cannot read properties of undefined (reading 'getBalance') #5967
-
I'm using ethers 6.7.0, while I'm testing the Here is my withdraw test describe("withdraw", function () {
// the contract need to have some ether before withdrawing
beforeEach(async function () {
await fundMe.fund({ value: sendValue });
});
it("withdraw Ether from contract to owner", async function () {
//* Arrange
// First, we need to retrieve the balance of the owner (deployer) and the contract
const startingFundMeBalance = await fundMe.provider.getBalance(
fundMe.address
);
const startingDeployerBalance = await fundMe.provider.getBalance(
deployer
);
//* Act
// Let's perform the withdraw and retrieve the result
const transactionResponse = await fundMe.withdraw();
const transactionReceipt = await transactionResponse.wait(1);
const endingFundMeBalance = await fundMe.provider.getBalance(
fundMe.address
);
const endingDeployerBalance = await fundMe.provider.getBalance(
deployer
);
//* Assert
// Need to compare if the contract's balance down to 0, and the owner's balance added contract's fund
assert.equal(endingFundMeBalance, 0);
assert.equal(
endingDeployerBalance,
startingDeployerBalance + startingFundMeBalance
);
});
}); This is my repo for further review. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
It's look like in ether v6, I can not use describe("withdraw", function () {
// the contract need to have some ether before withdrawing
beforeEach(async function () {
await fundMe.fund({ value: sendValue });
});
it("withdraw Ether from contract to owner", async function () {
//* Arrange
// First, we need to retrieve the balance of the owner (deployer) and the contract
const startingFundMeBalance = await ethers.provider.getBalance(
fundMe.address
);
const startingDeployerBalance = await ethers.provider.getBalance(
deployer
);
//* Act
// Let's perform the withdraw and retrieve the result
const transactionResponse = await fundMe.withdraw();
const transactionReceipt = await transactionResponse.wait(1);
const endingFundMeBalance = await ethers.provider.getBalance(
fundMe.address
);
const endingDeployerBalance = await ethers.provider.getBalance(
deployer
);
//* Assert
// Need to compare if the contract's balance down to 0, and the owner's balance added contract's fund
assert.equal(endingFundMeBalance, 0);
assert.equal(
endingDeployerBalance,
startingDeployerBalance + startingFundMeBalance
);
});
}); Please help me to figure out what cause the error. Thank you for giving attention |
Beta Was this translation helpful? Give feedback.
-
Hi @TLBTrung-222, In ethers v6, you have to replace Then, to use Another issue is the And finally, the My code that works : const { deployments, ethers, getNamedAccounts } = require("hardhat");
const { assert, expect } = require("chai");
describe("FundMe", async function () {
let fundMe;
let deployer;
let mockV3Aggregator;
const sendValue = ethers.parseEther("1"); // 1 ETH
beforeEach(async function () {
// deploy our fundMe contract
// using Hardhat deploy
// const accounts = await ethers.getSigners();
// const accountZero = accounts[0];
deployer = (await getNamedAccounts()).deployer;
await deployments.fixture(["all"]); // Deploy all the contract tags with "all"
// Get contracts
fundMe = await ethers.getContract("FundMe", deployer);
mockV3Aggregator = await ethers.getContract(
"MockV3Aggregator",
deployer,
);
});
describe("constructor", async function () {
it("Sets the aggregator addresses correctly", async function () {
const response = await fundMe.priceFeed();
assert.equal(response, mockV3Aggregator.target);
});
});
describe("fund", async function () {
it("Fails if you don't send enough ETH", async function () {
await expect(fundMe.fund()).to.be.revertedWith(
"Didn't send enough!",
);
});
it("Updated the amount funded data structure", async function () {
await fundMe.fund({ value: sendValue });
const response = await fundMe.addressToAmountFunded(deployer);
assert.equal(response.toString(), sendValue.toString());
});
it("Add funder to array of funders", async function () {
await fundMe.fund({ value: sendValue });
const response = await fundMe.funders(0);
assert.equal(response, deployer);
});
});
describe("withdraw", async function () {
beforeEach(async function () {
await fundMe.fund({ value: sendValue });
});
it("Withdraw ETH from a single founder", async function () {
// Arrange
const startingFundMeBalance = await ethers.provider.getBalance(
fundMe.target,
);
const startingDeployerBalance =
await ethers.provider.getBalance(deployer);
// Act
const transactionResponse = await fundMe.withdraw();
const transactionReceipt = await transactionResponse.wait(1);
// Get the Gas price used
const { gasUsed, gasPrice } = transactionReceipt;
const gasCost = gasUsed * gasPrice;
const endingFundMeBalance = await ethers.provider.getBalance(
fundMe.target,
);
const endingDeployerBalance =
await ethers.provider.getBalance(deployer);
// Assert
assert.equal(endingFundMeBalance, 0);
assert.equal(
startingFundMeBalance + startingDeployerBalance,
endingDeployerBalance + gasCost,
);
});
});
}); |
Beta Was this translation helpful? Give feedback.
Hello @TLBTrung-222
First solution: downgrade your ethers to 5.x.x
Second Solution: Ethers version 6 uses getAddress() function to retrieve the contract address, while the previous version 5 uses .address property. If you are still using ethers 6 (seems like you do) then replace .address with .getAddress() and it should fix your issue.