Listening for Events
Off-chain subscriptions.
Consuming Events Off-Chain
Events are useless until something listens for them. Off-chain apps - web front-ends, bots, and indexers - subscribe to logs to react to on-chain activity.
Libraries like ethers.js and web3.js make this straightforward.
Listening with ethers.js
With ethers.js you attach a listener using contract.on(eventName, callback). The callback receives the decoded parameters plus the raw event object.
<code>contract.on('Transfer', (from, to, amount, event) => {
console.log(from, '->', to, amount.toString());
});</code>All lessons in this course
- Declaring Events
- Indexed Parameters
- Listening for Events
- Events vs Storage