To understand the gas fee, reading this is enough

54a6e0c52b0a9cd780110b2c2417d366.png

Q: What is gas?

Gas refers to the "fuel" required to perform operations on Ethereum.

Ethereum provides a virtual machine (EVM, Ethereum Virtual Machine) on which developers can develop various applications. The advantage of EVM over BTC is that it is "Turing complete", but this brings a potential risk that a program may run endlessly, which is intolerable for EVM.

So, running the program takes gas. It's like driving a car that costs gas or electricity. When the gas or electricity runs out, the car will naturally stop.

Q: Are gas, gas price, and gas fee the same thing?

Great, you can notice this. Gasoline and gas fees are obviously not the same, right.

You use the EVM to execute a transaction, which requires a certain amount of gas, which is called the gas amount. Similar to liters of oil, degrees of electricity.

And every gas costs money, and gas has a price, called gasprice. This is similar to the price of a liter of gasoline, the price of one kilowatt of electricity.

The gas fee is the number of gas multiplied by the gas price.

For example, if you want to deploy a contract, you need 3,000,000 gas, and the gasprice is 200gwei. Then the money you have to spend is:

3 * 10 ^ 6 * 200 wei = 3 * 10 ^ 6 * 200 * 10 ^ (- 9) = 0.6 ETH

The measurement unit of gasprice is: gwei, one gwei is 1g wei, that is, 10^9 wei.

Yuu 1 wei = 10 ^ (-18) ETH, Reason: 1 gwei = 10 ^ 9 wei = 10 ^ (-9) ETH.

wei is the smallest unit of ETH, named after Wei Dai, the creator of b-money.

gwei is pronounced ge wei (sound: everyone, read faster: expensive).

Q: Who determines the gasprice?

The price of gas is not as we imagined, it is fixed by the government, No!

Nor is it priced by miners.

The price of gas was specified by the sender of the transaction. Before the London upgrade, the sender had to specify two values ​​in the transaction, a yes gaslimitand a yes gasprice.

Q: Why is the gasprice of Ethereum so expensive?

Because of the popularity of Ethereum, many people want to execute transactions on it. Whoever has a higher price, whose transaction is more likely to be executed and included in the block by miners, miners obviously like higher gas fees.

Therefore, this is more like an auction. People who want to trade give various gasprices, and miners give priority to those with high bids.

Q: So, what is gaslimit?

The more complex the operation, the more gas it needs to consume. Transaction senders sometimes don’t know how much gas they need to spend to perform operations, so they need to add a gas consumption limit to avoid accidentally spending their money. (this could happen without this mechanism)

The sender sets a gaslimit, and if this amount is not spent, the remaining value will be returned.

If the gaslimit is exhausted and the transaction has not been executed, the EVM will throw an exception, end the code execution, and roll back the changes. However, since the miners have already worked and spent the cost, the gas that has been spent is not refundable.

Therefore, the gas limit should be higher than too low, because it doesn't matter if it is higher, it will be returned if it is not spent, and if it is lower out of gas, not only the operation you want is not completed, but also the gas consumed will not be refunded to you. It can be said that the chickens fly the eggs.

An example:

张三向李四转移1 ETH(也即ether的transfer操作)。

张三将gaslimit设为3万,gasprice设为200 gwei。

以太坊规定,transfer操作花费21000个gas,所以实际发生总费用是:

21,000 * 200 = 4,200,000 gwei,即0.0042 ETH。

这样,张三发送1.006 ETH,李四获得1 ETH,矿工获得0.0042 ETH,然后退还张三0.0018 ETH。

张三虽然将gaslimit设为3万,但实际只花了2.1万个gas,实际支出1.0042 ETH。

但如果张三将gaslimit设置为1万,这个操作就无法完成,而且10000个gas也没了,白白损失10000*200=200万gwei=0.002 ETH。

Q: How is the gas number calculated?

The specific calculation is a bit complicated, but there are standards to check. You can check it out at evm-opcodes 1 and DynamicgasCosts 2 on GitHub .

In EVM, every operation, operation and storage requires gas, such as:

  • ADD: Addition operation 3gas

  • MUL: Multiplication operation 5gas

  • SUB: Subtraction operation 3gas

  • DIV: division operation 5gas

  • JUMP: Jump operation 8gas

  • MSTORE: 3gas for content storage operations

  • MLOAD: Content read operation 3gas

  • CREATE: create contract 32000gas (if tx.to == null)

  • SSTORE: Store 20000gas in the storage area (set from 0 to a non-zero value)

  • SHA3:Keccak256哈希 30gas + 6gas * (size of input in words) + mem_expansion_cost

  • Basic transaction fee: 21000gas (for example, Transfer will be so much)

