Ethereum "Gas" - How it Works

in #ethereum8 years ago (edited)

Purpose


Understanding how gas works is crucial for Ethereum users interested in participating in ICOs, using smart contracts, and even making simple transfers between wallets. In this post, I hope to explain what gas is and how it works. Skip to the end for a simple summary of things. To start off, we need to understand some basics about the Ethereum Virtual Machine.

Ethereum Virtual Machine and Gas


The Ethereum Virtual Machine (EVM) is a major part of the Ethereum ecosystem, but I'll be heavily glossing over this so we can focus on how it relates to gas. In short, the EVM is an environment where arbitrary code of smart contracts and other operations can be executed. Every node in the Ethereum network executes operations within the EVM to ensure redundantly correct execution and relies on consensus to agree on the answer.

All transactions, from simple transfers to ICO smart contracts, require some amount of operations to perform. Each of these operations has an associated cost in gas. Thus, simple transactions like transfers will require less gas to perform than more intense smart contracts.

For example, a simple operation like if(var > 1) may cost 1 gas, but a more complex operation to store a variable could cost 100 gas. The cumulative sum of all the operations is the total gas cost for the transaction.

Gas Limit


When on a site like MyEtherWallet, you're going to see a field called Gas Limit. This corresponds the the MAXIMUM amount of gas you are willing to spend on the transaction.
Important to note:

  • Different types of transactions will require different amounts of gas to complete
  • Providing too little gas will result in a failed transaction, the fees are kept by the miner
  • Extra, unspent gas is refunded automatically

What happens if I specify too little gas?

Your transaction will start to be executed, but will eventually run out of gas and be stopped. When this happens, you will not get back ether spend on the gas used, but since the transaction did not complete, the blockchain does not reflect a transfer, so the main funds essentially never left the wallet.

So I should always specify a large gas limit since I'll be refunded?

No. The minimum gas limit for transaction is 21000 + the amount to execute any fallback functions if it's sent to a contract, but the actual blocks also have a gas limit that specifies the maximum amount of gas all transactions in the block can consume. This is comparable to Bitcoin's block size in bytes, but ether miners have the option to increase or decrease the gas limit of blocks so that they are propagated quickly. You cannot exceed this amount if you wish for your transaction to be processed. Also, miners only get paid for the actual gas consumed by a transaction, so putting a high value for the gas limit fills up the block gas limit, but isn't computationally-dense enough for miners to want.

Gas Price


Gas price is the amount of ether you are willing to spend on every unit of gas. Yes, you determine the price of gas AND maximum amount of gas you want to spend on performing a transaction on the Ethereum network. Although gas limit is just a simple quantity, gas price is measured in ether (specifically some amount of wei, usually gwei). The value of gas is driven by the market and the nodes that prioritize higher gas prices when mining transactions. The current gas price can be seen on Etherscan or EthGasStation. As I mentioned, gas price is usually some amount of gwei. Wei is the smallest unit of ether, and gwei is simply 1000000000 wei. To put this in perspective, check out this code below:

var unitMap = {
    'wei':          '1',
    'kwei':         '1000',
    'ada':          '1000',
    'femtoether':   '1000',
    'mwei':         '1000000',
    'babbage':      '1000000',
    'picoether':    '1000000',
    'gwei':         '1000000000',
    'shannon':      '1000000000',
    'nanoether':    '1000000000',
    'nano':         '1000000000',
    'szabo':        '1000000000000',
    'microether':   '1000000000000',
    'micro':        '1000000000000',
    'finney':       '1000000000000000',
    'milliether':   '1000000000000000',
    'milli':        '1000000000000000',
    'ether':        '1000000000000000000',
    'kether':       '1000000000000000000000',
    'grand':        '1000000000000000000000',
    'einstein':     '1000000000000000000000',
    'mether':       '1000000000000000000000000',
    'gether':       '1000000000000000000000000000',
    'tether':       '1000000000000000000000000000000'
};

from https://github.com/ethereum/web3.js/blob/0.15.0/lib/utils/utils.js#L40

Setting Gas Limit and Gas Price


If you're using MetaMask, you will see something like this when trying to send Ether:

