我正在使用Polkadot API来获取网络中活动验证器的列表并编写以下代码:
Open in Polkadot js sandbox https://polkadot.js.org
// All code is wrapped within an async closure,
// allowing access to api, hashing, types, util.
// (async ({ api, hashing, types, util }) => {
// ... any user code is executed here ...
// })();
const [chain, nodeName, nodeVersion] = await Promise.all([
api.rpc.system.chain(),
api.rpc.system.name(),
api.rpc.system.version()
]);
console.log(`Connected to chain ${chain} using ${nodeName} v${nodeVersion}`);
// Retrieve active validators
const [validators] = await Promise.all([
api.query.session.validators()
]);
if (validators && validators.length > 0) {
// Map staking stats to validators
const validatorStaking = await Promise.all(
validators.map(authorityId => api.query.staking.ledger(authorityId))
);
var rank = 1;
validators.forEach((authorityId, index) => {
// Get staking stats for current validator
var validator = JSON.parse(validatorStaking[index])
// Get nominators, then print information
Promise.all([
api.query.staking.stakers(validator.stash)
]).then(nominators => {
return nominators
}).then(function(nominators) {
// Iterate over nominators list
var total = 0
var nominatorsList = ''
nominators[0].others.forEach((nominator, index) => {
total = total + parseInt(nominator.value)
nominatorsList += ' [' + (index + 1) + '] ' + nominator.who + ' => ' + parseFloat(nominator.value / 1000000000000000).toFixed(3) + ' DOT\n'
})
console.log('\n[ ' + rank + ' ] Validator: ' + authorityId.toString())
console.log( ' -> Controller: ' + authorityId.toString() )
console.log( ' -> Stash: ' + validator.stash)
console.log( ' -> Self bonded: ' + parseFloat(validator.total / 1000000000000000).toFixed(3) + ' DOT')
console.log( ' -> Bonded by nominators: ' + parseFloat(total / 1000000000000000).toFixed(3) + ' DOT' )
console.log( ' -> Total Bonded: ' + (parseFloat((total) / 1000000000000000) + parseFloat((validator.total) / 1000000000000000) ).toFixed(3) + ' DOT' )
console.log( ' -> Nominators:' )
console.log( nominatorsList )
rank++
}).catch(err => {
console.log(err);
})
})
}
该代码按预期在https://polkadot.js.org/apps/#/js中运行,打印出一些有关验证程序的有用信息。
我想在我的Polkadot完整节点中执行相同的代码,所以我使用api文档的第一个示例修改代码:
// @ts-check
// Required imports
const { ApiPromise, WsProvider } = require('@polkadot/api');
async function main () {
//
// Initialise the provider to connect to the local polkadot node
//
const provider = new WsProvider('ws://127.0.0.1:9944');
// Create the API and wait until ready
const api = await ApiPromise.create(provider);
// Retrieve the chain & node information
const [chain, nodeName, nodeVersion] = await Promise.all([
api.rpc.system.chain(),
api.rpc.system.name(),
api.rpc.system.version()
]);
console.log(`Connected to chain ${chain} using ${nodeName} v${nodeVersion}`);
// Retrieve active validators
const [validators] = await Promise.all([
api.query.session.validators()
]);
if (validators && validators.length > 0) {
// Map staking stats to validators
const validatorStaking = await Promise.all(
validators.map(authorityId => api.query.staking.ledger(authorityId))
);
var rank = 1;
validators.forEach((authorityId, index) => {
// Get staking stats for current validator
var validator = JSON.parse(validatorStaking[index])
// Get nominators, then print information
Promise.all([
api.query.staking.stakers(validator.stash)
]).then(nominators => {
return nominators
}).then(function(nominators) {
// Iterate over nominators list
var total = 0
var nominatorString = ''
nominators[0].others.forEach((nominator, index) => {
total = total + parseInt(nominator.value)
nominatorString += ' [' + (index + 1) + '] ' + nominator.who + ' => ' + parseFloat(nominator.value / 1000000000000000).toFixed(3) + ' DOT\n'
})
console.log('\n[ ' + rank + ' ] Validator: ' + authorityId.toString())
console.log( ' -> Controller: ' + authorityId.toString() )
console.log( ' -> Stash: ' + validator.stash)
console.log( ' -> Self bonded: ' + parseFloat(validator.total / 1000000000000000).toFixed(3) + ' DOT')
console.log( ' -> Bonded by nominators: ' + parseFloat(total / 1000000000000000).toFixed(3) + ' DOT' )
console.log( ' -> Total Bonded: ' + (parseFloat((total) / 1000000000000000) + parseFloat((validator.total) / 1000000000000000) ).toFixed(3) + ' DOT' )
console.log( ' -> Nominators:' )
console.log( nominatorString )
rank++
}).catch(err => {
console.log(err);
})
})
}
}
main().catch(console.error).finally(() => process.exit());
修改后的代码可以毫无问题地从本地polkadot节点获取信息。问题在于forEach语句中的console.logs无法显示任何内容。
我做错了什么?第一个代码可以在Polkadot网站上完美运行,显示以下输出:
Connected to chain Alexander using parity-polkadot v0.4.4
[ 1 ] Validator: 5GeJHN5EcUGPoa5pUwYkXjymoDVN1DJHsBR4UGX4XRAwK1Ez
-> Controller: 5GeJHN5EcUGPoa5pUwYkXjymoDVN1DJHsBR4UGX4XRAwK1Ez
-> Stash: 5DuiZFa184E9iCwbh4WjXYvJ88NHvWJbS8SARY8Ev1YEqrri
-> Self bonded: 500.000 DOT
-> Bonded by nominators: 3.384 DOT
-> Total Bonded: 503.384 DOT
-> Nominators:
[1] 5CpSTAh8e8Z8Xq47TDLJz8pn9wTWJ5o47F9bM5McP2kFggf9 => 0.002 DOT
[2] 5EKv5WtPrm3oE56XojPtAQR8wtiHVNJ9BaM8StPKgzqPPXpG => 0.100 DOT
[3] 5FeQ1oB2ek2LQGY2SyaHU7UftdwZLT3TNif8ksKvxES1joiD => 0.148 DOT
[4] 5EEe1QBewqUkVoUT6rmMWtxktcAXcjadFiB6zNwdAgkvM6pw => 0.100 DOT
[5] 5GryLKn5HrFGXhSQTcgAWEoaFqBGov2voUfawF5nVcsQs8QR => 0.100 DOT
[6] 5F6is3DBLJreMEpDCN1mDG6RAAHuhGyPx6rzxZ52BfVB9822 => 0.100 DOT
[7] 5CSopKqGJAyUyAwvxJvwz5NnEZe9eTNTxdXnLDdgSBXrwV36 => 0.148 DOT
[8] 5Gxn6e7xFYWFS4WZGrHVq7tLkUCTwDVzDJGttweMFAM2s1os => 0.100 DOT
[9] 5GZBbVudNqkyrwFr1B89N2kCKj5hSwphX8VyvCsWTNg2vb8k => 0.140 DOT
[10] 5FJArxJ2965RVmBtbUxSLPHMMW2Hkd39akTbRBvRqqffewqk => 0.180 DOT
[11] 5CHs11e7oWTX7jSW3A6qHJjGdw2BoQGrocoXTzmYoh9aTSZ5 => 0.100 DOT
[12] 5FCUUyVRBv1M3sjkKhsPEqjmftt43NRyH1fkbiKXswJwRQei => 0.388 DOT
[13] 5GsZJprFgwyLXiv91XBbNR6xMJU7poEkCCgnKRk8Y7RNa5mY => 0.100 DOT
[14] 5DqrgoGUigk6GKWE4cnxiqWqBFbDbD1dnEhcSCNBgibsveoS => 0.258 DOT
[15] 5GDftAqfcQNSnVuKviALyxiDTRP1kSGPKHpdueSz1gLTfgS2 => 0.128 DOT
[16] 5E7ibdmaVkV1jWbj3HPev2xPstRf17kFjQCjfnGU8iTNWKGq => 1.000 DOT
[17] 5DfnhKjxZaaUcPW1VYuHfBTuGaE9Fg1mDAnTQera45WCLv8N => 0.018 DOT
[18] 5CPHNVVLQ5b9esNbT9ZHZiPFvjfw2xbNf83CxP2nmA6yZQDM => 0.000 DOT
[19] 5FyGYnwUur577KDcdkyyMouprFweXgnQc5KQggJ2QD74MgkC => 0.275 DOT
[ 2 ] Validator: 5DPW1n4q2THUjKGj3QKkcqdh6oxY1bmLsSQ7t8FuiuNtRv4S
-> Controller: 5DPW1n4q2THUjKGj3QKkcqdh6oxY1bmLsSQ7t8FuiuNtRv4S
-> Stash: 5DqAzPeXXmBS8p6TjtvjWZPRkqkmeT15KqXoYAg2LShgu9pd
-> Self bonded: 500.000 DOT
-> Bonded by nominators: 1.333 DOT
-> Total Bonded: 501.333 DOT
-> Nominators:
[1] 5FHKn8H5L2s5Sar3uhpLoUMqto9J5JWYXNUio7dRKr8E84zu => 0.100 DOT
[2] 5E4zTNJRPNu9afjsMEjK88MTxVBzXEn55Exf1pW1rW25ysA9 => 0.177 DOT
[3] 5DPyJoPbVB2PcviCfBxaALj6aj33cS4smAsFz8qPSvKdcVFB => 0.100 DOT
[4] 5DsS6mvvg3CBxQsvoPFEsSsJB9T9mz13g9GqTnhj4u8KZStV => 0.120 DOT
[5] 5E9WSxB1YQTJQC7xxirsYKkjMw8vTUffkcLdmWkNPmAPgiA1 => 0.440 DOT
[6] 5HBbUGki4VmLb8AJ9mHEe8vK9ayiQUfpCanePt21EJchu3oS => 0.295 DOT
[7] 5ELanwMU5rC4tCGuEdxERA5tdyPnBEC32oLvsLfsWFDsTyK7 => 0.100 DOT
...
第二个仅在使用nodejs v10.16.0执行时显示此输出:
Connected to chain Alexander using parity-polkadot v0.4.4