VRFConsumerV2
Inherits: VRFConsumerBaseV2, RNG
Author: Simon Malatrait simon.malatrait@grenoble-inp.org
This contract implements the RNG standard and inherits from VRFConsumerBaseV2 to use Chainlink Verifiable Randomness Mechanism.
It allows to store the random number associated to the requests made.
Chainlink Subscription Method Documentation: https://docs.chain.link/vrf/v2/subscription
Chainlink Subscription Method Network Parameters: https://docs.chain.link/vrf/v2/subscription/supported-networks#arbitrum-mainnet
For SECURITY CONSIDERATIONS, you might also have look to: https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/VRFConsumerBaseV2.sol
For SECURITY CONSIDERATIONS, you might also have look to: https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol
State Variables
governor
address public governor;
keyHash
bytes32 public keyHash;
vrfCoordinator
VRFCoordinatorV2Interface public vrfCoordinator;
subscriptionId
uint64 public subscriptionId;
callbackGasLimit
uint32 public callbackGasLimit;
sortitionModule
ISortitionModule public sortitionModule;
numWords
uint32 public numWords;
requestConfirmations
uint16 public requestConfirmations;
lastRequestId
uint256 public lastRequestId;
requestsToRandomWords
mapping(uint256 => uint256) public requestsToRandomWords;
Functions
onlyBySortitionModule
modifier onlyBySortitionModule();
onlyByGovernor
modifier onlyByGovernor();
constructor
Constructs the ChainlinkRNG contract.
https://docs.chain.link/vrf/v2/subscription/examples/get-a-random-number#analyzing-the-contract
constructor(
address _governor,
address _vrfCoordinator,
address _sortitionModule,
bytes32 _keyHash,
uint64 _subscriptionId,
uint16 _requestConfirmations,
uint32 _callbackGasLimit,
uint32 _numWords
) VRFConsumerBaseV2(_vrfCoordinator);
Parameters
Name | Type | Description |
---|---|---|
_governor | address | The Governor of the contract. |
_vrfCoordinator | address | The address of the VRFCoordinator contract. |
_sortitionModule | address | The address of the SortitionModule contract. |
_keyHash | bytes32 | The gas lane key hash value - Defines the maximum gas price you are willing to pay for a request in wei (ID of the off-chain VRF job). |
_subscriptionId | uint64 | The unique identifier of the subscription used for funding requests. |
_requestConfirmations | uint16 | How many confirmations the Chainlink node should wait before responding. |
_callbackGasLimit | uint32 | The limit for how much gas to use for the callback request to the contract's fulfillRandomWords() function. |
_numWords | uint32 | How many random values to request. |
changeVrfCoordinator
Changes the vrfCoordinator
storage variable.
function changeVrfCoordinator(address _vrfCoordinator) external onlyByGovernor;
Parameters
Name | Type | Description |
---|---|---|
_vrfCoordinator | address | The new value for the vrfCoordinator storage variable. |
changeSortitionModule
Changes the sortitionModule
storage variable.
function changeSortitionModule(address _sortitionModule) external onlyByGovernor;
Parameters
Name | Type | Description |
---|---|---|
_sortitionModule | address | The new value for the sortitionModule storage variable. |
changeKeyHash
Changes the keyHash
storage variable.
function changeKeyHash(bytes32 _keyHash) external onlyByGovernor;
Parameters
Name | Type | Description |
---|---|---|
_keyHash | bytes32 | The new value for the keyHash storage variable. |
changeSubscriptionId
Changes the subscriptionId
storage variable.
function changeSubscriptionId(uint64 _subscriptionId) external onlyByGovernor;
Parameters
Name | Type | Description |
---|---|---|
_subscriptionId | uint64 | The new value for the subscriptionId storage variable. |
changeRequestConfirmations
Changes the requestConfirmations
storage variable.
function changeRequestConfirmations(uint16 _requestConfirmations) external onlyByGovernor;
Parameters
Name | Type | Description |
---|---|---|
_requestConfirmations | uint16 | The new value for the requestConfirmations storage variable. |
changeCallbackGasLimit
Changes the callbackGasLimit
storage variable.
function changeCallbackGasLimit(uint32 _callbackGasLimit) external onlyByGovernor;
Parameters
Name | Type | Description |
---|---|---|
_callbackGasLimit | uint32 | The new value for the callbackGasLimit storage variable. |
changeNumWord
Changes the numWords
storage variable.
function changeNumWord(uint32 _numWords) external onlyByGovernor;
Parameters
Name | Type | Description |
---|---|---|
_numWords | uint32 | The new value for the numWords storage variable. |
requestRandomness
Submit a request to the VRF Coordinator contract with the specified parameters.
Assumes the subscription is funded sufficiently; "Words" refers to unit of data in Computer Science Note Buffer of one requestId, as in RandomizerRNG, which should be enough with the callback function.
function requestRandomness(uint256) external onlyBySortitionModule;
fulfillRandomWords
Callback function used by VRF Coordinator
Stores the random number given by the VRF Coordinator and calls passPhase function on SortitionModule.
function fulfillRandomWords(uint256 _requestId, uint256[] memory _randomWords) internal override;
Parameters
Name | Type | Description |
---|---|---|
_requestId | uint256 | The same request Id initially returned by vrfCoordinator.requestRandomWords and stored in the lastRequestId storage variable. |
_randomWords | uint256[] | - array of random results from VRF Coordinator |
receiveRandomness
Get the random value associated to lastRequestId
function receiveRandomness(uint256) external view returns (uint256 randomNumber);
Returns
Name | Type | Description |
---|---|---|
randomNumber | uint256 | The random number. If the value is not ready or has not been required it returns 0. |
Events
RequestSent
Emitted when a request is sent to the VRF Coordinator
event RequestSent(uint256 indexed requestId, uint32 numWords);
RequestFulfilled
Emitted when a request has been fulfilled.
event RequestFulfilled(uint256 indexed requestId, uint256[] randomWords);