Loading...
Contract storage is expensive and hard to query. Events are cheap, indexed, and how frontends (and The Graph) read on-chain activity.
solidityevent Transfer(address indexed from, address indexed to, uint256 value); function transfer(address to, uint256 value) public returns (bool) { balances[msg.sender] -= value; balances[to] += value; emit Transfer(msg.sender, to, value); // log to blockchain's event index return true; }
Up to 3 params per event can be indexed. Indexed params go into a special log bloom filter — clients can filter by them efficiently:
typescriptconst filter = token.filters.Transfer(null, myAddress); // indexed `to` filter const logs = await token.queryFilter(filter, fromBlock, toBlock);
Non-indexed params are stored as ABI-encoded data. Still readable, just not filterable.
Every ERC-20 token emits Transfer on every balance change. Want to know the top 100 holders of a token? Scan Transfer events, aggregate. No contract modification needed.
Add a Deposited event to a vault contract that emits on every deposit.