Skip to content
Lattice

API3QRNGAdapterLib

Git Source

Title: API3QRNGAdapterLib

Authors: David Dada <daveproxy80@gmail.com> (https://github.com/dadadave80), Modified from API3 (https://github.com/api3dao/airnode)

Library that wraps API3 QRNG (quantum randomness) over the Airnode Request-Response Protocol (self-sponsoring). This is the request/track layer only — fulfillment bookkeeping is handled here, but consumer facets inheriting API3QRNGAdapter should override fulfillRandomNumber (calling super.fulfillRandomNumber first) to actually consume the delivered randomness.

Functions

api3QRNGAdapterStorage

Returns the ERC-7201 storage struct for API3QRNGAdapter.

function api3QRNGAdapterStorage() internal pure returns (API3QRNGAdapterStorage storage $);

Returns

NameTypeDescription
$API3QRNGAdapterStorage

__API3QRNGAdapter_init

Registers the IAPI3QRNGAdapter ERC-165 interface.

Must be called between preInitializer / postInitializer.

function __API3QRNGAdapter_init() internal;

registerInterface

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

function registerInterface() internal;

getConfig

Returns the current QRNG configuration.

function getConfig() internal view returns (IAPI3QRNGAdapter.QRNGConfig memory config);

Returns

NameTypeDescription
configIAPI3QRNGAdapter.QRNGConfig

getUserKey

Returns the user key associated with a pending request.

function getUserKey(bytes32 requestId) internal view returns (bytes32);

Parameters

NameTypeDescription
requestIdbytes32The RRP-assigned request ID.

Returns

NameTypeDescription
<none>bytes32

setConfig

Sets or replaces the QRNG configuration.

Caller must hold DEFAULT_ADMIN_ROLE. Reverts QRNGInvalidConfig if any field is zero.

function setConfig(IAPI3QRNGAdapter.QRNGConfig calldata c) internal;

Parameters

NameTypeDescription
cIAPI3QRNGAdapter.QRNGConfigThe new QRNG configuration.

setSelfSponsorship

Registers (or revokes) this contract as its own sponsor with the Airnode RRP.

Caller must hold DEFAULT_ADMIN_ROLE. Reverts QRNGNotConfigured if the Airnode RRP has not been set.

function setSelfSponsorship(bool status) internal;

Parameters

NameTypeDescription
statusboolTrue to self-sponsor, false to revoke.

requestRandomNumber

Requests a quantum random number via the Airnode RRP.

Caller must hold DEFAULT_ADMIN_ROLE. Stores the userKey against the returned requestId so the fulfillment callback can look it up. Reverts QRNGNotConfigured if the Airnode RRP has not been set.

function requestRandomNumber(bytes32 userKey) internal returns (bytes32 requestId);

Parameters

NameTypeDescription
userKeybytes32Arbitrary key the caller wants associated with this request.

Returns

NameTypeDescription
requestIdbytes32The RRP-assigned request ID.

fulfillRandomNumber

Called by the Airnode RRP to deliver the random number.

Verifies the caller is the configured Airnode RRP by its address (msg.sender), looks up the user key, clears the pending entry, decodes the random number, and emits RandomNumberFulfilled.

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

function fulfillRandomNumber(bytes32 requestId, bytes calldata data) internal;

Parameters

NameTypeDescription
requestIdbytes32The RRP-assigned request ID.
databytesThe ABI-encoded random number (abi.encode(uint256)).

_requireAirnodeRrp

Returns the configured Airnode RRP address, reverting if unset.

function _requireAirnodeRrp() private view returns (address r);

Returns

NameTypeDescription
raddress