Get a Random Number
This guide explains how to get random values using a simple contract to request and receive random values from Chainlink VRF v2.5. The guide uses the Subscription Manager to create and manage your subscription. Alternatively, you can also create and manage subscriptions programmatically. To explore more applications of VRF, refer to our blog.
Requirements
This guide assumes that you know how to create and deploy smart contracts on Ethereum testnets using the following tools:
If you are new to developing smart contracts on Ethereum, see the Getting Started guide to learn the basics.
Create and fund a subscription
For this example, create a new subscription on the Sepolia testnet.
-
Open MetaMask and set it to use the Sepolia testnet. The Subscription Manager detects your network based on the active network in MetaMask.
-
Check MetaMask to make sure you have testnet ETH and LINK on Sepolia. You can get testnet ETH and LINK at faucets.chain.link/sepolia.
-
Open the Subscription Manager at vrf.chain.link.
-
Click Create 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. Optionally, you can enter an email address and a project name for your subscription, and both of these are private. MetaMask opens and asks you to confirm payment to create the account onchain. After you approve the transaction, the network confirms the creation of your subscription account onchain.
-
After the subscription is created, 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 testnet funds to meet your minimum subscription balance to serve as a buffer against gas volatility.
- If you're paying with testnet LINK, fund your contract with 7 LINK. (After your request is processed, the actual cost will be around 0.06 LINK, and that amount will be deducted from your subscription balance.)
- If you're paying with testnet ETH, fund your contract with 0.03 ETH. (After your request is processed, the actual cost will be around 0.000247 ETH, and that amount will be deducted from your subscription balance.)
MetaMask opens to confirm the token transfer to your subscription. After you approve the transaction, the network confirms the transfer of your testnet funds to your subscription account.
-
After you add funds, click Add consumer. A page opens with your account 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.chain.link.
Now that you have a funded subscription account and your subscription ID, create and deploy a VRF compatible contract.
Create and deploy a VRF compatible contract
For this example, use the SubscriptionConsumer.sol sample contract. This contract imports the following dependencies:
The contract also includes pre-configured values for the necessary request parameters such as vrfCoordinator
address, gas lane keyHash
, callbackGasLimit
, requestConfirmations
and number of random words numWords
. You can change these parameters if you want to experiment on different testnets, but for this example you only need to specify subscriptionId
when you deploy the contract.
Build and deploy the contract on Sepolia.
-
Open the SubscriptionConsumer.sol in Remix.
-
On the Compile tab in Remix, compile the
SubscriptionConsumer.sol
contract. -
Configure your deployment. On the Deploy tab in Remix, select the Injected Provider environment, select the
SubscriptionConsumer
contract from the contract list, and specify yoursubscriptionId
so the constructor can set it. -
Click the Deploy button to deploy your contract onchain. MetaMask opens and asks you to confirm the transaction.
-
After you deploy your contract, copy the address from the Deployed Contracts list in Remix. Before you can request randomness from VRF v2.5, you must add this address as an approved consuming contract on your subscription account.
-
Open the Subscription Manager at vrf.chain.link and click the ID of your new subscription under the My Subscriptions list. The subscription details page opens.
-
Under the Consumers section, click Add consumer.
-
Enter the address of your consuming contract that you just deployed and click Add consumer. MetaMask opens and asks you to confirm the transaction.
Your example contract is deployed and approved to use your subscription balance to pay for VRF v2.5 requests. Next, request random values from Chainlink VRF.
Request random values
The deployed contract requests random values from Chainlink VRF, receives those values, builds a struct RequestStatus
containing them and stores the struct in a mapping s_requests
. Run the requestRandomWords()
function on your contract to start the request.
-
Return to Remix and view your deployed contract functions in the Deployed Contracts list.
-
Expand the
requestRandomWords()
function to send the request for random values to Chainlink VRF. UseenableNativePayment
to specify whether you want to pay in native tokens or LINK:- To use native tokens, set
enableNativePayment
totrue
. - To use LINK, set
enableNativePayment
tofalse
.
When you click transact, 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. At this point, a new keyrequestId
is added to the mappings_requests
.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.
- To use native tokens, set
-
To fetch the request ID of your request, call
lastRequestId()
. -
After the oracle returns the random values to your contract, the mapping
s_requests
is updated: The received random values are stored ins_requests[_requestId].randomWords
. -
Call
getRequestStatus()
specifying therequestId
to display the random words.
You deployed a simple contract that can request and receive random values from Chainlink VRF. Next, learn how to create and manage subscriptions programmatically by using a smart contract instead of the Subscription Manager.
Analyzing the contract
In this example, your MetaMask wallet is the subscription owner and you created a consuming contract to use that subscription. The consuming contract uses static configuration parameters.
// SPDX-License-Identifier: MIT
// An example of a consumer contract that relies on a subscription for funding.
pragma solidity 0.8.19;
import {VRFConsumerBaseV2Plus} from "@chainlink/contracts/src/v0.8/vrf/dev/VRFConsumerBaseV2Plus.sol";
import {VRFV2PlusClient} from "@chainlink/contracts/src/v0.8/vrf/dev/libraries/VRFV2PlusClient.sol";
/**
* Request testnet LINK and ETH here: https://faucets.chain.link/
* Find information on LINK Token Contracts and get the latest ETH and LINK faucets here: https://docs.chain.link/docs/link-token-contracts/
*/
/**
* THIS IS AN EXAMPLE CONTRACT THAT USES HARDCODED VALUES FOR CLARITY.
* THIS IS AN EXAMPLE CONTRACT THAT USES UN-AUDITED CODE.
* DO NOT USE THIS CODE IN PRODUCTION.
*/
contract SubscriptionConsumer is VRFConsumerBaseV2Plus {
event RequestSent(uint256 requestId, uint32 numWords);
event RequestFulfilled(uint256 requestId, uint256[] randomWords);
struct RequestStatus {
bool fulfilled; // whether the request has been successfully fulfilled
bool exists; // whether a requestId exists
uint256[] randomWords;
}
mapping(uint256 => RequestStatus)
public s_requests; /* requestId --> requestStatus */
// Your subscription ID.
uint256 public s_subscriptionId;
// Past request IDs.
uint256[] public requestIds;
uint256 public lastRequestId;
// The gas lane to use, which specifies the maximum gas price to bump to.
// For a list of available gas lanes on each network,
// see https://docs.chain.link/docs/vrf/v2-5/supported-networks
bytes32 public keyHash =
0x787d74caea10b2b357790d5b5247c2f63d1d91572a9846f780606e4d953677ae;
// Depends on the number of requested values that you want sent to the
// fulfillRandomWords() function. Storing each word costs about 20,000 gas,
// so 100,000 is a safe default for this example contract. Test and adjust
// this limit based on the network that you select, the size of the request,
// and the processing of the callback request in the fulfillRandomWords()
// function.
uint32 public callbackGasLimit = 100000;
// The default is 3, but you can set this higher.
uint16 public requestConfirmations = 3;
// For this example, retrieve 2 random values in one request.
// Cannot exceed VRFCoordinatorV2_5.MAX_NUM_WORDS.
uint32 public numWords = 2;
/**
* HARDCODED FOR SEPOLIA
* COORDINATOR: 0x9DdfaCa8183c41ad55329BdeeD9F6A8d53168B1B
*/
constructor(
uint256 subscriptionId
) VRFConsumerBaseV2Plus(0x9DdfaCa8183c41ad55329BdeeD9F6A8d53168B1B) {
s_subscriptionId = subscriptionId;
}
// Assumes the subscription is funded sufficiently.
// @param enableNativePayment: Set to `true` to enable payment in native tokens, or
// `false` to pay in LINK
function requestRandomWords(
bool enableNativePayment
) external onlyOwner returns (uint256 requestId) {
// Will revert if subscription is not set and funded.
requestId = s_vrfCoordinator.requestRandomWords(
VRFV2PlusClient.RandomWordsRequest({
keyHash: keyHash,
subId: s_subscriptionId,
requestConfirmations: requestConfirmations,
callbackGasLimit: callbackGasLimit,
numWords: numWords,
extraArgs: VRFV2PlusClient._argsToBytes(
VRFV2PlusClient.ExtraArgsV1({
nativePayment: enableNativePayment
})
)
})
);
s_requests[requestId] = RequestStatus({
randomWords: new uint256[](0),
exists: true,
fulfilled: false
});
requestIds.push(requestId);
lastRequestId = requestId;
emit RequestSent(requestId, numWords);
return requestId;
}
function fulfillRandomWords(
uint256 _requestId,
uint256[] calldata _randomWords
) internal override {
require(s_requests[_requestId].exists, "request not found");
s_requests[_requestId].fulfilled = true;
s_requests[_requestId].randomWords = _randomWords;
emit RequestFulfilled(_requestId, _randomWords);
}
function getRequestStatus(
uint256 _requestId
) external view returns (bool fulfilled, uint256[] memory randomWords) {
require(s_requests[_requestId].exists, "request not found");
RequestStatus memory request = s_requests[_requestId];
return (request.fulfilled, request.randomWords);
}
}
The parameters define how your requests will be processed. You can find the values for your network in the Configuration page.
-
uint256 s_subscriptionId
: The subscription ID that this contract uses for funding requests. -
bytes32 keyHash
: The gas lane key hash value, which is the maximum gas price you are willing to pay for a request in wei. It functions as an ID of the offchain VRF job that runs in response to requests. -
uint32 callbackGasLimit
: The limit for how much gas to use for the callback request to your contract'sfulfillRandomWords()
function. It must be less than themaxGasLimit
limit on the coordinator contract. Adjust this value for larger requests depending on how yourfulfillRandomWords()
function processes and stores the received random values. If yourcallbackGasLimit
is not sufficient, the callback will fail and your subscription is still charged for the work done to generate your requested random values. -
uint16 requestConfirmations
: How many confirmations the Chainlink node should wait before responding. The longer the node waits, the more secure the random value is. It must be greater than theminimumRequestBlockConfirmations
limit on the coordinator contract. -
uint32 numWords
: How many random values to request. If you can use several random values in a single callback, you can reduce the amount of gas that you spend per random value. The total cost of the callback request depends on how yourfulfillRandomWords()
function processes and stores the received random values, so adjust yourcallbackGasLimit
accordingly.
The contract includes the following functions:
-
requestRandomWords(bool enableNativePayment)
: Takes your specified parameters and submits the request to the VRF coordinator contract. UseenableNativePayment
to specify for each request whether you want to pay in native tokens or LINK:- To use native tokens, set
enableNativePayment
totrue
. - To use LINK, set
enableNativePayment
tofalse
.
- To use native tokens, set
-
fulfillRandomWords()
: Receives random values and stores them with your contract. -
getRequestStatus()
: Retrive request details for a given_requestId
.
Clean up
After you are done with this contract and the subscription, you can retrieve the remaining testnet tokens to use with other examples.
-
Open the Subscription Manager at vrf.chain.link and click the ID of your new subscription under the My Subscriptions list. The subscription details page opens.
-
On your subscription details page, expand the Actions menu and select Cancel subscription. A field displays, prompting you to add the wallet address you want to send the remaining funds to.
-
Enter your wallet address and click Cancel subscription. MetaMask opens and asks you to confirm the transaction. After you approve the transaction, Chainlink VRF closes your subscription account and sends the remaining LINK to your wallet.