Ethereum: Solidity gaslimit for chaining call?
Ethereum: Solution to the gas limit when chaining of calls
As an Ethereum developer, you are probably familiar with the concept of gas limits in intelligent contracts. If a call is made on a function that requires the execution by the Virtual Machine (EVM) from Ethereum, the EVM has only limited resources for every function call. Gas boundaries come into play here.
If you call up a different contract or another function from a different contract, it is common for the implemented chain calls to be called up in sequence. However, when it comes to the “gas limit” parameter of these calls, things become interesting.
In this article we will deal with the concept of gas boundaries and how to use calls to chain, an example on the Ethereum blockchain.
What is a gas limit?
A gas limit is a limit for the gas volume (a currency unit in the Ethereum network), which can be output from a function during the execution. The gas limit is determined if a contract or function is used and forced by EVM.
If a call is made to a function, the EVM checks whether it is available before execution of gas resources. If there is not enough gas in the account, the transaction is rejected and the wallet’s wallet is frozen until more gas is available.
Increasing calls: How gas boundaries work
Now let’s consider an example of chain calling “A” to another contract or another function using the “ChainCall” function. In this case, we will use the solidity language as an example.
`Solidity
Pragma solidity ^0.8.0;
Contract example {
// function A: a simple function that performs an action
Function a () public {
(BOOL Success,) = address (this) .Call {gas: 1000000} (Abi.encodeewselector (this.b.Selector));
console.log (“a successful action carried out”);
}
// Function B: Another function in which gas has to be handed over
Function B () Public {
require (ethers.utils.gaslimit ()> = 1000001, “inadequate gas that is invoiced”);
(BOOL Success,) = address (this) .Call {gas: 1500000} (Abi.encodeewithelector (B.Selector));
console.log (“B carried out successful action”);
}
}
`
In the example above we have two functions: “A” and “B”. Function A" calls a different contract or another function using the "ChainCall" function with a gas limit of 1 million. The EVM checks whether there is enough gas in the account before continuing with the execution.
If the EVM calls the available gas again, the available gas must be checked again and handed over at least 1.5 million gas (1000001 + 1500,000 ') from Function A. If there is not enough gas, the transaction is rejected.
Gas boundary in the chain of calls
In this example, we can see that the "gas limit" parameter is used to enforce a certain gas requirement for every call. When chaining calls, the EVM checks whether sufficient resources are available before continuing with any functional execution.
In order to solve for "gas limit", we have to take into account the total gas costs of all chained calls. The formula for calculating the required gas limit in this case is:
Gaslimit = Max ((Tholtascost / Gaspercall) + 1, min (gas limit, ceil (log2 (tholdascost)))
Here is a simplified example:
Solidity
Pragma solidity ^0.8.0;
Contract example {
// total gas costs of A: 1000001
Uint Totalgascost;
// gas per call for a
uint gas percall = 1500000;
// EVM constant
uint maxgas limit = 2000000; // arbitrary value
Function Trequiredgaslimit () Public view return (uint) {
Totalgascost = 1000001 + gas percall * 10; // Add a small amount of gas for safety
Return Maxgaslimit – Ceil (Log2 (Totalgascost));
}
}
`
In this example, we calculate the required gas limit by ittery all the chained calls and add their total gas costs.