49

Different instructions of the EVM have different associated gas costs -- you can look at the list of the gas-prices in the appendix G of the yellow paper.

While the rough magnitudes of those numbers look quite reasonable, the exact values still look like they were chosen somewhat arbitrarily. On the other hand some values look strangely specific. Is there any deeper reason for choosing those exact values?

1
  • This question still needs an answer, because the current spreadsheet answer doesn't mention anything about methodology: is there any deeper reason for choosing those exact values?
    – eth
    May 1, 2018 at 8:29

3 Answers 3

37

I share with you the spreadsheet which explains the formula behind the gas cost of each OPCODE.

Version 1.0 was created by Core Devs of Ethereum. I look forward how to articulate future updates of it. It is a meta-consensus challenge to resolve.

2
  • Related question - is the gas cost the same on Testnet, or is it different? Feb 16, 2017 at 21:13
  • @BrianArmstrong Yes, the same EVM and gas costs are on Ethereum testnets.
    – eth
    Aug 2, 2019 at 8:11
11

The total cost of a transaction that creates a contract or executes a contract is based on 2 factors:

gasUsed is the total gas that is consumed

gasPrice specified in the transaction

Total cost = gasUsed * gasPrice

gasUsed

Each operation in the EVM was assigned a number of how much gas it consumes. gasUsed is summing up all the gas for all the operations executed. There is a spreadsheet which offers a glimpse to some of the analysis behind them.

For estimating gasUsed, there is an estimateGas API that can be used but has some caveats.

gasPrice

A user constructs and signs a transaction, and each user may specify whatever gasPrice they desire, this includes zero. However, the Ethereum clients launched at Frontier had a default gasPrice of 0.05e12 wei. As miners optimize for their revenue, if most transactions are being submitted with a gasPrice of 0.05e12 wei, it would be difficult to convince a miner to accept a transaction that specified a lower, or zero, gasPrice. How the default was chosen is asked in this question.

Example

Let's take a contract that just adds 2 numbers. From the spreadsheet above ADD consumes 3 gas.

The approximate cost, using the default gas price, would be:

3 * 0.05e12 = 1.5e11 wei

Since 1 Ether is 1e18 wei, the total cost would be 0.00000015 Ether.

This is a simplification since it ignores some costs, such as the cost of passing the 2 numbers to contract, before they can even be added.

1
  • 2
    In case this answer doesn't make as much sense, it's because it was originally an answer to ethereum.stackexchange.com/q/799/42 before it was merged/moved here.
    – eth
    Apr 8, 2016 at 2:02
3

Each instruction in Ethereum Virtual Machine has a cost, measured in units of gas. The table of such costs for each instruction is called "Fee schedule" and is an appendix in the Ethereum Yellow paper (it might change in the future though). The only way to determine (in general) how much gas a certain call to a contract would cost is to run it (not necessarily on the blockchain, but in a 'local simulator'). However, if the contract code interacts with the state of the blockchain (e.g. using instructions BLOCKHASH, TIMESTAMP, BALANCE), the execution cannot be simulated accurately. Therefore, in some cases, the real cost of execution will only be known when the transaction is included in the block. That is why you can try to estimate the upper bound of how much gas will be required and send enough ether with the transaction. Because if it is not enough, transaction fails with "Out of Gas" exception, and the ether is not refunded.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.