docs
Gas Calculations

Understanding Gas Calculation for Oracle Callbacks

When interacting with blockchain smart contracts, every operation you execute costs gas. This guide will help you understand how to properly estimate the gas required for your oracle's callback when using the fulfillRequest() function in your contract.

Why Gas Estimation is Important

When the oracle returns a response to your request, it triggers the fulfillRequest() function in your smart contract. The gas required to process this response varies depending on the logic inside fulfillRequest().

Incorrectly estimating the gas cost can result in one of two issues:

  1. Insufficient Gas: If you don't send enough gas with your request, the transaction will fail when the oracle tries to call fulfillRequest().
  2. Excess Gas: Sending too much gas won't harm your transaction, but it could result in unnecessary spending. Fortunately, the oracle will refund any unused gas after the callback is completed.

Simulating the Gas Cost

To help you estimate the gas cost, we offer a tool that allows you to simulate the behavior of the fulfillRequest() function without executing a real request. You can input the request parameters, and our tool will simulate the oracle's response, calculating the gas usage of your logic.

This is especially useful if your fulfillRequest() logic involves complex operations or varying logic paths based on the oracle's response.

How the Gas Calculation Works

  • Simulation: The gas calculator simulates a fake oracle response to your contract, running the fulfillRequest() logic as if it were an actual response from the oracle.
  • Gas Estimate: After simulating the call, the tool will provide an estimated gas cost for your specific logic.

Once you know the estimated gas usage, you can then use this number to correctly set the gas limit when you make future requests.

Handling Dynamic Logic in fulfillRequest()

If your fulfillRequest() function contains multiple logic paths that could use varying amounts of gas, it's important to account for the highest possible gas usage. In other words, you should simulate the most expensive path of execution and use that gas cost as the basis for your gas limit.

Note: The oracle will refund any unused gas, so even if you overestimate the gas limit, you will only be charged for the gas actually consumed by the callback.

Here’s how it works in practice:

  1. Use the gas calculator tool to simulate the gas cost for each possible path in your fulfillRequest() logic.
  2. Identify the path that consumes the most gas.
  3. Use this gas estimate as the limit when making your request to ensure the transaction will succeed, no matter which logic path is triggered.

Example of Dynamic Logic

Let’s say your fulfillRequest() function behaves differently depending on the response from the oracle. In this scenario, based on the content of the oracle’s response, different functions in your contract may be executed, each consuming a different amount of gas.

For example:

  • If the oracle returns a specific type of data, your contract might execute a function that consumes X gas.
  • If the oracle returns a different type of data, it might trigger another function in your contract that consumes Y gas.

Since these two possible logic paths consume different amounts of gas, it is crucial to simulate the gas consumption for each possible response and identify the one that requires the most gas.

Steps for Handling Dynamic Logic:

  1. Simulate Both Scenarios: Use the gas calculator to simulate the gas usage for each function that could be executed based on the oracle's response:

    • Function A (Consumes X gas)
    • Function B (Consumes Y gas)
  2. Identify the Maximum Gas Usage: After simulating both paths, you should take the gas estimate from the most expensive path. This ensures that no matter which logic is triggered by the oracle's response, there will be enough gas available for the transaction to succeed.

  3. Set Gas Limit Based on the Higher Estimate: When making your request to the oracle, use the higher gas estimate. Even though the actual gas usage may be lower if the simpler logic is triggered, setting the gas limit based on the maximum usage guarantees that the callback will always complete successfully.

Important Note:

The oracle will refund any unused gas, so even if you overestimate the gas needed, you won’t lose anything. The unused gas will be returned to you once the fulfillRequest() function finishes execution.

By following this approach, you avoid the risk of underfunding the transaction, ensuring that your contract handles all possible oracle responses without running into gas issues.