blockchain - 如何用polkadot js查询当前所有的polkadot账户?

标签 blockchain substrate polkadot

我想查询所有 Polkadot 账户,这样我就可以按余额对它们进行排序。我应该使用哪个 javascript 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)
}

关于blockchain - 如何用polkadot js查询当前所有的polkadot账户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66228220/

相关文章:

blockchain - ERC20 标准中的批准和允许方法真正在做什么?

interface - 强制转换为接口(interface)和契约(Contract)实例之间有区别吗?

rust - 如何从Substrate中的Polkadot.js实现blake2AsHex功能?

blockchain - 如何在基板框架中使用--staging标志?

blockchain - 如何在 Hyperledger 1.0 (Node SDK) 中获取所有现有 channel

hash - 加密散列函数的特性

rust - 如何使用PalletS从PalletS中保存记录,而无需PalletA知道有关节省底物和使用rust 的内部知识的任何信息

node.js - 查询 Polkadot js 获取 Substrate 存储 key 的正确方法是什么?

rust - 如何在Substrate中具有不可变的键值映射?

rust - 为什么即使在更改底物链的源代码之后,存储值的可变性行为也没有变化?