Here you can see that the gas limit has been set to 31500 and the gas price at 20 gwei. This means we are only willing to spend a maximum on 31500 gas at the price of 20 gwei / gas on the transaction. 31500 x 20 gwei = .000630 ether.

MyEtherWallet has the Gas Limit field, but no Gas Price for their transactions.
*I assume they calculate Gas Price depending on the market value

Edit- MEW has a slider at the bottom right of the site for adjusting gas price.

Summary


  • Transactions on the Ethereum network require fees in the form of gas. The amount of gas depends on the amount of computation required to complete the transaction.
  • Gas Limit is the maximum amount of gas allotted to the transaction, 21000 being sufficient for simple transfers and much higher amounts for transfers to smart contracts like those in ICOs.
  • Insufficient gas in the Gas Limit will result in a failed transaction, the fee paid will be lost, but the ether transferred never leaves the wallet since the blockchain was never updated.
  • Gas Price is the price of each gas unit measured in a fractional amount of ether, typically gwei. Gas price varies, but 20-30 gwei is average as of writing this.
  • Current Gas Price can be checked online ( Etherscan or EthGasStation ), and the Gas Limit for things other than simple transfers can only be known by looking at the smart contract code or by looking at what the organization behind the ICO / smart contract says is appropriate for their smart contract.

Handy Sites

EthStats
Etherscan
EthGasStation
Ethereum and ERC20 token wallet, MyEtherWallet
MetaMask Chrome Plugin

Consider leaving a like or even resteem if you found this helpful! If you're really crazy, my MEW address is 0x5B13430b6D2E327DFCDB93D7430FC6e9a2E63E74. Please feel free to leave corrections and comments below :)

Sort:  

Hey, very helpful, but what's the MINIMUM amount of gwei to put to let the transaction does not fail? i mean, if i set 0.1 gwei, the transaction may go into "limbo" and never execute? if yes, what is the minimum price?( don't care if it will be slow)

There isn't one. You can do 0 gwei and miners can accept it if they want. It may never actually be accepted, but it can be.

Thanks, but do you think that with 0.1 or 0.5 gwei might be accepted or there are some chances to never be accepted? i must do thousands of transactions, so i'm trying to spend as low as possible..

If you're asking about chance, then sure, there's a chance they may never be mined.. It depends on the transaction pool, how long you want to wait, and what miners are willing to accept.

I've done 0 gwei free transactions before, and I've had problems with 20 gwei transactions never being mined. It depends.

If you're sending thousands of separate transactions, you're not really using cryptocurrency very intelligently. There are better, and cheaper ways.

Thanks a bunch man! I have multiple MEW accounts and this helped a lot in loading my ETH wallets for transfer fees. :D

thank you. No one has explained it properly like you did in this post. Thanx again!!

Glad it was helpful!

I have 1 question. What would it be the highest gas posible? And how would it cost? THX

As I mention in the post, there is a limit to the amount of gas a block can have, so the maximum gas a transaction could have and still be accepted is limited by this block size. See https://ethstats.net/ "gas limit" section to see the block gas limit (it's dynamic).

There is no maximum gas price, but it's bounded by the largest number you can put into that field. IIRC, every number is 128 bits, so it would be 2^128 - 1.

If this is how ethereum works, then that's some bullshit right there. A failed contract took an $18.00 fee in ethereum from me. If there is any error error then kiss your fee goodbye. Sounds like bad business to me.

I doubt the contract was written incorrectly, so it's likely that you either executed it improperly or executed it properly but you didn't know what it would do and it turned out different than what you expected.

In either case, you set the gas price and agreed to "lose" your money. It's your fault, not the Ethereum Networks, and almost certainly not the contract's.

This has been really helpful. I'm already in BTC, LTC and a few alts but recently purchased some Ether to play around with CryptoKitties and the Gas/Gwei thing was a little confusing from the outside. Not as worried about being overcharged now. Thanks! :)

This is a great explanation of ETH transaction fees, I have always steered away from ETH because of this learning curve. Thank you so much for taking the time to make this more clear for the ones newer in the crypto space like myself.

thank you so much!

Excellent explanation !!
Compliments. Thanks for sharing.

This post has helped me to understand gas and gwei. thanks.

