Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Solana Randomness Service
The Solana Randomness Service uses a Switchboard SGX enabled oracle to provide randomness to any Solana program using a callback instruction.
Program ID: RANDMo5gFnqnXJW5Z52KNmd24sAo95KAd5VbiCtq5Rh
NOTE: This program ID is applicable for mainnet-beta and devnet.
Request Lifecycle
- User's program invokes the
simple_randomness_v1
instruction with a CPI call along with the number of randomness bytes, the custom callback instruction, and the priority fee config- Creates a
SimpleRandomnessV1Account
account - Sets the custom callback
- Wraps funds into an escrow to reward the oracle for fulfilling the request
- Creates a
- Off-chain SGX enabled oracle reads the request account
- Generates random bytes inside of the enclave
- Builds a txn with your callback and desired priority fees
- Simulates the txn. If successful, relays the txn on-chain. If error, relays an error instruction with the error message which is viewable in an explorer.
- Transaction relayed on-chain
- Oracle rewarded for fulfilling request
- Oracle invokes the users callback instruction
- Request account is closed and the rent-exemption is returned to the original payer
Usage
Add the solana_randomness_service to your Cargo.toml
= { = "1", = ["cpi"] }
See the example program below on how to integrate the Solana Randomness Service into your Anchor program.
- Call the
simple_randomness_v1
instruction with your payer, callback, and your desired priority fee config - Build the callback isntruction that the randomness service will invoke with your requested randomness bytes
use SimpleRandomnessV1Account;
use TransactionOptions;
use ;
use *;
use get_ixn_discriminator;
declare_id!;
// The request_randomness macro breaks IDL generation. So we'll manually implement.
// #[request_randomness]
Typescript Client
The typescript client can be used to interact with the randomness service off-chain.
npm i @switchboard-xyz/solana-randomness-service
The randomness service client can be initialized from an anchor provider.
Switchboard Labs provides a set of off-chain oracles to fulfill any requests on devnet and mainnet.
For localnet, the RandomnessService will initialize itself and create its own Switchboard infrastructure. When you await a request to be settled, the randomness service will check and fulfill the request itself.
import * as anchor from "@coral-xyz/anchor";
import { RandomnessService } from "@switchboard-xyz/solana-randomness-service";
const provider = anchor.AnchorProvider.env();
const randomnessService = await RandomnessService.fromProvider(provider);
// Create a keypair for our request account. This account will be automatically closed on settlement and
// the rent will be returned to the original payer.
const requestKeypair = anchor.web3.Keypair.generate();
// Start watching for the settled event before triggering the request.
// If on localnet this will fulfill the randomness request for you in the background.
const settledRandomnessEventPromise = randomnessService.awaitSettledEvent(
requestKeypair.publicKey
);
// your program makes a CPI request to the RandomnessService
const signature = await program.methods
.requestRandomness()
.accounts({
randomnessService: randomnessService.programId,
randomnessRequest: requestKeypair.publicKey,
randomnessEscrow: anchor.utils.token.associatedAddress({
mint: randomnessService.accounts.mint,
owner: requestKeypair.publicKey,
}),
randomnessState: randomnessService.accounts.state,
randomnessMint: randomnessService.accounts.mint,
payer: provider.wallet.publicKey,
})
.signers([requestKeypair])
.rpc();
// Await the response from the Switchboard Service
const [settledRandomnessEvent, settledSlot] =
await settledRandomnessEventPromise;