Lesson 9: hh test - promise will not resolve #400
-
Problem is similar to #395. Contract compiled & deployed successfully (https://rinkeby.etherscn.io/address/0x4f598A1aDF635b2CA690d197ccc1040F6eCCfbCc#code) using a 90s interval. The VRF and Keeper work properly, the contract works correctly, and all the asserts/expects seem to pass (they don't throw an error and trigger the const { assert, expect } = require("chai")
// const win = require("global")
const { network, getNamedAccounts, deployments, ethers } = require("hardhat")
// const { isCallTrace } = require("hardhat/internal/hardhat-network/stack-traces/message-trace")
const { networkConfig, developmentChains } = require("../../helper-hardhat-config")
developmentChains.includes(network.name)
? describe.skip
: describe("Raffle staging tests", function () {
let raffle, raffleEntranceFee, deployer
// Deploy contracts
beforeEach(async function () {
console.log("Retrieving and connecting to contract...")
deployer = (await getNamedAccounts()).deployer
raffle = await ethers.getContract("Raffle", deployer)
raffleEntranceFee = await raffle.getEntranceFee()
console.log("Connected")
})
describe("fulfillRandomWords", function () {
it("Should use live Chainlink Keeper and VRF to pick a random winner, transfer funds, and reset the Raffle", async function () {
// Enter the raffle
const startingTimestamp = await raffle.getLatestTimestamp()
const accounts = await ethers.getSigners()
await new Promise(async (resolve, reject) => {
// setup listener before actually entering raffle in case blockchain moves very fast
console.log("Setting up listener...")
raffle.once("WinnerPicked", async () => {
console.log("WinnerPicked event detected")
try {
const recentWinner = await raffle.getRecentWinner()
// console.log("got winner") //DB
const raffleState = await raffle.getRaffleState()
// console.log("got state") //DB
const winnerEndingBalance = await accounts[0].getBalance()
// console.log("got balance") //DB
const endingTimestamp = await raffle.getLatestTimestamp()
// console.log("got time") //DB
// asserts
console.log("Check empty players array")
await expect(raffle.getPlayer(0)).to.be.reverted // no players
console.log("Check winner")
assert.equal(recentWinner.toString(), accounts[0].address)
console.log("Check RaffleState")
assert.equal(raffleState, 0)
console.log("Check balances")
// console.log(`Winner is ${recentWinner}`) //DB
// console.log(`Pre balance is ${winnerPreBalance}`) //DB
// console.log(`Starting balance is ${winnerStartingBalance}`) //DB
// console.log(`Ending balance is ${winnerEndingBalance}`) //DB
assert.equal(
winnerEndingBalance.toString(),
winnerStartingBalance.add(raffleEntranceFee).toString()
)
console.log("Check timestamps")
assert(endingTimestamp > startingTimestamp)
console.log("Resolving promise")
resolve()
console.log("You shouldn't see me") //DB
} catch (error) {
console.log(error)
reject(error)
}
})
// enter raffle
console.log("Entering raffle...")
const winnerPreBalance = await accounts[0].getBalance() //DB
const tx = await raffle.enterRaffle({ value: raffleEntranceFee })
const txReceipt = await tx.wait(1)
console.log("Awaiting winner via WinnerPicked event...")
const winnerStartingBalance = await accounts[0].getBalance()
// this block won't complete until listener has detected event and resolved/rejected
})
})
})
}) The event is detected, all the asserts are cleared, but
I have a feeling I must be doing something stupid, but can't figure out what. Any help would be appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
To get an event "WinnerPicked" you should run vrfCoordinatorV2Mock.fulfillRandomWords |
Beta Was this translation helpful? Give feedback.
-
This is quite interesting. What if you put the resolve outside the try catch? |
Beta Was this translation helpful? Give feedback.
-
Rinkeby is being really slow. |
Beta Was this translation helpful? Give feedback.
Rinkeby is being really slow.