EVM
Switchboard Randomness for EVM applications
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.
Installation
Solidity SDK:
TypeScript SDK: (For off-chain randomness resolution)
Forge (If applicable): Add the following lines to your
remappings.txt
file:
Step 1: EVM 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 here. https://switchboardxyz.gitbook.io/switchboard-on-demand/#deployments
Request Randomness: Call the
requestRandomness
function within your smart contract. The randomness is needed.
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! You will be able to save on costs.
Setup update feed handler
Given all the correct parameters, you are now able to call into the Switchboard oracle to resolve your randomness.
Step 2: Resolve Randomness Off-Chain (TypeScript)
Fetch Randomness:
Submit to Contract: Use a library like ethers.js or web3.js to submit the encoded data to your smart contract. Using ethers.js, it would look similar to:
In Summary
Integrate Switchboard randomness to get solidity randomness. Once the ID is given to the oracle, you can then use it on-chain for the randomness function. It would also be more efficient if the settlement of randoms is delayed until it gets resolved.
You've now implemented a secure and verifiable way to get randomness in your EVM smart contracts!
Last updated