Q: Is there any convenient way for me to calculate the gaslimit?

1. Your wallet will do the math for you, and your development tools will do the math for you.

2. Web3 has two built-in functions that can be used. web3.eth.getgasprice can provide suggestions for setting gasprice, and estimategas can estimate the gas that a function (with parameters) needs to spend.

3. If you want to see how much gas a contract's function calls cost, you can go to a website like etherscan.io to see how much gas (gas Used by Transaction) has actually taken place for such a transaction.

Q: How do I specify these two values ​​in a specific transaction?

Generally speaking, a wallet or development tool will do both of these things for you. (No labor required, you can set it up yourself)

For example, in Little Fox, there are three options for the user to choose: high, medium, and low.

You don't need to specify the gaslimit and gasprice yourself, you just need to choose high, medium, and low at most.

The meaning of high school and low is as follows:

450c57f996a4e48ead940f3e48dd7848.pngHow to choose the level of transaction fees

Basically, if you want your transaction to execute quickly, choose a high fee, and if you are not in a hurry, choose a low fee.

Now let's show roughly how different choices make a difference.

This is a Transfer transaction, in the case of medium fees, like this:

7313611014843ca16fcbcabf1c80bc2d.png

If you choose a high fee, Little Fox will automatically calculate and provide the following:

bee3f3ba475915bcf137599cf4f549fd.png

If Low Fee is selected, it is as follows:

0ea472ca403abf2fd4d8af5aa782f032.png

The "fuel limit" here is the gaslimit I just mentioned, but the gasprice I just mentioned does not appear, but the highest priority fee (max priority fee) and the highest fee (max fee). This is because after the London upgrade , there are new rules for gas fees, which will be introduced below.

Q: I heard that not only transactions have gaslimits, but blocks also have gaslimits?

Yes, each block has an upper limit of gas (that is, the total gas of all transactions in the block). When this upper limit is reached, a block must be generated, and new transactions cannot be included in the block.

And this upper limit is voted by miners.

Now (after the London upgrade) the upper limit is 30M (30 million gas). Note that it is not 30M storage space, but 30M gas.

The total amount of gas contained in a block can be called the block size.

Note: This article was first published on February 7, 2022

Q: What changes did the London upgrade bring?

After the London upgrade (Ethereum completed the London upgrade on August 5, 2021), the block size is flexible, the target size is 15M, and the upper limit is 30M.

After the London upgrade, the total gas fee calculation is still: gaslimit * gasprice.

But gasprice is divided into two parts:

gasprice =baseFee+ Tip

Among them basefee( 基本费用) is automatically calculated by the protocol according to the block size, but this part will be burned and the miners will not get it.

Miners can only get Tip. Tip is a tip, or a written point, called 优先费用(PriorityFee).

When the block is larger than 15M, the gas price will increase (by automatically adjusting the basefee), the purpose is to discourage more transactions, so that the block size will shrink back to 15M; conversely, when the block is smaller than 15M, Gasprice will be cheaper and transactions will increase, so that the block size will always fluctuate around the target size.

The London upgrade (especially burning basefee) brings many benefits, and I won't list them all here. If you are interested, you can read these two articles: Analysis of EIP-1559 3 , All you need to know about EIP-1559 4 .

Here's an example of how gas fees are calculated after the London upgrade:

张三给李四转1个ETH。

gaslimit为21,000,basefee为100 gwei,Tip为10 gwei,可以计算得出gas费为21,000 * (100 + 10) = 2,310,000 gwei,即0.00231 ETH。

当张三汇款时,1.00231 ETH将从张三的账户中扣除。李四将得到1个ETH。基本费用为0.0021 ETH会被烧掉,矿工获得0.00021 ETH的Tip。

Q: How are these values ​​set?

Of course, these are also set up by wallets or development tools for users.

The basefee does not need to be set (neither the wallet), it is automatically calculated by Ethereum through the algorithm based on the size of the previous block and the previous basefee.

In order to allow users to better control their own money, in actual transactions, maxfee(the maximum gasprice they are willing to give) and maxpriorityfee(the maximum tip they are willing to give) are set.

maxfee must be greater than basefee, then miners will calculate Tip according to the following algorithm:

Tip = min(maxpriorityfee, maxfee - basefee)

If maxfee > basefee + Tip, the excess fee will be returned to the transaction sender by the miner.

