Skip to content

Instantly share code, notes, and snippets.

@kianenigma
Created November 2, 2021 11:56
Show Gist options
  • Save kianenigma/8f0e3e2bdbb859aed78482c514010c6a to your computer and use it in GitHub Desktop.
Save kianenigma/8f0e3e2bdbb859aed78482c514010c6a to your computer and use it in GitHub Desktop.
Simple script to see the status of an account in bags-list. Just paste this in Polkadot JS App's Javascript page.
// This must be a STASH account.
const yourAccount = "xxx";
// This is your node in the bags list. Use `node.prev` and `.next` to see where you are located.
const node = await api.query.bagsList.listNodes(yourAccount);
// This is the bag to which you belong.
const bagUpper = api.createType("Balance", node.toJSON().bagUpper);
// This is the list of all the bags.
const bagThresholds = api.consts.bagsList.bagThresholds.map((x) => api.createType('Balance', x));
//
// This function will always indicate that for an account with stake (aka, weight) `w`, to which bag they belong to.
const notionalBagFor = (w) => bagThresholds.find((t) => t.gt(w)) || api.createType('Balance', new BN("18446744073709551615"))
const ctrl = (await api.query.staking.bonded(yourAccount)).unwrap();
const currentWeight = api.createType('Balance', (await api.query.staking.ledger(ctrl)).unwrapOrDefault().active);
// now we can check if you need a rebag or not?
console.log(`current bag: ${bagUpper.toHuman()}, correct bag: ${notionalBagFor(currentWeight).toHuman()}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment