api: Query Kusama's erasTotalStake and totalIssuance since genesis block until today

Hey there,

my aim is to query the full historic data of api.query.balances.totalIssuance and api.query.staking.erasTotalStake at every day in time for the Kusama network. So the end result should be to have the daily total staking rate since the genesis block.

At the moment I am running an archive node. The problem at the moment is, that I can not query the era of the genesis block. Code Example which should be something like that. Could you help me out?


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

async function main () {
  const api = await ApiPromise.create();

  let blockNumber = 0;
  while (true) {
    const blockHash = await api.rpc.chain.getBlockHash(blockNumber);
    const activeEra = await api.query.staking.activeEra.at(blockHash);  
    if (activeEra.isSome) {
      const era = activeEra.unwrap().index;

      const totalCoins = await api.query.balances.totalIssuance.at(blockHash);
      const stakedCoins = await api.query.staking.erasTotalStake.at(blockHash, era);
      console.log('%s block number, %s staked coins and %s total coins for block hash %s', blockNumber, stakedCoins.toHuman(), totalCoins.toHuman(), blockHash);
    }
    blockNumber++;
  }
}

main().catch(console.error).finally(() => process.exit());
    

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 19 (7 by maintainers)

Most upvoted comments

The required data would be api.query.staking.erasStakers to get the total staked coins with nominators of one validator, but I think this API call is also not available at the genesis block of the kusama network, because it has something to do with era.

However, just queried the metadata at block 1, there is balances.totalIssuance -

image

And it def has a value at block 1 -

image

.validators are only the validators. To get nominators you would need to query the staking.nominators entry (probably with .entriesAt(blockHash)

For the second part, the source of truth is the Substrate codebase, if it was “introduced after launch” there will be some PR that mentions it

So the api.query.session.validators got every nominator included? Where do I can find out how the api.query.balances.totalIssuance moved?

staking.bonded(stashId) to get the controller. Then staking.ledger(controllerId). Just grabbed this on Polkadot for a random validator (Polkadot was the network I had open) -

image

By the way: the exact block number for the first api.query.staking.erasTotalStake is 1491598.