Below you may find a simplified example of a contract requesting a random number and receiving the callback containing the random number and the proof.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IVRFConsumer {
function requestRandomness(uint256 seed) external returns (uint256);
}
contract SimpleVRFContract {
uint256 public lastRequestId;
bytes32 public lastRandomNumber;
bytes public lastProof;
function requestRandomNumber(uint256 seed) external {
// Initialize the interface for the VRFConsumer contract
IVRFConsumer vrfConsumer = IVRFConsumer(0x7efDa6beA0e3cE66996FA3D82953FF232650ea67);
// Request randomness from the VRFConsumer contract
lastRequestId = vrfConsumer.requestRandomness(seed);
}
function randomnessCallback(
bytes32 randomNumber,
uint256 requestId,
bytes memory proof
) external {
// Ensure the caller is the VRFConsumer contract
require(
msg.sender == address(0x7efDa6beA0e3cE66996FA3D82953FF232650ea67),
"Only the VRFConsumer can call this function"
);
// Check if the requestId matches the lastRequestId
require(requestId == lastRequestId, "Request ID does not match the last request");
// Store the results
lastRandomNumber = randomNumber;
lastProof = proof;
// Handle the random number and complete your game logic here
}
}
We say simplified because we do not verify the proof and we do not validate that the random number was generated by the VRF private key.
Move on the the next section to learn more about how it works and how we can optionally verify this information on chain.