Created
November 23, 2022 17:23
-
-
Save a2468834/4c4c805e0f47bd95747559a27ecb5972 to your computer and use it in GitHub Desktop.
A smart contract that can interact with Chainlink VRF V1.
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
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity ^0.8.1; | |
import "@chainlink/contracts/src/v0.8/VRFConsumerBase.sol"; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | |
/** | |
* @dev | |
* Purpose: Retrieve an `uint256` random number secured by Chainlink VRF v1 | |
* Usage example: | |
* Step-1: EOA calls `makeRequest()` | |
* Step-2: Waits for a few blocks (VRF coordinators processe the verification procedure.) | |
* Step-3: Once you watched a new `FulfillRandom` event, the `randomness` is ready for using. | |
*/ | |
contract MyRandom is VRFConsumerBase, Ownable { | |
/** | |
* @dev | |
* [Network] | |
* Mainnet, Goerli, or Polygon | |
* | |
* [Docs] | |
* https://docs.chain.link/vrf/v2/introduction | |
*/ | |
struct Randomness { | |
bytes32 request_id; // Chainlink request id | |
uint256 random_num; | |
} | |
// Constants | |
bytes32 public immutable key_hash; | |
uint256 public immutable fee_amount; | |
// State variables | |
Randomness public randomness; | |
uint256 public history_id; | |
mapping(uint256 => Randomness) public randomness_history; | |
// Events | |
event MakeRequest(bytes32 indexed request_id, uint256 indexed block_number); | |
event FulfillRandom(bytes32 indexed request_id, uint256 indexed random_number); | |
constructor( | |
address _LINK_token, | |
address _VRF_coordinator, | |
bytes32 _key_hash, | |
uint256 _fee_amount | |
) VRFConsumerBase( | |
_VRF_coordinator, | |
_LINK_token | |
) { | |
key_hash = _key_hash; | |
fee_amount = _fee_amount; | |
} | |
/** | |
* @dev | |
* Make a request for the VRF coordinator. | |
* Each request only gets one random number. | |
* You must wait for a few blocks, then VRF coordinator will call 'fulfillRandomness()'. | |
*/ | |
function makeRequest() public onlyOwner { | |
bytes32 request_id; | |
require(getLINKBalanceOf() >= fee_amount, "This contract doesn't have enough LINK tokens."); | |
request_id = requestRandomness(key_hash, fee_amount); | |
emit MakeRequest(request_id, block.number); | |
} | |
/** | |
* @dev | |
* Because only VRF coordinator can invoke `rawFulfillRandomness()`, so this | |
* function can be invoked indirectly by VRF coordinator as well. After | |
* successfully executed, the "new" random number will be ready to use. | |
*/ | |
function fulfillRandomness(bytes32 _request_id, uint256 _random_num) internal override(VRFConsumerBase) { | |
// Update the data records | |
randomness.request_id = _request_id; | |
randomness.random_num = _random_num; | |
randomness_history[history_id] = randomness; | |
// Emit an event | |
emit FulfillRandom(_request_id, _random_num); | |
// Update history id | |
history_id++; | |
} | |
function getLINKBalanceOf() public view returns (uint256) { | |
return LINK.balanceOf(address(this)); | |
} | |
function withdrawLINK() external onlyOwner { | |
LINK.transfer(owner(), LINK.balanceOf(address(this))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment