Prerequisites
- A smart contract deployed on the peaq network that emits events.
- Node.js, with ethers.js installed.
- A peaq network RPC endpoint, for example https://quicknode1.peaq.xyz.
- The address of the deployed contract and its ABI.
Instructions
1. Set Up Your Project
Install ethers.js:2. Initialize Connection to the peaq Network
Connect to the peaq network using ethers.js:3. Load the Smart Contract
To interact with the smart contract, load its address and ABI:4. Listen for Events
Using ethers.js, you can set up listeners for specific events emitted by the contract:5. Parse Events
To handle every log the contract emits, subscribe to the contract address and decode each log yourself.parseLog returns null for a log that does not match the ABI:
6. Filter Specific Events
To reduce overhead, use filters to listen for specific events:7. Testing the Listener
Trigger events in your smart contract (e.g., using a wallet or script) and ensure the listener captures them correctly. Example: Emit an event in your smart contract:Putting it all Together
Tips
-
Batch Processing: Combine historical event fetching (
getLogs) with real-time listeners so you do not lose events emitted while your listener was down. -
Indexed Parameters: Mark event parameters as indexed so you can filter by their values.
Then filter by the address:
-
Environment Variables: Keep RPC URLs and contract addresses in a
.envfile rather than in the source.

