Try it for one month, no fees!
Try it for one month, no fees!

Calculating the Cost of Gas in DeFi Transactions : A Complete Guide

In the exciting world of decentralized finance (DeFi), speed and efficiency are essential. However, one of the aspects that can often go unnoticed until a transaction is initiated is the cost associated with gas. Gas refers to the fees paid for transactions on the blockchain network, calculating these costs in advance is highly relevant to estimating the profitability of an investment.

In this article, we will explain how to calculate the gas costs for a DeFi transaction in EVM networks like Ethereum, if you want to know more about networks here we have an article with more information, and understand the factors that influence them.

 

Factors

In summary, the cost of a transaction will depend on the following 3 factors:

  • Gas:

    which is how Transaction computation costs are calculated. Each transaction in a blockchain network involves certain computational operations to validate and execute the required action. These operations consume network resources, such as processing power and memory. As transactions become more complex or require more computations, the associated gas cost increases accordingly.

In general, the gas needed is estimated by running gas estimation methods in libraries such as Web3.js o ethers.js. In case you do not know how to run these libraries it can be done via the block explorer https://etherscan.io/. Looking for the contract you want to interact with. When you are in it you go to the "Contract" section where you can see the write or read functions, fill in the necessary parameters and when you execute it the explorer will indicate an estimate of the gas.

Once the gas has been estimated, a "gasLimit" is indicated, which is the maximum gas you are willing to use for the transaction to be executed. It will always be higher than the estimate because the gas calculation is quite complex and does not have to be right.

 

  • Gas rate:

    which is marked by network congestion and transaction priority. In most blockchains, each block can hold a limited number of transactions and these are not included on a first-come, first-served basis, but are added first to those who are willing to pay more commission for the transaction.

In the main wallets the provider itself suggests you what commission you should enter to make the transaction. But in case it fails or for whatever reason you want to do the calculation yourself there are tools that calculate the gas you have to pay. Such as TXStreet, Etherscan Gas Tracker o GAS Now depending on the priority you want to give to the transaction. These tools help to avoid that a transaction takes too long to be completed, if you indicate a very low rate, or an excess of commission that does not contribute anything, if you indicate a very high rate.

As these rates change block by block, in a single day or week you can have very high variations. Understanding the price dynamics of the rates can help you earn extra profitability on your investments, especially if you make them with small amounts.

Finally, indicate that although the gas is paid in the main currency it is usually on a very low scale so to avoid having to say the 0's or scientific notation it is given a name for example in the case of Ethereum the gas is usually indicated in gwei which is no more than 1*10-9 ETH in other words 1,000,000,000,000 gwei=1ETH

 

  • Cryptocurrency Exchange Rate: Gas tariffs are generally paid in the native cryptocurrency of the grid. Therefore, the current value of the cryptocurrency compared to your reference currency also influences gas costs.

 

Therefore the cost of the transaction in our reference currency is the result of the following operation: Commission = Gas* Tariff* Exchange rate

 

How to calculate the cost of an Ethereum transaction

 

Below are three examples of commission calculation on the Ethereum network, depending on the transaction.

In all three examples, the following conditions are met:

  • Our reference currency is the USD and the exchange rate is currently at 1.666 USD = ETH
  • We want the transaction to have a high priority, and to be executed in less than 30 sec, and in this case by checking it in TXStreet the rate has to be 52 Gwei.

This rate may vary considerably.

 

Practical examples

 

Let's look at the three examples:

  • Ether (ETH) shipment:

When you send Ether between accounts without interacting with smart contracts, which is known as a "simple transfer", the cost is always 21,000 GAS units. This value in GAS is predefined in the Ethereum protocol. Source: Page 25 of the Ethereum YellowPaper. Each network has its predefined value.

Therefore, in this case the cost of the operation in our reference currency will be: 21,000*52*10-9*1.666 = $1,82

 

  • Sending an ERC20 Token (USDC) to an account that has never received it

Although all addresses in the Ethereum network can receive different types of ERC20 tokens. The gas required to send an ERC20 token is variable and depends for example on whether the recipient of the transaction has already received that token before.

On this occasion, we have opted for a simplified calculation to give us a general idea.

Based on information from Etherscan Gas Tracker and GasNow, transactions with ERC20 tokens, specifically USDC have a cost that ranges around 48,500 GAS units.

Next, we will perform the calculation again using the GAS value. We will take as an example the average of 48,500 units.

Therefore, in this case the cost of the operation in our reference currency will be: 48.500*52*10-9*1.666 = 4.20$

 

  • Calculation of a Uniswap transaction

Same logic as for ERC20 transactions but even more complicated. The cost in GAS of Uniswap transactions depends on many parameters, such as: what kind of action are you going to perform in Uniswap?

Examples of shares in Uniswap and their estimated prices by GASNOW :

-SWAP: 105.657

-ADD LIQUIDITY: 131.820

-REMOVE LIQUIDITY: 180.244

We could say that transactions in UNISWAP cost on average between 100000 y 200000 of gas. Then we can reproduce the same calculation. But, in a shorter way :

100.000*52*10-9*1.666 = 8.66$

 

Here we calculate the price for a simple swap in the best case scenario, to give you an idea.

 

Due to these high costs in the Ethereum network, as we have already explained in other articles such as the one on How to choose which blockchain to use to invest in DeFi? Blockchain technology is constantly evolving to avoid these problems with layer 2 solutions or other communication protocols.

Now you have knowledge on how the commissions you pay for making transactions within a blockchain are calculated! Follow us to learn everything you need to know to operate in this amazing world of DeFi.

Stay informed about new products, promotions, and more with our newsletter! Sign up for our newsletter.
Address
C/ Almagro, 15
28010 Madrid
Call us at
+34 919 618 540
Write to us at
info@alldefi.io
Registration: D669
Alldefi is a product of CherryNodes S.L. Spanish company CIF: B04945580 and is registered with the Bank of Spain as a provider of services of exchange of virtual currency for fiat currency and custody of electronic wallets.
Investing in cryptoassets is not regulated, may not be suitable for retail investors and the entire amount invested may be lost. It is important to read and understand the risks of this investment which are explained in detail at this location.