Events & Logs
Contracts can't push notifications to the outside world, and the outside world can't cheaply search through storage. Events solve both problems: a contract emits an event, the EVM records it as a log in the transaction receipt, and wallets, websites and indexers listen for those logs.
event Transfer(address indexed from, address indexed to, uint256 value);
emit Transfer(msg.sender, recipient, amount);Logs are much cheaper than storage, but contracts cannot read logs, not even their own. They're a one-way channel to off-chain software.
Emitting events
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Auction {
event BidPlaced(address indexed bidder, uint256 amount);
event NewLeader(address indexed bidder, uint256 previousHighest);
address public leader;
uint256 public highest;
function bid(address bidder, uint256 amount) external {
require(amount > highest, "bid too low");
emit BidPlaced(bidder, amount);
emit NewLeader(bidder, highest);
leader = bidder;
highest = amount;
}
}
contract Main {
function main() external {
Auction auction = new Auction();
auction.bid(address(0xA11CE), 100);
auction.bid(address(0xB0B), 150);
console.log("highest bid:", auction.highest());
}
}highest bid: 150
event BidPlaced(bidder: 0x00000000000000000000000000000000000A11cE, amount: 100)
event NewLeader(bidder: 0x00000000000000000000000000000000000A11cE, previousHighest: 0)
event BidPlaced(bidder: 0x0000000000000000000000000000000000000B0b, amount: 150)
event NewLeader(bidder: 0x0000000000000000000000000000000000000B0b, previousHighest: 100)
The playground prints console.log lines while the code runs and lists every event of the transaction afterwards, in the order they were emitted, with each parameter's name and value.
Indexed parameters
Each log has up to four topics (32-byte search keys) plus a data section. The first topic is the event's signature hash; parameters marked indexed (at most three) become additional topics. Clients can filter on topics efficiently: "all Transfer events to my address".
| Indexed | Not indexed | |
|---|---|---|
| Stored in | topics | data |
| Filterable | yes | no |
Dynamic types (string, arrays) |
only their hash is stored | the full value is stored |
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract Main {
event Registered(string indexed name, string displayName, uint256 id);
function main() external {
emit Registered("alice", "alice", 1);
}
}event Registered(name: 0x9c0257114eb9399a2985f8e75dad7600c5d89fe3824ffa99ec1c3eb8bf3b0501, displayName: alice, id: 1)
The indexed name appears as keccak256("alice"), not as text. If you need both filtering and the readable value, pass it twice (once indexed, once not), as above.
Listening from JavaScript
A front end uses a library such as ethers.js to subscribe to events or to query past ones:
// not runnable here: needs a provider connected to a real or local chain
const auction = new ethers.Contract(address, abi, provider);
auction.on("BidPlaced", (bidder, amount) => {
console.log(`${bidder} bid ${ethers.formatEther(amount)} ETH`);
});
const filter = auction.filters.BidPlaced(myAddress); // indexed parameter filter
const myBids = await auction.queryFilter(filter, 0, "latest");Events record history
A typical design stores only what the contract itself needs (the current leader and highest bid) and emits events for everything else (every bid). Off-chain indexers reconstruct the full history from the logs. Events also make a contract auditable: important state changes, especially admin actions, should always emit an event.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Settings {
event FeeChanged(uint256 oldFee, uint256 newFee, address indexed changedBy);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
address public owner = msg.sender;
uint256 public fee = 100;
function setFee(uint256 newFee) external {
require(msg.sender == owner, "not owner");
emit FeeChanged(fee, newFee, msg.sender);
fee = newFee;
}
function transferOwnership(address newOwner) external {
require(msg.sender == owner, "not owner");
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
}
contract Main {
function main() external {
Settings s = new Settings();
s.setFee(250);
s.transferOwnership(address(0xC0FFEE));
console.log("fee:", s.fee());
}
}fee: 250
event FeeChanged(oldFee: 100, newFee: 250, changedBy: 0x5FbDB2315678afecb367f032d93F642f64180aa3)
event OwnershipTransferred(previousOwner: 0x5FbDB2315678afecb367f032d93F642f64180aa3, newOwner: 0x0000000000000000000000000000000000C0FFEE)
Events disappear with a revert
If the transaction reverts, its logs are discarded together with every other change. Events therefore describe only things that really happened:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract Main {
event Step(uint256 n);
function main() external {
emit Step(1);
emit Step(2);
revert("changed my mind");
}
}main() failed: reverted with reason: changed my mind
Test yourself
Can a contract read the events it emitted earlier?
- Yes, with
logs() - Yes, but only its own
- No, logs are only accessible off-chain
- Only in view functions
What is stored in a topic for an indexed string parameter?
- The full string
- The first 32 characters
- The keccak256 hash of the string
- Nothing
How many parameters of a (non-anonymous) event can be indexed?
- 1
- 3
- 4
- Unlimited