-
Notifications
You must be signed in to change notification settings - Fork 1
/
VRF.sol
173 lines (135 loc) · 4.98 KB
/
VRF.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
import "@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol";
import "@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol";
/**
* @notice A Chainlink VRF consumer which uses randomness to mimic the rolling
* of a 20 sided dice
*/
contract VRFD20 is VRFConsumerBaseV2 {
uint256 private constant ROLL_IN_PROGRESS = 42;
VRFCoordinatorV2Interface COORDINATOR;
// Your subscription ID.
uint64 s_subscriptionId;
// Sepolia coordinator.
address vrfCoordinator = 0x8103B0A8A00be2DDC778e6e7eaa21791Cd364625;
// The gas lane to use, which specifies the maximum gas price to bump to.
bytes32 s_keyHash =
0x79d3d8832d904592c0bf9818b621522c988bb8b0c05cdc3b15aea1b6e8db0c15;
// Depends on the number of requested values that you want sent to the
// fulfillRandomWords() function.
uint32 callbackGasLimit = 40000;
// The default is 3, but you can set this higher.
uint16 requestConfirmations = 3;
// For this example, retrieve 1 random value in one request.
// Cannot exceed VRFCoordinatorV2.MAX_NUM_WORDS.
uint32 numWords = 1;
address s_owner;
// map rollers to requestIds
mapping(uint256 => address) private s_rollers;
// map vrf results to rollers
mapping(address => uint256) private s_results;
event DiceRolled(uint256 indexed requestId, address indexed roller);
event DiceLanded(uint256 indexed requestId, uint256 indexed result);
/**
* @notice Constructor inherits VRFConsumerBaseV2
*
* @dev NETWORK: Sepolia
*
* @param subscriptionId subscription id that this consumer contract can use
*/
constructor(uint64 subscriptionId) VRFConsumerBaseV2(vrfCoordinator) {
COORDINATOR = VRFCoordinatorV2Interface(vrfCoordinator);
s_owner = msg.sender;
s_subscriptionId = subscriptionId;
}
/**
* @notice Requests randomness
* @dev Warning: if the VRF response is delayed, avoid calling requestRandomness repeatedly
* as that would give miners/VRF operators latitude about which VRF response arrives first.
* @dev You must review your implementation details with extreme care.
*
* @param roller address of the roller
*/
function rollDice(
address roller
) public onlyOwner returns (uint256 requestId) {
require(s_results[roller] == 0, "Already rolled");
// Will revert if subscription is not set and funded.
requestId = COORDINATOR.requestRandomWords(
s_keyHash,
s_subscriptionId,
requestConfirmations,
callbackGasLimit,
numWords
);
s_rollers[requestId] = roller;
s_results[roller] = ROLL_IN_PROGRESS;
emit DiceRolled(requestId, roller);
}
/**
* @notice Callback function used by VRF Coordinator to return the random number to this contract.
*
* @dev Some action on the contract state should be taken here, like storing the result.
* @dev WARNING: take care to avoid having multiple VRF requests in flight if their order of arrival would result
* in contract states with different outcomes. Otherwise miners or the VRF operator would could take advantage
* by controlling the order.
* @dev The VRF Coordinator will only send this function verified responses, and the parent VRFConsumerBaseV2
* contract ensures that this method only receives randomness from the designated VRFCoordinator.
*
* @param requestId uint256
* @param randomWords uint256[] The random result returned by the oracle.
*/
function fulfillRandomWords(
uint256 requestId,
uint256[] memory randomWords
) internal override {
uint256 d20Value = (randomWords[0] % 20) + 1;
s_results[s_rollers[requestId]] = d20Value;
emit DiceLanded(requestId, d20Value);
}
/**
* @notice Get the house assigned to the player once the address has rolled
* @param player address
* @return house as a string
*/
function house(address player) public view returns (string memory) {
require(s_results[player] != 0, "Dice not rolled");
require(s_results[player] != ROLL_IN_PROGRESS, "Roll in progress");
return getHouseName(s_results[player]);
}
/**
* @notice Get the house name from the id
* @param id uint256
* @return house name string
*/
function getHouseName(uint256 id) private pure returns (string memory) {
string[20] memory houseNames = [
"Targaryen",
"Lannister",
"Stark",
"Tyrell",
"Baratheon",
"Martell",
"Tully",
"Bolton",
"Greyjoy",
"Arryn",
"Frey",
"Mormont",
"Tarley",
"Dayne",
"Umber",
"Valeryon",
"Manderly",
"Clegane",
"Glover",
"Karstark"
];
return houseNames[id - 1];
}
modifier onlyOwner() {
require(msg.sender == s_owner);
_;
}
}