Skip to main content
Contracts emit events during execution, and those events are written to the chain as logs. You can capture and parse them to build responsive dApps, monitor activity, or trigger off-chain workflows. This guide shows how to listen to and parse smart contract events on the peaq network with ethers.js.

Prerequisites

  1. A smart contract deployed on the peaq network that emits events.
  2. Node.js, with ethers.js installed.
  3. A peaq network RPC endpoint, for example https://quicknode1.peaq.xyz.
  4. 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:
Expected console output:

Putting it all Together

Tips

  1. Batch Processing: Combine historical event fetching (getLogs) with real-time listeners so you do not lose events emitted while your listener was down.
  2. Indexed Parameters: Mark event parameters as indexed so you can filter by their values.
    Then filter by the address:
  3. Environment Variables: Keep RPC URLs and contract addresses in a .env file rather than in the source.