So I came here expecting to learn about gas price and gas limit but it still seems confusing. Why the separation of the two? Why not have just one fee? I can add much gas and pay little for it or add little gas and pay a fortune for it. What is the difference exactly?

Gas itself exists to accurately represent computational work. Ether, and thus value, is not a good measurement of this work because the value is so volatile. Can you imagine if there was a flat fee in Ether that you paid for transactions, and it was set when Ether was $10? Why would you pay 50x more for the same work just because Ether is worth more?

You need gas so that somebody doesn't write code to execute an infinite loop that would stall the blockchain. You need gas so that each individual opcode in the EVM can be accurately weighted proportionally to electrical costs (or other costs such as storage) of performing the operation.

The actual fee is up to the free market to determine via gas price, but the computation is a set amount and needs a separate unit to be represented in.

A quick question arose from "So I should always specify a large gas limit since I'll be refunded?" phrase from your article.

A function takes 40,000 Gas Units to execute. Which one of the following function calls will miner prefer to take?

  1. Call with 100,000 Gas Units.
  2. Call with 50,000 Gas Units.

Provided that the miner is paid for just 40,000 gas unit operations, they will prefer 2nd function call as it takes less total block size limit compared to 1st call.

Does the algorithm for tx selection from txpool work like this?

Preferring the call with 50k is better for the miner since it frees up 50k in the block for additional transactions hat will actually result in fees.

However, I had a conversation with the person behind a lot of the ethgasstation.info site programming and we found that this is not how actual miners are working. Looking at historical transactions, miners are pretty dumb. They almost always prefer higher gas price and don't look at gas limit, regardless of whether it's high or low.

Miners don't usually account for gas limit until it exceeds 1M or so, in which case they simply ignore it regardless of gas price.

For a demographic that thrives on cost optimization, this was a strange result to find. I'd expect it to change in the future, but who knows.

You made it look so simple !!!

This article is very helpful, voted to help writer continuing to post such articles.

Hello there. A complete newbie with only 1 week worth of reading here! Dont be harsh on me!
So after finishing reading i have this one question.
There is ether which is the main currency which fuels everything and tokens are based on. And you can send and receive ether through transactions. What is the job of gas exactly in this happening?!
Thanks for the great article. Needs many rereads on my part. :))

Ok so i now know the answer (i think) and will put it here for it might be of use to someone noob like myself.
Sending and receiving ether and tokens are not free as i thought. A price should be paid for miners to put your transaction in a block and add it to the chain. And that price is payed in ether and is called gas for that transaction.

And now my question would be 1. Am i right on the fact that gas is awarded to miners or what miners mine has nothing to do with it?
And 2. So when buying tokens there are two things to consider. First how much we want to invest and second how much we are willing to spend on that attempt to invest. Whats the purpose of this second thing? Any analogy to the common banking system would be helpful and very appreciated.

To be clear, gas is a unit of measurement. It has no value, it simply represents the amount of work a miner must do while processing the transaction.

If a transaction consumes more gas, the miner has to do more work. Gas price is the amount of Ether you actually pay for each unit of gas that your transaction uses.

Miners receive the product of gas consumed * gas price for any transaction.

Since there are multiple transactions in a block, you add up all of these products and that becomes part of the block reward given to the miner who produces the block.

For your second question, you're using the word "invest" and that's a bit confusing to me.

I'll assume you're talking about gas limit and gas price. The "how much we want to invest" would be your gas limit, which is the upper bound of how much gas you're willing to pay for in a transaction. The "how much we are willing to spend on that attempt to invest" would be the gas price, the actual amount of Ether you spend on each unit of gas.

And also on rereading your comment, suddenly i have some weird problems :D
Gas limit is a set amount equal to the (technical?)(processing wise?) difficulty of mining it; thus that much will be payed as a fixed number called gas limit for that transaction. Multiplied by the price we set for every gas.

And i guess the word invest (for example as making a transaction for investing in EOS ICO) has nothing to do with anyone of those two per se. :))
But then again wouldnt that make gas price the amount we would want to invest in making this transaction happen. (And so would this make transactions with higher gas limit needs inherently to be preferred more by miners?!
Thanks!:)

