Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a demo script to listen the events #220

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to convert this dependency to CORD?

I wouldn't want our community members to have confusion on when to use which package. As a dependency they will always use the @cord.network/sdk package, which internally would have dependency on polkadot etc.

So let the demo scripts also include dependency on @cord.network/sdk itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been addressed - 7c2b974


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