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

การประกาศเหตุการณ์

ส่งบันทึกขึ้นเชน

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

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

What Are Events?

Events let a smart contract write entries to the blockchain's log. These logs are a cheap way to record that something happened and are the primary channel for contracts to communicate with off-chain applications.

Front-ends, indexers, and analytics tools all listen for events.

Declaring an Event

You declare an event with the event keyword and a list of typed parameters. The declaration defines the event's shape.

<code>event Transfer(address from, address to, uint256 amount);</code>

Emitting an Event

To fire an event you use the emit keyword followed by the event name and arguments. This appends a log entry to the transaction receipt.

<code>function send(address to, uint256 amount) public {
    // ... transfer logic
    emit Transfer(msg.sender, to, amount);
}</code>

Why Use Events?

Events solve two problems:

  • Cheap record-keeping - logs cost far less gas than storage
  • Off-chain communication - dApps cannot read storage in real time, but they can subscribe to events

Contracts themselves cannot read their own emitted events, however.

A Complete Example

Here is a small contract that emits an event whenever a value is updated, so any listening app knows the change immediately.

<code>contract Counter {
    uint256 public count;
    event Incremented(uint256 newCount);

    function increment() public {
        count += 1;
        emit Incremented(count);
    }
}</code>

Logs Are Not Storage

Event data is stored in a special log area of the transaction receipt, not in contract storage. This is why they are cheap, but it also means the contract cannot query past events on-chain.

Logs are accessible to off-chain clients and block explorers.

Naming and Conventions

By convention events are named with PascalCase and often describe a past action: Transfer, Approval, Deposit, OwnershipTransferred.

Standard interfaces like ERC-20 define required events you must emit.

<code>event Approval(address owner, address spender, uint256 value);
event Deposit(address account, uint256 amount);</code>

Multiple Parameters

Events can carry several parameters of any type, including string and bytes. The data is ABI-encoded into the log.

<code>event OrderPlaced(uint256 id, address buyer, string product, uint256 price);

function place(uint256 id, string memory p, uint256 price) public {
    emit OrderPlaced(id, msg.sender, p, price);
}</code>

Gas Cost of Events

Emitting an event costs gas, but much less than writing the same data to storage. The base cost plus a small per-byte fee makes events the preferred way to expose historical data.

Rule of thumb: store what the contract needs to read, emit what off-chain apps need to read.

Events in the ABI

Event declarations appear in the contract's ABI so that client libraries know how to decode the logs. The event signature is hashed to create a topic identifier.

<code>// ABI entry (simplified)
// { type: 'event', name: 'Transfer',
//   inputs: [from, to, amount] }</code>

Events for State Changes

A best practice is to emit an event for every meaningful state change. This gives applications a reliable, ordered history they can replay to rebuild state.

<code>address public owner;
event OwnerChanged(address previous, address next);

function setOwner(address next) public {
    emit OwnerChanged(owner, next);
    owner = next;
}</code>

Quick Check

Test your understanding of declaring events.

Recap

You learned how to declare and emit events:

  • Declare with event Name(types...)
  • Fire with emit Name(args...)
  • Logs are cheap, off-chain readable, and not queryable on-chain
  • Emit an event for every meaningful state change

Events are the bridge between your contract and the outside world.

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

บทเรียน “การประกาศเหตุการณ์” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การประกาศเหตุการณ์”

ส่งบันทึกขึ้นเชน คุณปฏิบัติ Web3 & DApp Development Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

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

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

บทเรียน “การประกาศเหตุการณ์” ใช้เวลานานแค่ไหน

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

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

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

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

  1. การประกาศเหตุการณ์
  2. พารามิเตอร์ที่จัดทำดัชนี
  3. การรับฟังเหตุการณ์
  4. เหตุการณ์เทียบกับพื้นที่จัดเก็บ
← กลับไปที่ Web3 & DApp Development Fundamentals