Gas is a measurement of computational work. Each instruction in the Ethereum Virtual Machine has an associated gas cost. For example, the opcode ADD has a gas cost of 3, and all it does is add two numbers. MUL costs 5 gas, and multiplies two numbers. These are pretty cheap, but an instruction like STORAGEADD costs 20,000 gas because it permanently stores a 256-bit word on the blockchain. It's not so computationally hard, but the memory consumed is substantial when you consider that these 256 bits must be stored on every full node mining.

Gas price could be thought of as "the amount invested in the transaction", but I don't think it's very useful to think of it that way. The main reason is because it's not really an investment. It has a binary outcome, either the transaction goes through and you pay, or it doesn't and you don't. There's no risk involved, so I don't think it's very comparable to an investment.

The exception to this would be when a transaction not being mined has an associated cost. A good example of this would be when you're trying to get into an ICO that is selling out quickly. If you don't specify a high enough gas price, your transaction might not go through quick enough and you lose your investment opportunity. That means there's an opportunity cost to your transaction, and in this scenario the gas price you specify is more like an investment in the transaction.

Yep exactly what i had in mind.

Thank you for taking your time @tomshwom
Why should we create competition between transactions with gas price? Wouldnt it be better to have a fixed gas price so in the end all transactions would go through?

P.S: thinking about it more this would make transactions with low gas limit, face problems as miners will go for high cost ones preferentially.

So let me put it this way. There is a fee for any transaction to be put in a blockchain and so be accepted by the community which is what gives it its credibility (this transaction is done).
Now you can set that fee higher so your transaction goes through faster.

It is unfortunate that you have to pay the full price for a failed transaction.
You have any comments on that? Why the full gas invested should be payed?

Thanks again :)

Gas fees exist because there are real costs to running the blockchain. Miners spend a lot of money running their mining rigs in a Proof of Work consensus blockchain, and there are electrical/hardware costs associated with that. You have to pay miners enough to incentivize them to actually maintain the blockchain for you, and you want that incentive to be large enough that you have a truly distributed network of miners so that 1) nobody can 51% attack the network with invalid transactions, and 2) even if all of the US goes offline, the blockchain is fine.

The reason why these fees are proportional to computation (which is where the gas measurement comes from) is because it simply makes more sense. If your transaction takes more computational steps, it burns more electricity and consumes more resources from the network. You should pay for that, and the amount should be proportional to the work. These fees also deter attacks on the network with things like infinite loops. I becomes economically impossible to sustain a spam attack when miners have the choice to ignore your transaction because it's not paying enough for them.

If a transaction is included in the blockchain, it hasn't failed, and this is when you pay your gas fees. Failed transactions do not cost anything because they are never computed by the network. However, transactions that did not specify a high enough gas limit to complete the computation, had incorrect data, or were wrong in some other way do cost money because they still cost the network to process these "failed" transactions. You cannot use a global distributed network, mess up, and hope for a refund. It's just not fair to the people who did the work for you.

In the future, contracts should be written to be able to anticipate failure ahead of time and exit quickly so that the gas consumed is small. This will help save people money when they mess up their transaction, but won't hurt miners.

Clear and thorough explanation. Kudos to you!

Please correct me if im wrong. When gas limit is specified as 100 thousand and your transaction needs 1 you will pay the limit you have specified if your transaction fails. Unlike when it succeeds!? Why is that so?

Thanks for this explanation, very informative. Still i have a question. How does the gas price relate to the fluctuation of Ether value? We've seen Ether as a very volatile currency so far, but it wouldn't be fair if the gas price doesn't consider this right?

Gas price doesn't have a real "going rate" like ETH does. Looking at http://ethgasstation.info/, There's 13% of blocks right now being mined by pools that accept 0 gwei for gas price, so it's totally possible to make transfers without paying any fees at all. The difference is that you may have to wait a bit longer for your transaction to be included in a block (because you only have a 13% chance that a pool who accepts your transaction will mine it).

Also, gas fees are very small. The median gas fee is about $0.26, which is actually quite high right now for Ethereum (in part due to the difficulty bomb before Metropolis).

If there was more competition over price, and if the prices were significant for people to actually profit, then gas fees might become more aligned with the ETH price. I'd rather have it be separate though.

Question: do only miners perform transactions on the network besides mining blocks or can a node also perform transactions (and reveive gas for it) without mining?

