Ethereum: How Transaction Costs are Calculated

Share this article

Ethereum: How Transaction Costs are Calculated

This article on Ethereum Transaction Costs was originally published at Bruno’s Bitfalls website, and is reproduced here with permission.

When sending a Bitcoin transaction, its fee is proportionate to its size. The more inputs and outputs, the more expensive it is. Add to that the factor of pending transactions, and transaction fees can skyrocket based on those two factors alone.


It’s recommended you read the following materials before diving into the rest of this post in order to better understand the terminology we’ll be mentioning.

Basic knowledge of programming terms (variables, loops) might also come in handy.


With Ethereum, given that we’re talking about a programming language within the protocol, it’s possible to be very computationally demanding with very little text or code (something which would be very cheap in the BTC-verse). Let’s look at this loop, for example:

while (i++ < 1000) {
    j = j + i;
}

This loop means “for as long as i is smaller than 1000, increase it by 1 and then sum up i and j and write the result into j, then do it all again.” This loop will execute 1000 times if i is 0, or more if it’s a negative number.

To pay for this computational cost in a fair way — since it has to be executed on all miners’ machines at once and they spend their resources and time on it — the concept of gas was introduced. Gas is used to pay for the execution of these so-called smart contracts (Ethereum programs) inside the EVM. For example, i + j above is a summation operation which costs 3 gas every time it’s executed, so 3000 gas if executed 1000 times.

To explain gas properly, let’s first cover the EVM.

EVM

EVM stands for Ethereum Virtual Machine. But what is a virtual machine anyway?

Virtual Machine

A virtual machine is software running on a specific computer which contains another operating system completely encapsulated inside the main one. For example, a virtual machine allows you to run Windows inside Linux, Linux inside Windows, Windows on macOS like in the image below, or any other combination.

Windows inside macOS

We use virtual machines to separate the environment in which we do our everyday computer use from the environment we work or program in. This lets us keep viruses at bay (they have no way of breaching the virtual machine and getting to the main operating system), helps prevent infinite loops from crashing our main operating system, and holds hard-drive corruptions like the infamous WannaCry ransomware at bay. Additionally, VMs let us use Windows games on Linux, for example, or allow us to program in different versions of the same programming language’s environment easily, without mixing them up.

EVM

The Ethereum Virtual Machine is built into the software running on the Ethereum protocol. It executes smart contracts — Ethereum programs written in the Solidity language. The EVM is contained in the full nodes of the Ethereum network, inside of which it executes these Ethereum-user-written programs.

Any miner of Ethereum simultaneously executes smart contract code. What this means is that Ethereum programs (dapps — decentralized apps) are executed on everyone’s computer at the same time (decentralized).

Execution of these programs isn’t free, however. Miners spend their own electricity, time, and hardware to do this. To pay them for their effort of executing computer instructions (like “store the value 5 into the variable X”), the concept of gas was introduced.

Gas, Ether, and GWei

Gas is a unit of cost for a particular operation a computer needs to execute, and it executes this instruction when we broadcast a transaction which contains an Ethereum program in order to run a dapp. For example, summing two numbers costs 3 gas. Multiplying them costs 5 gas. Storing a 256bit word into the blockchain costs 20000 gas, which means storing 1kb of data costs 640000 gas.

Just like the USD has cents, so too does ether have its own basic unit: wei. If we take wei as the basic unit of ether, we get the following table of definitions:

unit wei
wei 1
kwei / ada / femtotether 1.000
mwei / babbage / picoether 1.000.000
gwei / shannon / nanoether / nano 1.000.000.000
szabo / microether / micro 1.000.000.000.000
finney / milliether / milli 1.000.000.000.000.000
ether 1.000.000.000.000.000.000

Tip: use this converter to convert between Ether units.

According to this informative site, the current average price of gas is 10 GWei (10 gigawei). Seeing as 1 GWei is one billionth of an ether, the aforementioned cost of storing a 1kb word is 640000 * 10, which is 6.4 million GWei. That amounts to 0.0064 eth which, at a price of $450 per ether, amounts to around $2.88.

The text from the beginning of the above table all the way to >>this specific marker<< is around 1kb. So storing this little bit of text into the blockchain actually costs $2.88. However, that’s just the storage cost! Our smart contract could also have some logic, like summing or multiplying and then storing numbers, or triggers that activate on a specific mined block etc. Obviously, storing data into the blockchain itself is incredibly expensive. Storage is better off in BigchainDB or IPFS, while blockchain is a better solution for global processing and verification of that data.

Why Gas?

Why pay costs in gas and not ether directly?

All the gas prices of all the possible operations the EVM can perform are hard-coded in the Ethereum protocol and in the clients (programs) we connect to it, like Geth, Eth, Parity, etc. If the code listed them in ether, then we’d have to update the code every time ether’s value fluctuated to keep the price of computing efforts in a normal range and keep the system usable, which is obviously unsustainable.

By adding this gas layer on top of the costs, and paying for gas with GWei, we’re given the option to alter the amount of gas to use in a transaction and the amount of money to pay for it. It’s fully under our control, without throwing the system off balance.

This leads us to our last section.

Limit/Cost vs Price

Gas limit is the maximum amount of gas we’re willing to spend on a transaction. Most software we use to broadcast Ethereum transactions has the ability to auto-estimate the amount of gas that’ll be necessary to execute a function. It’ll usually suggest a figure right off the bat. For example, simple monetary A->B transactions usually need only 21000 gas. More complex ones which call specific smart contract functions might run into hundreds of thousands or even millions of gas. The spent amount of gas is called gas cost.

