What is Gas in Ethereum & how to Use It?

What is Gas in Ethereum & how to Use It?

in Cryptocurrency, Newbie, Altcoins
The Ethereum Virtual Machine (EVM) is a major part of the Ethereum ecosystem. In short, the EVM is an environment where an 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.
cryptocurrency wallet

Gas Price

Transaction fee = Gas Used X Gas Price

Miners keep the network alive by validating and generating new blocks. Not only are they rewarded 5 ether for being the first one to generate a new block, they also get to keep the transaction fees for all the transactions that are included in that new block.

There is one catch though — the miner gets to set a minimum gas price for each transaction to be accepted into the new block. So if the gas price offered by you is way below average, many miners will choose not to accept it until a willing miner comes along. This would mean it will take longer for your transaction to be included in a new block and eventually propagated in the network.

We can play around with the gas price and estimate the time to send an ether easily from here.

If you want to pay less for your transaction, you can do so by varying the other variable which also determines the final cost (or Tx fee) of the transaction. This variable is called “gas price”. But mind you, lowering the gas price will make the transaction take longer to be mined. This happens because all miners want to mine a transaction that has higher mining reward (i.e. higher Tx fee). On Ethereum, gas price is measured in a unit of Gwei. For 5 lines of code that need 5 units of gas, this would cost 5 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. 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:
ethereum
 

Why not use ETH directly instead of gas price?

Actually, this is done to decouple the cost of any operation from the market price of Ether. As you know, cryptocurrency prices are very volatile, and ETH is no exception. That’s why on Ethereum’s blockchain, the gas limit for each operation is constant and fixed so that market volatility doesn’t impact Ethereum’s usage. Just imagine a scenario in which instead of using this indirect way of fixed gas limits and variable gas prices, we had a fixed ETH cost. Let’s say the price of 1 ETH was $1000… No one would use the Ethereum platform because it would be too costly. That’s why it becomes easy to bypass this volatility of the market and simultaneously lower the operations cost by using another variable.  

Other important details

So, we know that Gas is not another currency, Gas is a measurement unit of the 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.

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


ethereum blockchain

This transaction just sends Ether (120.0) from one account to the other. So let’s go through Gas related information 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, the 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 the 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 a transaction is computed afterward. A 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 — the 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 — the maximum amount of Gas that might be used for all transactions. Sum of all transactions’ Gas limit values.
  • Gas Used — the total amount of Gas used during execution of all transactions.
 

F.A.Q.

  • Will increasing the gas price get it mined faster? Does setting a low gas price mean it won't ever be mined?
The transaction fees go to the miner who mines your block. When miners mine a block, they have to decide which transactions to include. They can choose to include no transactions, or they can choose to randomly select transactions. In order to encourage miners to include transactions in blocks, you want to set a "Gas Price" that is high enough to make them want to include it (since it is entirely up to them). Most miners follow a very simple strategy for inclusion. They include transactions they received sorted from highest Gas Price to lowest, then include them until either the block is full or they reach one that has a Gas Price set lower than they are willing to bother with. You want to set the Gas Price high enough so that a miner includes your transaction in a block. If you are in a hurry, you can set the Gas Price higher, so that you jump ahead of everyone in line. If you are not in a hurry, you just need to set a number high enough so that someone eventually includes your transaction.  
  • Why should I set a low Gas Price?
Because it's cheaper and because with the increasing price of ETH compared to USD, a transaction that used to cost half a cent, may cost a few cents. More expensive transactions, like bidding on an ENS name, can now cost a dollar or more! As a user, you should try sending non-urgent transactions with a lower gas price as the more transactions that occur at the lower gas price, the more likely miners will lower their minimums.  
  • Should I increase the gas limit for token sales, though?
You should put whatever the token sale holders tell you to put. If you do not know, then ask, before the token sale. This ensures that your transaction won't fail due to an "Out of Gas" error. Typically, a 200000 gas limit will be enough, but some require more. Increasing the amount to 1 500 000 or more will not increase the likelihood of getting in. All it will do is fill up the blocks faster and you will lose that TX fee if it doesn't go through. We have never seen a token sale that requires over a 800 000 gas limit.  
  • So I should send with a huge gas price for token sales, right?
Not necessarily. The risk of increasing it is that you could still not get in, and pay the fee anyway. The gas will NOT be returned to you if you send with a too-low gas limit, too early, or too late in the Token Creation Period. First, see if the token sale has a max gas price. If they do, use that as the gas price. Both Status and Bancor had a max gas of 50 GWEI. If they let you send with any amount of gas price, you should decide how much you are will to invest, and how much you are willing to spend on that attempt to invest. You must assume that you will pay the full fee and get nothing in return during Token Creation Periods. Take the gas limit (e.g. 200000), multiply by the gas price (e.g. 50 GWEI or 0.00000005 ETH) and that is how much you will pay for your attempt to get in. Before the BAT Token Creation Period, the average gas price was 20 GWEI (the default) for Token Creation Periods. Now it's who-the-fuck-even-knows:
  • 40 GWEI * 200000 == 0.008 ETH == $5.60 USD
  • 70 GWEI * 200000 == 0.014 ETH == $9.80 USD
  • 100 GWEI * 200000 == 0.02 ETH == $14.00 USD
Absurd Gas Prices From the BAT Token Creation Period (USD prices at time of TX)
  • 118 GWEI * 200000 == 0.0236 ETH == $6.13 USD
  • 7590 GWEI * 200000 == 1.518 ETH == $394.68 USD
  • 58000 GWEI * 200000 == 11.6 ETH == $3,016 USD
 

Conclusion

In conclusion, leave 21000 gas limit as default if you are doing a simple ether transfer to another person. If you are sending ether to a smart contract (eg. during an ICO), always ask the contract owner what gas limit to set to. The reason for that is that you risk losing transaction fees if you set too low or too high (if their contract is buggy). If the contract is well tested, setting high is not a problem. For a typical ICO contract, it is quite normal for a gas limit to go up to 200000.

If you are not rushing for time, you can reduce the transaction fees by lowering your gas price to 4 Gwei or lower.

Ethereum gas might sound confusing at first but not hard once you understand the concepts behind it.

   

Want to learn more about cryptocurrency? Follow us on Telegram


Facebook Comment
Coinmama Cryptocurrency Exchange Beginner's Guide

March 22, 2018

Coinmama Cryptocurrency Exchange Beginner's Read more