difference between VRFCoordinatorV2Interface
and VRFConsumerBaseV2
#2232
-
What's the difference between - So one of this is |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 11 replies
-
First about VFRConsumerBaseV2: function fulfillRandomWords(
uint256, /*requestId*/
uint256[] memory randomWords
) internal override {
uint256 indexOfWinner = randomWords[0] % s_players.length;
address payable recentWinner = s_players[indexOfWinner];
s_recentWinner = recentWinner;
(bool success, ) = recentWinner.call{value: address(this).balance}("");
if (!success) {
revert Lottery__TransferFailed();
}
emit WinnerPicked(recentWinner);
s_lotteryState=LotteryState.OPEN;
s_players = new address payable[](0);
s_lastTimeStamp=block.timestamp;
} which is the callback VRF function. Submit your VRF request by calling requestRandomWords of the VRF Coordinator like we did in our lottery contract: function performUpkeep(bytes calldata /* performData */)
external
override
{
(bool upkeepNeeded, ) = checkUpkeep("");
if(!upkeepNeeded){
revert Lottery__UpkeepNotNeeded(
address(this).balance,
s_players.length,
uint256(s_lotteryState),
((block.timestamp - s_lastTimeStamp) > i_interval),
upkeepNeeded
);
}
s_lotteryState=LotteryState.CALCULATING;
// Request the random number.
// This will return request id
s_requestId = i_vrfCoordinator.requestRandomWords(
i_gasLane,
i_subscriptionId,
REQUEST_CONFIRMATIONS,
i_callBackGasLimit,
NUM_WORDS
);
// This line is redundant as VRFCoordinator is already emitting the requestId
emit RequestedLotteryWinner(s_requestId);
}
then VRF coordinator emits an event. The VRF coordinator verifies the proof on-chain then calls back the consuming contract fulfillRandomWords function. and now VRFCoordinatorV2Interface private immutable i_vrfCoordinator; (to know more about interface refer #2132) |
Beta Was this translation helpful? Give feedback.
First about VFRConsumerBaseV2:
In order to use VRFCoordinator our contract as the consuming contract must inherit (i.e.
contract Lottery is VRFConsumerBaseV2 {}
) VRFConsumerBaseV2 and implement the fulfillRandomWords function inside our consumer contract itself,