|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity 0.8.24; |
| 4 | + |
| 5 | +import {VRFConsumerBaseV2Plus, IVRFCoordinatorV2Plus} from "@chainlink/contracts/src/v0.8/vrf/dev/VRFConsumerBaseV2Plus.sol"; |
| 6 | +import {VRFV2PlusClient} from "@chainlink/contracts/src/v0.8/vrf/dev/libraries/VRFV2PlusClient.sol"; |
| 7 | + |
| 8 | +import "./RNG.sol"; |
| 9 | + |
| 10 | +/// @title Random Number Generator that uses Chainlink VRF v2.5 |
| 11 | +/// https://blog.chain.link/introducing-vrf-v2-5/ |
| 12 | +contract ChainlinkRNG is RNG, VRFConsumerBaseV2Plus { |
| 13 | + // ************************************* // |
| 14 | + // * Storage * // |
| 15 | + // ************************************* // |
| 16 | + |
| 17 | + address public governor; // The address that can withdraw funds. |
| 18 | + address public sortitionModule; // The address of the SortitionModule. |
| 19 | + bytes32 public keyHash; // 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). |
| 20 | + uint256 public subscriptionId; // The unique identifier of the subscription used for funding requests. |
| 21 | + uint16 public requestConfirmations; // How many confirmations the Chainlink node should wait before responding. |
| 22 | + // 22 bytes remaining in slot |
| 23 | + uint32 public callbackGasLimit; // Gas limit for the Chainlink callback. |
| 24 | + uint256 lastRequestId; // The last request ID. |
| 25 | + mapping(uint256 requestId => uint256 number) public randomNumbers; // randomNumbers[requestID] is the random number for this request id, 0 otherwise. |
| 26 | + |
| 27 | + // ************************************* // |
| 28 | + // * Events * // |
| 29 | + // ************************************* // |
| 30 | + |
| 31 | + /// @dev Emitted when a request is sent to the VRF Coordinator |
| 32 | + /// @param requestId The ID of the request |
| 33 | + event RequestSent(uint256 indexed requestId); |
| 34 | + |
| 35 | + /// Emitted when a request has been fulfilled. |
| 36 | + /// @param requestId The ID of the request |
| 37 | + /// @param randomWord The random value answering the request. |
| 38 | + event RequestFulfilled(uint256 indexed requestId, uint256 randomWord); |
| 39 | + |
| 40 | + // ************************************* // |
| 41 | + // * Function Modifiers * // |
| 42 | + // ************************************* // |
| 43 | + |
| 44 | + modifier onlyByGovernor() { |
| 45 | + require(governor == msg.sender, "Governor only"); |
| 46 | + _; |
| 47 | + } |
| 48 | + |
| 49 | + modifier onlyBySortitionModule() { |
| 50 | + require(sortitionModule == msg.sender, "SortitionModule only"); |
| 51 | + _; |
| 52 | + } |
| 53 | + |
| 54 | + // ************************************* // |
| 55 | + // * Constructor * // |
| 56 | + // ************************************* // |
| 57 | + |
| 58 | + /// @dev Constructor, initializing the implementation to reduce attack surface. |
| 59 | + /// @param _governor The Governor of the contract. |
| 60 | + /// @param _sortitionModule The address of the SortitionModule contract. |
| 61 | + /// @param _vrfCoordinator The address of the VRFCoordinator contract. |
| 62 | + /// @param _keyHash 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). |
| 63 | + /// @param _subscriptionId The unique identifier of the subscription used for funding requests. |
| 64 | + /// @param _requestConfirmations How many confirmations the Chainlink node should wait before responding. |
| 65 | + /// @param _callbackGasLimit The limit for how much gas to use for the callback request to the contract's fulfillRandomWords() function. |
| 66 | + /// @dev https://docs.chain.link/vrf/v2-5/subscription/get-a-random-number |
| 67 | + constructor( |
| 68 | + address _governor, |
| 69 | + address _sortitionModule, |
| 70 | + address _vrfCoordinator, |
| 71 | + bytes32 _keyHash, |
| 72 | + uint256 _subscriptionId, |
| 73 | + uint16 _requestConfirmations, |
| 74 | + uint32 _callbackGasLimit |
| 75 | + ) VRFConsumerBaseV2Plus(_vrfCoordinator) { |
| 76 | + governor = _governor; |
| 77 | + sortitionModule = _sortitionModule; |
| 78 | + keyHash = _keyHash; |
| 79 | + subscriptionId = _subscriptionId; |
| 80 | + requestConfirmations = _requestConfirmations; |
| 81 | + callbackGasLimit = _callbackGasLimit; |
| 82 | + } |
| 83 | + |
| 84 | + // ************************************* // |
| 85 | + // * Governance * // |
| 86 | + // ************************************* // |
| 87 | + |
| 88 | + /// @dev Changes the governor of the contract. |
| 89 | + /// @param _governor The new governor. |
| 90 | + function changeGovernor(address _governor) external onlyByGovernor { |
| 91 | + governor = _governor; |
| 92 | + } |
| 93 | + |
| 94 | + /// @dev Changes the sortition module of the contract. |
| 95 | + /// @param _sortitionModule The new sortition module. |
| 96 | + function changeSortitionModule(address _sortitionModule) external onlyByGovernor { |
| 97 | + sortitionModule = _sortitionModule; |
| 98 | + } |
| 99 | + |
| 100 | + /// @dev Changes the VRF Coordinator of the contract. |
| 101 | + /// @param _vrfCoordinator The new VRF Coordinator. |
| 102 | + function changeVrfCoordinator(address _vrfCoordinator) external onlyByGovernor { |
| 103 | + s_vrfCoordinator = IVRFCoordinatorV2Plus(_vrfCoordinator); |
| 104 | + emit CoordinatorSet(_vrfCoordinator); |
| 105 | + } |
| 106 | + |
| 107 | + /// @dev Changes the key hash of the contract. |
| 108 | + /// @param _keyHash The new key hash. |
| 109 | + function changeKeyHash(bytes32 _keyHash) external onlyByGovernor { |
| 110 | + keyHash = _keyHash; |
| 111 | + } |
| 112 | + |
| 113 | + /// @dev Changes the subscription ID of the contract. |
| 114 | + /// @param _subscriptionId The new subscription ID. |
| 115 | + function changeSubscriptionId(uint256 _subscriptionId) external onlyByGovernor { |
| 116 | + subscriptionId = _subscriptionId; |
| 117 | + } |
| 118 | + |
| 119 | + /// @dev Changes the request confirmations of the contract. |
| 120 | + /// @param _requestConfirmations The new request confirmations. |
| 121 | + function changeRequestConfirmations(uint16 _requestConfirmations) external onlyByGovernor { |
| 122 | + requestConfirmations = _requestConfirmations; |
| 123 | + } |
| 124 | + |
| 125 | + /// @dev Changes the callback gas limit of the contract. |
| 126 | + /// @param _callbackGasLimit The new callback gas limit. |
| 127 | + function changeCallbackGasLimit(uint32 _callbackGasLimit) external onlyByGovernor { |
| 128 | + callbackGasLimit = _callbackGasLimit; |
| 129 | + } |
| 130 | + |
| 131 | + // ************************************* // |
| 132 | + // * State Modifiers * // |
| 133 | + // ************************************* // |
| 134 | + |
| 135 | + /// @dev Request a random number. SortitionModule only. |
| 136 | + function requestRandomness(uint256 /*_block*/) external override onlyBySortitionModule { |
| 137 | + // Will revert if subscription is not set and funded. |
| 138 | + uint256 requestId = s_vrfCoordinator.requestRandomWords( |
| 139 | + VRFV2PlusClient.RandomWordsRequest({ |
| 140 | + keyHash: keyHash, |
| 141 | + subId: subscriptionId, |
| 142 | + requestConfirmations: requestConfirmations, |
| 143 | + callbackGasLimit: callbackGasLimit, |
| 144 | + numWords: 1, |
| 145 | + extraArgs: VRFV2PlusClient._argsToBytes( |
| 146 | + // Set nativePayment to true to pay for VRF requests with ETH instead of LINK |
| 147 | + VRFV2PlusClient.ExtraArgsV1({nativePayment: true}) |
| 148 | + ) |
| 149 | + }) |
| 150 | + ); |
| 151 | + lastRequestId = requestId; |
| 152 | + emit RequestSent(requestId); |
| 153 | + } |
| 154 | + |
| 155 | + /// @dev Callback function called by the VRF Coordinator when the random value is generated. |
| 156 | + /// @param _requestId The ID of the request. |
| 157 | + /// @param _randomWords The random values answering the request. |
| 158 | + function fulfillRandomWords(uint256 _requestId, uint256[] calldata _randomWords) internal override { |
| 159 | + // Access control is handled by the parent VRFCoordinator.rawFulfillRandomWords() |
| 160 | + randomNumbers[_requestId] = _randomWords[0]; |
| 161 | + emit RequestFulfilled(_requestId, _randomWords[0]); |
| 162 | + } |
| 163 | + |
| 164 | + // ************************************* // |
| 165 | + // * Public Views * // |
| 166 | + // ************************************* // |
| 167 | + |
| 168 | + /// @dev Return the random number. |
| 169 | + /// @return randomNumber The random number or 0 if it is not ready or has not been requested. |
| 170 | + function receiveRandomness(uint256 /*_block*/) external view override returns (uint256 randomNumber) { |
| 171 | + randomNumber = randomNumbers[lastRequestId]; |
| 172 | + } |
| 173 | +} |
0 commit comments