What is Gas in Ethereum?

If you’re thinking of becoming a Dapp developer, gas is something that you’ll want to have a pretty solid grasp on before deploying your app to the Ethereum blockchain.

Gas is the metering unit that determines how long your app can run for.

What exactly does that mean? Why is it necessary?

Remember that Ethereum contracts are turing-complete. In simpler terms, this means that you could write an application that runs for a really long time, or even forever. Ethereum contracts can run instruction sets that are much more capable than those available to a bitcoin script. Notice the JUMP and JUMPI instructions, which would likely correspond to looping constructs in a compiled Solidity or LLL contract. Bitcoin scripts are purposefully not turing complete – they were designed to verify values (signatures, for example) in a small, standard set of transaction types.

Ok, so Ethereum contracts can be pretty robust. But what incentivizes miners to execute more complicated scripts when they could just run the easier ones? Gas is the answer to this question: the account posting the transaction pays more or less depending on the current set of required actions.

How are Gas prices calculated?

You pay for gas on the Ethereum network a little differently than you pay for gas to fill up your car. Because of the volatile and rapidly-changing nature of Ether pricing, it would be difficult to place an execution cost on a smart contract using Ether. To subdue the uncertainty of Ether pricing, the Ethereum protocol allows a transaction initiator to specify their own gas price as a bid to have their application run on the Ethereum Virtual Machine (EVM). In theory you could bid 0 wei (1e18 wei = 1 ether), but your transaction will probably never be posted to the blockchain.

Check out the historical gas prices on the Ethereum network. Gas is essentially a market like any other, with the goal of keeping computation costs more or less the same as the price of ether fluctuates.

On the other hand, every operation within the EVM has an associated and fixed gas cost. From ethdocs:

Screen Shot 2017-07-14 at 10.35.56 AM
A subset of EVM operations and their associated gas costs


To estimate how much your smart contract will take to run, consider the current gas prices and set of operations in your smart contract.


A real-world example

Let’s check out a real transaction on the Ethereum blockchain. I chose this one randomly from the home page of Etherscan.

The values that fall in the scope of this article are Gas Price, Gas Limit, Gas used by txn, cumulative gas used, and Actual Tx Cost/Fee. You can read descriptions of those key words in the Homestead Documentation, but I’ll describe each of them below:

  • Gas Price: This is the price that was suggested by the account that originally posted the transaction.
  • Gas Limit: The amount of gas after which the smart contract execution will halt. “gas limit * gas price” ether will be redeemed by the executing node as a fee.
  • Gas used by this txn: The amount of gas that, in the end, was needed to execute this contract.
  • Cumulative gas used: The amount of gas used so far this this block. Each block has a gas limit as well. This can also be seen as an upper-limit on transaction volume, is determined by miners, and can change over time.
  • Actual Tx Cost/Fee: The amount of Ether that is actually paid to the miner who executes the contract (also whose block ends up in the canonical chain).

Hopefully this gives some insight into how the gas system in Ethereum works. Happy coding!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s