Skill 1.3.4

Query past events and subscribe to new events

Key Concepts

contract.getPastEvents("Transfer", { filter: { from: addr }, fromBlock, toBlock }) retrieves historical logs with eth_getLogs and decodes their arguments into returnValues. Filters work on indexed parameters, which are stored as log topics; non-indexed parameters are stored in the log's data and must be filtered in the application. Node providers limit the block range of a single query, so large histories are read in pages.

contract.events.Transfer() creates a subscription that delivers each new matching event as it is mined, through the data event handler. Subscriptions need a WebSocket or IPC provider. Applications should be ready for chain reorganisations near the head of the chain, which can remove recently delivered events; web3.js marks such logs with removed: true.