Latest & Breaking Cryptocurrency, Blockchain, Ethereum, Bitcoin News- Coinedict
Explanatory articles

Ethereum Gas – A Comprehensive Guide

Ethereum Gas

Ethereum Gas is the fuel used to send Ether. It is a unit which measures the amount of computational effort required to execute operations. Every operation on the Ethereum platform takes some amount of Gas.

Smart contracts in EVM (Ethereum Virtual Machine) are coded using Solidity. Each line of code in Solidity requires a certain amount of Gas in order to be executed. According to the Ethereum Yellow paper, every transaction requires at least 21,000 Gas.

Ethereum Gas    If a person wants to get his fuel tank filled he follows these steps:

  1. He goes to the station and specifies the amount of gas to be filled in the tank.
  2. He gets gas filled in his car.
  3. He pays the due amount to the gas station.

If we draw parallels with Ethereum we can say:

  1. Driving the car- Operation to be executed
  2. Gas- Gas
  3. Gas Station – Miner
  4. Money paid – Miner fees

If a user wants to execute an operation in Ethereum, he should provide Gas for the following:

  1. For covering data
  2. For covering the entire computation

Need for Gas in the System

Gas is required for incentivization. Ethereum is dependent on the hashrate of its miners. If the platform has more miners it will automatically have more hashrate which leads to a faster and more secure system. The system needs to profitable and alluring to the miners, in order to attract more miners in the systems.

There are two ways miners can earn money in the ecosystem:

  • They can mine blocks and acquire block rewards.
  • They can become temporary dictators of their mined blocks.

As the miners are responsible for adding transactions in their blocks, they will some of their computational power to validate smart contracts. The Gas system allows them to charge a fee for doing so. This fee is also known as the miner’s fee. It helps incentivize the miners enough to take part in the ecosystem.

The amount of Gas depends on the number of EVM transactions need to be executed.

Converting Gas into Ether

There is no fixed price for converting Gas into Ether. A sender of a transaction can specify a Gas price. Similarly, a miner can decide on which transaction to verify. The average Gas price is typically on the order of about 20 Gwei (or 0.00000002 ETH). However, the price can increase during times of high network traffic as there are a large number of transaction competing to be included in the next block.

Ethereum Gas

Ethereum Gas Limit

Gas limit refers to the maximum amount of Gas the sender is willing to pay for this transaction. The sender of the transaction must specify a Gas limit before it is submitted to the network. The following points must be considered before specifying a Gas limit:

  • Different operations have different Gas costs.
  • Miners will stop executing the moment Gas is over.
  • The leftover Gas is immediately refunded to the operation generator.

Suppose, if two numbers are to be added, the smart contract will require the following amount of Gas:

  • Storing 15 in a variable which costs 50 Gas
  • Adding two variables, let’s say this costs 15 Gas
  • Storing the result which again costs 50 Gas

If the sender, specifies a Gas limit of 150 Gas, the total Gas exhausted by the miner is (50+10+50)=110 Gas. Therefore, the fee owned to the miner, taking 1 Gas costs 20 Gwei, is (150*20 Gwei)=0.000003 ETH.

Leftover Gas – 150 – 110 = 40 Gas

The 40 unused Gas is then returned back to the sender (40* 20 Gwei) = 0.0000008ETH.

Now, two situations arise

  • The specified Gas Limit is low.
  • The specified Gas limit is high.

Case 1: Low Gas Limit

In case the operation runs out of Gas, it is reverted back to its original state. However, the operation generator still needs to pay the miners the fee for using their computational power.

Consider again, if two numbers are to be added, the smart contract will require the following amount of Gas:

  • Storing 15 in a variable which costs 50 Gas
  • Adding two variables, let’s say this costs 15 Gas
  • Storing the result which again costs 50 Gas

This time, the sender sets a Gas limit of 100  Gas. However, the Gas required to fuel the transactions is 115 Gas, but the specified Gas limit is 100.

In this situation, the miner will carry out computation worth 100 Gas and then charge the sender accordingly which amounts to (100*20 Gwei)= 0.000002 ETH.

The contract then reverts to its original state and the transaction is added on the blockchain.

Case 2: High Gas Limit

Miners are limited by the block Gas limit. Suppose, it is 6,700,000 Gas. A simple transaction requires a minimum of 21,000 Gas. Miners can only add transactions which add up to or less than equal to the block Gas limit.Ethereum Gas

Suppose, there is a transaction A, and it has a specified limit of 43,000 Gas and two transactions B and C which have specified limits of 22, 000.

The miner will include B and C transactions, as a bloated Gas limit is not the ideal. It is apt to set a Gas limit which is a bit higher than the required amount of Gas for the transaction.

Ethereum Gas

The currently recommended Gas prices for different types of transaction speeds, according to ethGasstation are:

Gas Refund Scenarios

There are two commands in Solidity, which ensure that the sender gets some Gas back:

  • SUICIDE: It kills the smart contract. The sender gets back 24000 Gas.
  • SSTORE: The sender gets back 15,000 Gas.

Hence, if the contract uses up 14,000 Gas and deletes a storage then ideally,  1000 Gas will be refunded. In this case, miners will lose all incentive.

Thus, to avoid such a scenario a condition was put in. The refund that has been accumulated cannot exceed half the Gas used up during the computation.

If in case a smart contract uses 14,000 Gas and the Gas limit is 20,000 Gas. This smart contract includes an SSTORAGE command. The contract creator gets (20,000 -14,000) = 6000 units of unused Gas.

However, as SSTORAGE command has also been used, the creator should also get 15,000 Gas as well. Since the amount of Gas used in the contract is 14,000 and since 15,000 > 14,000/2, the generated REFUND will be 14,000/2 = 7000. The total Gas the creator acquires is 6000 + 7000 = 13,0000.

If the contract uses 70,000 Gas and it includes a SUICIDE function, it should give 24,000 Gas back which is < 70,000/2. The Gas refund, in this case, will be 24,000+ unused Gas.

Criticism of Ethereum Gas

The Gas system has come under criticism lately for being too expensive for developers and smart contract creators.

Consider the following scenario:

Ethereum Gas

When two numbers are added a million times in Ethereum it costs ~$26.55 in fees. Danny Ryan compared that to a standard AWS system. He said that he can add two numbers a million times using python in 0.04 seconds, which going by the $0.0059 hourly Amazon EC2 rate costs $0.000000066. It implies that computation in Ethereum is 400 million times more expensive.

He concluded that “To be fair, adding two numbers together 1 million times is a bit contrived. A well-written contract would likely move such computational complexity off-chain and deal more with updating state in the contract. Storing vast amounts of data to the blockchain is also not an ordinary task. Depending on the task, a user would likely store a cryptographic reference (a hash) of the data on-chain and keep the rest of the data off-chain.

That said, we as developers need to be aware of these costs, and design dapps accordingly. We need to find the balance between on-chain and off-chain complexity, while still leveraging the decentralized capabilities of the blockchain.”

While Ethereum Gas is truly the lifeblood of the Ethereum ecosystem, it has its own significant drawbacks. However, with the rapid progress, Ethereum network is making the fallacies of the ecosystem will be soon resolved.

Related posts

Trading Funds Explained

Sharukh Khan

Lawmakers Send Letter to IRS against Crypto Guidelines

Vijaya Bharti (Author)

Bitcoin Cash Gets Benefits as Bitcoin SV Continues Getting Delisted

Shreya Singh