The Chainlink Verified Random Function

Learn about how to securely generate random numbers for smart contracts.

Off-chain oracles

Smart contract transactions must be deterministic—Ethereum has that one limitation. Every node in the Ethereum network must be able to reproduce every operation in a transaction in order to validate it. This feature, which guarantees the integrity and decentralization of the blockchain, also poses two practical difficulties:

  • Smart contracts can't generate random numbers because validator nodes wouldn't be able to generate the same numbers.

  • They can't access off-chain data because validator nodes wouldn't necessarily be able to access the same version of these data.

But then, what if we need to get data from the outside world in our smart contract? Let's say a random number from 1 to 6 for an on-chain game of dice or the current ETH / USD exchange rate for a DeFi application. The solution is to use off-chain oracles. Oracles are off-chain services that are able to securely receive and fulfill data requests for smart contracts.

However, it should be noted that they are typically operated by centralized, third-party corporations—therefore, they imply some tradeoffs regarding the decentralization and security of our contracts. Smart contracts relying on oracles are known as hybrid smart contracts to highlight the fact that they are not fully on-chain and are potentially vulnerable to failures from these third parties. Moreover, these are paid services that require funding a subscription. Forgetting or failing to do so constitutes another new possible point of failure for hybrid smart contracts compared to fully on-chain smart contracts.

Chainlink's VRF

Currently, the main provider of Oracle services for various blockchains is Chainlink. For data feeds, such as conversion rates, Chainlink provides a service called Data Feeds. In this lesson, we'll focus on another one of their products, the Verifiable Random Function v2 (VRF), and see how to use it as a source of verifiable randomness for our games. Chainlink's VRF can hook to our contract and, upon request, generate one or more random values and cryptographic proof of how those values were generated (this is the “V” part of the VRF).

On Ethereum, the VRF is available for the Mainnet and the Goerli testnet, besides other chains (BNB Chain, Polygon, Avalanche, and Fantom). Moreover, requesting random numbers from Chainlink's VRF is a service we have to pay for using the company's own cryptocurrency, LINK.

For this lesson, we'll need to get some test ETH and test LINK for Goerli from the corresponding faucets.

Generating random numbers with the VRF

To request random numbers from the VRF, we'll need to do the following:

  • Create a subscription on Chainlink's website and fund it with LINK.

  • Write and deploy our contract requesting randomness.

  • Add the contract's deployed address to our subscription as a consumer.

Let's see how to do this in detail.

Creating and funding a subscription

  1. We first set our MetaMask network to the Goerli test network. MetaMask should display the test ETH and LINK we have previously requested from the faucets. If Goerli LINK does not appear, it must be added using the following link by clicking on “Add to wallet” or manually entering the contract's address to the list of assets recognized by MetaMask.

Get hands-on with 1400+ tech skills courses.