Declaring Events
Emit on-chain logs.
Declaring Events is a free Web3 & DApp Development Fundamentals lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Web3 & DApp Development Fundamentals learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Frequently asked questions
Is the “Declaring Events” lesson free?
Yes — the full text of “Declaring Events” is free to read here on the web, and the Web3 & DApp Development Fundamentals course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Web3 & DApp Development Fundamentals course, upgrade to CoddyKit PRO.
What will I learn in “Declaring Events”?
Emit on-chain logs. You practise Web3 & DApp Development Fundamentals with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Web3 & DApp Development Fundamentals?
No prior experience is required. Web3 & DApp Development Fundamentals on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Declaring Events” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Web3 & DApp Development Fundamentals lesson?
Yes. Every Web3 & DApp Development Fundamentals lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Declaring Events
- Indexed Parameters
- Listening for Events
- Events vs Storage