PatrickAlphaC / hardhat-smartcontract-lottery-fcc

MIT License
118 stars 182 forks source link

Error in Raffle.test.js #92

Closed Nikhil-blockchain closed 2 years ago

Nikhil-blockchain commented 2 years ago

1> Error: call revert exception; VM Exception while processing transaction: reverted with panic code 17 This error is for

it("returns false if enough time hasn't passed", async () => {
                  await raffle.enterRaffle({ value: raffleEntranceFee })
                  await network.provider.send("evm_increaseTime", [interval.toNumber() - 10]) // use a higher number here if this test fails
                  await network.provider.request({ method: "evm_mine", params: [] })
                  const { upkeepNeeded } = await raffle.callStatic.checkUpkeep([])
                  assert(!upkeepNeeded.toNumber())
              })

2> Timeout Error in "picks a winner, resets, and sends money"

it("picks a winner, resets, and sends money", async () => {
                  const additionalEntrances = 3 // to test
                  const startingIndex = 1
                  const accounts = await ethers.getSigners()
                  for (let i = startingIndex; i < startingIndex + additionalEntrances; i++) {
                      raffle = raffle.connect(accounts[i])
                      await raffle.enterRaffle({ value: raffleEntranceFee })
                  }

                  const startingTimeStamp = await raffle.getLastTimeStamp() // stores starting timestamp (before we fire our event)

                  // This will be more important for our staging tests...
                  await new Promise(async (resolve, reject) => {
                      raffle.once("WinnerPicked", async () => {
                          // event listener for WinnerPicked
                          log("WinnerPicked event fired!")
                          // assert throws an error if it fails, so we need to wrap
                          // it in a try/catch so that the promise returns event
                          // if it fails.

                          // Now lets get the ending values...
                          const recentWinner = await raffle.getRecentWinner()
                          log(recentWinner)
                          const raffleState = await raffle.getRaffleState()
                          const winnerBalance = await accounts[3].getBalance()
                          const endingTimeStamp = await raffle.getLastTimeStamp()
                          await expect(raffle.getPlayer(0)).to.be.reverted
                          // Comparisons to check if our ending values are correct:
                          assert.equal(recentWinner.toString(), accounts[3].address)
                          assert.equal(raffleState, 0)
                          assert.equal(
                              winnerBalance.toString(),
                              startingBalance // startingBalance + ( (raffleEntranceFee * additionalEntrances) + raffleEntranceFee )
                                  .add(
                                      raffleEntranceFee
                                          .mul(additionalEntrances)
                                          .add(raffleEntranceFee)
                                  )
                                  .toString()
                          )
                          assert(endingTimeStamp > startingTimeStamp)
                          resolve() // if try passes, resolves the promise
                      })

                      // kicking off the event by mocking the chainlink keepers and vrf coordinator
                      const tx = await raffle.performUpkeep("0x")
                      const txReceipt = await tx.wait(1)
                      const startingBalance = await accounts[3].getBalance()
                      await vrfCoordinatorV2Mock.fulfillRandomWords(
                          txReceipt.events[1].args.requestId,
                          raffle.address
                      )
                  })
              })

I think there is some problem in this part because while debugging my program was stopping here

await vrfCoordinatorV2Mock.fulfillRandomWords(
                          txReceipt.events[1].args.requestId,
                          raffle.address
                      )
RyanPaulGannon commented 2 years ago

Hey Nikhil

I've just quickly tested this, have a see if these work for you:

  1. I don't think you need to convert your upkeepNeeded assert, try it without toNumber() so it looks like this: assert(!upkeepNeeded).

  2. You don't appear to have wrapped the promise in the try catch, I've commented mine out to test and it does work so it's only a suggestion!

Let us know how you get on ...

RyanPaulGannon commented 2 years ago

Is this resolved, can this issue now be closed?

PatrickAlphaC commented 2 years ago

Thanks @RyanPaulGannon, I'm going to close this for now.