Skip to content
Lattice

ChainlinkVRFLib

Git Source

Title: ChainlinkVRFLib

Authors: David Dada <daveproxy80@gmail.com> (https://github.com/dadadave80), Modified from Chainlink (https://github.com/smartcontractkit/chainlink-evm/blob/develop/contracts/src/v0.8/vrf/VRFConsumerBaseV2Plus.sol)

Library that wraps Chainlink VRF V2.5 (subscription-funded) random word requests. This is the request/track layer only — fulfillment bookkeeping is handled here, but consumer facets inheriting ChainlinkVRF should override rawFulfillRandomWords (or add a follow-up hook) to actually consume the delivered randomness.

Functions

chainlinkVRFStorage

Returns the ERC-7201 storage struct for ChainlinkVRF.

function chainlinkVRFStorage() internal pure returns (ChainlinkVRFStorage storage $);

Returns

NameTypeDescription
$ChainlinkVRFStorage

__ChainlinkVRF_init

Registers the IChainlinkVRF ERC-165 interface.

Must be called between preInitializer / postInitializer.

function __ChainlinkVRF_init() internal;

registerInterface

Writes true to the ERC-165 map slot for IChainlinkVRF.

function registerInterface() internal;

getConfig

Returns the current VRF configuration.

function getConfig() internal view returns (IChainlinkVRF.VRFConfig memory config);

Returns

NameTypeDescription
configIChainlinkVRF.VRFConfig

getUserKey

Returns the user key associated with a pending VRF request.

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

Parameters

NameTypeDescription
vrfRequestIduint256The coordinator-assigned request ID.

Returns

NameTypeDescription
<none>bytes32

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(IChainlinkVRF.VRFConfig calldata config) internal;

Parameters

NameTypeDescription
configIChainlinkVRF.VRFConfigThe 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 fulfillment callback can look it up. Reverts VRFNotConfigured if coordinator has not been set.

function requestRandomWords(bytes32 userKey, uint32 numWords) internal 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 VRF coordinator by its address (msg.sender), looks up the user key, clears the pending entry, and emits RandomWordsFulfilled.

NOTE: This function only manages bookkeeping. Consumer facets that inherit ChainlinkVRF should override rawFulfillRandomWords (or provide a follow-up hook) to act on the delivered randomness.

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

Parameters

NameTypeDescription
requestIduint256The coordinator-assigned request ID.
randomWordsuint256[]The array of random words (unused at this layer).