Skip to content
Lattice

PythEntropyAdapterLib

Git Source

Title: PythEntropyAdapterLib

Authors: David Dada <daveproxy80@gmail.com> (https://github.com/dadadave80), Modified from Pyth (https://github.com/pyth-network/pyth-crosschain)

Library that wraps Pyth Entropy on-demand (commit/reveal) randomness requests. This is the request/track layer only — fulfillment bookkeeping is handled here, but consumer facets inheriting PythEntropyAdapter should override entropyCallback (calling super.entropyCallback first) to actually consume the delivered randomness.

Functions

pythEntropyAdapterStorage

Returns the ERC-7201 storage struct for PythEntropyAdapter.

function pythEntropyAdapterStorage() internal pure returns (PythEntropyAdapterStorage storage $);

Returns

NameTypeDescription
$PythEntropyAdapterStorage

__PythEntropyAdapter_init

Registers the IPythEntropyAdapter ERC-165 interface.

Must be called between preInitializer / postInitializer.

function __PythEntropyAdapter_init() internal;

registerInterface

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

function registerInterface() internal;

getConfig

Returns the current Entropy configuration.

function getConfig() internal view returns (IPythEntropyAdapter.EntropyConfig memory config);

Returns

NameTypeDescription
configIPythEntropyAdapter.EntropyConfig

getFee

Returns the fee (in wei) required for one request under the current config.

Reverts EntropyNotConfigured if the Entropy contract has not been set.

function getFee() internal view returns (uint256);

Returns

NameTypeDescription
<none>uint256

getUserKey

Returns the user key associated with a pending request.

function getUserKey(uint64 sequenceNumber) internal view returns (bytes32);

Parameters

NameTypeDescription
sequenceNumberuint64The Entropy-assigned sequence number.

Returns

NameTypeDescription
<none>bytes32

setConfig

Sets or replaces the Entropy configuration.

Caller must hold DEFAULT_ADMIN_ROLE. Reverts EntropyInvalidConfig if the Entropy contract is zero.

function setConfig(IPythEntropyAdapter.EntropyConfig calldata config) internal;

Parameters

NameTypeDescription
configIPythEntropyAdapter.EntropyConfigThe new Entropy configuration.

requestRandomNumber

Requests a random number from Pyth Entropy, caller-funded with excess refunded.

Caller must hold DEFAULT_ADMIN_ROLE. Stores the userKey against the returned sequenceNumber so the fulfillment callback can look it up. Reverts EntropyNotConfigured if the Entropy contract has not been set, EntropyInvalidUserKey on a zero userKey, and EntropyInsufficientFee if msg.value is below the quoted fee.

function requestRandomNumber(bytes32 userKey, bytes32 userRandomNumber) internal returns (uint64 sequenceNumber);

Parameters

NameTypeDescription
userKeybytes32Arbitrary key the caller wants associated with this request.
userRandomNumberbytes32The caller's commitment to its half of the randomness.

Returns

NameTypeDescription
sequenceNumberuint64The Entropy-assigned sequence number.

entropyCallback

Called by the Entropy contract to deliver a random number.

Verifies the caller is the configured Entropy contract by its address (msg.sender), looks up the user key, clears the pending entry, and emits RandomNumberFulfilled.

NOTE: This function only manages bookkeeping. Consumer facets that inherit PythEntropyAdapter should override entropyCallback (calling super.entropyCallback first) to act on the delivered randomness.

function entropyCallback(uint64 sequence, address provider, bytes32 randomNumber) internal;

Parameters

NameTypeDescription
sequenceuint64The Entropy-assigned sequence number.
provideraddressThe provider that fulfilled the request (unused at this layer).
randomNumberbytes32The delivered random number.

_resolveProvider

Resolves the effective provider, falling back to the Entropy default provider.

function _resolveProvider(PythEntropyAdapterStorage storage $) private view returns (address);

Parameters

NameTypeDescription
$PythEntropyAdapterStorage

Returns

NameTypeDescription
<none>address

_requireEntropy

Returns the configured Entropy contract or reverts EntropyNotConfigured.

function _requireEntropy() private view returns (address e);

Returns

NameTypeDescription
eaddress