Skip to content

Commit

Permalink
Add a demo script to listen the events
Browse files Browse the repository at this point in the history
Signed-off-by: Adi Bhagavath <[email protected]>
  • Loading branch information
adi-a11y committed May 15, 2024
1 parent 7b2d45d commit 8b0a40f
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions demo/src/listen-events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ApiPromise, WsProvider } from '@polkadot/api'

async function main() {
const networkAddress = process.env.NETWORK_ADDRESS
? process.env.NETWORK_ADDRESS
: 'ws://127.0.0.1:9944'
const provider = new WsProvider(networkAddress)
const api = await ApiPromise.create({ provider })

// 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)
})

0 comments on commit 8b0a40f

Please sign in to comment.