The concept of Gas/ Gas Price/ Gas Limit in smart contract development

Many friends contacted the development of Ethereum smart contracts for the first time, but they couldn't figure out what Gas is, let alone Gas Price and Gas Limit. This article will introduce and clarify the relationship between these three in smart contract development one by one. I believe you will understand these three gas-related concepts after reading it.

Gas

Gas corresponds to the actual operation steps of the Ethereum Virtual Machine (EVM) in a transaction. Simpler smart contract transactions, such as simple ether transfer contract transactions, require fewer computing steps and require less gas. Conversely, if some complex operations are to be calculated, the consumption of Gas will be large. Therefore, the transaction in the smart contract you submit requires the EVM to perform more calculations, and the required gas consumption is higher.

Gas Price

Gas Price is how much Eth you are willing to pay for a unit of Gas, generally using Gwei as the unit. Therefore, the higher the Gas Price, the more Eth will be paid for each operation step in the smart contract transaction.

You may be unfamiliar with the unit Gwei. Gwei is actually 10 ^ -9 Eth, which means 1 Gwei = 0.000000001 Eth. So, when you set Gas price = 20 Gwei , it means that you are willing to pay 0.00000002 Eth for a single step operation.

Having said that, if you are smart, you will realize that the formula for calculating fees in the development of Ethereum smart contracts is very simple:

交易手续费(Tx Fee) = 实际运行步数(Actual Gas Used) * 单步价格(Gas Price)

For example, the transaction in your smart contract requires Ethereum to perform 50 steps to complete the operation. Assuming that the Gas Price you set is 2 Gwei, then the transaction fee in the entire smart contract development is 50 * 2 = 100 Gwei.

Gas Limit

Gas Limit is the upper limit of Gas available in a transaction, that is, how many steps will be performed in your transaction at most. Due to the different complexity of transactions, the exact gas consumption will not be known until the transaction is completed, so before you submit the transaction, you need to set a gas consumption limit for the transaction.

If the transaction you submitted has not been completed, and the gas consumption has exceeded the Gas Limit you set, then the transaction will be cancelled, and the transaction fee that has been consumed will also be deducted-because you want to reward those who have paid the labor. miner. If the transaction has been completed and the gas consumed has not reached the Gas Limit, then the transaction service fee will only be charged based on the actual gas consumed. In other words, the highest possible service fee for a transaction is Gas Limit * Gas​​​ Price.

Finally, it is worth mentioning that the higher the Gas Price, the faster the transaction you submit will be accepted by the miners. But usually people are reluctant to pay more fees, so how much gas price should be set to ensure that transactions are confirmed on the blockchain within normal time (eg 10 mins)? This website can help you. As of this writing, 1 Gwei of Gas Price ensures that transactions are accepted in around 50 seconds.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325929031&siteId=291194637