Ethereum "Gas" - How it Works

Purpose


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 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.

Gas Limit


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. 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


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. 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:

var unitMap = {
    'wei':          '1',
    'kwei':         '1000',
    'ada':          '1000',
    'femtoether':   '1000',
    'mwei':         '1000000',
    'babbage':      '1000000',
    'picoether':    '1000000',
    'gwei':         '1000000000',
    'shannon':      '1000000000',
    'nanoether':    '1000000000',
    'nano':         '1000000000',
    'szabo':        '1000000000000',
    'microether':   '1000000000000',
    'micro':        '1000000000000',
    'finney':       '1000000000000000',
    'milliether':   '1000000000000000',
    'milli':        '1000000000000000',
    'ether':        '1000000000000000000',
    'kether':       '1000000000000000000000',
    'grand':        '1000000000000000000000',
    'einstein':     '1000000000000000000000',
    'mether':       '1000000000000000000000000',
    'gether':       '1000000000000000000000000000',
    'tether':       '1000000000000000000000000000000'
};

from https://github.com/ethereum/web3.js/blob/0.15.0/lib/utils/utils.js#L40

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. 31500 x 20 gwei = .000630 ether.

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.

Summary


  • 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.

Handy Sites

EthStats
Etherscan
EthGasStation
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 :)

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center