From 96c9705c6c937f40a4050b76fd01e88174285964 Mon Sep 17 00:00:00 2001 From: Adi Date: Wed, 10 Jul 2024 12:18:37 +0530 Subject: [PATCH] Add a demo script to listen the events (#220) Signed-off-by: Adi Bhagavath --- demo/src/listen-events.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 demo/src/listen-events.ts diff --git a/demo/src/listen-events.ts b/demo/src/listen-events.ts new file mode 100644 index 00000000..26f5fba5 --- /dev/null +++ b/demo/src/listen-events.ts @@ -0,0 +1,36 @@ +import * as Cord from "@cord.network/sdk"; + +async function main() { + const networkAddress = process.env.NETWORK_ADDRESS + ? process.env.NETWORK_ADDRESS + : 'ws://127.0.0.1:9944' + // const networkAddress = 'ws://127.0.0.1:9944' + Cord.ConfigService.set({ submitTxResolveOn: Cord.Chain.IS_IN_BLOCK }) + await Cord.connect(networkAddress) + + const api = Cord.ConfigService.get('api') + + // Subscribe to system events + api.query.system.events((events) => { + // Loop through the array of events + events.forEach((record) => { + // Extract the phase, event type, and event data + const { event, phase } = record + const types = event.typeDef + const args = event.data.map((data) => data.toString()) + + // Print the event details + console.log( + `\nEvent: ${event.section}.${event.method} [${phase.toString()}]` + ) + console.log(`\tParameters:`) + args.forEach((arg, index) => { + console.log(`\t\t${types[index].type}: ${arg}`) + }) + }) + }) +} + +main().catch((error) => { + console.error('Error:', error) +})