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.sol
contract and remove the v1VRFConsumerBase.sol
import. This contract includes thefulfillRandomWords
function. -
Add a
VRFV2WrapperConsumerBase
constructor as shown in the Get a Random Number example and use the correct VRF v2 Direct funding configuration. -
You can still call the
requestRandomness
function. However, the v2requestRandomness
function requires several different parameters (callbackGasLimit
,requestConfirmations
,numWords
). See the Supported networks page to adjust them for your own needs. -
Change
fulfillRandomness
function calls tofulfillRandomWords
. Update the call to handle the returneduint256[]
array instead of the singleuint256
variable.