Last active
November 7, 2021 09:03
-
-
Save shawntabrizi/b63e233ef1f816391f5869975be619ac 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); |
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://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 last_key = ""; | |
let total = {}; | |
while (true) { | |
console.log("last key: ", last_key.toString()); | |
let result = await api.query.staking.ledger.entriesPaged({ args: [], pageSize: 50, startKey: last_key }); | |
if (result.length == 0) { | |
break; | |
} | |
for (ledger of result) { | |
last_key = ledger[0]; | |
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 = 10; | |
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