What is Ethereum gas? Ultimate Guide For Solidity developers

Gas is a unit used in Ethereum transactions. It is one of the most difficult concept for Ethereum newbies who wonder:

  • What is gas?
  • What is the purpose of gas?
  • Who pays for gas?
  • How to calculate gas?

As an aspiring Ethereum or Solidity developer, you will deal with gas every time you want to modify data on the blockchain, i.e for every transaction. It is absolutely essential that you understand how gas work in Ethereum.

In this article, I will give you a clear, simple explanation of what is gas and how it is used is used in Ethereum transactions. I will also explain gas-related concepts needed by Ethereum developers such of gas price and gas limit, and how to compute these 2 parameters for a transaction.

TLDR very short;

  • gas measure the computational effort required to run an Ethereum transaction

TLDR slightly longer;

  • Miners are paid transaction fees for processing Ethereum transactions
  • Transaction fees are measured in a unit called gas…
  • …but they are paid in Ether
  • The conversion between gas and Ether is determined by the gas Price…
  • …which is set freely by senders of transactions

Transactions cost (real) money

Let’s start from the beginning. When miners mine transactions, they use their server and consume electricity. They need to get paid for this. That’s why there are transaction fees. Transaction senders (i.e users) accept to pay some Ether amount to miners in exchange for mining their transactions. So far so good, there is no gas.

Some transactions cost more than other

On the Bitcoin network, all transactions are the same: party A sends X amount of Bitcoin to party B. Simple. But on Ethereum, beside simple financial transfers, transactions can also run arbitrary computations when they execute smart contracts. Smart contract functions can be more or less complex and as a result be more or less computationally intensive.

If we decide to pay miners with a fixed fee for each transaction, this is not going to work. For large transactions, there will be underpaid, whereas for small ones, they will be overpaid

Fixed transaction fee

Let’s assume that there was no gas on the Ethereum network. Instead, there is just a fixed transaction fee in Ether, no matter the size of the transaction. Life would be so simple, isn’t?

There are several problems with this:

  • First, miners would start to prioritize simple transactions over smart contract transactions since the reward is the same (the fixed transaction fee), but the work is variable, with more work for the smart contract. Smart contracts would have a hard time getting their transaction mined.
  • Second, bad people could spam the Ethereum network with very large transactions and clog it.

Variable transaction fee with Ether

From the previous section we understand that we need to be more flexible for transaction fees: if a transaction runs a computationally intensive smart contract, transaction fees should be higher than for other transactions that are more simple.

What we could do is charge a transaction fee based on the computational difficulty of the transaction. On the Ethereum virtual machine, smart contract executes a series of elementary opcodes. This is similar to machine code run by computers.

...
PUSH 40
PUSH 20
ADD
MSTORE
...

We could map each of these elementary opcodes to an Ether cost. To compute the total cost of running a smart contract function, we would just need to add the Ether code of all the opcodes:

...
PUSH 40  -> 0.002 Ether 
PUSH 20  -> 0.002 Ether  
ADD      -> 0.004 Ether
MSTORE   -> 0.001 Ether
...
...      TOTAL: 0.01 Ether

The problem with this approach is that the price of Ether fluctuates. At the time of deciding the Ether cost of each opcode, it might make sense given the current price of Ether. But in the future, after the price of Ether would have changed a lot, the actual costs of the opcodes might have changed dramatically: if the price of Ether went up a lot, actual transaction costs in real money would go up a lot, and conversely if the price of Ether went down.

Variable transaction fee with gas

From the previous section, we got that we not only need to have a variable transaction cost, but also that we can’t use directly assign a cost in Ether for EVM (Ethereum virtual machines) opcodes.

The solution found by the Ethereum developers was to introduce an abstract unit just for the purpose of measuring the computational effort required for each EVM opcode. By using a different unit that Ether, we are still able to measure computational effort while being immune to price movement of Ether. This what we call gas:

... 
PUSH 40 -> 100 gas 
PUSH 20 -> 100 gas 
ADD     -> 2000 gas 
MSTORE  -> 3000 gas 
... 
... 
TOTAL: 45000 gas

The absolute values of gas costs are not important. What matters is the relative values: if opcode A cost 100 gas and opcode B cost 200 gas, that means opcode B is twice as computationally intensive as opcode A.

Gas is uniquely used to measure the computational difficulty of running a transaction. But the transaction fees are not paid in gas. Actually, it’s not possible to buy, sell or own any gas. This is just an abstract unit.

Going from gas to Ether

We just said that gas is an abstract unit. That’s great, but miners don’t care about abstract concepts. They want hard, real money to cover their real-life expenses!

We can convert a gas transaction fee to Ether by using the simple formula:

etherCost = gasPrice * gasCost

gasPrice is in wei / gas. Usually we actually use Gwei / gas, and as a shortcut we just mention Gwei.

Deciding on the gasPrice

The gasCost is determined by the Ethereum Virtual Machine (EVM), but the gasPrice is picked freely by the sender of the transaction.

What? If that is the case, why can’t the sender of the transaction just pick 0 for the gasPrice, so that the Ether cost is 0?

Well, he / she could do that, but the transaction would never be mined, since miners won’t have any incentive to include this transaction in the next block. Users are in competition between each others and need to include a transaction fee that is attractive for miners. Which entails to set a competitive gasPrice.

A good place to check the current market price of gasPrice is EthGasStation. Beside showing you what is the recommended gasPrice, it also has a tool where you can play with the gasPrice and see how long will it take to mine your transaction: the higher the gasPrice, the faster the transaction.

gasLimit

A last parameter that is worth mentioning is gasLimit. The gasLimit specifies the maximum gas consumption of a transaction.

Why do we need this? Don’t we already have a gas cost computed by the EVM? Well, there are for different thing. The gas cost is computed by the EVM at the end of the execution of a smart contract. The gasLimit is there to protect users to spend too much Ether in a transaction.

Let’s assume that we have this smart contract:

function foo() external {
  if(block.timestamp % 2 == 0) {
     //Do something simple
  } else {
    //Do something complex that will consume a LOT of gas
  }
}

It is totally possible that the else branch is executed, which would consume a lot of gas. To protect against this, we can set the gasLimit parameter. If the gasLimit is reached during the execution of the contract, the transaction will be reverted. That means that:

  • the execution stops
  • all state changes are reverted
  • the transaction is marked as failed in the transaction receipt

Lastly, if you set a high gasLimit but not all gas is consumed, then you get to keep the surplus of gas.

Leave a Reply

Your email address will not be published. Required fields are marked *