LESSON 9: -VRFCoordinatorV2Mock.fulfillRandomWords question #1037
-
In the unit test following the video, we used the mock to call fulfillRandomWords const tx = await raffle.performUpkeep("0x")
const txReceipt = await tx.wait(1)
await VRFCoordinatorV2Mock.fulfillRandomWords(
txReceipt.events[1].args.requestId,
raffle.address
) after calling this we waited for the winnerpicked event , which would set the winner. But looking at the code in the mock contract, the fullfillrandomwords does not assign winner to a winner var, so how does const recentwinner = await raffle.getRecentWinner() return a winner that is not undefined? VRFMock fullfillrandomwords function: function fulfillRandomWords(uint256 _requestId, address _consumer) external {
uint256 startGas = gasleft();
if (s_requests[_requestId].subId == 0) {
revert("nonexistent request");
}
Request memory req = s_requests[_requestId];
uint256[] memory words = new uint256[](req.numWords);
for (uint256 i = 0; i < req.numWords; i++) {
words[i] = uint256(keccak256(abi.encode(_requestId, i)));
}
VRFConsumerBaseV2 v;
bytes memory callReq = abi.encodeWithSelector(v.rawFulfillRandomWords.selector, _requestId, words);
(bool success, ) = _consumer.call{gas: req.callbackGasLimit}(callReq);
uint96 payment = uint96(BASE_FEE + ((startGas - gasleft()) * GAS_PRICE_LINK));
if (s_subscriptions[req.subId].balance < payment) {
revert InsufficientBalance();
}
s_subscriptions[req.subId].balance -= payment;
delete (s_requests[_requestId]);
emit RandomWordsFulfilled(_requestId, _requestId, payment, success);
} My guess: |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
I can answer "is it because we override fullfillrandomwords in raffle.sol? But we are still calling the method on the mock, so I am confused on which fullfillrandomwords() function it will run. I thought the method will correspond to the object called on.." if you send your VRFCoordinatorV2Mock contract. |
Beta Was this translation helpful? Give feedback.
-
@AlexK020908 We are calling a Hope it clears your query. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
here is my shallow understanding and hopes it will help. Personally there are some confusions I met when clarifying myself with the problem above:
There are two
fulfillRandomWords()
method one is from our ownRaffle.sol
and another is fromVRFCoordinatorV2Mock.sol
by which both methods will be called sequentially in one random word request, hence there is nothing like overriding occurs here. As a reminder, we need theVRFCoordinatorV2Mock.sol
contract becauseVRFCoordinatorV2Mock.sol
is inheritingVRFCoordinatorV2Interface
interface and in ourRaffle.sol
contract, we are having an immutable to store a contract of typeVRFCoordinatorV2Interface
and we will call therequestRandomWords()
…