Skip to content

Commit 370f1ba

Browse files
committed
feat: add ChainlinkRNG contract
1 parent 54df464 commit 370f1ba

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* @authors: [@MerlinEgalite]
3+
* @reviewers: []
4+
* @auditors: []
5+
* @bounties: []
6+
* @deployments: []
7+
*/
8+
pragma solidity ^0.6.6;
9+
10+
import "@chainlink/contracts/src/v0.6/VRFConsumerBase.sol";
11+
import "./RNG.sol";
12+
13+
/**
14+
* @title Random Number Generator using Chainlink Verifiable Randomness Mechanism
15+
* @author Merlin Egalite - <egalite.merlin@gmail.com>
16+
*
17+
* @dev This contract implements the RNG standard and inherits from VRFConsumerBase to use Chainlink Verifiable Randomness Mechanism.
18+
* @dev It allows to store the random number associated to the requests made.
19+
* @dev Note that to make requests to Chainlink, the contract needs to be funded with some LINK.
20+
* @dev Chainlink documentation: https://docs.chain.link/docs/chainlink-vrf/
21+
* @dev For SECURITY CONSIDERATIONS, you might also have look to: https://github.com/smartcontractkit/chainlink/blob/master/evm-contracts/src/v0.6/VRFConsumerBase.sol
22+
*/
23+
contract ChainlinkRNG is RNG, VRFConsumerBase {
24+
25+
/* Storage */
26+
27+
bytes32 internal keyHash; // The key hash to use for Chainlink's VRFCoordinator.
28+
uint256 internal fee; // The fee to pay to Chainlink.
29+
address public owner; // The owner of the contract.
30+
31+
mapping (bytes32 => uint256) public randomNumber; // randomNumber[requestId] is the random number for the requestId, 0 otherwise.
32+
33+
modifier onlyOwner() {
34+
require(msg.sender == owner, "Not the owner");
35+
}
36+
37+
/* Constructor */
38+
39+
/**
40+
* @dev Constructs the ChainlinkRNG contract.
41+
* @param _vrfCoordinator Address of VRFCoordinator contract.
42+
* @param _link Address of LINK token contract.
43+
* @param _keyHash The _keyHash for the VRF Coordinator.
44+
* @param _fee The amount of LINK to send with a request.
45+
*
46+
* @dev https://docs.chain.link/docs/link-token-contracts
47+
*/
48+
constructor(address _vrfCoordinator, address _link, bytes32 _keyHash, uint256 _fee)
49+
VRFConsumerBase(_vrfCoordinator, _link) public
50+
{
51+
keyHash = _keyHash;
52+
fee = _fee;
53+
owner = msg.sender;
54+
}
55+
56+
/**
57+
* @dev Withdraws all LINK tokens locked in this contract.
58+
*/
59+
function withdrawLink() external onlyOwner {
60+
require(LINK.transfer(msg.sender, LINK.balanceOf(address(this))), "Unable to transfer");
61+
}
62+
63+
/**
64+
* @dev Changes the fee used when requesting a new random number.
65+
*/
66+
function changeFee(uint256 _newFee) external onlyOwner {
67+
fee = _newFee;
68+
}
69+
70+
/**
71+
* @dev Requests a random number.
72+
* @dev The _seed parameter is vestigial, and is kept only for API
73+
* @dev compatibility with older versions. It can't *hurt* to mix in some of
74+
* @dev your own randomness, here, but it's not necessary because the VRF
75+
* @dev oracle will mix the hash of the block containing your request into the
76+
* @dev VRF seed it ultimately uses.
77+
* @param _seed seed mixed into the input of the VRF.
78+
* @return requestId unique ID for this request.
79+
*/
80+
function requestRN(uint _seed) public returns (uint256 requestId) {
81+
require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK to pay the fee");
82+
return requestRandomness(keyHash, fee, _seed);
83+
}
84+
85+
/**
86+
* @dev Returns the random number associatd to a request Id.
87+
* @param _requestId The Id initially returned by requestRN.
88+
* @return RN Random Number. If the number is not ready or has not been required it returns 0.
89+
*/
90+
function getRN(bytes32 _requestId) public returns (uint256 RN) {
91+
return randomNumber[_requestId];
92+
}
93+
94+
/**
95+
* @dev Stores the random number given by the VRF Coordinator.
96+
* @dev This is the callback function used by the VRF Coordinator.
97+
* @param _requestId The Id initially returned by requestRN.
98+
* @param _randomness the VRF output.
99+
*/
100+
function fulfillRandomness(bytes32 _requestId, uint256 _randomness) internal override {
101+
randomNumber[_requestId] = _randomness;
102+
}
103+
}

0 commit comments

Comments
 (0)