Ethereum: Solving for Gas Limit in Chaining Calls
As an Ethereum developer, you’re likely familiar with the concept of gas limits in smart contracts. When a call is made to a function that requires execution by the Ethereum Virtual Machine (EVM), the EVM has limited resources available for each function call. This is where gas limits come into play.
When calling another contract or function from within a different contract, it’s common to see chaining calls implemented, where multiple functions are called in sequence. However, when it comes to the gaslimit parameter of these calls, things get interesting.
In this article, we’ll delve into the concept of gas limits and how they’re used in chaining calls, with an example using Solidity on the Ethereum blockchain.
What is a Gas Limit?

A gas limit is a limit on the amount of gas (a unit of currency in the Ethereum network) that can be spent by a function during execution. The gas limit is set when a contract or function is deployed and is enforced by the EVM.
When a call is made to a function, the EVM checks if it has available gas resources before proceeding with the execution. If there’s not enough gas left in the account, the transaction is rejected and the sender’s wallet will be frozen until more gas becomes available.
Chaining Calls: How Gas Limits Work
Now, let’s consider an example of chaining calls from A to another contract or function using the chaincall function. In this case, we will use the Solidity language as an example.
pragma solidity ^0.8.0;
contract Example {
// Function A: a simple function that performs some action
function A() public {
(bool success,) = address(this).call{gas: 1000000}(abi.encodeWithSelector(this.B.selector));
console.log("A performed successful action");
}
// Function B: another function that requires gas to be passed
function B() public {
require(ethers.utils.gasLimit() >= 1000001, "Insufficient gas left in account");
(bool success,) = address(this).call{gas: 1500000}(abi.encodeWithSelector(B.selector));
console.log("B performed successful action");
}
}
In the above example, we have two functions: A and B. Function A calls another contract or function using the chaincall function with a gas limit of 1 million. The EVM checks if it has enough gas left in the account before proceeding with the execution.
However, when calling B, the EVM checks the available gas again and requires at least 1.5 million gas (1000001 + 1500000) to be passed from function A. If there’s not enough gas, the transaction will be rejected.
Gas Limit of Chaining Calls
In this example, we can see that the gaslimit parameter is used to enforce a specific gas requirement for each call. In chaining calls, the EVM checks if it has sufficient resources available before proceeding with each function execution.
To solve for gaslimit, we need to consider the total gas cost of all chained calls. The formula for calculating the required gas limit in this case is:
gaslimit = max((totalGasCost / gasPerCall) + 1, min(gasLimit, ceil(log2(totalGasCost)))
Here’s a simplified example:
pragma solidity ^0.8.0;
contract Example {
// Total gas cost of A: 1000001
uint totalGasCost;
// Gas per call for A
uint gasPerCall = 1500000;
// Constant EVM
uint maxGasLimit = 20000000; // arbitrary value
function getRequiredGasLimit() public view returns (uint) {
totalGasCost = 1000001 + gasPerCall * 10; // add a small amount of gas for safety.
return maxGasLimit - ceil(log2(totalGasCost));
}
}
In this example, we calculate the required gas limit by iterating over all chained calls and adding up their total gas costs.
