Getting the reward points of a validator by account

415 views Asked by At

I am trying to find the reward points belonging to a validator. I started with this:

const activeEra = await api.query.staking.activeEra()

const rewardPoints = await api.query.staking.erasRewardPoints(activeEra.unwrap().index)
const individualRewardPoints = activeEraRewardPoints.individual

Now, it looks like the individualRewardPoints is some kind of map keyed by validator accounts, however I couldn't find how to get the specific item (I don't want to iterate over the map). Having a string, I tried these:

const alice = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'

individualRewardPoints.get(alice)
individualRewardPoints.get(Buffer.from(alice))

This looked promising but still doesn't work:

{ decodeAddress } = require ('@polkadot/keyring')
individualRewardPoints.get(decodeAddress(alice))

All of them return undefined. What's the way to get the reward points of a validator?

1

There are 1 answers

0
zeke On

I bumped into this same challenge and unfortunately I did not find away around iterating through.

eraRewardPoints.individual is a BTreeMap<AccountId, RewardPoint>, so the key should be an AccountId. Under the hood this BTreeMap is represented by a JS Map and in TS we see that it has AccountId as its key type. To create that type we could do api.createType('AccountId', alice). However, for me creating the type and using it to key in did not work. My guess is that it creates a new object that JS does not recognize as the same AccountId object as in the map.

So my solution was:

for (const [id, points] of eraRewardPoints.individual.entries()) {
  if (id.toString() === validatorId) {
    return points;
  }
}

You can find the exact code in substrate-api-sidecar, which has an endpoint (accounts/{accountId}/staking-payouts) that gives the payout for an address using arithmetic done in Rust: https://github.com/paritytech/substrate-api-sidecar/blob/ee0a0488bf8100a42e713fc287f08d72394677b9/src/services/accounts/AccountsStakingPayoutsService.ts#L301