2

我正在查询所有 Polkadot 帐户,因此我可以按余额对它们进行排序。我应该使用哪个 javascript api?我不只是在寻找具有身份的帐户。我正在寻找所有帐户 非常感谢

4

1 回答 1

4

使用波卡 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)
}
于 2021-02-16T17:05:05.273 回答