What is Gas in Ethereum & how to Use It?in Cryptocurrency, Newbie, Altcoins
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.
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:
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:
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.
- Will increasing the gas price get it mined faster? Does setting a low gas price mean it won't ever be mined?
- Why should I set a low Gas Price?
- Should I increase the gas limit for token sales, though?
- So I should send with a huge gas price for token sales, right?
- 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
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