In order to participate in ICOs, play-to-earn games, smart contracts, or even just simple wallet transfers, you as an Ethereum user need to understand how gas works. This post aims to explain what gas is and how it works. Let’s start off by going over some Ethereum Virtual Machine (EVM) basics.
Ethereum Virtual Machine and Gas
We’re going to quickly gloss over the Ethereum Virtual Machine basics in order to focus on how it relates to gas in the Ethereum ecosystem. Essentially, EVM is a programming environment where arbitrary code can be executed for smart contracts and other operations.
Each node in the Ethereum network executes operations within the EVM to ensure redundantly correct execution and relies on consensus to resolve disagreements. Whether it is a simple transfer or an ICO smart contract, all transactions require some amount of operations. Each of these operations has an associated gas cost. However, the gas cost is moreover determined by the current network traffic and Ethereum trading volume.
Therefore, simpler smart contracts such as transfers will displace more complex ones in terms of gas requirements. A simple operation could cost 1 gas, whereas a more complex operation such as storing a variable might cost 100 gas. The total gas cost is the result of all the operations involved.
Gas Limit Of Ethereum
When you use any wallet on the Ethereum Chain to make a transfer to your favorite blockchain game, for example, you will see a box called “gas limit”. This corresponds to how much gas you are willing to commit to the transaction.
- Gas consumption will vary depending on what type of transaction is being accomplished.
- In the event of too little gas being provided, the transaction will fail and the miners will keep the fees.
- Unused gas is automatically refunded.
What if I input too little gas?
Transactions will begin to be processed, but eventually, run out of gas and are stopped. The gas will not be refunded, but because the transaction was not completed, the blockchain will not reflect a transfer, so the main funds essentially never left the wallet.
Should I always input a large gas limit since I’ll get a refund?
Definitely not. Currently, transactions have a gas limit of 21000 plus the amount of gas to execute any fallback functions if a transaction is sent to a contract. However, every block also has a gas limit that specifies the maximum amount of gas that all transactions in the block can consume.
Ethereum miners have the option of increasing or decreasing gas limits so the blocks are propagated as quickly as possible, similar to bitcoin’s block size in bytes. Your transaction cannot be processed if you exceed this amount. In addition, miners are only paid for the actual gas consumed by a transaction, so putting a high value for the gas limit fills up the block gas limit, but isn’t dense enough for miners to use.
Gas Price Of Ethereum
Every unit of gas is assigned a price, which you pay in ether. When you perform a transaction on the Ethereum network, you will decide the price of the gas and the amount of gas you wish to spend.
However, ether (specifically some amount of wei, usually gwei) is used in place of gas limit to create gas price. Gas value is determined by a number of factors, including market prices and the factors that are prioritized by nodes when mining transactions. You can find the current gas price on Etherscan or EthGasStation.
In Summary
- Gas is a fee associated with transactions on the Ethereum network. To complete a transaction, gas is needed depending on the amount of computation.
- A Gas Limit is the maximum amount of gas allowed for a transaction. Amounts exceeding 21000 would be required for transactions involving smart contracts, such as those found in ICOs.
- Insufficient gas will cause a failed transaction, so the fee will be lost, but the Ether never leaves the wallet since it isn’t updated on the blockchain.
- A gas price is a fractional amount of ether, typically gwei, measured to determine the cost of one gas unit. Currently, gas prices are averaging between 20-30 gwei.
- Gas prices can be easily accessed through websites like Etherscan or EthGasStation. To see other than simple transfers, a smart contract code is necessary.