1

As described, I would like to construct a listener to be alerted when a new referenda has just started. Currently, I am using mostly Subscan API.

Thanks.

1 Answer 1

2

With PolkadotJS you can be listening the events as it is explained here in the docs:

const { ApiPromise, WsProvider } = require('@polkadot/api');

async function main () {
  const wsProvider = new WsProvider('ws://127.0.0.1:9900');
  const api = await ApiPromise.create({ provider: wsProvider });

  // Subscribe to system events via storage
  api.query.system.events((events) => {
    console.log(`\nReceived ${events.length} events:`);

    // Loop through the Vec<EventRecord>
    events.forEach((record) => {
      // Extract the phase, event and the event types
      const { event, phase } = record;
      const types = event.typeDef;
      console.log(event.toHuman());
    });
  });
}

main().catch((error) => {
  console.error(error);
  process.exit(-1);
});

When a referendum begins you query the event Started.

If you want to get when a proposal is created you query the event Proposed.

See here all the rest of events from the Governance Pallet.

The event you will get from the code above will looks like this:

{
  method: 'Proposed',
  section: 'democracy',
  index: '0x0d00',
  data: { proposalIndex: '2', deposit: '3,333,333,300' }
}

In Kusama we have already deployed the new protocol for governance OpenGov. In this case we have to take a look in the events form the Referenda pallet.

For your specific question we have to query the event DecisionStarted that is triggered when a referendum has moved into the deciding phase.

2
  • 1
    Thanks for the answer! This is useful to listen to referenda on Polkadot (Gov1.0), but what about referenda created on Kusama (OpenGov)? What pallet/event is the relevant in that case? Note that this will be the case for Polkadot soon too.
    – Iker
    Commented Jan 25, 2023 at 12:37
  • thanks for updating the answer with the complete info :)
    – Iker
    Commented Jan 30, 2023 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.