Science Corner 09 | Ethereum Gas and Fees
Smart contracts, as the name suggests, refer to contracts that computer codes can automatically execute; the Ethereum virtual machine is used to execute smart contracts; smart accounts are the carriers on which smart contracts can be executed. In other words, the smart account, this "account" can be controlled by the Ethereum virtual machine, based on what control, according to the smart contract.
There is no free lunch in the world. The function of smart contracts is so good that it is not in vain. You also have to pay the miners’ fees when you conduct transactions in Ethereum. Then on the Ethereum system, the fees you have to pay are What's going on, what is the relationship between Gas and handling fees?

1. What is Ethereum Gas?
Gas is an English word, Chinese means: gas, gasoline, this thing is a consumable in daily life. Ethereum Gas is similar to car fuel, and the driving of smart contracts requires Ethereum Gas.
Why does Ethereum produce "fuel"? What does Gas in Ethereum mean? Ethereum's Gas is closely related to transaction fees. Ethereum transactions require handling fees, and this Gas is the calculation model of Ethereum handling fees.
In the setting of Ethereum, the transaction fee is similar to an encrypted fuel, that is, Gas, which can drive the movement of smart contracts. When Ethereum executes transactions on the blockchain, the gas will be gradually consumed according to the rules of the characteristics. From this point of view, Gas is the same as its original intention, just like car fuel. To start a car, you must need fuel.
2. What are the differences between Ethereum Gas and Bitcoin transaction fees?
There is no free lunch in the world. Both Bitcoin and Ethereum require handling fees, but the handling fee models of the two are different.
Bitcoin pays Bitcoin directly as a transfer fee, but Ethereum is not like this:
Ethereum is essentially a virtual machine. This virtual machine is decentralized. It is a "world-class computing network" formed by the joint virtual machines controlled by people from all over the world.
When you send tokens, execute contracts, transfer ether, or do other things on the block, the computer needs to perform calculations when processing the transaction, and this calculation process consumes network resources.
In this way, you have to pay the "gas fee" (that is, Gas) to let the computer work for you and let the miners process transactions for you.
Generally, the higher the gas price that the sender is willing to pay, the greater the value miners get from the transaction, and the more likely the miners will choose the transaction.
In this way, miners are free to choose transactions. In order to set the gas price for the sender as a reference, miners can directly propose the minimum gas price they need to execute the transaction.
3. How to calculate the consumption of Ethereum Gas?
When the Ethereum virtual machine processes transactions, the virtual machine will process them one by one according to the operation instructions determined in the transaction, and each operation instruction has a clearly stipulated Gas consumption.
The Ethereum system stipulates two accounts: one is a normal account and the other is a smart account:
Ordinary transfer transactions, that is, calling "normal accounts", require a fixed Gas of 21,000;
When calling a "smart account", because the complexity of the smart contract is different, the gas required is also different. The more resources (computation, memory, etc.) used to process transactions, the more Gas will be required. For example, performing an addition operation will consume 3 Gas, and more complex operations will consume more Gas.
You may ask a question: When a user's transaction involves a malicious smart contract, this contract is super complicated, and the execution of this contract consumes infinite fuel, what should I do?
The solution of the Ethereum system is: in order to avoid unlimited Gas consumption caused by malicious smart contracts, users need to set the gas limit allowed to be consumed when sending transactions, that is, GasLimit. In this way, even if there are malicious smart contracts, the worst case is only Consumption of fuel within the scope specified by GasLimit.
4. What is the relationship between Ethereum Gas and transaction fees?
On the Ethereum system, the fee you pay is equal to: GasPrice multiplied by GasUsed.
We can understand GasPrice as the unit price of fuel, and GasUsed as how many liters of fuel the car needs
For cars, let's take an example:
If a liter of gasoline is 20 yuan, 10,000 liters of gasoline is 200,000 yuan. For Ethereum, each Gas is 20 GW (GW is the quantitative unit of Ethereum), 10,000 Gas is: 20 multiplied by 10,000, equals 200,000 GW, 20,000 GW equals 0.0002 Ethereum, that is to say, The transaction procedure is 0.0002 Ethereum.
(See the table below for the specific exchange value)

▲ Note: The basic unit of the amount of Ether is "Wei", and the units of Ether are "Wei, Finney, Ether", among which "Ether" is used for ordinary transactions; "Finney" is used for micro-transactions; "Saab" and "Wei" are used to carry out fees and contracts.
From this, we can find that Gas is not Ethereum, it is a separate system, its exchange rate is in a certain proportion with Ethereum, and after proportional exchange, it finally forms a transaction fee.
For specific exchange rate inquiries, you can view the following website: https://jin10086.github.io/etherconVerter/
Both Gas price and Ethereum price are freely regulated by the market, but the two are different. Their differences are:
The price of Ethereum fluctuates according to market conditions,
The price of Gas is determined by the miners. If the gas price is lower than the minimum requirement of the miners, the miners will refuse to process the transaction.
Gas and Ethereum are separated, which protects the system from possible fluctuations with rapid changes in the price of Ethereum.
Most miners will choose interests first. When processing transactions, they will arrange them according to the gas price from high to low, and give priority to those with high gas prices. If you are in a hurry to trade, you need to increase the gas price so that the miners can see you earlier; if you Don't worry, you just need to set a gas price, which is above the bottom line of the gas price set by the miners.
5. How to get Gas?
In fact, Gas is the Ethereum purchased from the miners. The Ethereum in the user's own account can purchase Gas from the miners. The Ethereum client will automatically use Ethereum to purchase Gas according to the specified transaction maximum expenditure limit.
6. Where did Gas go in the end?
For each transaction, the transaction initiator must set the Gas limit and Gas price of the transaction. Different operations will generate different Gas costs. When the Gas is used up, the miners will stop executing, and the used Gas will be rewarded to the mining miners. , which involves several situations:
In the first case, if there is remaining Gas, then the remaining Gas will be refunded to the transaction initiator or smart contract creator. For example, if I send 1 Ethereum to Yiyi, the gas limit I set is 50,000, and the gas that needs to be consumed normally is 21,000, then the remaining 29,000 that has not been consumed will be returned to me.
The second situation is that if the Gaslimit I set is too low, or the Ethereum in my account is not enough to pay for my Gas consumption, then the transaction will be canceled due to insufficient Gas, and the Gas used for calculation will not Back to my account.
The third case is that if the transaction fails, I also have to pay the processing fee for the computing resources already occupied.
7. How to set a reasonable Gas price?
Before each transaction, you can check this website to confirm the Gas price that needs to be set: https://ethGasstation.info/


