NEW

Attend SmartCon 2023 to explore the future of Web3. Sign up now.

Estimating VRF Costs

This guide explains how to estimate VRF costs for both the subscription and direct funding methods.

Understanding transaction costs

For Chainlink VRF v2 to fulfill your requests, you must maintain a sufficient amount of LINK in your subscription balance. Gas cost calculation includes the following variables:

  • Gas price: The current gas price, which fluctuates depending on network conditions.

  • Callback gas: The amount of gas used for the callback request that returns your requested random values.

  • Verification gas: The amount of gas used to verify randomness on-chain.

The gas price depends on current network conditions. The callback gas depends on your callback function, and the number of random values in your request. The cost of each request is final only after the transaction is complete, but you define the limits you are willing to spend for the request with the following variables:

  • Gas lane: The maximum gas price you are willing to pay for a request in wei. Define this limit by specifying the appropriate keyHash in your request. The limits of each gas lane are important for handling gas price spikes when Chainlink VRF bumps the gas price to fulfill your request quickly.

  • Callback gas limit: Specifies the maximum amount of gas you are willing to spend on the callback request. Define this limit by specifying the callbackGasLimit value in your request.

Estimate gas costs

You need to pre-fund your subscription enough to meet the minimum subscription balance in order to have a buffer against gas volatility.

After the request is complete, the final gas cost is recorded based on how much gas is used for the verification and callback. The actual cost of the request is deducted from your subscription balance.

The total gas cost in wei for your request uses the following formula:

(Gas price * (Verification gas + Callback gas)) = total gas cost

The total gas cost is converted to LINK using the ETH/LINK data feed. In the unlikely event that the data feed is unavailable, the VRF coordinator uses the fallbackWeiPerUnitLink value for the conversion instead. The fallbackWeiPerUnitLink value is defined in the coordinator contract for your selected network.

The LINK premium is added to the total gas cost. The premium is defined in the coordinator contract with the fulfillmentFlatFeeLinkPPMTier1 parameter in millionths of LINK.

(total gas cost + LINK premium) = total request cost

The total request cost is charged to your subscription balance.

Ethereum example

This is an example calculation of a VRF request on the Ethereum network. The values for other supported networks are available on the Supported Networks page.

Estimate minimum subscription balance

You need to have the minimum subscription balance for your requests to be processed. This provides a buffer in case gas prices go higher when processing the request. The actual cost of the request is usually lower than the minimum subscription balance.

ParameterValue
Gas lane500 gwei
Callback gas limit100000
Max verification gas200000
LINK premium0.25 LINK
  1. Calculate the total gas cost, using the maximum possible gas price for the selected gas lane, the estimated maximum verification gas, and the full callback gas limit:

    Gas cost calculationTotal gas cost
    Gas price x (Verification gas + Callback gas)
    500 gwei x (200000 + 100000)150000000 gwei (0.15 ETH)
  2. Convert the gas cost to LINK using the LINK/ETH feed. For this example, assume the feed returns a conversion value of Ξ0.004 ETH per 1 LINK.

    ETH to LINK cost conversionTotal gas cost (LINK)
    0.15 ETH / 0.004 ETH/LINK37.5 LINK
  3. Add the LINK premium to get the total maximum cost of a request:

    Adding LINK premiumMaximum request cost (LINK)
    Total gas cost (LINK) + LINK premium
    37.5 LINK + 0.25 LINK37.75 LINK

This example request requires a minimum subscription balance of 37.75 LINK. Check the Max Cost in the Subscription Manager to view the minimum subscription balance for all your contracts. When your request is processed, the actual cost of the request is deducted from your subscription balance.

Estimate VRF request cost

This example reflects an estimate of how much a VRF request costs. Check Etherscan for current gas prices.

ParameterValue
Actual gas price50 gwei
Callback gas used95000
Verification gas used115000
LINK premium0.25 LINK
  1. Calculate the total gas cost:

    Gas cost calculationTotal gas cost
    Gas price x (Verification gas + Callback gas)
    50 gwei x (115000 + 95000)10500000 gwei (0.0105 ETH)
  2. Convert the gas cost to LINK using the LINK/ETH feed. For this example, assume the feed returns a conversion value of Ξ0.004 ETH per 1 LINK.

    ETH to LINK cost conversionTotal gas cost (LINK)
    0.0105 ETH / 0.004 ETH/LINK2.625 LINK
  3. Add the LINK premium to get the total cost of a request:

    Adding LINK premiumTotal request cost (LINK)
    Total gas cost (LINK) + LINK premium
    2.625 LINK + 0.25 LINK2.875 LINK

This example request would cost 2.875 LINK, which is deducted from your subscription balance.

Arbitrum example

Similarly to supported L1 networks, VRF gas costs are calculated based on the amount of verification gas and callback gas used, multiplied by the gas price:

(L2 gas price * (Verification gas + Callback gas)) = total gas cost

Set your callbackGasLimit to account for L2 gas costs only. As with VRF requests on L1 chains, the request fails if your callbackGasLimit is too low.

Although the total transaction costs for using Arbitrum involve both L2 gas costs and L1 costs, the Chainlink node pays any L1 gas costs to post the transaction. To understand how Arbitrum's total transaction fees are calculated, including L1 costs, refer to this Arbitrum gas estimation tutorial.

VRF direct funding is not yet supported on Arbitrum.

What's next

Stay updated on the latest Chainlink news