Mécanismes de l’EVM et du gas
Comprenez l’EVM, l’environnement d’exécution des contrats intelligents, ainsi que le concept de gas utilisé pour payer les frais de transaction.
Mécanismes de l’EVM et du gas est une leçon Web3 & DApp Development Fundamentals gratuite sur CoddyKit. Ceci est la leçon 3 sur 3. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Web3 & DApp Development Fundamentals, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Web3 & DApp Development Fundamentals comprend 3 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
The Brain of Ethereum
Ethereum isn't just a ledger; it's a decentralized computer! This "computer" is called the Ethereum Virtual Machine (EVM).
Think of the EVM as a global, single-instance computer that runs all smart contracts and updates the state of the blockchain.
Every node in the Ethereum network runs the EVM, ensuring everyone agrees on the exact state of the network.
A Deterministic State Machine
The EVM is a deterministic state machine. This means that given the same starting state and the same transaction, it will always produce the same output state.
It processes transactions, executes smart contract code, and updates the blockchain's state (account balances, contract storage, etc.) accordingly.
This determinism is crucial for a decentralized network, as it allows all participants to independently verify the blockchain's integrity.
EVM Opcodes: Machine Language
Smart contracts, written in languages like Solidity, are compiled into EVM bytecode. This bytecode consists of low-level instructions called opcodes.
Each opcode performs a specific task, such as adding two numbers, pushing data onto the stack, or storing data in memory.
The EVM executes these opcodes sequentially, processing the logic defined by the smart contract.
Why Do We Need Gas?
Running computations on a global, decentralized computer isn't free. This is where Gas comes in.
Gas is a unit of computational effort required to perform operations on the Ethereum network.
It's used to:
- Prevent infinite loops in smart contracts.
- Compensate miners/validators for their computational resources.
- Prioritize transactions based on the fee offered.
Gas Limit: Your Transaction's Budget
When you send a transaction, you specify a Gas Limit. This is the maximum amount of gas you're willing to spend on that transaction.
It acts as a safety mechanism. If the transaction requires more gas than the limit, it will revert (fail), but you still pay for the gas used up to that point.
A standard Ether transfer typically costs 21,000 gas units. Smart contract interactions can cost much more depending on their complexity.
Gas Price: Cost Per Unit
While Gas Limit is the quantity, Gas Price is the cost per unit of gas. It's typically denominated in Gwei.
1 Gwei = 0.000000001 Ether. So, 1 Ether = 1,000,000,000 Gwei.
You can set a higher Gas Price to incentivize miners/validators to include your transaction in a block sooner, especially during network congestion.
Calculating Transaction Fees
The total transaction fee you pay is calculated as:
Gas Units Used * Gas Price
For example, if a transaction uses 21,000 gas units and the gas price is 50 Gwei:
- Fee = 21,000 * 50 Gwei = 1,050,000 Gwei
- Which is 0.00105 Ether
Remember, you only pay for the gas actually used, up to your Gas Limit.
The 'Out of Gas' Error
If your transaction runs out of gas before completing its execution, it will result in an "Out of Gas" error.
When this happens:
- All state changes made by the transaction are reverted.
- The transaction fails and is not recorded on the blockchain (except as a failed transaction).
- Crucially, you still pay the transaction fee for all the gas consumed up to the point of failure.
Gas Refunds: A Small Reward
Ethereum offers gas refunds for certain operations that reduce the blockchain's state size.
For instance, if a smart contract deletes data from storage, a portion of the gas spent on that deletion is refunded.
This incentivizes developers to write efficient contracts that don't unnecessarily bloat the blockchain's state.
Quick Check: EVM & Gas
Test your understanding of the Ethereum Virtual Machine and Gas mechanics.
EVM & Gas: The Essentials
In this lesson, we explored the core mechanics of the Ethereum network:
- The Ethereum Virtual Machine (EVM) is the global, deterministic computer executing smart contracts.
- Gas is the unit of computational effort required for transactions.
- Gas Limit sets the maximum gas a transaction can consume.
- Gas Price (in Gwei) determines the cost per unit of gas.
- Total transaction fees are
Gas Used * Gas Price. - Running "out of gas" reverts the transaction but still consumes the spent gas.
Understanding these concepts is vital for developing efficient and cost-effective DApps!
Apprends Web3 & DApp Development Fundamentals avec un tuteur IA — gratuit
Écris et exécute du vrai code dans ton navigateur, obtiens de l'aide instantanée d'un tuteur IA disponible 24h/24, et reprends là où tu t'es arrêté sur le web ou dans l'app.
- Cours
- 29
- Leçons
- 105
Questions Fréquemment Posées
La leçon « Mécanismes de l’EVM et du gas » est-elle gratuite ?
Oui — le texte complet de « Mécanismes de l’EVM et du gas » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Web3 & DApp Development Fundamentals, passe à CoddyKit PRO. Le cours Web3 & DApp Development Fundamentals comprend 3 leçons au total.
Qu'est-ce que j'apprendrai dans « Mécanismes de l’EVM et du gas » ?
Comprenez l’EVM, l’environnement d’exécution des contrats intelligents, ainsi que le concept de gas utilisé pour payer les frais de transaction. Tu pratiques Web3 & DApp Development Fundamentals avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer Web3 & DApp Development Fundamentals ?
Aucune expérience préalable n'est requise. Web3 & DApp Development Fundamentals sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 3 sur 3.
Combien de temps prend la leçon « Mécanismes de l’EVM et du gas » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon Web3 & DApp Development Fundamentals ?
Oui. Chaque leçon Web3 & DApp Development Fundamentals inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- La blockchain Ethereum
- Que sont les contrats intelligents ?
- Mécanismes de l’EVM et du gas