Interpreting the calculation of Ethereum gas fees How to reduce transaction costs?

Author: Rosario Borgesi

Translation: Huohuo, Plain Language Blockchain

The Ethereum blockchain completely changes decentralized applications and smart contracts, but every transaction will generate Ethereum gas fees. In this guide, we will uncover the mystery of Ethereum gas fees, understand its basic principles, calculation methods, factors affecting costs, and effective strategies for optimizing transactions while controlling costs.

1. Understanding Ethereum Gas

Gas is the fuel for transactions and smart contract execution on the Ethereum blockchain. It represents the computational work required for these operations and is priced in small denominations of Ether called gwei (1 gwei = 10^-9 ETH).

In other words, gas is the unit of measurement for computational work, equivalent to a certain amount of Ether.

This is why gas fees are crucial in the Ethereum ecosystem:

1) Resource allocation: Gas fees act as a mechanism for allocating network resources. By requiring users to pay for the computational power and storage space they use, the network ensures fair access and prevents resource abuse.

2) Preventing network spam: Without gas fees, malicious actors could flood the network with spam transactions, causing system overload and slowing down legitimate transactions. Gas fees make sending spam to the network costly and economically impractical.

3) Incentivizing miners: Gas fees incentivize miners to include transactions in blocks. Miners prioritize transactions with higher gas fees, encouraging users to provide competitive fees for timely transaction processing.

4) Network security: Gas fees contribute to the security of the Ethereum network. They ensure the correct execution of transactions and smart contracts by requiring users to pay for the computational steps involved. This helps prevent potential attacks and vulnerabilities.

5) Scalability and efficiency: By attaching costs to each operation, gas fees encourage developers to write efficient and optimized code. The focus on efficiency is crucial for scaling the network as it encourages best practices and reduces the pressure on the blockchain.

6) Economic model: Gas fees are a part of the Ethereum economic model. They facilitate a sustainable ecosystem, compensating miners for their efforts and promoting the growth and stability of the network.

2. Components of Gas Fees

Understand the main components that constitute Ethereum gas fees:

1) Base fee: Set by the protocol, you must pay at least this amount for your transaction to be considered valid.

2) Priority fee: A tip added to the base fee to make your transaction appealing to validators so that they choose to include it in the next block.

Transactions that only pay the base fee are technically valid but are unlikely to be included because they do not incentivize validators to choose them.

The priority fee depends on the network usage at the time you send the transaction: if the demand is high, you may need to set a higher priority fee, but when the demand is low, you can pay a lower fee.

3. Transaction Fee Calculation

For example, let’s assume Jordan needs to pay 1 ETH to Taylor. A transfer of ETH requires 21,000 units of gas, with a base fee of 10 gwei. Jordan includes a tip of 2 gwei.

The total fee is now:

Units of gas used * (base fee + priority fee)

where the base fee is a value set by the protocol and the priority fee is a value set by the user as a tip for validators.

So, 21,000 * (10 + 2) = 252,000 gwei (0.000252 ETH).

When Jordan makes the transfer, 1.000252 ETH will be deducted from Jordan’s account. Taylor will receive 1.0000 ETH. Validators will receive a tip of 0.000042 ETH. The base fee of 0.00021 ETH will be burned, meaning it will be removed from circulation.

4. Reading and Writing Data

Ethereum network makes a distinction between writing data to the network and reading data from the network, which plays an important role in how you write your applications. Generally, writing data is called a transaction, while reading data is called a call.

1) Transaction: Transactions change the network state when writing or modifying data. They can be as simple as sending Ether to another account or as complex as executing contract functions or adding new contracts to the network. To perform transactions, we must pay gas fees, and they take time to process.

2) Call: Calls can be used to execute code on the network, but they do not permanently change data. They are primarily used for reading data, so they can run for free (without spending gas). When you execute a contract function through a call, you will receive the return value immediately.

5. Transaction Parameters

You can set the following parameters in a transaction object to specify the amount of gas you are willing to pay when submitting a transaction:

1) GasLimit: The maximum amount of gas a transaction can consume. The EVM specifies the gas units required for each computational step.

2) maxPriorityFeePerGas: The highest price in gas consumed included in the validator tip.

3) maxFeePerGas: The highest cost per unit of gas that you are willing to pay for the transaction (including baseFeePerGas and maxPriorityFeePerGas).

6. London Upgrade (EIP-1559)

Prior to the London upgrade, the fee calculation did not differentiate between base fee and priority fee.

In the transaction object, we can set:

1) GasLimit/startGas: The maximum amount of gas a transaction can consume, same as before.

2) GasPrice: The amount of wei you are willing to pay per unit of gas.

