Ethereum: Solidity gaslimit for chaining call?

Ethereum: resolution of the gas limit in chain calls

Ethereum: Solidity gaslimit for chaining call?

As an Ethereum developer, you probably know the concept of gas limits in smart contracts. When a call is made on a function that requires execution by the Ethereum virtual machine (EVM), the EVM has limited resources available for each function call. This is where the gas limits come into play.

When you call another contract or function from another contract, it is common to see the implemented chaining calls, where several functions are called in sequence. However, with regard to the “Gaslimit” parameter of these calls, things become interesting.

In this article, we will immerse ourselves in the concept of gas limits and how they are used in chaining calls, with an example using solidity on Ethereum blockchain.

What is a gas limit?

A gas limit is a limit of the quantity of gas (a currency unit in the Ethereum network) which can be spent by a function during execution. The gas limit is set when a contract or a function is deployed and applied by the EVM.

When a call is made on a function, the EVM checks if it has available gas resources before proceeding. If there is no longer enough gas in the account, the transaction is rejected and the sender’s portfolio will be frozen until more gas is available.

Chaining calls: how the gas limits work

Now let’s see an example of “A” chaining calls to another contract or function using the “Chaincall” function. In this case, we will use solidity language as an example.

`Solidity

Pragma Solidity ^ 0.8,0;

Example of contract {

// Function A: a simple function that performs an action

Public function {

(Bool Success,) = Address (this) .Call {Gas: 1000000} (abi.encodewithselector (this.b.selector));

Console.log ("A successful successful action");

}

// Function B: Another function that requires that the gas has passed

Public function b () {

require (Ethers.utils.Gaslimit ()> = 100,0001, "Insufficient gas left into account");

(Bool Success,) = Address (This) .Call {Gas: 1500000} (Abi.Ecodewithselector (B.selector));

Console.log ("B has carried out a successful action");

}

}

'

In the example above, we have two functions:A andB-. The "A" function calls for another contract or function using the "Chaincall" function with a gas limit of 1 million. The EVM checks if it has enough gas in the account before proceeding with the execution.

However, when calling "B", the EVM again checks the available gas and requires at least 1.5 million gases (100,0001 + 1500000″) to be transmitted from the A. if n ‘There is not enough gas, the transaction will be rejected.

gas limit in chaining calls

In this example, we can see that the “Gaslimit” parameter is used to apply a specific gas requirement for each call. In chaining calls, the EVM checks if it has sufficient resources available before carrying out each function execution.

To resolve for “Gaslimit”, we must consider the total cost of the gas of all chained calls. The formula to calculate the gas limit required in this case is:

Gaslimit = Max ((Totalgascost / Gaspercall) + 1, min (Gaslimit, Ceil (Log2 (Totalgascost))

Here is a simplified example:

`Solidity

Pragma Solidity ^ 0.8,0;

Example of contract {

// Total cost of gas from A: 100,0001

Uint Totalgascost;

// gas by call for a

Uint GasperCall = 1500,000;

// Constant EVM

uint maxgaslimit = 20000000; // Arbitrary value

GETREQUIREDGASlimit function () Returns of public view (Uint) {

TOTALGASCOST = 100,0001 + 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 on all the appelled calls and by adding their total gas costs.

Bitcoin Curve Rank

Related posts