ScaledEntropyProvider

Provides cryptographically secure random number generation using Pyth Network entropy. Used for provably fair drawing winner selection.

State Variables

Variable
Type
Description

entropy

IEntropyV2

Pyth Network entropy contract

entropyProvider

address

Pyth entropy provider address

pending

mapping

Pending randomness requests

Structs

SetRequest

struct SetRequest {
    uint8 samples;          // Number of random values to generate
    uint256 minRange;       // Minimum value (inclusive)
    uint256 maxRange;       // Maximum value (inclusive)
    bool withReplacement;   // Allow duplicate values
}

PendingRequest (internal)

struct PendingRequest {
    address callback;       // Contract to call with results
    bytes4 selector;        // Function selector for callback
    bytes context;          // Additional data for callback
    bytes32 userRandomNumber; // User-provided randomness
    SetRequest[] setRequests; // Randomness specifications
}

Events

ScaledRandomnessDelivered

EntropyFulfilled

Functions

getFee

Get the fee required for an entropy request.

Parameters:

Name
Type
Description

_gasLimit

uint32

Gas limit for callback execution

Returns:

Type
Description

uint256

Fee amount in wei

Example:


getEntropyContract

Get the address of the Pyth Network entropy contract.

Returns:

Type
Description

address

Pyth Network entropy contract address


getEntropyProvider

Get the currently configured entropy provider.

Returns:

Type
Description

address

Entropy provider address


getPendingRequest

Get details of a pending entropy request.

Parameters:

Name
Type
Description

_sequence

uint64

Request sequence number

Returns:

Type
Description

PendingRequest

Request details including callback info

How Pyth Entropy Works

Request Flow

  1. Request: Contract calls requestAndCallbackScaledRandomness with fee

  2. Pyth Processing: Pyth Network generates cryptographic randomness

  3. Callback: Pyth calls back with random number

  4. Scaling: Provider scales randomness to requested ranges

  5. Delivery: Callback function receives scaled results

Lottery Drawing Example

For the Jackpot drawing:

Randomness Methods

Fisher-Yates Shuffle

Used for sampling without replacement (unique values):

  • Guarantees no duplicate values

  • Unbiased selection algorithm

  • Used for normal ball selection

Rejection Sampling

Used for unbiased scaling:

  • Prevents modulo bias

  • Ensures uniform distribution

  • Used for both methods

Security Properties

Cryptographic Security

  • Randomness generated by Pyth Network validators

  • Unpredictable before reveal

  • Verifiable on-chain

Bias Prevention

  • Rejection sampling eliminates modulo bias

  • Fisher-Yates provides uniform permutation

  • No advantage from timing or position

Provable Fairness

  • All randomness is on-chain verifiable

  • Request and callback are auditable

  • Results cannot be manipulated

Last updated