The GetRandomNumberExample
contract is an example of how to use the GeneralRandcastConsumerBase
to request and consume randomness of type Randomness
.
Copy // SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import {
GeneralRandcastConsumerBase ,
BasicRandcastConsumerBase
} from "randcast-user-contract/user/GeneralRandcastConsumerBase.sol" ;
contract GetRandomNumberExample is GeneralRandcastConsumerBase {
/* requestId -> randomness */
mapping ( bytes32 => uint256 ) public randomResults;
uint256 [] public randomnessResults;
constructor ( address adapter) BasicRandcastConsumerBase (adapter) {}
/**
* Requests randomness
*/
function getRandomNumber () external returns ( bytes32 ) {
bytes memory params;
return requestRandomness (RequestType.Randomness , params);
}
/**
* Callback function used by Randcast Adapter
*/
function fulfillRandomness ( bytes32 requestId , uint256 randomness) internal override {
randomResults[requestId] = randomness;
randomnessResults. push (randomness);
}
function lengthOfRandomnessResults () public view returns ( uint256 ) {
return randomnessResults.length;
}
function lastRandomnessResult () public view returns ( uint256 ) {
return randomnessResults[randomnessResults.length - 1 ];
}
}
Here's a step-by-step explanation of the GetRandomNumberExample
contract:
Inherit the GeneralRandcastConsumerBase
contract:
Copy contract GetRandomNumberExample is GeneralRandcastConsumerBase { ... }
Define a mapping randomResults
to store randomness associated with each request ID:
Copy mapping ( bytes32 => uint256 ) public randomResults;
Define an array randomnessResults
to store all received randomness values:
Copy uint256 [] public randomnessResults;
Implement the constructor to initialize the adapter
address from the GeneralRandcastConsumerBase
contract:
Copy constructor ( address adapter) BasicRandcastConsumerBase (adapter) {}
Provide the getRandomNumber()
function to request randomness:
Copy function getRandomNumber () external returns ( bytes32 ) {
bytes memory params;
return requestRandomness (RequestType.Randomness , params);
}
This function calls the requestRandomness()
function from the GeneralRandcastConsumerBase
contract, which sends the randomness request to the adapter
.
Override the fulfillRandomness()
callback function:
Copy function fulfillRandomness ( bytes32 requestId , uint256 randomness) internal override {
randomResults[requestId] = randomness;
randomnessResults. push (randomness);
}
When the adapter
fulfills the randomness request, this function is called with the requestId
and the generated randomness
. The randomness value is stored in both the randomResults
mapping and the randomnessResults
array.
Provides helper functions to retrieve the length of the randomnessResults
array and the last randomness result:
Copy function lengthOfRandomnessResults () public view returns ( uint256 ) {
return randomnessResults.length;
}
function lastRandomnessResult () public view returns ( uint256 ) {
return randomnessResults[randomnessResults.length - 1 ];
}
These functions allow users to query the length of the randomnessResults
array and retrieve the last received randomness value.
Next, we will look at another example that uses the received random number as an entropy to throw a dice.