7

Using either a Rust or JavaScript client library I would like to get the amount of DOT that is currently being unbonded on the relay chain.

Some staking information is available in the Polkadot JS interface:

Staking information

But it does not include any information about the amount of DOT that is being unstaked (i.e. DOT that is currently in the 28 day unbonding period).

1 Answer 1

5

Here's an example of how I managed to get this information using the @polkadot JavaScript libraries.

  1. Import the Polkadot JS libraries
yarn add @polkadot/api @polkadot/util
var { ApiPromise, WsProvider } = require('@polkadot/api');
var util = require('@polkadot/util');
  1. 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()
  1. 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
1
  • P.S. I'm not sure If I've done everything correctly in the script but the result seems to make sense.
    – Rtsne42
    Commented Mar 22, 2022 at 7: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.