for example:

张三给李四转1个ETH。

gaslimit为21,000,maxfee为150 gwei,maxpriorityfee设为10 gwei。

basefee为100 gwei,所以,Tip= min( 10,150-100) = 10 gwei,可以计算得出gas费为21,000 * (100 + 10) = 2,310,000 gwei,即0.00231 ETH。

当张三汇款时,21000 * 150 = 1.00315 ETH将从张三的账户中扣除。李四将得到1个ETH。基本费用为0.0021 ETH会被烧掉,矿工获得0.00021 ETH的Tip,矿工退回张三0.00084 ETH(即21000 * 40 gwei)。

Note: The maxfe, basefee, and Tip mentioned above are all for per gas. When calculating the real cost, you need to multiply the gas number.

The symbols you see on different occasions, some are with pergas, some are not, pay attention to distinguish them, generally refer to pergas.

Another point, if maxfee and maxpriorityfee are set to the same value, maxfee is equivalent to the previous gasprice.

Q: So, how exactly is basefee calculated automatically?

The basefee is calculated automatically by Ethereum's protocol, which compares the size of the previous block to 目标大小(15M). If the target block size is exceeded, the base fee for the next block will increase in proportion to the target block, up to a maximum of 12.5%.

For example, a block is 30M, reaching the maximum limit, its size is twice the target size, and its basefee is 112.5% ​​of the previous basefee.

If the block size is always kept at 30M, each basefee is 12.5% ​​higher than the previous one, which produces an exponential growth. When the basefee is so high that people are reluctant to trade, the block size will naturally fall back.

In EIP-1559 5 , the specific details of this algorithm are described (it looks complicated, but it is not difficult to understand) :

Note: // is integer division, round down.
BASE_FEE_MAX_CHANGE_DENOMINATOR = 8
if parent_gas_used == parent_gas_target:
            expected_base_fee_per_gas = parent_base_fee_per_gas
elif parent_gas_used > parent_gas_target:
            gas_used_delta = parent_gas_used - parent_gas_target
            base_fee_per_gas_delta = max(parent_base_fee_per_gas * gas_used_delta // parent_gas_target // BASE_FEE_MAX_CHANGE_DENOMINATOR, 1)
            expected_base_fee_per_gas = parent_base_fee_per_gas + base_fee_per_gas_delta
else:
            gas_used_delta = parent_gas_target - parent_gas_used
            base_fee_per_gas_delta = parent_base_fee_per_gas * gas_used_delta // parent_gas_target // BASE_FEE_MAX_CHANGE_DENOMINATOR
            expected_base_fee_per_gas = parent_base_fee_per_gas - base_fee_per_gas_delta

Q: Will these values ​​be reflected in the transaction message?

Yes, here is an example 6 of a transaction :

{
  from: "0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8",
  to: "0xac03bb73b6a9e108530aff4df5077c2b3d481e5a",
  gaslimit: "21000",
  maxFeePergas: "300",
  maxPriorityFeePergas: "10",
  nonce: "0",
  value: "10000000000"
}

See something related to gas in it.

In the traditional transaction message, the user directly specifies the gasprice; in the EIP-1559 transaction message above, the user specifies MaxFeePerGas and MaxPriorityFeePerGas.

The actual unit price you pay per gas is:

min( MaxPriorityFeePerGas + basefee, MaxFeePerGas )

It’s worth noting that users will still be able to send transactions using the traditional transaction format for the foreseeable future, but this format may eventually be deprecated at the protocol layer.

Q: Finally, can you give me some more study materials?

If you want to see authentic popular science, you can see this 7 .

If you love watching videos, check out this 8 , it's a great talk.

If you want to track gas prices, you can check it out on Etherscan's gastracker 9 .

If you are interested in the details, you can read EIP-1559.

Text|Wei Jianvan


  1. https://github.com/wolflo/evm-opcodes 

  2. https://github.com/wolflo/evm-opcodes/blob/main/gas.md 

  3. Analysis of EIP-1559(https://insights.deribit.com/market-research/analysis-of-eip-1559/)  

  4. All you need to know about EIP-1559(https://uncommoncore.co/eip-1559/) 

  5. eip-1559.md(https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1559.md) 

  6. https://ethereum.org/en/developers/docs/transactions/ 

  7. GAS AND FEES(https://ethereum.org/en/developers/docs/gas) 

  8. https://youtu.be/MGemhK9t44Q 

  9. https://etherscan.io/gastracker

Guess you like

Origin blog.csdn.net/vigor2323/article/details/122817104