声明事件
在链上发出日志
声明事件 是 CoddyKit 上的免费 Web3 & DApp Development Fundamentals 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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 导师)并解锁 Web3 & DApp Development Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。
「声明事件」这节课中我会学到什么?
在链上发出日志 你通过在浏览器中直接运行的动手代码来练习 Web3 & DApp Development Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Web3 & DApp Development Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Web3 & DApp Development Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「声明事件」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Web3 & DApp Development Fundamentals 课中编写并运行代码吗?
能。每节 Web3 & DApp Development Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。