βœ… Master Event Logs & Real-Time dApps

Emit events, use indexed parameters, and listen for blockchain updates

←
Previous Section
Event Listening

πŸŽ“ Module Complete: Key Takeaways

πŸŽ‰ Congratulations!

You've mastered blockchain event logs! You now understand how to emit, filter, and listen to eventsβ€”essential skills for building reactive dApps.

πŸ“š What You've Learned

πŸ“‹

Event Fundamentals

  • β€’Logs stored in transaction receipts, not contract storage
  • β€’50x cheaper than storage (375 gas vs 20,000)
  • β€’Accessible off-chain only, not by other contracts
πŸ”

Event Structure

  • β€’Topic 0: Event signature (keccak256 hash)
  • β€’Topics 1-3: Up to 3 indexed parameters
  • β€’Data field: Unlimited non-indexed parameters
🏷️

Indexed Parameters

  • β€’Enable efficient filtering via Bloom filters
  • β€’Max 3 indexed params per event
  • β€’Use for addresses, IDs, and filter criteria
πŸ‘‚

Event Listening

  • β€’WebSocket listeners for real-time updates
  • β€’Polling with getLogs for batch processing
  • β€’The Graph for complex historical queries
⚑

Best Practices

  • β€’Index searchable params (addresses, IDs)
  • β€’Always remove listeners on cleanup
  • β€’Handle WebSocket reconnections gracefully
🎯

Use Cases

  • β€’Real-time dApp UI updates
  • β€’Transaction notifications
  • β€’Analytics and monitoring dashboards

🎯 Interactive: Event Logs Knowledge Quiz

Test your understanding with 5 questions about blockchain events and logging.

πŸ› οΈ Quick Reference Guide

Event Declaration

event Transfer(
  address indexed from,
  address indexed to,
  uint256 amount
);

Event Emission

emit Transfer(
  msg.sender,
  recipient,
  amount
);

Event Listening (ethers.js)

contract.on("Transfer",
  (from, to, amount) => {
    console.log(from, to);
  }
);

Event Filtering

const filter = contract
  .filters.Transfer(
    null,
    myAddress
  );

πŸ’‘ Real-World Applications

πŸ’Ό
DeFi Protocols

Track swaps, liquidity adds/removes, yield farming rewards in real-time.

🎨
NFT Marketplaces

Monitor mints, transfers, sales, and listings with instant UI updates.

πŸ—³οΈ
DAO Governance

Log votes, proposals, executions for transparency and auditability.

πŸ‘›
Wallet Applications

Notify users of incoming transactions, approvals, and balance changes.

πŸš€ Next Steps

1️⃣
Build a dApp with events: Create a simple token contract and build a React frontend that listens for Transfer events in real-time.
2️⃣
Explore The Graph: Deploy a subgraph to index your contract events and query historical data with GraphQL.
3️⃣
Study production contracts: Examine how Uniswap, Aave, and other protocols use events for monitoring and analytics.
4️⃣
Implement error handling: Build robust event listeners with reconnection logic and proper error handling.
πŸ“‹

Event Logs Mastered!

You now have the knowledge to emit efficient events, filter them strategically, and build reactive dApps that respond to blockchain activity in real-time.

"Events are the nervous system of the blockchain, carrying signals from smart contracts to the outside world."