Understanding how gas works is crucial for Ethereum users interested in participating in ICOs, using smart contracts, and even making simple transfers between wallets. In this post, I hope to explain what gas is and how it works.
Skip to the end for a simple summary of things. To start off, we need to understand some basics about the Ethereum Virtual Machine.
Ethereum Virtual Machine and Gas
The Ethereum Virtual Machine (EVM) is a major part of the Ethereum ecosystem, but I'll be heavily glossing over this so we can focus on how it relates to gas. In short, the EVM is an environment where 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 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.
When on a site like MyEtherWallet, you're going to see a field called Gas Limit.
This corresponds the the MAXIMUM amount of gas you are willing to spend on the transaction.
Important to note:
- Different types of transactions will require different amounts of gas to complete
- Providing too little gas will result in a failed transaction, the fees are kept by the miner
- Extra, unspent gas is refunded automatically
What happens if I specify too little gas?
Your transaction will start to be executed, but will eventually run out of gas and be stopped.
When this happens, you will not get back ether spend on the gas used, but since the transaction did not complete, the blockchain does not reflect a transfer, so the main funds essentially never left the wallet.
So I should always specify a large gas limit since I'll be refunded?
No. The minimum gas limit for transaction is 21000 + the amount to execute any fallback functions if it's sent to a contract, but the actual blocks also have a gas limit that specifies the maximum amount of gas all transactions in the block can consume.
Like what you’re reading?
This is comparable to Bitcoin's block size in bytes, but ether miners have the option to increase or decrease the gas limit of blocks so that they are propagated quickly. You cannot exceed this amount if you wish for your transaction to be processed. Also, miners only get 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 computationally-dense enough for miners to want.
Gas price is the amount of ether you are willing to spend on every unit of gas.
Yes, you determine the price of gas AND maximum amount of gas you want to spend on performing a transaction on the Ethereum network. Although gas limit is just a simple quantity, gas price is measured in ether (specifically some amount of wei, usually gwei). The value of gas is driven by the market and the nodes that prioritize higher gas prices when mining transactions.
What is Gas?
The current gas price can be seen on Etherscan or EthGasStation. As I mentioned, 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:
Setting Gas Limit and Gas Price
If you're using MetaMask, you will see something like this when trying to send Ether:
Here you can see that the gas limit has been set to 31500 and the gas price at 20 gwei.
This means we are only willing to spend a maximum on 31500 gas at the price of 20 gwei / gas on the transaction.
MyEtherWallet has the Gas Limit field,
but no Gas Price for their transactions. *I assume they calculate Gas Price depending on the market value
Edit- MEW has a slider at the bottom right of the site for adjusting gas price.
- Transactions on the Ethereum network require fees in the form of gas.
The amount of gas depends on the amount of computation required to complete the transaction.
- Gas Limit is the maximum amount of gas allotted to the transaction, 21000 being sufficient for simple transfers and much higher amounts for transfers to smart contracts like those in ICOs.
- Insufficient gas in the Gas Limit will result in a failed transaction, the fee paid will be lost, but the ether transferred never leaves the wallet since the blockchain was never updated.
- Gas Price is the price of each gas unit measured in a fractional amount of ether, typically gwei.
Gas price varies, but 20-30 gwei is average as of writing this.
- Current Gas Price can be checked online ( Etherscan or EthGasStation ), and the Gas Limit for things other than simple transfers can only be known by looking at the smart contract code or by looking at what the organization behind the ICO / smart contract says is appropriate for their smart contract.
Ethereum and ERC20 token wallet, MyEtherWallet
MetaMask Chrome Plugin
Consider leaving a like or even resteem if you found this helpful!
If you're really crazy, my MEW address is 0x5B13430b6D2E327DFCDB93D7430FC6e9a2E63E74. Please feel free to leave corrections and comments below :)