@@ -4,21 +4,25 @@ pragma solidity ^0.8.7;
4
4
import "@chainlink/contracts/src/v0.8/vrf/VRFV2WrapperConsumerBase.sol " ;
5
5
import "@chainlink/contracts/src/v0.8/shared/access/ConfirmedOwner.sol " ;
6
6
7
- /**
8
- * Spin the wheel to get a random number from chainlink VRF
7
+ /** Simple contract that request random numbers from chainlink VRF
8
+ *
9
+ * this example uses the "Direct Funding" method
10
+ * https://docs.chain.link/vrf#two-methods-to-request-randomness
9
11
*/
10
12
11
13
contract VRFConsumer is VRFV2WrapperConsumerBase , ConfirmedOwner {
12
- // State Variables
14
+ // if gas consumed by fulfillRandomWords() exceeds this limit, the transaction will revert
15
+ uint32 callbackGasLimit = 100000 ;
16
+ // blocks before chainlink node responds (must be greater than a minimum amout set by VRF coordinator contract)
17
+ uint16 requestConfirmations = 3 ;
18
+ // how many random numbers to generate
19
+ uint32 numValues = 1 ;
13
20
address public linkAddress;
14
- uint32 callbackGasLimit = 100000 ; // limit for gas can be used when chainlink node calls fulfillRandomWords()
15
- uint16 requestConfirmations = 3 ; // blocks before chainlink node responds (must be greater than a minimum amout set by VRF coordinator contract)
16
- uint32 numValues = 1 ; // how many random numbers to generate
17
21
22
+ // keeping track of who requested which random number
18
23
mapping (uint256 => address ) public s_spinners; // requestId => msg.sender
19
24
mapping (address => uint256 ) public s_results; // msg.sender => random number
20
25
21
- // Events
22
26
event WheelSpun (uint256 indexed requestId , address indexed spinner );
23
27
event WheelResult (
24
28
uint256 indexed requestId ,
@@ -38,7 +42,7 @@ contract VRFConsumer is VRFV2WrapperConsumerBase, ConfirmedOwner {
38
42
39
43
/** This function triggers the request to chainlink node that generates the random number
40
44
*
41
- * " requestRandomness()" is inherited from VRFV2WrapperConsumerBase
45
+ * requestRandomness() is inherited from VRFV2WrapperConsumerBase
42
46
*
43
47
* @return requestId each request has a unique ID
44
48
*/
@@ -68,7 +72,6 @@ contract VRFConsumer is VRFV2WrapperConsumerBase, ConfirmedOwner {
68
72
uint256 requestId ,
69
73
uint256 [] memory randomWords
70
74
) internal override {
71
- // The remainder of division by 6 can only be 0 - 5
72
75
uint256 randomNumber = (randomWords[0 ] % 6 );
73
76
// update mapping to record who received which random number by using the requestId
74
77
s_results[s_spinners[requestId]] = randomNumber;
@@ -83,7 +86,6 @@ contract VRFConsumer is VRFV2WrapperConsumerBase, ConfirmedOwner {
83
86
);
84
87
}
85
88
86
- // Getters
87
89
function getLinkBalance () public view returns (uint256 ) {
88
90
LinkTokenInterface link = LinkTokenInterface (linkAddress);
89
91
return link.balanceOf (address (this ));
0 commit comments