-
-
Save TomaszWaszczyk/2b6fd9847adc1731db1aaa34764dcfe2 to your computer and use it in GitHub Desktop.
unlocking data on substrate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var { ApiPromise, WsProvider } = require('@polkadot/api'); | |
var { BN_ZERO } = require('@polkadot/util'); | |
async function main() { | |
const provider = new WsProvider("wss://kusama-rpc.polkadot.io/", false); | |
provider.connect(); | |
const api = await ApiPromise.create({ provider }); | |
// Get general information about the node we are connected to | |
const [chain, nodeName, nodeVersion] = await Promise.all([ | |
api.rpc.system.chain(), | |
api.rpc.system.name(), | |
api.rpc.system.version() | |
]); | |
console.log( | |
`Connected to ${chain} using ${nodeName} v${nodeVersion}` | |
); | |
let result = await api.query.staking.ledger.entries(); | |
let total = {}; | |
for (ledger of result) { | |
let data = ledger[1].unwrap().unlocking; | |
for (chunk of data) { | |
let era = chunk.era.toNumber(); | |
let value = chunk.value.toBn(); | |
if (!(era in total)) { | |
total[era] = BN_ZERO; | |
} | |
total[era] = total[era].add(value); | |
} | |
} | |
for (const [era, value] of Object.entries(total)) { | |
console.log(`${era}: ${toFixed(value)}`) | |
} | |
provider.disconnect() | |
} | |
function toFixed(input) { | |
let total_string = input.toString(); | |
let fixed_point = 12; | |
let insert_point = total_string.length - fixed_point; | |
var total_fixed = total_string.substring(0, insert_point) + "." + total_string.substring(insert_point); | |
return total_fixed; | |
} | |
main().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment