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.
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.