Understanding Gas Fees in Blockchain
Gas fee is a term used in blockchain technology that refers to the cost associated with executing a transaction or a smart contract on a network. Users pay the gas fee to compensate validators who process and verify these transactions on the network. This fee is usually denominated in the network’s native cryptocurrency and is determined by the complexity of the transaction or smart contract code and the amount of computational resources needed to execute it.
Gas fees serve two primary purposes. First, they are used to prioritize transactions on the network, with higher gas fees resulting in faster transaction confirmations. This prioritization ensures that more important transactions are processed first. Second, gas fees protect against spamming and malicious attacks on the network, as each transaction requires a specific amount of gas fee to be processed. Therefore, the higher the gas fee, the more expensive it is to launch an attack on the network, making it more secure.
Very early, spamming problem was detected on the cosmos ecosystem : Learn more about it
Understanding the Difference Between GasWanted and GasUsed in Blockchain Transactions
The gas amount set by a user as “GasWanted” for a transaction often differs from the actual gas amount used, known as “GasUsed”. This disparity arises because estimating the gas required for a particular transaction can be challenging and depends on several factors, such as the complexity of the transaction and the current network congestion.
To ensure that our transactions are executed successfully, we, as users, often tend to overreact by paying more gas fees than necessary. This is because we don’t want to face the frustration of paying gas fees and waiting for a transaction that ultimately fails to be executed.
By examining gas consumption in the OKP4 blockchain, we can investigate potential solutions for executing transactions with the ideal gas fees. But before starting, let’s connect our KEPLR wallet to the OKP4 blockchain. Don’t have a KEPLR wallet : follow our step-by-step Guide!
Are you ready?
1 – Connect to Aviaone Blockchain Service and click on the link as following:
2 – You will be redirected to OKP4 Explorer and click on “Connect Wallet“
3 – Click “Next“
4- Click “Approve” to add the OKP4 Network to your wallet
5 – Add a Account Name and click “Next“
6 – Click “Save” and your wallet is all set! Congratulations
Now, Let’s talk about …. Gas!
According this page Gas fee can be calculated as following :
[Gas fee] = [Gas units] * [Gas price]
The term “gas units” refers to the amount of computational effort necessary to perform certain operations on the network, while “gas price” is a price that varies depending on the level of network congestion. This price is determined by the network and can automatically fluctuate based on the current network congestion.
We will focus on the gas units here in this document.
This article covers Phase 4 of the Testnet called “Nemeton“, where gas-prices are set to 0, resulting in no gas fees being charged when executing a transaction. However, in the mainnet, it is recommended that validators increase the minimum fee (minimum-gas-prices parameter) to prevent spam, which means that each transaction will incur a real cost in uKNOW.
The minimum fees accepted in the network are determined by validators, and each validator can specify a different minimum value for their fees. Therefore, the same transaction can have varying gas fees.
Let’s experiment with adjusting the gas units with the Gas Adjustment parameter and see how it affects the transaction. To begin, we can try sending some tokens to another wallet and observe the outcome.
The –gas-adjustment parameter is an optional tool that allows users to scale up the gas amount in order to avoid underestimation. By specifying a gas adjustment value of 1.15, for instance, a user can use 1.15 times (or 15%) the estimated gas for a transaction.
okp4d tx bank send okp417x0ewz3gwwk52eeryhg89q6g0zknpwlm8pvw7p okp41qkunw6ey2j5ltm8s3nyffnz3tu6pygmglyy9hl 1uknow --chain-id=okp4-nemeton-1 --gas-adjustment=1.15 --fees=0.000001uknow
Transaction executed! Check it out
When we look at the transaction details, we can see that the GasWanted was set to 200,000, but the actual GasUsed was only 77,128. This means that 122,872 gas was sent unnecessarily. To avoid such gas losses, we can explore ways to optimize our gas usage in the future.
If we don’t define the gas adjustment and only specify “gas auto,” the gas will be automatically estimated by the network.
As we can see, GasWanted (60686) is lower than GasUsed (61155) so transaction failed.
Let’s try setting a gas adjustment of 1.15 and use gas auto to see the impact on the gas consumption.
Success Transaction : Check it out !
The result of the experiment shows that the GasUsed and GasWanted are very close to each other, which is the intended outcome. However, as a user, you might wonder how to utilize the gas adjustment in your own transaction with KEPLR.
Keplr offer this possibility as followed :
Let’s increase the gas adjustment to 1.5 to see if it actually works as expected:
As expected, the GasWanted has increased. Check it out !
It appears that determining the appropriate gas adjustment value is crucial for achieving the optimal gas fee amount, which is closest to the GasUsed. However, what if users were able to input a significant amount of gas fees and receive a refund for the unused gas during the transaction?
[RefundGas] = [GasWanted] – [GasUsed]
Implemented on Ethermint in 2021:
But there are still conversations about it in 2022.
Objectarium smart contract provided by OKP4 team
The functionality of the okp4-objectarium smart contract involves the storage of objects in any Cosmos blockchain using the CosmWasm framework. This contract facilitates the storage, pinning, and unpinning of objects for a specified sender address. It also allows for object removal or forgetting if they are no longer pinned.
Let’s try to instantiate with gas auto parameter :
As observed, the use of gas auto results in a GasWanted value that is relatively high compared to a regular transaction.
Let’s try to high up the gas manually to 100000 :
Transaction failed : check it out!
Let’s try to high up manually to 120000
Let’s high up to 200000 :
It appears that an average gas unit of 200,000 is suitable for the instantiate action, while a simpler transaction requires only around 65,000 gas units. This suggests that each specific action requires a different average gas amount, which makes sense as more complex requests would require more computing power compared to simpler ones.
In summary, there are three possible solutions to accurately determine the right amount of gas for a transaction:
- Conduct a large quantity of the same action and calculate the average of GasUsed to determine the right Gas adjustment to enter on the request. The formula for this solution could be:
Gas adjustment = (((Sum(GasUsed n….n + 1))/n) – GasWanted)*100/GasWanted)+1
- Calculate the RefundGas. In this case, as long as the gasWanted is higher than the GasUsed, the transaction will be executed and the extra gas will be refunded to the sender of the request.
- Use the Aviaone API service and execute a simulation of a transaction similar to the one you want to execute to see the estimated fees.