@@ -10,12 +10,16 @@ import "./IRandomizer.sol";
1010 * https://randomizer.ai/
1111 */
1212contract RandomizerRNG is RNG {
13- uint256 public constant CALLBACK_GAS_LIMIT = 50000 ;
1413 address public governor; // The address that can withdraw funds.
14+ uint256 public callbackGasLimit = 50000 ; // Gas limit for the randomizer callback
1515
1616 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+ // ************************************* //
1923
2024 modifier onlyByGovernor () {
2125 require (governor == msg .sender , "Governor only " );
@@ -31,44 +35,70 @@ contract RandomizerRNG is RNG {
3135 governor = _governor;
3236 }
3337
38+ // ************************ //
39+ // * Governance * //
40+ // ************************ //
41+
3442 /** @dev Changes the governor of the contract.
3543 * @param _governor The new governor.
3644 */
3745 function changeGovernor (address _governor ) external onlyByGovernor {
3846 governor = _governor;
3947 }
4048
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+
4163 /**
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.
4366 */
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);
4769 }
4870
71+ // ************************************* //
72+ // * State Modifiers * //
73+ // ************************************* //
74+
4975 /**
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.
5277 */
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;
5781 }
5882
5983 /**
6084 * @dev Callback function called by the randomizer contract when the random value is generated.
6185 */
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 " );
6488 randomNumbers[_id] = uint256 (_value);
6589 }
6690
91+ // ************************************* //
92+ // * Public Views * //
93+ // ************************************* //
94+
6795 /**
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 .
7098 */
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];
73103 }
74104}
0 commit comments