Migrating from VRF v1
Chainlink VRF v2 includes several improvements and changes to the way you fund and request randomness for your smart contracts.
Subscription management: Chainlink VRF v2 introduces a Subscription Manager application that allows smart contract applications to pre-fund multiple requests for randomness using a single LINK token balance. This reduces the gas fees for VRF requests by eliminating the need to transfer LINK tokens for each individual request. You transfer LINK tokens to the subscription balance only when it requires additional funding. Read the Subscription Manager page to learn more.
Variable Callback Gas Limit: Chainlink VRF v2 lets you adjust the callback gas limit when your smart contract application receives verifiable randomness. Consuming contracts can execute more complex logic in the callback request function that receives the random values. Tasks involving the delivered randomness are handled during the response process. The new gas limits are higher than the VRF V1 limit, and vary depending on the underlying blockchain you use. See the gas limits on the VRF Supported Networks page.
More configuration capability: You can define how many block confirmations must pass before verifiable randomness is generated and delivered on-chain when your application makes a request transaction. The range is from 3 to 200 blocks. VRF V1 always waited 10 blocks on Ethereum before delivering on-chain randomness. Select a value that protects your application from block re-organizations while still providing sufficiently low latency from request to response. See the Security Considerations page to learn more.
Multiple Random Outputs in a Single Request: The VRF Coordinator contracts in VRF v2 allow you to request multiple random numbers (multi-word) in a single on-chain transaction, which reduces gas costs. The fulfillment is also a single transaction, which reduces the latency of responses.
Unified Billing - Delegate Subscription Balance to Multiple Addresses: Chainlink VRF v2 allows up to 100 smart contract addresses to fund their requests for verifiable randomness from a single LINK subscription balance, which is managed by the subscription owner.
Read the Chainlink VRF v2 blog post for a detailed explanation about the benefits and use cases for VRF v2.
To modify your existing smart contract code to work with VRF v2, complete the following changes. See the Get a Random Number guide for an example.
Set up and fund a subscription in the Subscription Manager at vrf.chain.link.
Import the new
VRFConsumerBaseV2.solcontract and remove the v1
VRFConsumerBase.solimport. This contract includes the
VRFCoordinatorV2Interface.solinterface. This interface includes the new
VRFConsumerBaseV2constructor as shown in the Get a Random Number example.
requestRandomnessfunction calls to
requestRandomWordsfunction requires several additional parameters.
fulfillRandomnessfunction calls to
fulfillRandomWords. Update the call to handle the returned
uint256array instead of the single