Ethereum – Gas for Dummies

I’ve been trying to figure out what Gas means in Ethereum. There is lots of information out there but I will add to it by describing what I found in my own words.

Gas is just a unit of currency, like a token. It is used to set a price on different types of code execution inside the Ethereum Virtual Machine (EVM). For example, saving a variable cost 100 gas; executing an IF statement cost 1 gas. There is a list of how much gas things cost here.

In a typical transaction you would say something like

method.sendTransaction({from:eth.accounts[0], gas:300000})

The gas part of that statement means that you are willing to spend up to 300,000 of these gas tokens in executing this method.
Now, there is another, optional, parameter in that statement, which is gasprice. This is the price in Wei that you are willing to pay for each gas token. If gasprice is not specified, then a default value is used. At the moment that value is 10 szabo, or 0.00001 Ether.

What is the point of all this?

Contracts on the Ethereum blockchain are of varying sizes and complexity. Therefore, a miner who is investing their computing power executing a method (a part of a contract) needs to be rewarded according to how much of that power they need to use. By inserting an amount of gas and a gasprice, you are signalling to the miner how much reward they can reap from running your code.

This is fundamentally different to the Bitcoin blockchain, where the vast majority of code executions are identical (transferring bitcoin from one wallet to another) and therefore miners can predict how much it will cost them to execute every transaction in a block.

So, going back to our example, when you send in the above transaction, the paying account (eth.accounts[0]) is deducted 300,000 * 0.00001 ETH, i.e. 3 ETH, and that value is sent in with the method call.

After that, one of two things will happen:

  1. The execution requires as many or fewer of the gas tokens that you pledged. In that case the execution is succesful and you are refunded however much ether is unspent (e.g. if you said you were willing to use up 300,000 gas tokens at 0.00001 ETH each, but the execution only required 150,000 gas tokens, then you get refunded 150,000 * 0.00001 ETH. The miner keeps the rest as a reward for their efforts.
  2. The execution requires more gas tokens that you are willing to spend. In that case, execution of the code is aborted when the gas limit is reached and the contract state goes back to whatever it was before your execution started, i.e. nothing actually gets done. BUT crucially you DON’T get your money back (because work was done by the miner, so they used up their computing power and electricity).

Some thoughts

  • The higher the gasprice you insert the more attractive it is for miners to process your transaction because they get paid gasprice for every gas token that they need to use in executing your transaction.
  • It seems almost impossible to be able to calculate how much gas a transaction will require. A dummy like me has to send a big number of gas to ensure execution. There are obviously ways of calculating this, because if you use the Mist browser to execute a method, it seems to have a way of forecasting the amount of gas you might need.
  • It is also very difficult to find out that your transaction failed because of lack of gas. It just dies and you are left to puzzle it.
  • Interestingly, blocks also have a gas limit. But that is probably for another post!

    Useful links

    I found these links useful when reading up on gas. Enjoy!