VRF (Randomness) in Solidity
Introduction
Flow provides secure, native onchain randomness that developers can leverage through Cadence Arch, a precompiled contract available on the Flow EVM environment. This guide walks you through how Solidity developers can use Cadence Arch to access Flow's verifiable randomness using Solidity.
What is Cadence Arch?
Cadence Arch is a precompiled smart contract that allows Solidity developers on Flow EVM to interact with Flow's randomness and other network features like block height. This contract can be accessed using its specific address, and Solidity developers can make static calls to retrieve random values and other information.
Prerequisites
- Basic Solidity knowledge
- Installed Metamask extension
- Remix IDE for compilation and deployment
- Flow EVM Testnet setup in Metamask
Network information for Flow EVM
See Network information for more details.
Steps to connect Flow EVM testnet to metamask
See Wallets & Configurations for more details.
Solidity commit reveal
Make sure you review the Solidity version of the commit reveal to learn more about Flow EVM's native secure randomness through a simple demonstration.
Obtaining testnet FLOW
You can fund your account with testnet FLOW using the Flow Faucet.
Enter your Flow-EVM testnet address, and you'll receive testnet FLOW tokens to interact with smart contracts.
Solidity code example: retrieving random numbers
The following is a simple Solidity contract that interacts with the Cadence Arch contract to retrieve a pseudo-random number:
_17// SPDX-License-Identifier: GPL-3.0_17pragma solidity >=0.7.0 <0.9.0;_17_17contract CadenceArchCaller {_17 // Address of the Cadence Arch contract_17 address constant public cadenceArch = 0x0000000000000000000000010000000000000001;_17_17 // Function to fetch a pseudo-random value_17 function revertibleRandom() public view returns (uint64) {_17 // Static call to the Cadence Arch contract's revertibleRandom function_17 (bool ok, bytes memory data) = cadenceArch.staticcall(abi.encodeWithSignature("revertibleRandom()"));_17 require(ok, "Failed to fetch a random number through Cadence Arch");_17 uint64 output = abi.decode(data, (uint64));_17 // Return the random value_17 return output;_17 }_17}
Explanation of the contract
-
Cadence Arch Address:
The
cadenceArch
variable stores the address of the Cadence Arch precompiled contract (0x0000000000000000000000010000000000000001
), which is constant across Flow EVM. -
Revertible Random:
The
revertibleRandom()
function makes a static call to therevertibleRandom<uint64>()
function to fetch a pseudo-random number. If the call is successful, it decodes the result as auint64
random value.
Deploying and testing the contract
Compile and deploy the contract
-
Open Remix IDE.
-
Create a new file and paste the Solidity code above.
-
Compile the contract by selecting the appropriate Solidity compiler version (0.8.x).
-
Connect Remix to your Metamask wallet (with Flow EVM testnet) by selecting Injected Web3 as the environment.
-
Deploy the contract.