Ethereum: Everything you want to know about Gas

Łukasz Żuchowski
SoftwareMill Tech Blog
6 min readNov 14, 2017

--

Gas ring by Alex Sukontsev

Gas keeps Ethereum Blockchain alive, thanks to it we can transfer Ether and other Ethereum tokens such as: GameCredits (GAME), OmiseGo (OMG) or Golem (GNT), it also allows to smart contracts to do their job. In this blogpost I’m going to explain: what is Gas? how is it used? and why is it so important for the future of Ethereum?
Important: Don’t be misled by the Token named GAS which is something completely different.

Fuel of the Blockchain

Ethereum blockchain is run by nodes that keep the blockchain state but also calculate new blocks. New blocks are needed to change Blockchain’s state e.g. move Ethereum from one account to another. Calculation of the new block is made by miners, to cover their effort transaction sender must pay a fee. Transaction fee depends on complexity of transaction sender wants to make, if it’s a regular “send Ether transaction” or more complex one like “create smart contract” (smart contract — a special kind of the blockchain account, that can not only keep Ether but also computer program with it’s state). Sending Ether from one account to the other costs 21,000 Gas. On the other hand creating smart contract which is responsible for handling OmiseGo Token costed 1,197,977 Gas. So the more complex transaction, the more Gas we need to pay for it’s execution on Blockchain. Main complexity factors are:

  • operations performed by the smart contract’s code e.g. arithmetical operations
  • data that is stored on blockchain e.g. storing information in the smart contract or updating an amount of Ether on the account

How much does Gas cost?

We know more or less what Gas is, but how much does it cost? The answer is as always — it depends. Each transaction sender (e.g. person who is sending Ether) is defining price of Gas for created transaction (e.g. 1 Gas = 0.000000001 ETH). If the price is high enough, transaction will be executed sooner, since miners will execute transactions with the highest gas price first. If price will be too low, we may end up waiting very long for execution of our transaction. Many tools especially wallets set transaction fee on their own not to hustle user and provide smoother experience, but it doesn’t mean there is no fee.

How to set the Gas price right? Many tools will give us default value that is good enough e.g. Metamask but sometimes we should adjust it (e.g. we want this transaction to be executed ASAP). There are statistics on average Gas price and transaction time available e.g. on GasStation or Etherscan that may help us setting the right price.

Diving deeper

Ok, so we know the basics. Gas is not another currency, Gas is a measurement unit of complexity of the transaction (the more complex transaction, the more Gas it will consume). The price of Gas is defined by the sender but it’s miners’ choice in which order they will execute transactions. If you don’t want to learn more about transaction processing and calculating Gas cost, skip that part and go to the next section.

There is a bit more of terms worth explaining here. Let’s try to analyse 2 different transactions in terms of Gas:

Transaction on Etherscan.io

This transactions just sends Ether (120.0) from one account to the other. So let’s go through Gas related informations about that transaction:

  • Gas limit — that’s the maximum amount of Gas that user commits to the transaction. If transaction will need more Gas than it was defined in Gas limit, transaction will fail with “out of Gas” status (if your transaction goes out-of-gas you pay for it, even if it failed). Think about it as credit card blocking
  • Gas used by transaction — that’s actual amount of Gas that was used during execution. Sometimes it’s hard to predict how much Gas transaction will cost, so the actual cost of transaction is computed afterwards. Sender is charged for used Gas and the difference is returned to the sender.
  • Gas price — that’s the Gas price in ETH that sender defined at transaction creation.
  • Actual Tx Cost — gas used by transaction * gas price (in ETH)
  • Cumulative gas used — The total amount of gas used when this transaction was executed in the block (gas used by previous transactions and this one together)

Those where transaction related information regarding Gas and its usage. Each block on the blockchain may contain one or more transaction. Blocks have their gas properties:

  • Gas Limit — maximum amount of Gas that might be used for all transactions. Sum of all transactions’ Gas limit values.
  • Gas Used — total amount of Gas used during execution of all transactions

What Gas can tell us?

Gas might not only be the fuel of the Blockchain, it may also be an important source of information. Since Ethereum was designed not only as crypto-currency but also to host distributed applications (such us tokens), Gas has significant impact on Ethereum’s economy. Currently there are tens or even hundreds of ERC20 compliant (and non-ERC) tokens that run on Ethereum Blockchain. Every transaction of those tokens requires Gas, it’s just a transaction that calls a smart contract’s function after all. The sum of all token transactions is huge and each one requires Gas, which is paid in Ethereum. In other words every token transaction increase Ethereum demand. On the other hand, every token transfer will increase “miners” reward. In the near future proof-of-stake will replace proof-of-work, this will lead to the significant reduction of “the block reward” and Gas will become the main miners’ income. So next time you will hear about ICO of the new Ethereum token think: higher Ethereum demand and more work (and Gas spend) for miners.

We may analyse transactions’ Gas usage even more. The structure of Gas usage and its variance in time will tell us how Ethereum Blockchain is used and diagnose the technology state. We may filter transaction with ERC20 tokens and other distributed applications (Dapplications or Dapps) and see: how much are they used? how much Gas/Ether is spent on interaction by their users? That may help us to find answers for the questions regarding future of Ethereum and design better Dapps.

Other important factor that we may analyse is the Gas price. Behavior of users that are sending transactions might be an interesting area of research. It may also be a key to shape Ethereum’s Gas price policies. Since the cheaper Gas is the cheaper cost of running Dapps. If Dapps will become more popular, then higher transaction volume will compensate lower Gas prices to the miners. On the other hand the more popular Ethereum Blockchain is, the higher transaction throughput is expected. It may lead to Blockchain’s performance problems. Currently main effort of Ethereum development is focused on performance, scalability and privacy. Solutions such as Casper or Plasma are just around the corner.

Etherlytics

Currently I’m working on a pet project that will provide Ethereum Blockchain analytics, the project code name is Etherlytics. My idea is to use data from Blockchain to find interesting insights for the Ethereum community, investors and developers. I want to take advantage of tools such as: Scala, Akka and Spark to build efficient and distributed solution. If it sounds interesting to you, please leave your email address below. I promise that I will use it only to send information regarding project launch (or public beta) and won’t send you any marketing BS or spam. It will also encourage me to spend more time on it. If this post was interesting please 👏 and stay tuned.

--

--

Blockchain enthusiast, Ethereum believer, Software Engineer that solves problems mainly with Java, Scala and JavaScript, Consultant