NEW

Chainlink Data Streams have officially launched on mainnet. Sign up for early access.

Back

Getting Started with Chainlink VRF

VRF v2 - Developer Walkthrough

In this guide, you will learn about generating randomness on blockchains. This includes learning how to implement a Request and Receive cycle with Chainlink oracles and how to consume random numbers with Chainlink VRF in smart contracts.

What is the Request and Receive cycle?

The Data Feeds Getting Started guide explains how to consume Chainlink Data Feeds, which consist of reference data posted onchain by oracles. This data is stored in a contract and can be referenced by consumers until the oracle updates the data again.

Randomness, on the other hand, cannot be reference data. If the result of randomness is stored onchain, any actor could retrieve the value and predict the outcome. Instead, randomness must be requested from an oracle, which generates a number and a cryptographic proof. Then, the oracle returns that result to the contract that requested it. This sequence is known as the Request and Receive cycle.

What is the payment process for generating a random number?

VRF requests receive funding from subscription accounts. The Subscription Manager lets you create an account and pre-pay for VRF requests, so that funding of all your application requests are managed in a single location. To learn more about VRF requests funding, see Subscriptions limits.

How do I deploy to testnet?

You will deploy this contract on the Sepolia test network. You must have some Sepolia testnet ETH in your MetaMask account to pay for the gas. Testnet ETH is also available from several public faucets.

This deployment is slightly different than the example in the Deploy Your First Contract guide. In this case, you pass in parameters to the constructor upon deployment.

Once compiled, you'll see a dropdown menu that looks like this in the deploy pane:

Remix contract selected

Select the VRFD20 contract or the name that you gave to your contract.

Click the caret arrow on the right hand side of Deploy to expand the parameter fields, and paste your subscription ID.

Remix contract parameters to deploy

Then click the Deploy button and use your MetaMask account to confirm the transaction.

At this point, your contract should be successfully deployed. However, it can't request anything because it is not yet approved to use the LINK balance in your subscription. If you click rollDice, the transaction will revert.

How do I add my contract to my subscription account?

After you deploy your contract, you must add it as an approved consumer contract so it can use the subscription balance when requesting for randomness. Go to the Subscription Manager and add your deployed contract address to the list of consumers. Find your contract address in Remix under Deployed Contracts on the bottom left.

Remix contract address

How do I test rollDice?

After you open the deployed contract tab in the bottom left, the function buttons are available. Find rollDice and click the caret to expand the parameter fields. Enter an Ethereum address to specify a "dice roller", and click 'rollDice'.

It takes a few minutes for the transaction to confirm and the response to be sent back. You can get your house by clicking the house function button with the address passed in rollDice. After the response is sent back, you'll be assigned a Game of Thrones house!

Further Reading

To read more about generating random numbers in Solidity, read our blog posts:

What's next

Stay updated on the latest Chainlink news