EVM
Switchboard Randomness for EVM applications
Switchboard On-Demand Randomness provides verifiable, tamper-proof random numbers for EVM smart contracts. This guide covers both the smart contract implementation and the off-chain resolution process.
Use Cases
Gaming: Fair loot drops, random encounters, card shuffling
NFTs: Random trait generation, blind box reveals
DeFi: Random liquidation selection, lottery mechanisms
Prerequisites
Basic understanding of Ethereum and smart contracts.
Familiarity with common smart contract development tools (e.g., Hardhat, Foundry).
A development environment set up and ready to go.
Supported Networks
For a complete list of deployments, see EVM Identifiers.
Installation
Solidity SDK:
TypeScript SDK: (For off-chain randomness resolution)
Forge (If applicable): Add the following lines to your
remappings.txtfile:
Smart Contract Implementation
Step 1: Contract Setup (Solidity)
Import Switchboard Interfaces:
Implementation:
Note that one has to inject the address of the Switchboard contract on deployment and select the appropriate network for the queue. For reference, the latest deployments can be found in EVM Identifiers.
Step 2: Request Randomness
Call the requestRandomness function within your smart contract:
randomnessId has to be unique for tracking your request. Then inject it into your chain-appropriate queue using the deployed Switchboard's address and requestRandomness function.
IMPORTANT: Ensure sufficient delay time for randomness to be resolved by oracles! The minimum settlement delay must pass and the request data must propagate on-chain.
Step 3: Setup Update Feed Handler
Given all the correct parameters, you are now able to call into the Switchboard oracle to resolve your randomness.
Off-Chain Resolution (TypeScript)
Complete Working Example
This example demonstrates the full randomness flow from request to settlement:
Step-by-Step Flow
1. Create Randomness Request
2. Wait for Settlement Delay
3. Fetch Randomness Reveal from Crossbar
4. Settle Randomness On-Chain
5. Read and Use the Random Value
Simple Resolution Example
For a simpler approach using an existing contract with randomnessId exposed:
Integration Tips
Use the raw 256-bit value for maximum entropy
Apply modulo for bounded ranges:
value % rangeFor multiple random values, hash the result with different salts
Store
randomnessIdto verify the result matches your requestAlways add a buffer (5-10 seconds) to the settlement delay for clock skew
Summary
Integrate Switchboard randomness to get secure, verifiable randomness in your EVM smart contracts. The flow is:
Create a randomness request on-chain
Wait for the settlement delay
Fetch the randomness reveal from Crossbar
Settle the randomness on-chain
Read and use the verified random value
You've now implemented a secure and verifiable way to get randomness in your EVM smart contracts!
Last updated