Skip to content
Lattice

IPythEntropyAdapter

Git Source

Title: IPythEntropyAdapter

Author: Modified from Pyth (https://github.com/pyth-network/pyth-crosschain)

Interface for the PythEntropyAdapter Diamond facet — on-demand (commit/reveal) randomness.

Request/track layer over Pyth Entropy. A request is caller-funded (the Entropy fee is quoted via getFee and any excess msg.value is refunded); the provider fulfils via entropyCallback, which is gated so only the configured Entropy contract may call it. Consumer facets that inherit PythEntropyAdapter should override entropyCallback (calling super.entropyCallback first) to act on the delivered randomness.

Functions

getConfig

Returns the current Entropy configuration.

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

Returns

NameTypeDescription
<none>EntropyConfig

getFee

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

function getFee() external view returns (uint256);

Returns

NameTypeDescription
<none>uint256

getUserKey

Returns the user key associated with a pending request.

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

Parameters

NameTypeDescription
sequenceNumberuint64The Entropy-assigned sequence number.

Returns

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

setConfig

Sets or replaces the Entropy configuration.

Caller must hold DEFAULT_ADMIN_ROLE. Reverts EntropyInvalidConfig if entropy is zero.

function setConfig(EntropyConfig calldata config) external;

Parameters

NameTypeDescription
configEntropyConfigThe new configuration.

requestRandomNumber

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

Caller must hold DEFAULT_ADMIN_ROLE and send msg.value >= getFee().

function requestRandomNumber(bytes32 userKey, bytes32 userRandomNumber)
        external
        payable
        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, looks up the user key, clears the pending entry, and emits RandomNumberFulfilled. Consumer facets should override this to consume the randomness.

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

Parameters

NameTypeDescription
sequenceuint64The Entropy-assigned sequence number.
provideraddressThe provider that fulfilled the request.
randomNumberbytes32The delivered random number.

Events

EntropyConfigSet

Emitted when the Entropy configuration is updated.

event EntropyConfigSet(address entropy, address provider);

Parameters

NameTypeDescription
entropyaddressThe Pyth Entropy contract.
provideraddressThe randomness provider (or address(0) to use the default provider).

RandomNumberRequested

Emitted when a random number is requested.

event RandomNumberRequested(uint64 indexed sequenceNumber, bytes32 indexed userKey);

Parameters

NameTypeDescription
sequenceNumberuint64The Entropy-assigned sequence number.
userKeybytes32The caller-supplied key associated with this request.

RandomNumberFulfilled

Emitted when the provider fulfils a request.

event RandomNumberFulfilled(uint64 indexed sequenceNumber, bytes32 indexed userKey, bytes32 randomNumber);

Parameters

NameTypeDescription
sequenceNumberuint64The Entropy-assigned sequence number.
userKeybytes32The caller-supplied key originally associated with this request.
randomNumberbytes32The delivered random number.

Errors

EntropyNotConfigured

Entropy has not been configured (entropy contract address is zero).

error EntropyNotConfigured();

EntropyInvalidConfig

setConfig was called with a zero Entropy contract address.

error EntropyInvalidConfig();

EntropyInvalidUserKey

requestRandomNumber was called with a zero userKey.

bytes32(0) is the sentinel used to detect a missing pending request.

error EntropyInvalidUserKey();

EntropyInsufficientFee

msg.value was less than the Entropy fee.

error EntropyInsufficientFee(uint256 provided, uint256 required);

Parameters

NameTypeDescription
provideduint256The value sent.
requireduint256The required fee.

EntropyRefundFailed

The excess-fee refund to the caller failed.

error EntropyRefundFailed();

EntropyOnlyEntropy

entropyCallback was called by an address other than the Entropy contract.

error EntropyOnlyEntropy(address caller);

Parameters

NameTypeDescription
calleraddressThe unauthorised caller.

EntropyRequestNotFound

The given sequence number has no pending entry.

error EntropyRequestNotFound(uint64 sequenceNumber);

Parameters

NameTypeDescription
sequenceNumberuint64The unknown sequence number.

Structs

EntropyConfig

Pyth Entropy configuration.

struct EntropyConfig {
        /// @notice The Pyth Entropy contract.
        address entropy;
        /// @notice The randomness provider (`address(0)` resolves to the Entropy default provider).
        address provider;
    }

Properties

NameTypeDescription
entropyaddress
provideraddress