Skip to content
Lattice

IChainlinkVRF

Git Source

Title: IChainlinkVRF

Author: Modified from Chainlink (https://github.com/smartcontractkit/chainlink-evm/blob/develop/contracts/src/v0.8/vrf/VRFConsumerBaseV2Plus.sol)

Interface for the ChainlinkVRF Diamond facet.

This module handles request tracking for Chainlink VRF V2.5 (subscription-funded). The rawFulfillRandomWords entry point is called by the coordinator and manages bookkeeping only; consumer facets that inherit ChainlinkVRF should override it to act on the randomness.

Functions

getConfig

Returns the current VRF configuration.

function getConfig() external view returns (VRFConfig memory);

Returns

NameTypeDescription
<none>VRFConfig

getUserKey

Returns the user key associated with a pending VRF request.

function getUserKey(uint256 vrfRequestId) external view returns (bytes32);

Parameters

NameTypeDescription
vrfRequestIduint256The coordinator-assigned request ID.

Returns

NameTypeDescription
<none>bytes32The user-supplied key, or bytes32(0) if not found.

setConfig

Sets or replaces the VRF configuration.

Caller must hold DEFAULT_ADMIN_ROLE. Reverts VRFInvalidConfig if coordinator is zero, subscriptionId is zero, or keyHash is zero.

function setConfig(VRFConfig calldata config) external;

Parameters

NameTypeDescription
configVRFConfigThe new VRF configuration.

requestRandomWords

Requests random words from the VRF coordinator.

Caller must hold DEFAULT_ADMIN_ROLE. Stores the userKey against the returned vrfRequestId so the fulfilment callback can look it up.

function requestRandomWords(bytes32 userKey, uint32 numWords) external returns (uint256 vrfRequestId);

Parameters

NameTypeDescription
userKeybytes32Arbitrary key the caller wants associated with this request.
numWordsuint32Number of random words to request.

Returns

NameTypeDescription
vrfRequestIduint256The coordinator-assigned request ID.

rawFulfillRandomWords

Called by the VRF coordinator to deliver random words.

Verifies the caller is the configured coordinator, looks up the user key, clears the pending entry, and emits RandomWordsFulfilled. Consumer facets should override this function to consume the randomness.

function rawFulfillRandomWords(uint256 requestId, uint256[] calldata randomWords) external;

Parameters

NameTypeDescription
requestIduint256The coordinator-assigned request ID.
randomWordsuint256[]The generated random words.

Events

VRFConfigSet

Emitted when the VRF configuration is updated.

event VRFConfigSet(address coordinator, uint256 subscriptionId, bytes32 keyHash);

Parameters

NameTypeDescription
coordinatoraddressAddress of the VRF coordinator.
subscriptionIduint256Subscription ID funding the requests.
keyHashbytes32Key hash identifying the oracle's VRF public key.

RandomWordsRequested

Emitted when random words are requested from the coordinator.

event RandomWordsRequested(uint256 indexed vrfRequestId, bytes32 indexed userKey, uint32 numWords);

Parameters

NameTypeDescription
vrfRequestIduint256The coordinator-assigned request ID.
userKeybytes32The caller-supplied key associated with this request.
numWordsuint32The number of random words requested.

RandomWordsFulfilled

Emitted when the coordinator fulfils a request.

event RandomWordsFulfilled(uint256 indexed vrfRequestId, bytes32 indexed userKey);

Parameters

NameTypeDescription
vrfRequestIduint256The coordinator-assigned request ID.
userKeybytes32The caller-supplied key originally associated with this request.

Errors

VRFNotConfigured

VRF has not been configured (coordinator address is zero).

error VRFNotConfigured();

VRFOnlyCoordinator

rawFulfillRandomWords was called by an address other than the coordinator.

error VRFOnlyCoordinator(address caller);

Parameters

NameTypeDescription
calleraddressThe unauthorised caller.

VRFRequestNotFound

The given VRF request ID has no pending entry.

error VRFRequestNotFound(uint256 vrfRequestId);

Parameters

NameTypeDescription
vrfRequestIduint256The unknown request ID.

VRFInvalidConfig

setConfig was called with an invalid configuration (zero coordinator, zero subscription ID, or zero key hash).

error VRFInvalidConfig();

VRFInvalidUserKey

requestRandomWords was called with a zero userKey.

bytes32(0) is the sentinel used by rawFulfillRandomWords to detect a missing pending request. Allowing a zero userKey would make the fulfillment callback indistinguishable from an unknown request ID.

error VRFInvalidUserKey();

Structs

VRFConfig

Full VRF subscription configuration.

struct VRFConfig {
        /// @notice Address of the VRF coordinator.
        address coordinator;
        /// @notice Subscription ID funding requests.
        uint256 subscriptionId;
        /// @notice Key hash identifying the oracle's VRF public key.
        bytes32 keyHash;
        /// @notice Minimum confirmations before the oracle responds.
        uint16 requestConfirmations;
        /// @notice Gas limit for the fulfillment callback.
        uint32 callbackGasLimit;
    }

Properties

NameTypeDescription
coordinatoraddress
subscriptionIduint256
keyHashbytes32
requestConfirmationsuint16
callbackGasLimituint32