GeneralRandcastConsumerBase

The GeneralRandcastConsumerBase contract aims to simplify the process of requesting and consuming randomness provided by Randacst. This contract combines the following aspects: requestId generation, gas estimation, callback handling, and gas limit/gas fee configuration.


Here is a detailed breakdown of what the GeneralRandcastConsumerBase contract does:

Imports the necessary contracts and libraries:

import "../utils/RequestIdBase.sol";
import "../utils/GasEstimationBase.sol";
import "./BasicRandcastConsumerBase.sol";
import "openzeppelin-contracts/contracts/access/Ownable.sol";

RequestIdBase helps generate unique request IDs for randomness requests.

GasEstimationBase aids in calculating the gas needed for the callback functions.

BasicRandcastConsumerBase is the base contract for handling randomness requests and fulfillment.

Lastly, OpenZeppelin's Ownable contract is used for access control.


Inherits the imported contracts and libraries:

abstract contract GeneralRandcastConsumerBase is
    BasicRandcastConsumerBase,
    RequestIdBase,
    GasEstimationBase,
    Ownable
{ ... }

Sets important constants:

uint256 private constant USER_SEED_PLACEHOLDER = 0;
uint256 private constant GAS_FOR_CALLBACK_OVERHEAD = 30_000;
uint256 private constant RANDOMNESS_PLACEHOLDER = 103921425973949831153159651530394295952228049817797655588722524414385831936256;

The variables represent the user seed placeholder, the gas overhead for callback execution, and a placeholder for initial entropy.


Defines public variables for callback gas limit, max gas fee, and request confirmations(blocks):

uint256 public callbackGasLimit;
uint256 public callbackMaxGasFee;
uint16 public requestConfirmations;

Note: These variables can be set manually or calculated automatically.


Provides the setCallbackGasConfig() function and setRequestConfirmations() function to allow the owner to set the callbackGasLimit, callbackMaxGasFee and requestConfirmations manually:

function setCallbackGasConfig(uint256 _callbackGasLimit, uint256 _callbackMaxGasFee) external onlyOwner {
    callbackGasLimit = _callbackGasLimit;
    callbackMaxGasFee = _callbackMaxGasFee;
}

function setRequestConfirmations(uint16 _requestConfirmations) external onlyOwner {
    requestConfirmations = _requestConfirmations;
}

Implements the requestRandomness() function for requesting randomness from the Adapter:

function _requestRandomness(RequestType requestType, bytes memory params)
    internal
    returns (bytes32)
{ ... }

This function takes requestType and params as arguments and estimates the required gas for the user-implemented callback function when callbackGasLimit is not set.


Estimates the required gas for the callback function:

function _dryRunCallbackToEstimateGas(RequestType requestType, bytes memory params, uint64 subId)
    internal
    isDryRun
    returns (uint256)
{
    // Prepares the message call of the callback function according to the request type
    bytes memory data;
    ...
}

When the callbackGasLimit is not set, this function will estimate the required gas for the callback function based on the request type.


Sends a raw randomness request to the Adapter:

return _rawRequestRandomness(
      requestType,
      params,
      subId,
      _USER_SEED_PLACEHOLDER,
      requestConfirmations,
      callbackGasLimit,
      callbackMaxGasFee == 0 ? tx.gasprice * 3 : callbackMaxGasFee
);   

The rawRequestRandomness() function sends the randomness request to the Adapter with the proper parameters, including the request type, params, subscription ID, user seed, request confirmations, callback gas limit, and callback max gas price. If the callbackMaxGasFee is not set, it defaults to three times the transaction gas price. If requestConfirmations is not set, it defaults to minimumRequestConfirmations in the Adapter config.


In summary, the GeneralRandcastConsumerBase contract provides an extendable base for contracts that need to request randomness from Randcast. It streamlines requesting and receiving randomness by automating nonce management, gas estimation, and callback handling.

Additionally, it allows manual configuration of requestConfirmations, callback gas limits and fees for flexibility.

Note: To know more about the implementation, please refer to the full source code of the smart contract.

Next, we will look at an example of requesting a random number by extending GeneralRandcastConsumerBase.

Last updated