PatrickAlphaC / hardhat-smartcontract-lottery-fcc

MIT License
117 stars 182 forks source link

FullFillRandomWords stacks in Raffle.test.ts #73

Closed dannius closed 2 years ago

dannius commented 2 years ago

Hi there! i am stuck a bit.. Actually my problem is that raffle.once('WinnerPicked' ) never called.

My main question is that i didnt get how should it works.

Because we invoke WinnerPicked inside Raffle contract, but fulfillRandomWords we invoke on vrfCoordinatorV2Mock contract (last row of code ex.) and vrfCoordinatorV2Mock event dont know about 'WinnerPicked' event (as per my understanding). So how come winnerPicket should be emmit here?

it works on video but doesnt works for me, i can not see console.log('pick winner') in console.

tried to debug it as well - instead of my custom WinenrPicker event - fullfillRandomWords emits "RandomWordsFulfilled" event which is stored in VrfCoordV2Mock contract

so this is my contract

contract Raffle {
    function fulfillRandomWords(uint256, uint256[] memory randomWords) internal override {
        uint256 indexOfWinner = randomWords[0] % s_players.length;

        address recentWinner = s_players[indexOfWinner];
        s_prevBlockTimestamp = block.timestamp;
        s_recentWinner = recentWinner;
        s_players = new address[](0);
        s_raffleState = RaffleState.OPEN;
        emit WinnerPicked(recentWinner);

        (bool success,) = recentWinner.call{ value: address(this).balance }("");

        if (!success) {
            revert Raffle__TransferFailed(); 
        }

        emit WinnerPicked(recentWinner);
    }
...
}

And here is tests


        beforeEach(async () => {
          await raffle.enterRaffle({ value: raffleEntranceFee });
          await network.provider.send('evm_increaseTime', [raffleInterval.toNumber() + 1]);
          await network.provider.send('evm_mine', []);
        })

        it('picks a winner, reest the lottery and sends money', async () => {
          const additionalEntrants = 3;
          const startingAccountIndex = 2;
          const accounts = await ethers.getSigners();

          for (let i = startingAccountIndex; i < startingAccountIndex + additionalEntrants; i++) {
            const connectedAccount = raffle.connect(accounts[i]);

            await connectedAccount.enterRaffle({ value: raffleEntranceFee });
          }

          const startingTimeStamp = await raffle.getLatestTimestamp();

          await new Promise<void>(async (resolve, reject) => {
            raffle.once('WinnerPicked', async () => {
              console.log('pick winner');
              try {

              } catch (e) {
                reject(e);
              }

              resolve();
            });

            const tx = await raffle.performUpkeep([]);
            const txReceipt = await tx.wait(1);
            const { requestId } = txReceipt.events![1].args!;

            await vrfCoordinatorV2Mock.fulfillRandomWords(requestId, raffle.address);
          });
        }) 
      })```
dannius commented 2 years ago

Resolved! problem was with callbackGasLimit