Remix – How to Estimate Execution Costs in Solidity

remixsolidity-0.5.x

I understand the difference between transaction costs and execution costs, but I don't understand two things:

  1. What does the "gas" readout in Remix refer to?
  2. How do I create an estimate of how much doing something will cost?

When I run a transaction in Remix, I get the following output when I click the down arrow for more details on the transaction:

Solidity Output

I thought that "gas" would just be a sum of transaction cost and execution cost, but that's obviously not the case. So what does this gas line mean?

In addition, using this information, how would I compute how much money (in dollars) it would take to have completed this transaction?

I know I can see how much gas costs here. (Currently at 114.43 Gwei.)
Then, since there is 1 billion Gwei in an Ether, and the price of an Ether can be looked up here, I can figure out how much in dollars each gas costs.

But what numbers do I multiply by this cost to get the total cost of running something? Should I do (gas + transaction cost + execution cost) * gas price? Or just (transaction cost + execution cost) * gas price? Or does only the execution cost matter?

(Also note that I have read through the answers to this question as well, but they don't answer my question (1) and aren't very clear about my question (2). Thus, I have asked a new question.)

Best Answer

EDIT: I believe I misunderstood your question; this is probably a better response:

What does the "gas" readout in Remix refer to?

The gas line is referred to the total maximum amount of gas provided with the transaction (you are including 80... gas to be used)

the execution is how much gas is effectively used of the amount provided; so in your example 80.... gas is probably too much compared to the real necessity (anyway the unused gas will be returned, so you are not paying the whole amount)


How do I create an estimate of how much doing something will cost?

in order to estimate your gas consumption there is a function called gasEstimate()

you can check an usage here: https://medium.com/truffle-suite/ethereum-gas-exactimation-1158a996eb8c

Here is a truffle example test:

const Optim = artifacts.require("Optim");

contract("Optim", () => {
    it("should have different gas", async() => {
        const O1 = await Optim.new();

        const method1 = await O1.method1.estimateGas(params);
        const method2 = await O1.method2.estimateGas(params);

        console.log("method1 cost: " + method1, "method2 cost: " + method2);
        assert(method1 < method2, "method 1 < method2");
    });
});

On a broader context, to better understand what operations cost more than others:

here, as an example, (https://github.com/crytic/evm-opcodes) you can check that an ADD operation needs 3 gas.

so : more complex code = more gas needed

-- if you are well versed in math you can check the H.1 appendice in the ETH yellow paper

https://ethereum.github.io/yellowpaper/paper.pdf to identify which operations requires more or less gas


To know how much USD you are spending, you need to convert from ETH to USD gas_used * gas_price

the gas price is gwei (ETH) and fluctuates constantly, so you won't have a sure "carved in stone" answer (it depends on the network status);

Related Topic