Let’s assume Alice needs to pay 1 ETH to Bob. In the transaction, the gas limit is 21,000 units, and the gas price is 200 gwei.

The total fee is: Gas units (limit) * Gas price per unit, which is 21,000 * 200 = 4,200,000 gwei (0.0042 ETH).

7. Fee Calculation in Complex Transactions

To better understand transaction costs, we can look at EVM opcodes. So let’s calculate the cost of executing this function:

function doMath(uint a, uint b) { a + b; b – a; a * b; a == 0; }

a + b (ADD) costs 3 gas units, b – a (SUB) costs 3 gas units, a * b (MUL) costs 5 gas units, and a == 0 (EQ) costs 3 gas units.

So the total cost of this transaction is 14 gas units.

One important thing to understand is that if the gasLimit specified in the transaction object is equal to 6, only the first two operations will be executed, and then the transaction will be stopped, and the paid gasFee will not be refunded because we have to pay for the work done.

Therefore, if we have to set the gasLimit parameter to a sufficient value so that our transaction can be executed without losing our ether.

In this particular example, calculating the gas fee is relatively simple. However, for more complex functions, especially those involving loops with different variable sizes, it can be challenging to determine the gas cost in advance.

In such cases, we can use the functionality provided by two of the most famous development libraries, ethers.js and web3.js, which is the estimateGas function. This function allows us to estimate the gas cost associated with executing a specific function on the Ethereum blockchain.

However, it is worth noting that setting gasLimit and maxFeePerGas is always a good practice to avoid executing transactions that consume too much ether.

8. Strategies to Reduce Gas Fees – 1) Utilizing Layer 2

Layer-2 is an auxiliary framework or protocol built on top of the Ethereum blockchain. These solutions aim to enhance scalability and optimize transaction speed by handling the majority of transactions outside of the Ethereum main chain (referred to as Layer 1). Layer 1 involves the underlying Ethereum blockchain, where transaction costs are high and scalability issues may arise due to high demand.

Among various Layer-2 methods, there are Rollups adopted in Arbitrum and Optimism.

Rollups aggregate multiple transactions into a single transaction, compress data, and store it on the Ethereum mainnet. This significantly reduces gas fees and improves scalability, as a single mainnet transaction can represent a large number of off-chain transactions.

2) Running during periods of network congestion

If we want to operate on Layer 1 and save costs, the solution seems obvious: we must send transactions when the network is less congested.

In fact, the Ethereum blockchain allows only about 20-40 transactions per second (TPS) (which is the total number of Ethereum users); when the limit is reached, users are forced to compete with each other to get their transactions through, resulting in increased fees.

To check network load, we can use Etherscan.

3) Minimizing the number of transactions as much as possible

Every transaction on the Ethereum blockchain incurs a cost called gas fee. To reduce these fees, it is necessary to merge as many operations as possible into a single transaction. For example, bundling multiple token transfers or interactions with smart contracts into a single transaction can significantly reduce overall costs.

4) Optimizing code complexity

Ethereum transactions involve executing code stored on the blockchain. Complex and inefficient code requires more computational resources, resulting in higher gas fees. By optimizing and simplifying the code, you can reduce the gas required for execution. This involves efficient coding practices, minimizing redundant calculations, and utilizing efficient programming techniques.

5) Energy-saving Smart Contracts

When developing smart contracts, it is crucial to design them in a way that minimizes natural gas consumption. This includes avoiding unnecessary storage operations, optimizing loops, and reducing the complexity of contract logic. Gas optimization tools and best practices should be utilized during the development process.

9. Conclusion

In conclusion, gas fees are an important component of Ethereum as they maintain network integrity, incentivize proper usage, ensure security, and support scalability, all of which contribute to building an efficient and sustainable blockchain network.

In this article, we have explored the calculation and estimation of gas fees, delving into various strategies to mitigate their impact. By adopting optimization techniques, utilizing Layer2 solutions, and implementing smart contract code that saves gas, users and developers can effectively reduce transaction costs and improve overall efficiency of applications on the Ethereum network.

With a deeper understanding of gas fees and practical strategies for managing them, you are now prepared to navigate the Ethereum environment and optimize your coding work. Happy coding, and may your Ethereum interactions be both cost-effective and innovative!

Like what you're reading? Subscribe to our top stories.

We will continue to update Gambling Chain; if you have any questions or suggestions, please contact us!

Follow us on Twitter, Facebook, YouTube, and TikTok.

Share:

Was this article helpful?

93 out of 132 found this helpful

Gambling Chain Logo
Industry
Digital Asset Investment
Location
Real world, Metaverse and Network.
Goals
Build Daos that bring Decentralized finance to more and more persons Who love Web3.
Type
Website and other Media Daos

Products used

GC Wallet

Send targeted currencies to the right people at the right time.