VRF
Last updated
Was this helpful?
Last updated
Was this helpful?
Chainlink VRF (Verifiable Random Function) is a provably 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.
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.
To learn more about the benefits of Chainlink VRF v2, see our blog post . For help with your specific use case, to connect with one of our Solutions Architects. You can also ask questions about Chainlink VRF on .
Chainlink VRF v2 offers two methods for requesting randomness:
: Create a subscription account and fund its balance with LINK tokens. Users can then connect multiple consuming contracts to the subscription account. When the consuming contracts request randomness, the transaction costs are calculated after the randomness requests are fulfilled and the subscription balance is deducted accordingly. This method allows you to fund requests for multiple consumer contracts from a single subscription.
: Consuming contracts directly pay with LINK when they request random values. You must directly fund your consumer contracts and ensure that there are enough LINK tokens to pay for randomness requests.
This section is the most important thing in Chainlink doc. Make sure you read it multiple times.
Gaining access to high quality randomness on-chain requires a solution like Chainlinkβs VRF, but it also requires you to understand some of the ways that miners or validators can potentially manipulate randomness generation. Here are some of the top security considerations you should review in your project.
requestId
to match randomness requests with their fulfillment in orderIf your contract could have multiple VRF requests in flight simultaneously, you must ensure that the order in which the VRF fulfillments arrive cannot be used to manipulate your contractβs user-significant behavior.
Blockchain miners/validators can control the order in which your requests appear on-chain, and hence the order in which your contract responds to them.
For example, if you made randomness requests A
, B
, C
in short succession, there is no guarantee that the associated randomness fulfillments will also be in order A
, B
, C
. The randomness fulfillments might just as well arrive at your contract in order C
, A
, B
or any other order.
We recommend using the requestID
to match randomness requests with their corresponding fulfillments.
In principle, miners/validators of your underlying blockchain could rewrite the chain's history to put a randomness request from your contract into a different block, which would result in a different VRF output. Note that this does not enable a miner to determine the random value in advance. It only enables them to get a fresh random value that might or might not be to their advantage. By way of analogy, they can only re-roll the dice, not predetermine or predict which side it will land on.
You must choose an appropriate confirmation time for the randomness requests you make. Confirmation time is how many blocks the VRF service waits before writing a fulfillment to the chain to make potential rewrite attacks unprofitable in the context of your application and its value-at-risk.
Any re-request of randomness is an incorrect use of VRFv2. Doing so would give the VRF service provider the option to withhold a VRF fulfillment if the outcome is not favorable to them and wait for the re-request in the hopes that they get a better outcome, similar to the considerations with block confirmation time.
Re-requesting randomness is easily detectable on-chain and should be avoided for use cases that want to be considered as using VRFv2 correctly.
Consider the example of a contract that mints a random NFT in response to a userβs actions.
The contract should:
Record whatever actions of the user may affect the generated NFT.
Stop accepting further user actions that might affect the generated NFT and issue a randomness request.
On randomness fulfillment, mint the NFT.
Generally speaking, whenever an outcome in your contract depends on some user-supplied inputs and randomness, the contract should not accept any additional user-supplied inputs after it submits the randomness request.
Otherwise, the cryptoeconomic security properties may be violated by an attacker that can rewrite the chain.
fulfillRandomWords
must not revertVRFConsumerBaseV2
in your contract, to interact with the VRF serviceVRFv2WrapperConsumer.sol
in your contract, to interact with the VRF serviceIf you want to get multiple random values from a single VRF request, you can request this directly with the numWords
argument:
If you want to have multiple VRF requests processing simultaneously, create a mapping between requestId
and the response. You might also create a mapping between the requestId
and the address of the requester to track which address made each request.
You could also map the requestId
to an index to keep track of the order in which a request was made.
If you want to process VRF responses depending on predetermined conditions, you can create an enum
. When requesting for randomness, map each requestId
to an enum. This way, you can handle different execution paths in fulfillRandomWords
. See the following example:
If your fulfillRandomWords()
implementation reverts, the VRF service will not attempt to call it a second time. Make sure your contract logic does not revert. Consider simply storing the randomness and taking more complex follow-on actions in separate contract calls made by you, your users, or an .
If you implement the , use VRFConsumerBaseV2
. It includes a check to ensure the randomness is fulfilled by VRFCoordinatorV2
. For this reason, it is a best practice to inherit from VRFConsumerBaseV2
. Similarly, donβt override rawFulfillRandomness
.
If you implement the , use VRFv2WrapperConsumer
. It includes a check to ensure the randomness is fulfilled by the VRFV2Wrapper
. For this reason, it is a best practice to inherit from VRFv2WrapperConsumer
. Similarly, donβt override rawFulfillRandomWords
.
These are example best practices for using Chainlink VRF. To explore more applications of VRF, refer to our .
If you need to generate a random number within a given range, use to define the limits of your range. Below you can see how to get a random number in a range from 1 to 50.
If you are using the VRF v2 subscription method, see the guide for an example where one request returns multiple random values.
If you are using the VRF v2 direct funding method, see the guide for an example where one request returns multiple random values.