如何通过NEAR API获取钱包质押收益、待释放等核心信息
问题:获取NEAR钱包质押及奖励相关数据
我需要通过NEAR API获取NEAR钱包展示的基本信息,核心是质押及质押奖励类数据,包括:
- 已赚取奖励(Reward earned)
- 待释放金额(Pending release)
- 可提取金额(Available for withdrawal)
目前我已经能获取质押的NEAR数量,但无法获取上述三类数据。我参考过NEAR的RPC合约相关文档和near-api-js的合约工具文档,编写了如下代码:
async function getAccountBalance(accountId) { const near = await connect(config); const protocolConfig = await near.connection.provider.experimental_protocolConfig({ finality: 'final' }); let state = await near.connection.provider.query({ request_type: 'view_account', account_id: accountId, finality: 'optimistic' }); const costPerByte = new bn_js_1.default(protocolConfig.runtime_config.storage_amount_per_byte); console.log(formatNearAmount(costPerByte)); const stateStaked = new bn_js_1.default(state.storage_usage).mul(costPerByte); console.log(formatNearAmount(stateStaked)); const staked = new bn_js_1.default(state.locked); console.log(formatNearAmount(staked)); const totalBalance = new bn_js_1.default(state.amount).add(staked); console.log(formatNearAmount(totalBalance)); const availableBalance = totalBalance.sub(bn_js_1.default.max(staked, stateStaked)); console.log(formatNearAmount(availableBalance)); console.log("Storage cost - total balance ---? ", formatNearAmount(availableBalance)); }
内容的提问来源于stack exchange,提问作者Mohit Chandel
相关产品推荐
相关产品推荐

