docs ⦿ dedot 🧑‍💻
TypinkXTelegramGithub
dedot 🧑‍💻
dedot 🧑‍💻
  • Welcome to Dedot
  • Why Dedot?
  • Getting started
    • Installation
    • Connect to network
    • @polkadot/api -> dedot
    • Packages structure
  • Clients & Providers
    • Providers
    • Clients
  • Client API
    • ChainApi
    • Constants
    • Runtime APIs
    • Storage Queries
    • Transactions
    • Events
    • Errors
  • ink! Smart Contracts
    • Introduction
    • Generate Types & APIs
    • Deploy contracts
    • Queries
    • Transactions
    • Storage
    • Events
    • Handle errors
    • Utilities
  • CLI
  • Keyring & Signer
  • Runtime upgrades
  • Type system
  • Utilities
    • HexString
    • Uint8Array (U8a)
    • String
    • Hash functions
    • Address
    • BigInt & number
    • Balances
    • Merkleized Metadata
  • Help & FAQ
    • Tutorials
      • Develop ink! dApp using Typink
    • Built with Dedot
    • Forum Posts
    • Telegram
    • Github
    • API Reference
Powered by GitBook
On this page
  • Working with on-chain events
  • Event API

Was this helpful?

Edit on GitHub
  1. Client API

Events

PreviousTransactionsNextErrors

Last updated 1 month ago

Was this helpful?

Events for each pallet emit during runtime operations and are defined in the medata. Available events are also exposed in ChainApi interface so we can get information of an event through syntax client.events.<pallet>.<eventName>. E.g: Events for Polkadot network can be found , similarly for other network as well.

This client.events is helpful when we want quickly check if an event matches with an event that we're expecting in a list of events, the API also comes with type narrowing for the matched event, so event name & related data of the event are fully typed.

Working with on-chain events

Example to list new accounts created in each block:

// ...
const ss58Prefix = client.consts.system.ss58Prefix;
await client.query.system.events(async (records) => {
  const newAccountEvents = client.events.system.NewAccount.filter(records); // or find, is

  console.log(newAccountEvents.length, 'account(s) was created in block', await client.query.system.number());

  newAccountEvents.forEach((event, index) => {
    console.log(`New Account ${index + 1}:`, event.palletEvent.data.account.address(ss58Prefix));
  });
});
// ...

Event API

is

Check for a specific on-chain event or event record.

const records = await client.query.system.events();

const instantiatedEvent = records.map(({ event }) => event)
                                .find(client.events.contracts.Instantiated.is); // narrow down the type for type suggestions
                                
// OR
const instantiatedEventRecord = records.find(client.events.contracts.Instantiated.is);

find

Find an on-chain event from a list of system event records.

const records = await client.query.system.events();

const instantiatedEvent = client.events.contracts.Instantiated.find(records);

filter

Return a list of a specific on-chain event from a list of system event records.

const records = await client.query.system.events();

const instantiatedEvents = client.events.contracts.Instantiated.filter(records);

watch

const unsub = await client.events.system.NewAccount.watch((events) => {
  console.log('New Account Created', events)
})
here