A blockchain oracle is a third-party service that connects smart contracts with the outside world, primarily to feed information from the world and reverse. Data from the world encapsulates multiple sources so that decentralized knowledge is obtained. Information to the world includes making payments and notifying parties. The oracle is the layer that queries, verifies, and authenticates external data sources, usually via trusted APIs, proprietary corporate data feeds and internet of things feeds, and then relays that information.

Examples

Many Ethereum applications use oracles. For example, the prediction market Augur would use election data to settle corresponding bets. Projects like Chainlink provide decentralized oracle network services to many blockchains.

Examples of data transmitted by oracles to smart contracts include price information, the successful completion of payment, the temperature measured by a sensor, election outcomes, etc. Data can be supplied by other software (databases, servers, or essentially any online data source), or by hardware (sensors, barcode scanners, etc.). Human oracles are individuals with specialized knowledge who can verify the authenticity of information before relaying it to smart contracts, and who prove their identity cryptographically.

Outbound oracles send information from smart contracts to the external world. For example, a smart contract receiving a payment could send information through an outbound oracle to a mechanism that unlocks a smart lock.

What is ChainLink Verifiable Random Function?

Chainlink VRF (Verifiable Random Function) is a probably fair and verifiable random number generator (RNG) that enables smart contracts to access random values without compromising security or usability.

For each request, Chainlink VRF generates one or more random values and cryptographic proof of how those values were determined. The proof is published and verified on-chain before any consuming applications can use it. This process ensures that results cannot be tampered with or manipulated by any single entity including oracle operators, miners, users, or smart contract developers.

Possible use cases

Use Chainlink VRF to build reliable smart contracts for any applications that rely on unpredictable outcomes:

  • Building blockchain games and NFTs.
  • Random assignment of duties and resources. For example, randomly assigning judges to cases.
  • Choosing a representative sample for consensus mechanisms.

Practical example

Before generating random numbers you need to create a subscription account in subscription manager and pre-pay for service, charging a sufficient amount of LINK, for example, using your metamask wallet. Then you need to create a consumer (your own contract deployment) and deploy it into the blockchain, adding the contract address to the subscription.

Here, I’ll show a little example of a functional random generator contract, always using Goerli tesnet.

Create and fund a subscription

  1. Open MetaMask and set it to use the Goerli testnet. The Subscription Manager detects your network based on the active network in MetaMask.
  2. Check MetaMask to make sure you have testnet ETH and LINK on Goerli. You can get testnet ETH and LINK at one of the available Goerli faucets.
  3. Open the Subscription Manager.
  4. Click Create Subscription and follow the instructions to create a new subscription account. MetaMask opens and asks you to confirm payment to create the account on-chain. After you approve the transaction, the network confirms the creation of your subscription account on-chain.
  5. After the subscription is created, click Add funds and follow the instructions to fund your subscription. For this example, a balance of 2 LINK is sufficient. MetaMask opens to confirm the LINK transfer to your subscription. After you approve the transaction, the network confirms the transfer of your LINK token to your subscription account.
  6. After you add funds, click Add consumer. A page opens with your account details and subscription ID.
  7. Record your subscription ID, which you need for your consumer contract. You will add the consumer to your subscription later.

Create and deploy a VRF Contract

The contract includes pre-configured values for the necessary request parameters such as vrfCoordinator address, link token contract address, and gas lane keyHash. You can change these parameters if you want to experiment on different testnets, but for this example, you only need to specify subscription Id when you deploy the contract.

Build and deploy the contract on Goerli.

1. Open the [VRFv2Consumer.sol](<https://remix.ethereum.org/#url=https://docs.chain.link/samples/VRF/VRFv2Consumer.sol>) contract in Remix.

2. On the Compile tab in Remix, compile the VRFv2Consumer.sol contract.

3. Configure your deployment. On the Deploy tab in Remix, select the Injected Provider Environment, select the VRFv2Consumer contract from the contract list, and specify your subscriptionId so the constructor can set it.

4. Click the Deploy button to deploy your contract on-chain. MetaMask opens and asks you to confirm the transaction.

5. After you deploy your contract, copy the address from the Deployed Contracts list in Remix. Before you can request randomness from VRF v2, you must add this address as an approved consumer on your subscription account.

6. Open the Subscription Manager and click the ID of your new subscription under the My Subscriptions list. The subscription details page opens.

7. Under the Consumers section, click Add consumer.

8. Enter the address of your consumer contract that you just deployed and click Add consumer. MetaMask opens and asks you to confirm the transaction.

Request Random Values

The deployed contract requests random values from Chainlink VRF receives those values and stores them in the s_randomWords array. Run the requestRandomWords() function on your contract to start the request.

  1. Return to Remix and view your deployed contract functions in the Deployed Contracts list.
  2. Click the requestRandomWords() function to send the request for random values to Chainlink VRF. MetaMask opens and asks you to confirm the transaction. After you approve the transaction, Chainlink VRF processes your request. Chainlink VRF fulfills the request and returns the random values to your contract in a callback to the fulfillRandomWords() function.
  3. Depending on current testnet conditions, it might take a few minutes for the callback to return the requested random values to your contract. You can see a list of pending requests for your subscription ID at vrf.chain.link.
  4. After the oracle returns the random values to your contract, the s_randomWords variable stores an array with all of the requested random values. Specify the index of the array that you want to display and click s_randomWords to print the value. Because this example requests two random values, check the value at index 0 and then check the value at index 1.

Finally, you deployed a simple contract that can request and receive random values from Chainlink VRF.

Authors

Agustin Curto

Julian Schtutman