Beginner
Events & Logs — Kudos board build (step 2/6)
Understand how contracts emit data to the chain.
Developer Beginner
7/7 — Events & Logs — Kudos board build (step 2/6)
GoalAdd per-user storage so everyone can leave a personalized kudos.
Cryptography is about how to maintain control over information.
What to write
- Mapping
declaremapping(address => string) private _lastKudos;.mapping(address => string) private _lastKudos;Mappings give O(1) lookups keyed by address and grow only when written.
- Setter
store a message for a recipient.function setKudos(address to, string calldata message) external { _lastKudos[to] = message; }External + calldata keeps the ABI lean and avoids memory copies.
- Getter
read the latest kudos for an address.function lastKudos(address user) external view returns (string memory) { return _lastKudos[user]; }View reads are free off-chain and return a memory copy of stored data.
Why Mappings scale per-user; a focused setter/getter keeps the ABI minimal.
Try it Deploy, call setKudos(to, message), then lastKudos(to) to confirm storage.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract KudosBoard {
mapping(address => string) private _lastKudos;
function setKudos(address to, string calldata message) external {
_lastKudos[to] = message;
}
function lastKudos(address user) external view returns (string memory) {
return _lastKudos[user];
}
}
NextWe’ll keep extending this in the following lesson; this version already deploys and runs.