You're missing what "performing transactions" actually means. The blocks are the transactions. Nothing is valid if it's not included in a block that is part of the canonical blockchain. Miners are the ones who assemble blocks from several transactions, and link them to the blockchain.

Things like side-channels are performed off-chain, but are still settled and only valid on-chain. Since they are side-chain, there is no gas or transaction fees (unless that's part of the specific transaction), and mining isn't required until settlement, which is on-chain.

Great read! I especially like that you showed all the units of ether. It really helped me understand it more with it displayed there like that. Thanks :D

Thank you. This was the first time ever I have a clear understanding of GAS and Gwei. So far I just used to go with the default values everywhere.

If you don't need your transaction to go through super fast, you can put 0 gwei and it will be mined in 5-7 minutes completely for free. Check out http://ethgasstation.info/ and see the pools accepting 0 gwei as their minimum. (right now it's 13% of blocks being mined by pools that accept 0 gwei)

This is actually one of the better articles I could find on Google about Ethereum gas, lol.

incredibly informative @tomshwom, great share my friend!

thanks man! glad my first post was useful

I enjoyed this. Keep up the good work -- followed.

Hoi that is awesome explained! great job thanks man

Hi @tomshwom, Can you elaborate a bit on how can we reliably determine the gas for ERC20 transfer method for all the tokens available in the market? Each token seems to be using different amounts of gas and the gas amount is also observed to vary with each transaction. Can you clarify what determines the gasUsed and why is this varying a lot?

ERC20 is only a spec in terms of which functions are required to exist and what they do, not how they are written or what additional functions are present. Because of this, you can have many ERC20 compliant tokens that do completely different things, meaning they have different code and thus require different amounts of gas to execute in the EVM.

You can't reliably determine gas without pre-executing the contract. Even then, if something depends on the state, you can't even be correct 100% of the time with pre-execution. However, you can reliably estimate the amount by simply checking similar transactions to the contract that were successful. This is how many interfaces estimate gas cost, and it's generally good enough.

clear explanation on gas limit and gwei!

Is there any api to find the current gas price, which i can see in ethgasstation, also i wonder how meta mask determine the current gas price automatically.

Gas price is calculated on ethgasstation and metamask by looking at the average gas price of previous blocks. You can't query the blockchain for gas price, it's not something the blockchain manages or has control over.

Here's a link to the ethgasstation-api github repo: https://github.com/ethgasstation/ethgasstation-api

Is there any reliable way to determine how much gas your smart contract will consume based on the current gas price? That's the one piece that still confuses me.

It depends. Gas price is set for each instruction in the EVM, and you can see exactly which instructions will be run in a smart contract. In this sense, you can absolutely know how much gas a function in a contract will cost. The tricky part is that there can be loops/jumps that will change how many times a function runs, and that can't be known sometimes.

Ok enough subposts!

I have another question may you take the time to answer!

One of the great things about a cryptocurrency based on blockchains is that you dont need to pay taxes and or any fees to banks and institution.

Is gas in fact some sort of tax/incentive payed not to bankers but the whole network of miners who agree to keep record of everything(i.e. mine)?

Is this what comprises the famous PoW system?(just connecting what i know :shy)

Whoa, you are very, very wrong to think you don't have to pay taxes on crypto. I won't speak to other countries, although I do know they have tax laws that effect crypto, but in the US you must file capital gains tax for any crypto holdings you buy/sell/trade.

I think what you're really trying to get at is that crypto can eliminate the middle-man who takes a cut. While this can be done, there's many times when middle-men are still present in crypto too.

One example is decentralized exchanges using the 0x protocol. The trustless trade of one token for another at a certain exchange rate can be done without a fee, but decentralized exchanges acting as transaction relayers are providing a service at a cost, so they should and do inject a fee into these transactions.

Is gas in fact some sort of tax/incentive payed not to bankers but the whole network of miners who agree to keep record of everything(i.e. mine)?

Gas is the scaling factor that makes the fee proportional to the computational work done, not the fee itself. The fee is paid in Ether.

This fee exists to prevent people from attacking the blockchain and to incentivize miners to maintain the network. If you didn't pay miners, then they wouldn't mine and you wouldn't have a blockchain.

