如何使用polkadot js查询所有当前的polkadot账户? [英] how to query all current polkadot accounts with polkadot js?

查看:26
本文介绍了如何使用polkadot js查询所有当前的polkadot账户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望查询所有Polkadot帐户,以便我可以按余额对它们进行排序。我应该使用哪个Java脚本API?我不仅仅是在寻找有身份的账户。我正在寻找所有帐户,非常感谢

推荐答案

使用Polkadot JS:https://polkadot.js.org/docs/

要查询所有账户,您需要查看system.account

的所有条目
let users = substrate.query.system.account.entries();

要查看特定用户的总余额,您需要查看他们的data.free并将其添加到他们的data.reserved中。

您将如何获取第一个用户的数据:

let account_id = util_crypto.encodeAddress(users[0][0].slice(-32));
let free_balance = users[0][1].data.free.toNumber();
let reserved_balance = users[0][1].data.reserved.toNumber();

从那里,您应该能够确定如何对列表进行排序并创建所需的输出。

编辑:

以下是其他人的完整脚本:

var { ApiPromise, WsProvider } = require('@polkadot/api');
var { encodeAddress } = require('@polkadot/util-crypto')

async function main() {
    // Substrate node we are connected to and listening to remarks
    const provider = new WsProvider('ws://localhost:9944');
    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(
        `You are connected to chain ${chain} using ${nodeName} v${nodeVersion}`
    );

    // Adjust how many accounts to query at once.
    let limit = 50;
    let result = [];
    let last_key = "";

    while (true) {
        let query = await api.query.system.account.entriesPaged({ args: [], pageSize: limit, startKey: last_key });

        if (query.length == 0) {
            break
        }

        for (const user of query) {
            let account_id = encodeAddress(user[0].slice(-32));
            let free_balance = user[1].data.free.toString();
            let reserved_balance = user[1].data.reserved.toString();
            result.push({ account_id, free_balance, reserved_balance });
            last_key = user[0];
        }
    }

    console.log(result)
}

这篇关于如何使用polkadot js查询所有当前的polkadot账户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