0Pricing
Web3 & DApp Development Fundamentals · บทเรียน

กลไก EVM และแก๊ส

ทำความเข้าใจ Ethereum Virtual Machine (EVM) ในฐานะสภาพแวดล้อมขณะทำงานของสัญญาอัจฉริยะ และแนวคิดเรื่องแก๊สสำหรับค่าธรรมเนียมธุรกรรม

กลไก EVM และแก๊ส เป็นบทเรียน Web3 & DApp Development Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 3 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web3 & DApp Development Fundamentals และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 3 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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!

คำถามที่พบบ่อย

บทเรียน “กลไก EVM และแก๊ส” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “กลไก EVM และแก๊ส” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web3 & DApp Development Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 3 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “กลไก EVM และแก๊ส”

ทำความเข้าใจ Ethereum Virtual Machine (EVM) ในฐานะสภาพแวดล้อมขณะทำงานของสัญญาอัจฉริยะ และแนวคิดเรื่องแก๊สสำหรับค่าธรรมเนียมธุรกรรม คุณปฏิบัติ Web3 & DApp Development Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web3 & DApp Development Fundamentals หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web3 & DApp Development Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 3 บทเรียน

บทเรียน “กลไก EVM และแก๊ส” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Web3 & DApp Development Fundamentals นี้ได้ไหม

ได้ บทเรียน Web3 & DApp Development Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. บล็อกเชน Ethereum
  2. สัญญาอัจฉริยะคืออะไร
  3. กลไก EVM และแก๊ส
← กลับไปที่ Web3 & DApp Development Fundamentals