Ethereum transaction fee calculation

The Chinese translation of Gas is: gas, gasoline, which represents a flammable gas. This is a vivid metaphor for the transaction fee calculation model of Ethereum. Unlike Bitcoin, which directly pays Bitcoin as a transfer fee, Ethereum is regarded as a decentralized computing network. When you send Token, execute a contract, and transfer Ethereum Or when doing other things on this block, the computer needs to perform calculations and consume network resources when processing this transaction, so you have to pay a fuel fee to buy fuel to let the computer work for you. The final gas fee is paid to the miners as a handling fee, regardless of whether the transaction is successful or not.

Reference article

What is gas? Why do transactions take so long?
Ethereum technology and implementation of Gas

Classic example

The example of calculating Gas during transfer given in the Ethereum white paper is as follows. Assume that the contract storage is empty at the beginning, and a transaction with a gas price of 10 ETH, 2000 Gas, and 0.001 Ethereum is sent, and there are two data fields: [2 , 'CHARLIE']:

  1. Verify transaction validity and format : Check whether the transaction is valid and in the correct format. This includes verifying the structure of the transaction, its signature, and whether the sender has sufficient funds to cover the maximum possible gas fee.
  2. Check sender's funds : Make sure the transaction sender has at least 2000 * 0.001 = 2 ETH in their account. If so, then 2 Ether is deducted from the sender's account as the maximum possible gas fee.注意:这是从交易发送者的钱包里扣除的,不是从10 ETH的交易金额中扣除的
  3. Initializing Gas and deducting byte fees : Initializing Gas is 2000. Assume that the transaction length is 170 bytes and the cost per byte is 5, so 850 (170*5) is deducted, leaving 1150 Gas.
  4. Process the transaction amount : deduct another 10 Ether from the sender's account and add it to the contract account. This is the transfer amount specified in the transaction.
  5. Execute code : run the contract code. In this example, the operation is relatively simple: check whether index 2 of the contract storage has been used, and find that it has not been used, so set the storage value of index 2 to 'CHARLIE'. Assume that this process consumes 187 Gas, and the remaining Gas is 1150 - 187 = 963.
  6. Refund remaining Gas : Convert unused Gas back into Ether and return it to the sender. In this example, the refund is 963 * 0.001 = 0.963 ETH. Finally, the status after transaction execution is returned.
  7. Calculate the total amount deducted : ETH sent (10 ETH) + gas fee actually consumed (1.037 ETH) = total amount ultimately reduced in the user account (11.037 ETH).

Practical examples

The following are some coins that I worked hard to mine on the test network using an old ThinkPad computer. You can also mine and try the
GoerliETH Faucet address.
Insert image description here

Now we transfer 0.001ETH from Account1 to Account2, and the estimated gas fee is 0.0000315ETH. We will calculate it directly for you.
In other words, the estimated total amount of our transaction = 0.001 ETH + 0.0000315 ETH = 0.0010315 ETH
Insert image description here

After waiting for a while, after the transfer is confirmed, we click View on block explorer to go to the web page. We can also search for the transaction ID
on the Goerli Testnet Explorer website.
Insert image description here

You can see the entire transaction process from the following interface
Transaction Fee = Gas Used * Gas ​​Price = 21,000 Gas * 1.500000007 Gwei = 31500.000147 Gwei
注意:21000 Gas 是标准转账交易的gasUsed。因此一笔标准的转账交易你可以设置 gasLimit 为21000,多退少也不能补,因为少了的话就打水漂了。还有这个大小和你转账多少ETH没有关系,转0.001用的是21000Gas,转账1000ETH也是21000Gas,如果想要提高转账的速度,可以通过提高Gas价格(Gas Price)来实现。
Insert image description here

Conversion unit

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_41656968/article/details/135314005