Intermediate4 min readNov 4, 2026

Events, Errors and Gas-Efficient Patterns

Gas-efficient strategies for logging events and using custom errors in Solidity.

Logs and revert data are the primary channels a contract has to interact with the outside world. Because events are not stored in contract state, they are significantly cheaper than storage-based logging. Mastery of these patterns is essential for building indexable, gas-efficient protocols that can communicate effectively with frontend interfaces.

Custom errors further optimize the EVM by providing a standardized, cheap way to surface failure reasons without the overhead of strings. This article breaks down the gas arithmetic behind LOG opcodes and explores how to structure events for maximal searchability by off-chain indexers like The Graph.

Logs are not storage

Logs are stored in the transaction receipt, not the contract state. This is a critical distinction because contract code cannot read its own events. When an event is emitted, it is recorded in the block receipt, which indexers scan to rebuild the state of the blockchain for applications to query.

Because logs are not accessible within the EVM, they are perfect for tracking historical activity without inflating the gas cost of future state updates. This architectural separation ensures that read-heavy applications don't burden the critical path of execution. However, it also means developers must be careful to include all necessary data in the log at the moment of emission.

The cost structure of logs is deterministic. The base cost is 375 gas, with an additional 375 gas per indexed topic, and 8 gas per byte of data. By moving data from storage to logs, developers can save thousands of gas per transaction, improving the user experience and the feasibility of complex logic.

Declaring and indexing events

Events are defined using the event keyword and can include indexed parameters. An indexed parameter is hashed and stored in the topic slots of the log, allowing for efficient filtering. Ethereum supports up to four topics for non-anonymous events; one topic is always reserved for the signature hash of the event definition.

Limiting indexed parameters to three provides the best balance between searchability and cost. If you define more than three indexed parameters, you hit the EVM limit for topics. Anonymous events can bypass the signature hash requirement, freeing up a slot for an additional indexed parameter, but this makes it harder for frontends to filter them properly.

Developers should prioritize indexing variables that users frequently query, such as user addresses or transaction IDs. Non-indexed parameters should contain large datasets or metadata that do not need to be used as filter criteria. This strategy ensures the event logs remain readable by standard off-chain tooling while keeping gas costs predictable and low.

Custom errors and revert data

Custom errors, introduced in Solidity 0.8.4, are a major improvement over the older string-based require statements. A custom error encodes a 4-byte selector followed by the arguments, which is much cheaper to store and transmit. This approach replaces the long string buffers that used to bloat the revert data.

Standard require strings consume significant memory and gas due to the need to store the error message in the bytecode. Custom errors are essentially lightweight ABI-encoded tuples. When a transaction reverts, the frontend can decode the 4-byte selector to display a specific, localized error message to the user, improving the overall UX.

Beyond gas efficiency, custom errors clarify the intent of the failure. Distinguishing between Unauthorized and InsufficientBalance allows for much cleaner error handling logic in dApps. By using the revert keyword with custom errors, developers can provide actionable feedback, helping users understand exactly why a transaction failed without consuming excess gas during the revert process.

The gas arithmetic

Understanding the gas cost of logs is essential for writing optimized contracts. Every event incurs a base fee plus charges for indexed and non-indexed bytes. Anonymous events save 375 gas by omitting the topic0 hash, but they force indexers to rely on the event definition rather than the hash for identification.

Efficiency Category | Gas Cost Impact | Best Practice | ------------------ | --------------- | ------------- | Indexed Topic | +375 Gas | Index only keys | Data Byte | +8 Gas | Minimize large data | Anonymous Event | -375 Gas | Use for logs only | Revert String | High | Replace with errors

Comparing traditional strings with custom errors is stark. A 32-character string might cost over 1,000 gas, whereas a custom error with two parameters might cost less than 100 gas in overhead. As a developer, shifting toward these optimized primitives is one of the easiest ways to improve the gas profile of your contract.

Patterns that hold up at scale

At scale, the goal is to minimize state storage and maximize data availability via logs. By emitting granular events, you enable robust indexers to reconstruct your contract's history. This pattern is the backbone of successful DeFi protocols, where external monitors need to track position updates, liquidations, and fee distributions across millions of transactions.

Contracts should use a mix of custom errors for logic control and events for observability. Do not overload events with too much data, as this increases the receipt size significantly, which can lead to issues with nodes archiving the blockchain history. Balance is key; keep events lean and ensure they remain compliant with the standard ABI encoding.

Finally, avoid using events for control flow logic. Events should be treated as output, not input. If a contract needs data from a past event, the architecture must change to store that data in state or pass it as an argument. Designing with this separation of concerns is the hallmark of professional-grade smart contract architecture.

Code

Efficient Error and Logging
event Transfer(address indexed from, address indexed to, uint256 amount); error InsufficientBalance(uint256 available, uint256 required); function withdraw(uint256 amount) external { if (balance < amount) revert InsufficientBalance(balance, amount); emit Transfer(msg.sender, address(0), amount); }

References

  1. [1]Solidity events— Solidity documentation
  2. [2]Solidity errors and revert— Solidity documentation

Something wrong or missing?

Editors review every submission. If a source is broken or a claim is wrong, tell us here.

Keep reading

More in Solidity