如何通过Polkadot JS API查询地址Crowdloan(众贷)锁仓KSM信息
Kusama 众贷参与记录查询方案
Kusama 网络的众贷锁定资产独立存储在crowdloans模块的存储项下,不会关联到普通账户的reserved、miscFrozen等余额字段,因此常规的账户信息接口无法返回这部分数据。
你可以通过Polkadot JS API直接调取众贷模块的存储接口查询指定地址的参与记录,具体实现逻辑如下:
- 首先调取
crowdloan.funds接口获取链上所有众贷项目的基础信息,包含项目索引、对应平行链ID、募资额度等 - 遍历所有众贷项目索引,针对每个索引调用
crowdloan.accounts(fundIndex, 查询地址)接口,返回非空即代表该地址参与了对应众贷项目,返回值中可直接提取锁定的KSM金额
代码示例
const { ApiPromise, WsProvider } = require('@polkadot/api'); async function getAddressCrowdloanRecords(queryAddress) { // 连接Kusama公共RPC节点 const provider = new WsProvider('wss://kusama-rpc.polkadot.io'); const api = await ApiPromise.create({ provider }); const result = []; // 获取所有众贷项目列表 const allCrowdloanFunds = await api.query.crowdloan.funds.entries(); for (const [fundKey, fundData] of allCrowdloanFunds) { const currentFundIndex = fundKey.args[0].toNumber(); // 查询地址在当前众贷项目的贡献记录 const userRecord = await api.query.crowdloan.accounts(currentFundIndex, queryAddress); if (!userRecord.isEmpty) { result.push({ 众贷项目索引: currentFundIndex, 对应平行链ID: fundData.paraId.toNumber(), 锁定KSM数量: userRecord.toHuman().balance }) } } await api.disconnect(); return result; } // 调用示例,替换为实际查询的KSM地址 getAddressCrowdloanRecords('Your KSM Address').then(console.log);
注:如果众贷项目已结束且用户已完成资金赎回,对应的贡献记录会被链上清除,无法通过该接口查询历史参与记录
内容的提问来源于stack exchange,提问作者JohnUopini
相关产品推荐
相关产品推荐

