Ethereum : Qu'est-ce que le gas (ou gaz) dépensé pendant une transaction ?
Gas is a concept that is unique to the Ethereum network. It is a kind of fuel (gas in English) that it is necessary to spend to carry out transactions on the blockchain. Where some cryptocurrencies charge fees in their native token, Ethereum has made the choice to use this system.
Another definition: The gas is a virtual unit inEthereum allowing to organize the payment of transaction fees. By extension, the same concept is used in many smart contract platforms. You can track the price of an Ethereum transaction on our dedicated Ethereum price page.
If you have already made a transfer on Ethereum, you may have noticed the presence of parameters to be modified such as thegaslimit or thegasprice, without understanding what the link was with the fees paid in ethers(ETH). In this article, we explain everything about how the gas system works.
Ethereum Ethereum: The gas for trading on the ETH network
As you may know, Ethereum is similar to a decentralized global computer for running programs called smart contracts. It is based on a virtual machine, called the Ethereum Virtual Machine or EVM, which runs simultaneously on each of the nodes in a peer-to-peer network.
Thus, each of the computers connected to the Ethereum network performs all the operations processed by the system. The transactions are included in the transactions. In order to prevent denial of service attacks, these operations require a fee, which is paid through the gas system.
Gas is a unit in Ethereum that acts as an intermediary between the user who makes a transaction and the validator (miner) who confirms that transaction. The gas is entirely virtual and no one can hold or exchange it: the user pays the fees in ethers (ETH) and the validator also recovers these fees in ethers.
The gas is “purchased” (in ethers) at the time of the transaction validation, and then it is “consumed” as fuel for the virtual machine. The validator who includes the transaction in his block recovers the ether used to buy the gas.
Each transaction in Ethereum has a fixed cost in gas. This is calculated on the basis of the computational resource required for the other operations.
A transfer of ether (ETH) between two accounts thus always consumes 21,000 gases. On the execution side of smart contracts, each operation has its own cost, for example:
Add two whole numbers.
Multiply two whole numbers.
Performs hashing of an information by the Keccak-256 function
30 + 6 × (size of information in multiples of 256)
Gets the balance of a given account.
If you want to know the gas cost of each operation, there are lists like this one.
The gas makes sure to avoid infinite loops and recursions in Ethereum. This is why it is sometimes said that the EVM is almost Turing-complete.
When a contract is executed by the Ethereum virtual machine, it consumes gas. When it runs out of gas, it stops working.
There is therefore a parameter in the transaction, the gas limit, which corresponds to the maximum quantity of gas that the user is prepared to use.
Since contracts can call each other, it is often very difficult to know in advance how much gas a transaction will consume: it must be estimated.
Three situations may arise:
The user indicates a limit that is too low. The execution is done but cannot go through, so the transaction is invalidated but the fees are kept by the validator.
The user correctly estimates the gas cost and indicates an equal limit. This happens regularly when transferring ether between two accounts: there is no point in indicating a gas limit other than 21,000 since the cost is fixed.
The user reports a gas limit that is too high. In this case, everything goes as planned and the unused gas is refunded (in ETH) to the user. This is why it is advisable, when you don’t know how much gas your transaction will consume, to set a high limit.
The gas price is the second parameter to specify when sending a transaction. As the name suggests, this is the price in ethers at which the user buys the gas.
The price of gas is usually expressed in gigaweis (Gwei). The wei is to Ethereum what the satoshi is to Bitcoin: it is the smallest subunit of ether, being equal to one attoether or 10-18 ETH. One Gwei is one billionth of an ether. In summary:
The price of gas increases the fees paid by a transaction, and therefore the priority with which it will be processed by validators (miners). Indeed, since a block is limited to a certain amount of gas consumed (12.5 million gas in July 2020), it cannot accommodate all pending transactions, and validators select the most profitable transactions first.
The fees paid are calculated by the formula :
cost (Gwei) = gas cost (gas consumed) × gas price (Gwei)
For example, for a typical transaction (21,000 gas consumed) and a gas price of 30 Gwei, the fee will be 630,000 Gweis i.e. 0.00063 ETH, or 17 euro cents at the current price (July 2020).
First, as a system that requires transactions to be paid for, it limits denial-of-service attacks: in order to paralyze the network with a large number of transactions, an attacker would have to spend a lot of money. Furthermore, by doing so, he would raise the overall level of charges, and would not be able to sustain his attack over a long period of time.
Secondly, gas allows the costs of running the Ethereum network to be decoupled from the price of ether. Unlike the system of transaction fees on Bitcoin which are measured in relation to the weight of a transaction (in satoshis per virtual byte), the Ethereum model is to give a fixed cost to each transaction so as to have as realistic an estimate as possible of the processing done by the nodes.
Third, thanks to the gas limit, this model allows to have a virtual machine that can run general purpose programs while stopping infinite loops.
Although the calculation is very often done automatically by the portfolios, users sometimes have to change the gas limit and gas price parameters themselves.
Ethereum Ethereum : Calculate the right gas limit for a transaction
For an ether transfer, as mentioned, the gas cost is 21,000. So you don’t have to change the gas limit: in fact, even if you specify a limit of, say, 50,000, the 29,000 unconsumed gas is refunded to you.
For a transfer of ERC-20 tokens, the gas cost can vary from 25,000 to 150,000 gas. Portfolios usually adjust this setting automatically, so you don’t have to worry about it. If it happens that this is not the case, do not hesitate to indicate a high gas limit: the amount not consumed will be returned to you.
For an interaction with a smart contract, such as one managing an ICO for example, the cost in gas is not always known in advance. Be sure to ask the developers of the smart contract who usually indicate this in their official releases.
⚠ There is no point in increasing the gas limit to make your transaction go through faster. The parameter to be changed for this is the gas price (see below).
Ethereum Ethereum : Estimate the price of gas correctly
The gas price (measured in Gwei) is how the user changes the level of fees paid by his transaction, and therefore the priority with which it will be processed by the network. The more you are in a hurry, the more you will have to increase this setting to make your transaction go before the others. This can be crucial during popular ICOs that only last a short time, such as the Basic Attention Token in May 2017.
Often, your wallet estimates the amount of fees directly, and can offer you alternatives so that you choose the amount that suits your usage. Metamask offers three fee options: slow, average and fast.
If you want to calculate the optimal price yourself, there is a website that gives an estimate of the waiting time according to the price level: ETH Gas Station. On the main page of the site, you will have three price indications:
Safe Low: the minimum number of Gwei for the transaction to be confirmed in less than 30 minutes.
Standard: the minimum number of Gwei for the transaction to be confirmed in less than 5 minutes.
Fast: the minimum number of Gwei for the transaction to be confirmed in less than 2 minutes.
In any case, the price you quote will depend on your preference: do you want to save on transaction fees even if you have to wait longer, or do you want your transaction to be confirmed very quickly even if you pay a lot of fees?
Ethereum : Small parenthesis, the gas of the Neo network
Before concluding, it should be noted that another smart contract platform, called Neo, also uses the term in a different sense. The difference is that Neo’s gas (GAS) is what ether is to Ethereum: the platform’s central unit of account that has a market price and pays fees and rewards validators.
On the platform, there is a second token that intervenes in the consensus: it is the NEO. This is a kind of action, allowing to select validators by proof of stake and giving dividends in GAS to each NEO token holder.
We hope this short article on the Ethereum network gas has helped you learn more about the subject. As you can see, it’s not very complicated to understand.
Today, when you perform transactions, a gas amount and its associated price in Gwei are inserted by default. Therefore, most of the time, you won’t really have to ask yourself questions about the amounts to be inserted.
However, you may change these values as you wish. This can be useful when you need your payment to be sent as soon as possible, or on the contrary if you want to save on fees in case you are not in a hurry.