Here's an example of how I managed to get this information using the @polkadot
JavaScript libraries.
- Import the Polkadot JS libraries
yarn add @polkadot/api @polkadot/util
var { ApiPromise, WsProvider } = require('@polkadot/api');
var util = require('@polkadot/util');
- Initialize the WebSocket provider
Here we use the publicly provided WebSocket RPC endpoint from polkadot.io (wss://rpc.polkadot.io
).
const provider = new WsProvider("wss://rpc.polkadot.io", false);
provider.connect();
const api = await ApiPromise.create({ provider });
// ...
provider.disconnect()
- Get staking/unlocking information starting from current era
// ...
let currentEra = await api.query.staking.currentEra()
let storageKey = ''
let total = util.BN_ZERO
while (true) {
let stakingEntries = await api.query.staking.ledger.entriesPaged({ args: [], pageSize: 100, startKey: storageKey })
if (stakingEntries.length == 0) {
break
}
for (entry of stakingEntries) {
storageKey = entry[0]
for (item of entry[1].unwrap().unlocking) {
if (item.era.toNumber() >= currentEra) {
total = total.add(item.value.toBn())
}
}
}
}
The full program looks like this:
var { ApiPromise, WsProvider } = require('@polkadot/api')
var util = require('@polkadot/util')
async function main() {
const provider = new WsProvider('wss://rpc.polkadot.io', false)
provider.connect()
const api = await ApiPromise.create({ provider })
let currentEra = await api.query.staking.currentEra()
let storageKey = ''
let total = util.BN_ZERO
while (true) {
let stakingEntries = await api.query.staking.ledger.entriesPaged({ args: [], pageSize: 100, startKey: storageKey })
if (stakingEntries.length == 0) {
break
}
for (entry of stakingEntries) {
storageKey = entry[0]
for (item of entry[1].unwrap().unlocking) {
if (item.era.toNumber() >= currentEra) {
total = total.add(item.value.toBn())
}
}
}
console.log('Processed entries:', stakingEntries.length)
console.log('Storage key:', storageKey.toString())
}
console.log('From era:', currentEra.toString())
console.log('Total unstaking:', total.toString().substring(0, total.toString().length-10), 'DOT')
provider.disconnect()
}
main().catch(console.error).finally(() => process.exit())
The final output looks like this:
From era: 658
Total unstaking: 33881435 DOT