What is Gas and How is it Used?

Deric Cheng
Dev Education at Web3U

Gas in Ethereum refers to the additional fee required to execute a smart contract or transaction on the blockchain network.

Why does this exist? Well, there's a couple major limitations when it comes to running smart contracts:

  1. Every deployed transaction, smart contract, or execution of a smart contract, must be run on every single full node on the Ethereum blockchain to guarantee validity. This is wildly inefficient (though newer blockchains are streamlining this)!
  2. Because smart contracts are Turing complete, they can potentially execute forever, locking up every single node on the blockchain.

What is Turing completeness?

Practically, a programming language that is Turing complete is able to solve or represent any computational problem, no matter how complex, given enough time and resources. In particular, that has a couple of implications: 

  1. Any Turing complete language can theoretically be used to represent the logic of another Turing complete language, though the implementation may be unreasonably long.
  2. Turing complete programs can end up looping and executing forever. In fact, there is no universal way to prove that such a program will not end up running forever (otherwise known as the "halting problem").

For example, a regular calculator is not Turing complete, because it allows for only a few types of calculations. However, a computer or scientific calculator is Turing complete because any type of program can be executed on it.

Since smart contract programs can run forever, gas has become the practical way in Ethereum to manage the impact of a blockchain program! Every computation or transaction made on the blockchain costs some fees. These fees prevent expensive (or endless) contract executions, ensure miners are fairly compensated for the work they do, and provide a fair market to prioritize which transactions make it onto the blockchain.

Calculating Gas Costs

For any given program, the total gas used is calculated as the sum of the gas for each operation executed by the Ethereum Virtual Machine. For example, adding two numbers in a smart contract costs 3 gas, whereas sending a transaction costs 21,000 gas.

The total cost of gas is found by taking the amount of gas used in by a smart contract and multiplying by the gas price, a value set by you, the transaction sender.

Setting a higher gas price for your transaction means it's more likely to be confirmed on the blockchain, as the Ethereum blockchain can only confirm about 15 transactions a second. However, it also costs more Ether for the sender.

Another important value that can be set is the gas limit, or the maximum amount of gas you're willing to spend on your transaction.

By multiplying the gas price by the gas limit, you'll get the maximum amount of Ether you're allowing Ethereum to spend on gas fees for any particular transaction.

What is "gwei" or "wei", and how do they relate to ETH?

“Wei” is the smallest unit of Ether, where 10¹⁸ Wei represents 1 Ether. One gwei is 10⁹ wei, and there are 10⁹ gwei per Ether.

Learn about gas execution

When a smart contract call is made, the call (or transaction) will attempt to use the gas provided while the program is executed.

  • If the call succeeds, the unused gas will be returned to the sender.
  • If the call fails because it ran out of gas, the entire transaction will revert, undoing all changes to the blockchain. None of the gas will be returned, since it will all have been used up during the computation process.

A portion of gas fees from a successful transaction will be burned (or removed from the total supply) according to the new EIP-1559 spec, and the rest will be sent to the miner that added your transaction to the blockchain.

What is EIP-1559 and how does it affect gas fees? 

Prior to the implementation of Ethereum proposal EIP-1559 on August 5, 2021, transactions were included on to the blockchain using a first-price gas auction. Whi

A Brief History of NFTs
Tutorial
Tutorial
How to Create an NFT
What Are NFTs Used For?
Tutorial
How to Mint an NFT Using Ethers.js
Tutorial
Tutorial
How to Set a Price on a NFT
Tutorial
How to Build a Full-Stack NFT dApp
tutorial
Building a Successful NFT Project
Tutorial
How to View Your NFT in Your Mobile Wallet
Tutorial
Additional Resources