Proof of Work consensus is it's own thing. It can be applied to blockchain technology, and most blockchains do use it, but it is not limited to this one application. PoW has a lot more to it than just fees & computers doing "stuff".

Thanks a lot!

If you can wait a while, let's say a day, for the transaction to go through. You said the gas limit should be minimum 21000, but what's the minimum gas price I should use?

0 is the minimum since you literally can't go lower.. In a practical sense, the amount you want to use for gas price fluctuates since it depends on market value. Go to ethgasstation.info to see current competitive gas prices.

Thanks!

Very nice article. Doesn't this seem a bit cumbersome of a process in order to execute a smart contract? I don't see my mother or family members being able to take the time to learn how to execute this type of a contract and that will essentially limit the adoption rate of blockchain tech.

Your mother & family probably aren't capable of securely owning their private key either, so they can and should allow trusted 3rd parties like exchanges manage their funds. Leave it to these entities to also take the complexity out on the user's end.

If that's a problem, then I really don't think gas is particularly complicated. You have to put enough gas in your car to make a trip, and that's universally accepted as a simple fact. It's pretty much the same with Ethereum, except there's a competitive market for gas prices.

Again, a user that doesn't understand the very basics of blockchain / smart contracts / simple public-key cryptography, and things like gas really should not be part of this ecosystem right now. Crypto is in alpha-stage, and it gets significantly more complicated that these fundamental building blocks. It's actually been abstracted extremely well so that all the people that are in crypto today can actually use it.

Thanks @tomshwom. I appreciate this response and you make many good points.
"so they can and should allow trusted 3rd parties like exchanges manage their funds."
Very true. The sooner that we, as blockchain developers, can simply the secure handling of private keys for the layperson, the more rapidly crypto will become mainstream. I think future economy is really going to be technology and information based so those who don't learn these simple skills will be left for the lions.

Thanks very informative and to the point....

ok.. Your article is super informative, single place to all queries.. I read your article & still lost some eth in failed transaction due to low gas..

https://etherscan.io/tx/0xab645b8a38a9c60beb3fd77e61c50aaeaffef11724aa7decf430dcb9b037cbef

Why this transaction failed.. I put 150000 gas, 1.1 gwei & it still ran out of gas where minimum gas was 90000..

Please explain

The EOS Crowdsale contract has an iterative function. That means part of the code can be called many many times, and the exact amount depends from person to person. Unfortunately, there is no good way to estimate how much gas a variable execution function will consume unless you execute it virtually off-chain to see.

You lost ~$0.26, so not big loss. Try again with 500k maybe.

thanks for your input.. i lost more than 0.26.. & second.. how to execute it off the chain

You lost 0.000275 Ether on that transaction. At even $1000 per Ether, which is an overestimate right now, that's $0.275. Even a $10,000 Ether would only bring your loss up to a couple dollars - it's almost nothing.

You started with a 210k gas limit that failed, so you moved up to 900k. Seems reasonable, but then that also fails and you decide for whatever reason to try it again with the same insufficient gas limit. Not only that, you also try with even less at 250k...

Finally, you move up to 1000k, which still fails. A lot of the (very little) money you're wasting here is completely unnecessary and more due to user failure than the problem of figuring out a proper gas limit.

To execute it off-chain, you'd need to know how to run a private testnet and then copy the mainnet state over. I think you're better off just asking the EOS community for advice, but if you want to go this route then look into how public testnets have been created.

thanks for looking into it.. all my spare eth got burned in these failed transaction.. just a few cents left now.

I will seek help from eos community. thanks for your valuable input.

Hello tomshwom. Would you happen to be familiar with @schmux here on steemit? It's an account that only follows one person: you.

I am asking because I sent some steem to @schmux by mistake. I wanted to send the steem to my friend who has the account @schmux99, and I did a mistake by entering the wrong username. I already sent a message to @schmux but he doesn't seem to be active on steemit so I worry he will never see it.

We really need your help so if you know @schmux don't hesitate to contact me on my email [email protected] so that we can retrieve the funds.

Thank you!

Don't know anything about them, sorry.

Okay, thanks.

This was super helpful! Thanks for the post.

cool - came here from a @ivanli course