@@ -10,12 +10,16 @@ import "./IRandomizer.sol";
10
10
* https://randomizer.ai/
11
11
*/
12
12
contract RandomizerRNG is RNG {
13
- uint256 public constant CALLBACK_GAS_LIMIT = 50000 ;
14
13
address public governor; // The address that can withdraw funds.
14
+ uint256 public callbackGasLimit = 50000 ; // Gas limit for the randomizer callback
15
15
16
16
IRandomizer public randomizer; // Randomizer address.
17
- mapping (uint128 => uint256 ) public randomNumbers; // randomNumbers[requestID] is the random number for this request id, 0 otherwise.
18
- mapping (address => uint128 ) public requesterToID; // Maps the requester to his latest request ID.
17
+ mapping (uint256 => uint256 ) public randomNumbers; // randomNumbers[requestID] is the random number for this request id, 0 otherwise.
18
+ mapping (address => uint256 ) public requesterToID; // Maps the requester to his latest request ID.
19
+
20
+ // ************************************* //
21
+ // * Function Modifiers * //
22
+ // ************************************* //
19
23
20
24
modifier onlyByGovernor () {
21
25
require (governor == msg .sender , "Governor only " );
@@ -31,44 +35,70 @@ contract RandomizerRNG is RNG {
31
35
governor = _governor;
32
36
}
33
37
38
+ // ************************ //
39
+ // * Governance * //
40
+ // ************************ //
41
+
34
42
/** @dev Changes the governor of the contract.
35
43
* @param _governor The new governor.
36
44
*/
37
45
function changeGovernor (address _governor ) external onlyByGovernor {
38
46
governor = _governor;
39
47
}
40
48
49
+ /** @dev Change the Randomizer callback gas limit.
50
+ * @param _callbackGasLimit the new limit.
51
+ */
52
+ function setCallbackGasLimit (uint256 _callbackGasLimit ) external onlyByGovernor {
53
+ callbackGasLimit = _callbackGasLimit;
54
+ }
55
+
56
+ /** @dev Change the Randomizer address.
57
+ * @param _randomizer the new Randomizer address.
58
+ */
59
+ function setRandomizer (address _randomizer ) external onlyByGovernor {
60
+ randomizer = IRandomizer (_randomizer);
61
+ }
62
+
41
63
/**
42
- * @dev Request a random number. The id of the request is tied to the sender.
64
+ * @dev Allows the governor to withdraw randomizer funds.
65
+ * @param _amount Amount to withdraw in wei.
43
66
*/
44
- function requestRandomness (uint256 /*_block*/ ) external override {
45
- uint128 id = uint128 (randomizer.request (CALLBACK_GAS_LIMIT));
46
- requesterToID[msg .sender ] = id;
67
+ function randomizerWithdraw (uint256 _amount ) external onlyByGovernor {
68
+ randomizer.clientWithdrawTo (msg .sender , _amount);
47
69
}
48
70
71
+ // ************************************* //
72
+ // * State Modifiers * //
73
+ // ************************************* //
74
+
49
75
/**
50
- * @dev Return the random number.
51
- * @return randomNumber The random number or 0 if it is not ready or has not been requested.
76
+ * @dev Request a random number. The id of the request is tied to the sender.
52
77
*/
53
- function receiveRandomness (uint256 /*_block*/ ) external view override returns (uint256 randomNumber ) {
54
- // Get the latest request ID for this requester.
55
- uint128 id = requesterToID[msg .sender ];
56
- randomNumber = randomNumbers[id];
78
+ function requestRandomness (uint256 /*_block*/ ) external override {
79
+ uint256 id = randomizer.request (callbackGasLimit);
80
+ requesterToID[msg .sender ] = id;
57
81
}
58
82
59
83
/**
60
84
* @dev Callback function called by the randomizer contract when the random value is generated.
61
85
*/
62
- function randomizerCallback (uint128 _id , bytes32 _value ) external {
63
- require (msg .sender == address (randomizer), "Caller not Randomizer " );
86
+ function randomizerCallback (uint256 _id , bytes32 _value ) external {
87
+ require (msg .sender == address (randomizer), "Randomizer only " );
64
88
randomNumbers[_id] = uint256 (_value);
65
89
}
66
90
91
+ // ************************************* //
92
+ // * Public Views * //
93
+ // ************************************* //
94
+
67
95
/**
68
- * @dev Allows the governor to withdraw randomizer funds .
69
- * @param _amount Amount to withdraw in wei .
96
+ * @dev Return the random number .
97
+ * @return randomNumber The random number or 0 if it is not ready or has not been requested .
70
98
*/
71
- function randomizerWithdraw (uint256 _amount ) external onlyByGovernor {
72
- randomizer.clientWithdrawTo (msg .sender , _amount);
99
+ function receiveRandomness (uint256 /*_block*/ ) external view override returns (uint256 randomNumber ) {
100
+ // Get the latest request ID for this requester.
101
+ uint256 id = requesterToID[msg .sender ];
102
+ randomNumber = randomNumbers[id];
73
103
}
74
104
}
0 commit comments