I need to find my account's rank in the semi sorted bagsList hence I look for an API to do fetch that. why is it SEMIsorted that then needs to a rebag call that cost a fee? why it is not sorted at first place or kind of get sorted in the start of each era, snapshot time or any other suitable time? Thank you
1 Answer
After some more research I found that BagsList is not sorted because it is a Map not an array!
If someone wants to find if its address is included in the current round/era of staking:
const MY_ADDRESS = '....';
const assignments: Map<string, bigint> = new Map();
const currentEra = (await api.query.staking.currentEra()).unwrap();
const stakers = await api.query.staking.erasStakers.entries(currentEra);
stakers.map((x) => x[1].others).flat().forEach((x) => {
const nominator = String(x.who);
const amount = BigInt(x.value);
if (assignments.get(nominator)) {
assignments.set(nominator, amount + (assignments.get(nominator)!))
} else {
assignments.set(nominator, amount)
}
});
console.log('Am I in? :', !!assignments.get(MY_ADDRESS))