Migrating from VRF v1
Comparison between VRF v1 and VRF v2 (Direct funding method)
The main similarity between VRF v1 and VRF v2 Direct funding method is that consuming contracts must be funded with LINK to pay for requests. However, Chainlink VRF v2 includes several improvements.
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 that you use. See the gas limits on the 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 Wrapper 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.
Updating your applications to use 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.
Import and inherit the new
VRFV2WrapperConsumerBase.solcontract and remove the v1
VRFConsumerBase.solimport. This contract includes the
VRFV2WrapperConsumerBaseconstructor as shown in the Get a Random Number example and use the correct VRF v2 Direct funding configuration.
You can still call the
requestRandomnessfunction. However, the v2
requestRandomnessfunction requires several different parameters (
numWords). See the Supported networks page to adjust them for your own needs.
fulfillRandomnessfunction calls to
fulfillRandomWords. Update the call to handle the returned
uint256array instead of the single