Get a Random


This guide will help you understand how to create a basic contract to request and receive random values from Pythia VRF.

What You Need

This guide assumes that you know how to create and deploy smart contracts on Canto testnets using the following tools:

Create and fund a subscription

  1. Open MetaMask and set it to use the Canto testnet. The Subscription Manager will detect your network based on the active network in MetaMask.
  2. Go to the Subscription Manager at vrf.pythia.solutions.
  3. Click Create a Subscription and follow the instructions to create a new subscription account.
    • If you connect your wallet to the Subscription Manager, the Admin address for your subscription is prefilled and not editable.
    • 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.
  4. After the subscription is created, enter a custom amount and click Add funds and follow the instructions to fund your subscription.
    • For your request to go through, you need to fund your subscription with enough tokens to meet your minimum subscription balance to serve as a buffer against gas volatility. For this example, a balance of 5 Canto is sufficient. (If you don't have sufficient amount of Canto in your wallet, you can request faucet from cantofaucet.com)
    • MetaMask opens to confirm the toeken transfer to your subscription. After you approve the transaction, the network confirms the transfer of your token to your subscription account.
  5. After you add funds, you can Add consumer to your subscription. For this example you don't need to add consumer at this stage, click skip. A page opens with your subscription details and subscription ID.
    • Record your subscription ID, which you need for your consuming contract.
    • You will add the consuming contract to your subscription later.
    • You can always find your subscription IDs, balances, and consumers at vrf.pythia.solutions.

Create and deploy a VRF consumer contract

You can simply create and deploy a consumer contract with your prefered tools. Here we are going to build and deploy VRFConsumer.sol using Remix IDE.

This contract:

Build and deploy the contract on Testnet.

  1. Open the VRFConsumer.sol contract in Remix.
  2. On the Compile tab in Remix, compile the VRFConsumer.sol contract.
  3. Configure your deployment. On the Deploy tab in Remix, select the Injected Provider environment, select the VRFConsumer contract from the contract list, and specify your subscriptionId so the constructor can set it.
    Deploy contract with the subscriptionID field filled in Remix
    Deploy contract with the subscriptionID field filled in Remix
  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, you must add this address as an approved consuming contract on your subscription account.
    Contract address listed under the Contracts list in Remix
    Contract address listed under the Contracts list in Remix
  6. Open the Subscription Manager at vrf.pythia.solutions and click the ID of your new subscription. The subscription details page opens.
  7. On the top left senction, click Add consumer.
  8. Enter the address of your consuming contract that you just deployed and click Add consumer. MetaMask opens and asks you to confirm the transaction.

The example contract is deployed and approved to use your subscription balance to pay for VRF requests. Next, request random values from VRF.

Request random values

The deployed contract requests random values from Pythia VRF, receives values, builds a struct Request containing them and stores the struct in the history mapping. Run the requestRandom() function on your contract to start the request.

  1. On Remix view your deployed contract functions in the Deployed Contracts list.
  2. Click the requestRandom() function to send the request for random values. MetaMask opens and asks you to confirm the transaction. After you approve the transaction, Pythia VRF processes your request. VRF fulfills the request and returns the random values to your contract in a callback to the fulfillRandomWords() function. A new key requestId will add to the history mapping.
  3. To fetch the request ID of your request, call lastRequestId().
  4. Call checkRequest() specifying the requestId to display the random words from desired request.