I'm writing the test cases for a contract, and I have a problem trying to match the balance, because of the transaction gas. what is the correct way to do it?

 it("should allow the owner of the campaign to withdraw funds if the campaign is successful", async () => {
const target = ethers.utils.parseEther("1");

await contract.connect(aliceAccount).createCampaign(
  "Test Campaign",
  "Test Description",

// Bob and Alice donate to the campaign
await contract
  .donateToCampaign(0, { value: ethers.utils.parseEther("5") });
await contract
  .donateToCampaign(0, { value: ethers.utils.parseEther("5") });

// check if the campaign has reached its target
const campaign = await contract.campaigns(0);

// get the balance of the owner before withdrawing funds
const balanceBefore = await ethers.provider.getBalance(aliceAccount.address);

// call the withdrawFunds function
const tx = await contract.connect(aliceAccount).withdrawFunds(0);

// get the balance of the owner after withdrawing funds
const balanceAfter = await ethers.provider.getBalance(aliceAccount.address);

// WHAT IS THE COORECT WAY TO CHECK THIS? the problem is the gas 
expect(balanceAfter == balanceBefore + 10 ether )


1 Answer 1


Solve it.

I was calculating the gas used wrong way

const gasUsed = receipt.effectiveGasPrice.mul(receipt.cumulativeGasUsed)

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.