-
I think in staging test we are not considering the Gas Cost while asserting Winner Ending Balance and Starting Balance so how did it work in the video when Patrick Tested it??? Error
const { assert, expect } = require("chai")
const { getNamedAccounts, deployments, ethers, network } = require("hardhat")
const { developmentChains, networkConfig } = require("../../helper-hardhat-config")
developmentChains.includes(network.name)
? describe.skip
: describe("Raffle Unit Tests", function () {
let raffle, raffleEntranceFee, deployer
beforeEach(async function () {
deployer = (await getNamedAccounts()).deployer
raffle = await ethers.getContract("Raffle", deployer)
raffleEntranceFee = await raffle.getEntranceFee()
})
describe("fulfillRandomWords", function () {
it("works with live Chainlink Keepers and Chainlink VRF, we get a random winner", async function () {
console.log("Setting up test...")
const startingTimeStamp = await raffle.getLastTimeStamp()
const accounts = await ethers.getSigners()
console.log("Setting up Listener...")
await new Promise(async (resolve, reject) => {
raffle.once("WinnerPicked", async () => {
console.log("WinnerPicked event fired!")
try {
const recentWinner = await raffle.getRecentWinner()
const raffleState = await raffle.getRaffleState()
const winnerEndingBalance = await accounts[0].getBalance()
const endingTimeStamp = await raffle.getLastTimeStamp()
await expect(raffle.getPlayers(0)).to.be.reverted
assert.equal(recentWinner.toString(), accounts[0].address)
assert.equal(raffleState, 0)
assert.equal(
winnerEndingBalance.toString(),
winnerStartingBalance.add(raffleEntranceFee).toString()
)
assert(endingTimeStamp > startingTimeStamp)
resolve()
} catch (error) {
console.log(error)
reject(e)
}
})
console.log("Entering Raffle...")
await raffle.enterRaffle({ value: raffleEntranceFee })
console.log("Ok, time to wait...")
const winnerStartingBalance = await accounts[0].getBalance()
})
})
})
}) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
The transaction However, in my case, I fixed the issue by modifying the code console.log("Entering Raffle...")
const tx = await raffle.enterRaffle({ value: raffleEntranceFee })
const txReceipt = await tx.wait(1)
console.log("Ok, time to wait...")
const winnerStartingBalance = await accounts[0].getBalance() We're now explicitly waiting for the transaction to be mined/confirmed before checking the account balance. After doing this, Hope that helps. |
Beta Was this translation helpful? Give feedback.
The transaction
await raffle.enterRaffle({ value: raffleEntranceFee })
transfers theraffleEntranceFee
to the Raffle contract and also costs some gas. Sinceconst winnerStartingBalance = await accounts[0].getBalance()
is executed after the transaction, in principlewinnerStartingBalance
should already include the gas cost.However, in my case,
winnerStartingBalance
was not reflecting the updated account balance - possibly because theenterRaffle
transaction wasn't being mined fast enough (?) - leading the to the same starting/ending balance assertion error that you're seeing.I fixed the issue by modifying the code