On EVM Networks
Tutorial for using Randomness on EVM
Randomness
There's a Solidity-SDK that you can use to interact with the oracle contract on-chain and leverage customized oracle data within your smart contracts. For querying oracle randomness off-chain for on-chain submission, you can use the Switchboard On-Demand Typescript-SDK.
Prerequisites
To use Switchboard On-Demand, you will need to have a basic understanding of Ethereum and smart contracts. For more on Switchboard's Architecture, see the docs.
Installation
You can install the Switchboard On-Demand Solidity SDK by running:
And you can install the cross-chain Typescript SDK by running:
Forge (Optional)
If you're using Forge, add following to your remappings.txt file:
Step 1: Roll Randomness
The first step to using Switchboard randomness on EVM is to create the contract that will actually call into Switchboard. This requires an active Switchboard deployment. Switchboard is currently supported on the following networks:
Deployments
Core Mainnet: 0x33A5066f65f66161bEb3f827A3e40fce7d7A2e6C
Core Testnet: 0x2f833D73bA1086F3E5CDE9e9a695783984636A76
Arbitrum Sepolia: 0xa2a0425fa3c5669d384f4e6c8068dfcf64485b3b
Arbitrum One: 0xad9b8604b6b97187cde9e826cdeb7033c8c37198
Morph Holesky: 0x3c1604DF82FDc873D289a47c6bb07AFA21f299e5
Making the Contract
Import the Switchboard interface:
Call requestRandomness with the right parameters. Here, randomnessId just has to be a unique
bytes32
of your choice. Pick the queue based on whether the network you're on is a mainnet network or not (Note: Morph Holesky uses mainnet queue).
So let's break this down:
Get the Switchboard interface,
switchboard
Create
randomNumber
to receive the random numberGet the correct
queueId
,switchboardDevnetQueue
for devnet/testnets andswitchboardQueue
for mainnets (and Morph Holesky)Create function roll to request Switchboard randomness.
Get some
randomnessId
- this can be generated any way you want. There just can't be duplicates.Call
requestRandomness
with the id, authority address (which has the authority to re-roll randomness), queueId, and min settlement delay.minSettlementDelay
is set to 30 seconds, meaning the randomness can't be resolved by an oracle for 30 seconds from when this tx settles.
Make function for resolving randomness. This is done by passing encoded Switchboard updates (requested off-chain from oracles), and calling
updateFeeds
, which routes the encoded random number into the user's randomness request.
Let's also add a function for resolving randomness:
Here we're just calling updateFeeds
which is routing the encoded updates into the randomness feed update.
Step 2: Resolve Randomness Off-Chain
The next step is to get the encoded randomness resolution from Switchboard. Here's an example using Crossbar and its Typescript SDK.
Once you have the randomness resolved, you can put this into a function call for randomness resolution.
Here's what a call using ethers could look like:
Last updated