-
Notifications
You must be signed in to change notification settings - Fork 1
/
RandomNumberGenerator.sol
97 lines (82 loc) · 3.47 KB
/
RandomNumberGenerator.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// This example code is designed to quickly deploy an example contract using Remix.
pragma solidity 0.6.6;
import "https://raw.githubusercontent.com/smartcontractkit/chainlink/master/evm-contracts/src/v0.6/VRFConsumerBase.sol";
contract RandomNumberConsumer is VRFConsumerBase {
bytes32 internal keyHash;
uint256 internal fee;
uint256 public randomResult;
address[] whitelist;
uint whitelistCount;
address public owner;
modifier isWhitelist() {
bool pass = false;
for (uint i=0; i<whitelistCount; i++){
if (msg.sender == whitelist[i]){
pass = true;
break;
}
}
require(pass, "Message sender not found in whitelist!");
_;
}
modifier onlyOwner() {
require(msg.sender == owner, "Only the owner can call this function!");
_;
}
/**
* Constructor inherits VRFConsumerBase
*
* Network: Mumbai
* Chainlink VRF Coordinator address: 0x8C7382F9D8f56b33781fE506E897a4F1e2d17255
* LINK token address: 0x326C977E6efc84E512bB9C30f76E30c160eD06FB
* Key Hash: 0x6e75b569a01ef56d18cab6a8e71e6600d6ce853834d4a5748b720d06f878b3a4
*/
constructor()
VRFConsumerBase(
0x8C7382F9D8f56b33781fE506E897a4F1e2d17255, // VRF Coordinator
0x326C977E6efc84E512bB9C30f76E30c160eD06FB // LINK Token
) public
{
keyHash = 0x6e75b569a01ef56d18cab6a8e71e6600d6ce853834d4a5748b720d06f878b3a4;
fee = 0.1 * 10 ** 18; // 0.1 LINK
owner = msg.sender;
}
/**
* Requests randomness from a user-provided seed
************************************************************************************
* STOP! *
* THIS FUNCTION WILL FAIL IF THIS CONTRACT DOES NOT OWN LINK *
* ---------------------------------------------------------- *
* Learn how to obtain testnet LINK and fund this contract: *
* ------- https://docs.chain.link/docs/acquire-link -------- *
* ---- https://docs.chain.link/docs/fund-your-contract ----- *
* *
************************************************************************************/
function getRandomNumber(uint256 userProvidedSeed) public isWhitelist returns (bytes32 requestId) {
require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - fill contract with faucet");
return requestRandomness(keyHash, fee, userProvidedSeed);
}
/**
* Callback function used by VRF Coordinator
*/
function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
randomResult = randomness;
}
function seeRandomNumber() external returns(uint) {
return randomResult;
}
/**
* Withdraw LINK from this contract
*
*/
function withdrawLink(address _address) external onlyOwner {
require(LINK.transfer(_address, LINK.balanceOf(address(this))), "Unable to transfer");
}
function addToWhitelist(address _address) external onlyOwner{
whitelist.push(_address);
whitelistCount++;
}
function transferOwnership(address _address) external onlyOwner {
owner = _address;
}
}