What exactly is gas in ethereum
Gas is unique to ethereum and paramount to how ethereum operates. This concept might be foreign to people coming from earlier cryptocurrencies like bitcoin.
If you have send ether to someone before, you might have noticed the option to set the gas limit and price in your wallet. Many people leave them as default most of the time but what exactly do they mean? What are the pros and cons if you change their values?
This is a screenshot from the famous myetherwallet.com (Users might miss the gas price option on the top right)
Metamask is more straight forward.
Gas is like petrol for your car. Your car needs gas in order to function.
Every transaction in ethereum consumes gas. In ethereum, a transaction is not just sending money (ether) to someone, it could also be storing a value in a smart contract. Every mathematical operations such as addition or multiplication in a smart contract require gas.
The ethereum core team came up with the gas concept to prevent people from modifying contract states or deploying contracts just for fun. Sending ether to someone is straight forward and uses 21000 gas. However, if you are interacting with a contract, you need to increase the gas limit. The reason is that if the smart contract that you are interacting with exhausts all your gas before completing the task, you lose your gas and all the mathematical operations performed will be reverted. However, if your gas limit is higher than the actual gas used by the smart contract, you only pay for the actual gas used and the unused gas refunded. It is therefore good to have a high gas limit. Having said that, if the contract is buggy, it might consume all your gas buffer in an endless loop and yet not perform the desired task.
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 http://ethgasstation.info/calculator.php
There is also a nice chart from ethgasstation.info home page to give us an idea of the lowest gas price that the top 10 miners are willing to accept.
At the time of writing, f2pool and ethermine pools make up 50% of the total hashing power. Like ethermine, many other smaller mining pools also accept 4 gwei gas price. Therefore, you shouldn’t need to wait long for your transaction to pass through if you use 4 gwei rather than using the default 20 gwei in many wallets.
In theory, you could even set the gas price to 0 based on the chart above. However, chances are that you will need to wait for a long time for your transaction to get through. At the end of the day, nothing is for free.
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, its quite normal for 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. I’ve even used 1 gwei before and it worked well.
Ethereum gas might sound confusing at first but not hard once you understand the concepts behind it.