forked from ansonla3/kusama-validator-stats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index-polkadot.js
87 lines (73 loc) · 3.64 KB
/
index-polkadot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const{ ApiPromise, WsProvider } = require('@polkadot/api')
const { isHex } = require('@polkadot/util')
const DOT_DECIMAL_PLACES = 10000000000;
let lowest = "no one";
let highest = "no one";
let highestAmount = NaN;
let lowestAmount = NaN;
let highestCommission = "no one";
let lowestCommission = "no one";
let highestCommissionAmount = NaN;
let lowestCommissionAmount = NaN;
(async () => {
const provider = new WsProvider('wss://rpc.polkadot.io')
const api = await ApiPromise.create({ provider })
const [ currentValidators, totalIssuance, currentEra ] = await Promise.all([
api.query.session.validators(),
api.query.balances.totalIssuance(),
api.query.staking.currentEra(),
]);
const totalKSM = parseInt(totalIssuance.toString())
const totalBondingStake = await api.query.staking.erasTotalStake(currentEra.toString())
for (let i=0; i<currentValidators.length; i++) {
const validatorStake = await api.query.staking.erasStakers(currentEra.toString(), currentValidators[i])
const validatorComissionRate = await api.query.staking.erasValidatorPrefs(currentEra.toString(), currentValidators[i])
const validatorTotalStake = validatorStake['total'].toString() / DOT_DECIMAL_PLACES
const validatorOwnStake = validatorStake['own'].toString() / DOT_DECIMAL_PLACES
const validatorNominators = validatorStake['others'].toJSON()
check(currentValidators[i].toString(), parseInt(validatorTotalStake), parseInt(validatorComissionRate['commission'].toString()))
console.log(`Stash Address: ${currentValidators[i].toString()}.\n\tTotal stake: ${validatorTotalStake}\n\tSelf stake: ${validatorOwnStake}`)
for (let j=0; j<validatorNominators.length; j++) {
console.log(`\tAddress: ${validatorNominators[j].who}, Stake: ${validatorNominators[j].value / DOT_DECIMAL_PLACES}`)
}
console.log(`\tCommission: ${validatorComissionRate['commission'].toString() / 10000000}%`)
console.log('\tNominators:', validatorNominators.length)
}
console.log()
console.log("\nSummary Data:")
console.log(`Total DOT: ${totalKSM / DOT_DECIMAL_PLACES}`)
console.log(`Bonding Stake: ${totalBondingStake.toString() / DOT_DECIMAL_PLACES} KSM`)
console.log(`Staking Rate: ${totalBondingStake.toString() / totalKSM * 100} %`)
console.log(`Highest-staked validator: ${highest} : ${highestAmount} KSM`)
console.log(`Lowest-staked validator: ${lowest} : ${lowestAmount} KSM`)
console.log(`Highest commission validator: ${highestCommission} : ${highestCommissionAmount / 10000000}%`)
console.log(`Lowest commission validator: ${lowestCommission} : ${lowestCommissionAmount / 10000000}%`)
process.exit()
})()
const check = (currentValidator, stake, commission) => {
if (isNaN(highestAmount)) {
// If highest_amount is NaN, this must be the
// first. Set this validator to highest and lowest everything.
lowest = highest = currentValidator
lowestAmount = highestAmount = stake
lowestCommission = highestCommission = currentValidator
lowestCommissionAmount = highestCommissionAmount = commission
} else {
// Check total stake
if (stake > highestAmount) {
highest = currentValidator
highestAmount = stake
} else if (stake < lowestAmount) {
lowest = currentValidator
lowestAmount = stake
}
// Check commissions
if (commission > highestCommissionAmount) {
highestCommission = currentValidator
highestCommissionAmount = commission
} else if (commission < lowestCommissionAmount) {
lowestCommission = currentValidator
lowestCommissionAmount = commission
}
}
}