//event-listener.js
import dotenv from 'dotenv';
dotenv.config();
import { ethers } from "ethers";
// Load environment variables
const RPC_URL = process.env.PEAQ_RPC_URL || "https://quicknode.peaq.xyz"; // Replace with your preferred peaq RPC URL
const CONTRACT_ADDRESS = process.env.CONTRACT_ADDRESS; // Replace with your contract address
import CONTRACT_ABI from "./contract_abi.json"; // Replace with the path to your contract ABI JSON file
// Initialize provider and contract
const provider = new ethers.JsonRpcProvider(RPC_URL);
const contract = new ethers.Contract(CONTRACT_ADDRESS, CONTRACT_ABI, provider);
// Function to listen for events
const listenForEvents = () => {
console.log("Listening for events on contract:", CONTRACT_ADDRESS);
// Replace "YourEventName" with the event name from your contract ABI
contract.on("YourEventName", (param1, param2, event) => {
console.log("Event Received:");
console.log(`Param 1: ${param1}`);
console.log(`Param 2: ${param2}`);
console.log("Raw Event Data:", event);
});
// Add a generic log listener (optional)
provider.on("logs", (log) => {
try {
const parsedLog = contract.interface.parseLog(log);
console.log("Parsed Event Log:");
console.log(parsedLog);
} catch (error) {
// Not a relevant event, ignore
}
});
};
// Error handling and script execution
const main = async () => {
try {
listenForEvents();
} catch (error) {
console.error("Error in event listener:", error);
process.exit(1);
}
};
main();