Search code examples
ethereumsolidityremix

What are Gas, transaction cost, execution cost On RemixIDE?


I am digging into gas, But I am unsure what are the difference among Gas and transaction cost, execution cost.

contract Simple {

 uint256 public num = 0;

 function setToOne() external {
     num = 1;
 }
}

When I call setToOne function, I can see

gas 49821 gas

transaction cost 43322 gas

execution cost 22258 gas

Okay, so I assume that gas may indicate the gas limit? if so, why is it 49821 gas.. ?

transaction cost seems to include execution cost?

so Basically, I can roughly calculate the execution cost, Gsset (20,000) + GColdsload (2,100) = 22100 ( which is similar to 22,258)

I guess the transaction cost seems like excution cost (22,258) + Gtrnsaction (21,000) = 43258 (which is similar to 43322)

I am still missing 64 gas.. , I am pretty unsure what 64 gas is for?

Please let me understand it...


Solution

  • In your example, you are calling the "setToOne" function so you're gonna see three different gas-related values:

    Gas limit: This is the most gas you will ever shell out for a single transaction. Your situation involves 49,821 gas. The transaction will fail and you will lose any gas used up to that point if it ends up using more gas than the allotted amount. The user sets the gas limit to control how much money they are ready to spend on a certain transaction.

    Transaction cost: This represents the total gas used during the transaction. Both the execution cost and the transaction's inherent cost are included. The transaction cost in your case is 43,322 gas.

    Execution cost: This is the quantity of gas needed to carry out the smart contract function in your situation, 22,258 gas. This expense relates to the different activities carried out within the function, including placing a value in storage.

    -> You mentioned that the execution cost and the inherited transaction cost make up the transaction cost. For a straightforward transaction, the cost is usually 21,000 gas (Gtransaction). Nonetheless, there can be other supplementary expenses, such data (calldata) prices, which would make up for the additional 64 gas you are omitting. Every byte of data you include in an Ethereum transaction costs gas, and the gas price changes based on whether the data contains zero or non-zero bytes.

    Summary...

    Gas limit: The most you're willing to spend on petrol for the transaction (49,821 gas in this case). Transaction cost: The sum of the execution cost and the intrinsic transaction cost for the transaction (43,322 gas in this case). Cost of execution: The gas used to carry out the smart contract's operation (22,258 gas in this case).

    references: https://docs.soliditylang.org/en/v0.8.19/security-considerations.html#sending-and-receiving-ether