Blockchain definition of the week: Ethereum Gas

Just like the gas you put in your car, almost.


This article is the first of a series of articles titled “Blockchain definition of the week”. Every Wednesday, I will publish an article addressing an important definition of the decentralized world. These definitions may be relevant to the technology as a whole, some individual aspect of blockchain, or be regarding a specific blockchain protocol. The aim is to dive deeper than your average definition, yet explain the chosen topic in a way that little or no previous knowledge of it is required.

The choice of “Ethereum Gas” for the topic of the first week comes after noticing in my workshops that this is a concept that people struggle to grasp, as well as being extremely important to the Ethereum protocol.

Additionally, if you speak Portuguese, you can check out my other series, titled “Smart Contracts”, with articles published every Tuesday.


<article>

<definition>

For those of you already familiar with Ether transactions, you’ve probably heard of gas. It pops up in your MetaMask when you make a transaction, saying the “gas limit” is something like 21000 and the “gas price” is 1. So what does that all mean?

Well, gas is a unit that measures the use of computational power in the Ethereum network. That is, how much effort computers processing a certain operation will need to put in to successfully execute the operation. Operations in Ethereum are, for example, sending Ether to another address, publishing a contract, calling a function on a contract, among others. When these happen, miners process that information in the EVM (Ethereum Virtual Machine) to reach the proper outcomes, such as executing a function and returning the right data or sending the ETH to the right address. In doing this, these miners are using their computational power, and the more complex a transaction is, the more power it will consume, hence using more gas.

Gas acts as the fuel of the Ethereum platform

Gas limit

Gas limit defines the maximum amount of gas an operation can use. For example, when sending some Ether from Alice to Bob, the gas limit is usually set at a standard of 21000, assuming no more data is included in the transaction. 21000 is the minimum amount of gas an operation on Ethereum will use. This limit is used to guarantee that the transaction will be executed. The limit is usually provided by the clients/wallets, which estimate the maximum boundary for gas use of an operation, to assure that it will be executed properly without the user having to worry. In this case, the unused Ether used to pay for the extra gas will be returned to your account if the operation uses less gas than the limit. While many times the limit is set for you, it can also be set manually, in which case the user runs the risk of the operation failing, if the limit is not enough.

More complex operations will use up more gas. See the example list below, ordered from lowest to highest gas requirements.

  1. Sending Ether to another address
  2. Sending Ether to another address with some added metadata
  3. Calling a function on a smart contract
  4. Publishing a simple smart contract
  5. Publishing a complex smart contract

Lastly, if you want to know how the gas limit of an operation is calculated, the formula provided by the Ethereum Yellow Paper is as follows:

gasLimit = G(transaction) + G(txdatanonzero) × dataByteLength

Gas price

Gas price is the comparative equivalent of the miner fee in Bitcoin. In Bitcoin, you choose how much you want to pay the miner to process your transaction, being so that the higher the fee, the faster the transaction will be included in a block (in general, assuming participants rationally follow the financial incentive structure).

In Ethereum, things work similarly. Once the gas limit for a transaction is set, you can choose how much you wish to pay for each unit of gas. The more you pay, the faster you expect your operation to execute. Gas price is most often calculated in Gwei, a subunit/denomination of Ether, equal to 1/10⁹ ETH. Usually, when operating in the mainnet, users will pay between 1–60 Gwei per unit of gas, depending on how overloaded the network is.

Hence, the equation for how much in Ether you will pay in “network fees” is:

cost (ETH) = gasLimit × gasPrice × (1/10⁹)

Example — Alice sends 2ETH to Bob, choosing a gas price of 2 Gwei:

21000 × 2 × (1/10⁹) = 0.000042 ETH

Important note: The gas price is not selected by the network. It is chosen by the user, or sometimes, by the client, which shields the user from this information and sets a standard gas price, for the sake of usability (yet often at a higher cost for the end user).

For future reference, you should check the following website:

The website tells you what it calls the “SafeLow Cost for Transfer”, which is the minimum gas price you should set to have a high chance of your transaction being included in one of the next blocks (usually over 90% chance of it being included in the next 10 blocks).

As a rule of thumb, you should set higher gas prices for urgent transactions, and lower prices for transactions you don’t mind taking a while to confirm.

It is also worth noting that if your gas limit is set at, for example, 300000, you choose to pay 40 Gwei for each unit of gas, and ultimately, the transaction only uses 21000 gas, you will be refunded:

[(300000–21000) × 40 × (1/10⁹)] = 0.01116ETH

The takeaway here is: you are refunded for unused gas, accounted for the price you paid for it, but you are not refunded for setting the gas price too high.

</definition>

<questions>

Why not set enormous gas limits for every transaction?

So to guarantee my transaction will not run out of gas, I can set the gas limit extremely high, right?

Yes, you can. The problem with doing so is that you will have to pay a lot of Ether up-front, which you may not have or that you might need while your transaction is being processed. It is also a question of optimization.

However, you can, and should, put the gas limit slightly higher than the amount of gas you expect the transaction to consume when dealing with gas limits manually.

Common errors

“Transaction underpriced” or “transaction fee too low”

Means that your gas price is set too low and your transaction may not confirm, or will take too long too confirm, since it will always be at the end of the list of transactions to be included in the next block. Wallets will often offer you the option to increase your gas price to change this, or refuse to send the transaction.

“Transaction ran out of gas”

Means that the gas limit was set too low, and the transaction ended up using more gas than you paid for. Transactions with a lower gas limit than the gas used will never execute. In this case, you will not be refunded the Ethers spent. That is because the miner used valuable computational power to attempt to execute the transaction, but was unable to. Regardless of the transaction confirming or not, computational power was used, and it was paid for.

Gas limit of blocks

Instead of implementing a hard memory limit on blocks, like Bitcoin’s 1MB, Ethereum sets its block limits with gas. The current limit is set at 8000000 units of gas per block, but it is in fact not a hard limit, but a parameter the network follows as a mean limit for blocks. Blocks can be over the limit, but ultimately, the network aims to keep the average below 8000000. There is discussion regarding setting this limit in the actual protocol, with the benefit of preventing blocks from being too large and hard to compute, especially for nodes with less computational power available. The counterargument is that this limit may prevent complex dApps which consume a lot of gas from running.

More on that here and here.

Gas and smart contracts

Smart contracts require gas to run and execute its functions. The more complex a contract or function are, the more gas they will use. Because of this, optimization of smart contracts is extremely important, to prevent excess use of gas, which is costly, and overloads the network for no reason.

Commonly, Remix’s static analysis of contracts will return a warning regarding the high gas requirements of a function. It is also important to watch out for loops, which can consume excessive gas.

Publishing complex contracts will use more gas than publishing simpler contracts, and the same applies to the execution of these contracts.

Why are these fees important?

These fees are important because they provide a fair incentive scheme for keeping the network running properly, since the more you utilize the network, the more you pay for it. But perhaps most importantly, it helps protect the network against attacks (namely DDoS attacks), since malefactors will have to pay a high price to harm the network.

</questions>

</article>