-
Notifications
You must be signed in to change notification settings - Fork 49
Chainlink RNG #1778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Chainlink RNG #1778
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
14d46fa
feat: chainlink rng, updated randomizer rng for consistency
jaybuidl 09a13de
chore: prettier plugin missing in config
jaybuidl b0fba7f
feat: chainlink vrf rng deploy script
jaybuidl 2866a7a
test: chainlink vrf testing with coordinator mock
jaybuidl 3f2baa0
chore: randomizer rng does not need to be upgradable
jaybuidl 555f04a
feat: added support for the chainlink rng in the keeper bot
jaybuidl 0e2264a
chore: deploy scripts now use the chainlink rng
jaybuidl eabb03b
chore: chainlink RNG deployed, not used by the sortition module yet
jaybuidl a5944f5
fix: workaround graphql-request ESM import in keeper-bot
jaybuidl 03749f3
fix: rng deploy script
jaybuidl 83c4367
test: added tests for concurrent requests for both chainlink and rand…
jaybuidl e317649
Merge branch 'dev' into feat/chainlink-vrf-as-rng2
jaybuidl ab392c8
Merge branch 'dev' into feat/chainlink-vrf-as-rng2
jaybuidl 9e0f05b
chore: dependencies resolutions
jaybuidl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity 0.8.24; | ||
|
||
import {VRFConsumerBaseV2Plus, IVRFCoordinatorV2Plus} from "@chainlink/contracts/src/v0.8/vrf/dev/VRFConsumerBaseV2Plus.sol"; | ||
import {VRFV2PlusClient} from "@chainlink/contracts/src/v0.8/vrf/dev/libraries/VRFV2PlusClient.sol"; | ||
|
||
import "./RNG.sol"; | ||
|
||
/// @title Random Number Generator that uses Chainlink VRF v2.5 | ||
/// https://blog.chain.link/introducing-vrf-v2-5/ | ||
contract ChainlinkRNG is RNG, VRFConsumerBaseV2Plus { | ||
// ************************************* // | ||
// * Storage * // | ||
// ************************************* // | ||
|
||
address public governor; // The address that can withdraw funds. | ||
address public sortitionModule; // The address of the SortitionModule. | ||
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). | ||
uint256 public subscriptionId; // The unique identifier of the subscription used for funding requests. | ||
uint16 public requestConfirmations; // How many confirmations the Chainlink node should wait before responding. | ||
// 22 bytes remaining in slot | ||
uint32 public callbackGasLimit; // Gas limit for the Chainlink callback. | ||
uint256 lastRequestId; // The last request ID. | ||
mapping(uint256 requestId => uint256 number) public randomNumbers; // randomNumbers[requestID] is the random number for this request id, 0 otherwise. | ||
|
||
// ************************************* // | ||
// * Events * // | ||
// ************************************* // | ||
|
||
/// @dev Emitted when a request is sent to the VRF Coordinator | ||
/// @param requestId The ID of the request | ||
event RequestSent(uint256 indexed requestId); | ||
|
||
/// Emitted when a request has been fulfilled. | ||
/// @param requestId The ID of the request | ||
/// @param randomWord The random value answering the request. | ||
event RequestFulfilled(uint256 indexed requestId, uint256 randomWord); | ||
|
||
// ************************************* // | ||
// * Function Modifiers * // | ||
// ************************************* // | ||
|
||
modifier onlyByGovernor() { | ||
require(governor == msg.sender, "Governor only"); | ||
_; | ||
} | ||
|
||
modifier onlyBySortitionModule() { | ||
require(sortitionModule == msg.sender, "SortitionModule only"); | ||
_; | ||
} | ||
|
||
// ************************************* // | ||
// * Constructor * // | ||
// ************************************* // | ||
|
||
/// @dev Constructor, initializing the implementation to reduce attack surface. | ||
/// @param _governor The Governor of the contract. | ||
/// @param _sortitionModule The address of the SortitionModule contract. | ||
/// @param _vrfCoordinator The address of the VRFCoordinator contract. | ||
/// @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). | ||
/// @param _subscriptionId The unique identifier of the subscription used for funding requests. | ||
/// @param _requestConfirmations How many confirmations the Chainlink node should wait before responding. | ||
/// @param _callbackGasLimit The limit for how much gas to use for the callback request to the contract's fulfillRandomWords() function. | ||
/// @dev https://docs.chain.link/vrf/v2-5/subscription/get-a-random-number | ||
constructor( | ||
address _governor, | ||
address _sortitionModule, | ||
address _vrfCoordinator, | ||
bytes32 _keyHash, | ||
uint256 _subscriptionId, | ||
uint16 _requestConfirmations, | ||
uint32 _callbackGasLimit | ||
) VRFConsumerBaseV2Plus(_vrfCoordinator) { | ||
governor = _governor; | ||
sortitionModule = _sortitionModule; | ||
keyHash = _keyHash; | ||
subscriptionId = _subscriptionId; | ||
requestConfirmations = _requestConfirmations; | ||
callbackGasLimit = _callbackGasLimit; | ||
} | ||
|
||
// ************************************* // | ||
// * Governance * // | ||
// ************************************* // | ||
|
||
/// @dev Changes the governor of the contract. | ||
/// @param _governor The new governor. | ||
function changeGovernor(address _governor) external onlyByGovernor { | ||
governor = _governor; | ||
} | ||
|
||
/// @dev Changes the sortition module of the contract. | ||
/// @param _sortitionModule The new sortition module. | ||
function changeSortitionModule(address _sortitionModule) external onlyByGovernor { | ||
sortitionModule = _sortitionModule; | ||
} | ||
|
||
/// @dev Changes the VRF Coordinator of the contract. | ||
/// @param _vrfCoordinator The new VRF Coordinator. | ||
function changeVrfCoordinator(address _vrfCoordinator) external onlyByGovernor { | ||
s_vrfCoordinator = IVRFCoordinatorV2Plus(_vrfCoordinator); | ||
emit CoordinatorSet(_vrfCoordinator); | ||
} | ||
|
||
/// @dev Changes the key hash of the contract. | ||
/// @param _keyHash The new key hash. | ||
function changeKeyHash(bytes32 _keyHash) external onlyByGovernor { | ||
keyHash = _keyHash; | ||
} | ||
|
||
/// @dev Changes the subscription ID of the contract. | ||
/// @param _subscriptionId The new subscription ID. | ||
function changeSubscriptionId(uint256 _subscriptionId) external onlyByGovernor { | ||
subscriptionId = _subscriptionId; | ||
} | ||
|
||
/// @dev Changes the request confirmations of the contract. | ||
/// @param _requestConfirmations The new request confirmations. | ||
function changeRequestConfirmations(uint16 _requestConfirmations) external onlyByGovernor { | ||
requestConfirmations = _requestConfirmations; | ||
} | ||
|
||
/// @dev Changes the callback gas limit of the contract. | ||
/// @param _callbackGasLimit The new callback gas limit. | ||
function changeCallbackGasLimit(uint32 _callbackGasLimit) external onlyByGovernor { | ||
callbackGasLimit = _callbackGasLimit; | ||
} | ||
|
||
// ************************************* // | ||
// * State Modifiers * // | ||
// ************************************* // | ||
|
||
/// @dev Request a random number. SortitionModule only. | ||
function requestRandomness(uint256 /*_block*/) external override onlyBySortitionModule { | ||
// Will revert if subscription is not set and funded. | ||
uint256 requestId = s_vrfCoordinator.requestRandomWords( | ||
VRFV2PlusClient.RandomWordsRequest({ | ||
keyHash: keyHash, | ||
subId: subscriptionId, | ||
requestConfirmations: requestConfirmations, | ||
callbackGasLimit: callbackGasLimit, | ||
numWords: 1, | ||
extraArgs: VRFV2PlusClient._argsToBytes( | ||
// Set nativePayment to true to pay for VRF requests with ETH instead of LINK | ||
VRFV2PlusClient.ExtraArgsV1({nativePayment: true}) | ||
) | ||
}) | ||
); | ||
lastRequestId = requestId; | ||
emit RequestSent(requestId); | ||
} | ||
|
||
/// @dev Callback function called by the VRF Coordinator when the random value is generated. | ||
/// @param _requestId The ID of the request. | ||
/// @param _randomWords The random values answering the request. | ||
function fulfillRandomWords(uint256 _requestId, uint256[] calldata _randomWords) internal override { | ||
// Access control is handled by the parent VRFCoordinator.rawFulfillRandomWords() | ||
randomNumbers[_requestId] = _randomWords[0]; | ||
emit RequestFulfilled(_requestId, _randomWords[0]); | ||
} | ||
|
||
// ************************************* // | ||
// * Public Views * // | ||
// ************************************* // | ||
|
||
/// @dev Return the random number. | ||
/// @return randomNumber The random number or 0 if it is not ready or has not been requested. | ||
function receiveRandomness(uint256 /*_block*/) external view override returns (uint256 randomNumber) { | ||
randomNumber = randomNumbers[lastRequestId]; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.