如何使用 polkadot js 查询所有当前的 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();

从那里,您应该能够弄清楚如何对列表进行排序并创建您想要的输出。