VRF Developer Tutorial

Learn how to use SecretPath on EVM to access on-chain verifiable random numbers.

Overview

SecretVRF over SecretPath enables EVM developers to access on-chain verifiable random numbers at a fraction of the cost and block time of traditional RNG oracles such as ChainlinkVRF. With fewer than 100 lines of code, you will have access to an infinite supply of randomness.

See a fullstack cross-chain SecretVRF demo here

To learn how SecretVRF works underneath the hood, refer to the docs here. 🤓

Getting Started

To get started, clone the Secret Labs examples repo:

git clone https://github.com/scrtlabs/examples.git

EVM Prerequisites

Configuring Environment Variables

cd into examples/EVM-secretpath-RNG:

cd examples/EVM-secretpath-RNG

Install the node dependencies:

npm install

Update the env file with your EVM wallet private key and Infura API key.

Make sure your Infura API key is configured for Amoy testnet 😎

Upload & Instantiate RandomnessReceiver.sol

Compile your Solidity smart contract:

npx hardhat compile

Once the contract is compiled successfully, upload the contract to Polygon testnet:

npx hardhat run scripts/deploy.js --network polygon

Note the contract address:

RandomnessReceiver deployed to: 0x08D05bC52e503C68c38A32c1fA997FB521e614C4

Add the RandomnessReceiver contract address to your env file:

RANDOMNESS_RECEIVER_CONTRACT_ADDRESS="0x08D05bC52e503C68c38A32c1fA997FB521e614C4"

Execute RandomnessReceiver.sol

Now that you've uploaded your contract, it's time to set the SecretPath gateway address for Polygon Amoy and then request on-chain verifiable random numbers!

Gateways are the on-chain smart contracts that handle the broadcasting, receipt, packaging, and verification of messages.

Set Gateway Contract

First, set the gateway address for Polygon Amoy testnet. You can do this by executing set_gateway.js:

npx hardhat --network polygon run ./scripts/set_gateway.js

This tutorial is for Polygon testnet, but you can find a list of additional EVM gateway contract addresses here.

Create Randomness Event Listener

Next, create an event listener so you can listen to when the random numbers that you request have been fulfilled.

Open a new terminal window and cd into examples/EVM-secretpath-RNG:

cd examples/EVM-snakepath-RNG

Then, create the event listener by executing fulfill_randomness_event.js:

npx hardhat --network polygon run ./scripts/fulfill_randomness_event.js

Request Random Numbers

Now it's time to request random numbers! Currently, request_random.js is configured to request 3 random numbers, but you can update how many numbers you would like to request here (up to 2000 for this example).

Once you have configured how many random numbers you want to request, execute request_random.js:

npx hardhat --network polygon run ./scripts/request_random.js

Upon successful execution, your terminal will log the following:

Current gas price: 1.500000016 gwei
Amount of gas: 202500002160000
Transaction hash: 0x47efe733c6b64a5c65fae68a5fa0f2eb39be107a7d4930325104dfcee36474c2
Random Numbers requested successfully!

Navigate to your event listener terminal to see the returned random numbers:

Random numbers fulfilled for request ID: 7
Random Numbers: 94412630379044474934232934838909700375960606882138821083837396872559692127250,113337239238407277551866961530595655396141218773986266698805816049961297644274,27422614896457590254145871678336430245204859898445275988406473498974116581231

Congrats! You've just used SecretPath to request your first verifiable on-chain random numbers! 🎉

Conclusion

Secret VRF offers an innovative and cost-effective solution for EVM developers seeking access to verifiable random numbers. By following this guide, you've successfully set up your environment, deployed the RandomnessReceiver.sol contract, and interacted with the SecretPath network to request and receive random numbers. Dive into the world of decentralized randomness with SecretPath, where security meets simplicity. 🌟

Last updated