PythEntropyAdapterLib
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
| Name | Type | Description |
|---|---|---|
| $ | 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
| Name | Type | Description |
|---|---|---|
| config | IPythEntropyAdapter.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
| Name | Type | Description |
|---|---|---|
| <none> | uint256 |
getUserKey
Returns the user key associated with a pending request.
function getUserKey(uint64 sequenceNumber) internal view returns (bytes32);Parameters
| Name | Type | Description |
|---|---|---|
| sequenceNumber | uint64 | The Entropy-assigned sequence number. |
Returns
| Name | Type | Description |
|---|---|---|
| <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
| Name | Type | Description |
|---|---|---|
| config | IPythEntropyAdapter.EntropyConfig | The 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
| Name | Type | Description |
|---|---|---|
| userKey | bytes32 | Arbitrary key the caller wants associated with this request. |
| userRandomNumber | bytes32 | The caller's commitment to its half of the randomness. |
Returns
| Name | Type | Description |
|---|---|---|
| sequenceNumber | uint64 | The 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
| Name | Type | Description |
|---|---|---|
| sequence | uint64 | The Entropy-assigned sequence number. |
| provider | address | The provider that fulfilled the request (unused at this layer). |
| randomNumber | bytes32 | The delivered random number. |
_resolveProvider
Resolves the effective provider, falling back to the Entropy default provider.
function _resolveProvider(PythEntropyAdapterStorage storage $) private view returns (address);Parameters
| Name | Type | Description |
|---|---|---|
| $ | PythEntropyAdapterStorage |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | address |
_requireEntropy
Returns the configured Entropy contract or reverts EntropyNotConfigured.
function _requireEntropy() private view returns (address e);Returns
| Name | Type | Description |
|---|---|---|
| e | address |