We, as a user, can modify the amount of gas we want to spend on a transaction and reduce it, but if the transaction runs out of gas during execution, we lose the gas we sent in. It’s been spent and the transaction is rejected. On the other hand, if we provide more gas than is needed, the rest is refunded to us. Hence, it’s always better to send more gas than you might need to execute a transaction.

Gas cost is the GWei price per unit of gas.

Thus, the total cost of an Ethereum transaction is actually the amount of necessary gas multiplied by the price in GWei per gas unit. This is the maximum transaction fee we’ll pay; any extra gas is refunded, so fees are often vastly overestimated.

Let’s look at an example.

25 Gwei

In the above image, we’re executing a transaction which, due to its complexity, estimates it’ll need 135963 gas. With a 25 GWei price, the maximum transaction fee we’ll pay is $1.57, which is trivial when looking at the total amount we’re sending (0.38 eth = $178). If we raise the price of gas to 250 GWei, the transaction becomes proportionately more expensive:

250 GWei

A higher tx fee effectively encourages miners to process our transaction before others in the queue, thereby speeding up execution. If we’re not in a rush, we can leave the price of gas at its starting value and the transaction’s confirmation shouldn’t take more than 10 minutes. But if we’re in a rush and want it done in seconds (if, for example, we’re dealing with an auction), it’s easy to increase the price of gas and sacrifice some money for a guarantee of success.

Conclusion

Gas is the unit of work expended per computational operation in the Ethereum Virtual Machine. It’s paid for in ether, the token of the Ethereum protocol, and each computational operation has a different gas cost. The gas price (in GWei or 1 billionth of an ether) varies according to the network congestion and the user’s preference for a speedy confirmation.

For a smart contract which we want to deploy on the Ethereum network, two terms are important: gas limit, and gas price. Gas limit is the maximum amount of gas we’re willing to spend on executing the transaction. The amount of gas actually required (known only once the transaction has been executed) is called gas cost. Gas price is the price per unit of gas, expressed in GWei (or billionths of ether). The total cost of a transaction will be the product of gas cost and gas price, while the maximum transaction fee will be the product of gas limit and gas price. The difference is refunded to the transaction’s sender to keep the system fair and usable.

Frequently Asked Questions (FAQs) about Ethereum Transaction Costs

How are Ethereum transaction costs calculated?

Ethereum transaction costs, also known as gas fees, are calculated based on the computational effort required to execute a transaction or contract on the Ethereum network. This includes operations like sending Ether, deploying a smart contract, or interacting with a contract. Each operation has a set gas cost, and the total gas cost of a transaction is the sum of the gas costs of all its operations. The gas price, set by the sender, is the amount of Ether the sender is willing to pay for each unit of gas. The total transaction cost is the product of the gas cost and the gas price.

Why do Ethereum transaction costs fluctuate?

Ethereum transaction costs fluctuate due to supply and demand dynamics on the Ethereum network. When the network is congested with many transactions, the demand for gas increases. To ensure their transactions are processed quickly, users may opt to pay a higher gas price. This increased competition drives up the average transaction cost. Conversely, when the network is less busy, the average transaction cost decreases.

What is the role of miners in Ethereum transaction costs?

Miners play a crucial role in Ethereum transaction costs. They are responsible for validating and adding transactions to the Ethereum blockchain. Miners prioritize transactions offering higher gas prices because they receive these fees as rewards. Therefore, if you want your transaction to be processed faster, you can increase the gas price to make it more attractive to miners.

How can I estimate the gas fee for my Ethereum transaction?

There are several tools available online that can help you estimate the gas fee for your Ethereum transaction. These tools consider factors like the current network congestion and average gas price to provide an estimate. One such tool is the ETH Gas Station, which provides real-time data on Ethereum gas prices.

Can I set my own gas price for an Ethereum transaction?

Yes, you can set your own gas price for an Ethereum transaction. However, it’s important to note that if you set a gas price that’s too low, miners may not prioritize your transaction, and it could take a long time to be processed. If the gas price is set too high, you may end up overpaying for the transaction.

What happens if I don’t provide enough gas for my Ethereum transaction?

If you don’t provide enough gas for your Ethereum transaction, the transaction will fail, and all operations will be reverted. However, you will still be charged for the gas used before the transaction failed.

Can I get a refund for unused gas in an Ethereum transaction?

Yes, if your transaction uses less gas than you provided, the unused gas will be refunded to you.

What is a gas limit in Ethereum transactions?

A gas limit is the maximum amount of gas you’re willing to use for a transaction. It serves as a safety measure to prevent you from spending more on a transaction than you intended.

How does Ethereum’s Proof of Stake impact transaction costs?

Ethereum’s transition to Proof of Stake (PoS) is expected to reduce transaction costs. PoS is more energy-efficient and can process transactions faster than the current Proof of Work system, which should reduce network congestion and, consequently, transaction costs.

What is EIP-1559 and how does it affect Ethereum transaction costs?

EIP-1559 is an Ethereum Improvement Proposal that aims to make Ethereum transaction costs more predictable. It introduces a base fee that adjusts according to network congestion, making it easier for users to estimate transaction costs. It also includes a mechanism to burn part of the transaction fee, which could potentially make Ether deflationary.

Bruno SkvorcBruno Skvorc
View Author

Bruno is a blockchain developer and technical educator at the Web3 Foundation, the foundation that's building the next generation of the free people's internet. He runs two newsletters you should subscribe to if you're interested in Web3.0: Dot Leap covers ecosystem and tech development of Web3, and NFT Review covers the evolution of the non-fungible token (digital collectibles) ecosystem inside this emerging new web. His current passion project is RMRK.app, the most advanced NFT system in the world, which allows NFTs to own other NFTs, NFTs to react to emotion, NFTs to be governed democratically, and NFTs to be multiple things at once.

etherethereumEthereum Virtual Machineethereum-hublearn-ethereum
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week