javascript - 使用 api.rpc.payment.queryFeeDetails 获取外部费用

标签 javascript substrate polkadot-js

我想使用 api.rpc.payment.queryFeeDetails api 调用 ( https://polkadot.js.org/docs/substrate/rpc#queryfeedetailsextrinsic-bytes-at-blockhash-feedetails ) 来获取有关 Substrate 区 block 链中特定外部的费用的信息。

我正在使用这段代码来做到这一点:

const blockHash = '0x8ad4960941190d155b22bf3c1c6aeacb4cdcfcc87b519a87106be7dfd342ae1d';
const { block } = await api.rpc.chain.getBlock(blockHash);
const info = await api.rpc.payment.queryFeeDetails(block.extrinsics[1], blockHash);
console.log(JSON.stringify(info, null, 2));

但是我收到了这个错误:

2021-04-05 18:37:35        RPC-CORE: queryInfo(extrinsic: Bytes, at?: BlockHash): RuntimeDispatchInfo:: 1: Unable to query dispatch info.: Execution, ApiCould not convert parameter `uxt` between node and runtime: Invalid transaction version
Error: 1: Unable to query dispatch info.: Execution, ApiCould not convert parameter `uxt` between node and runtime: Invalid transaction version
    at RpcCoder._checkError (/root/test/node_modules/@polkadot/rpc-provider/coder/index.cjs:84:13)
    at RpcCoder.decodeResponse (/root/test/node_modules/@polkadot/rpc-provider/coder/index.cjs:47:10)
    at WsProvider.value (/root/test/node_modules/@polkadot/rpc-provider/ws/index.cjs:231:90)
    at W3CWebSocket.value [as onmessage] (/root/test/node_modules/@polkadot/rpc-provider/ws/index.cjs:211:153)
    at W3CWebSocket._dispatchEvent [as dispatchEvent] (/root/test/node_modules/yaeti/lib/EventTarget.js:107:17)
    at W3CWebSocket.onMessage (/root/test/node_modules/websocket/lib/W3CWebSocket.js:234:14)
    at WebSocketConnection.<anonymous> (/root/test/node_modules/websocket/lib/W3CWebSocket.js:205:19)
    at WebSocketConnection.emit (events.js:315:20)
    at WebSocketConnection.processFrame (/root/test/node_modules/websocket/lib/WebSocketConnection.js:554:26)
    at /root/test/node_modules/websocket/lib/WebSocketConnection.js:323:40

我做错了什么?

最佳答案

基于@kiaenigma 回答的完整工作片段:

https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fkusama-rpc.polkadot.io#/js

const blockHash = '0x8ad4960941190d155b22bf3c1c6aeacb4cdcfcc87b519a87106be7dfd342ae1d';
const { block } = await api.rpc.chain.getBlock(blockHash);
console.log('extrinsic:', JSON.stringify(block.extrinsics[1].toHuman(), null, 2));
const queryFeeDetails = await api.rpc.payment.queryFeeDetails(block.extrinsics[1].toHex(), blockHash);
console.log('queryFeeDetails:', JSON.stringify(queryFeeDetails.toHuman(), null, 2));
const queryInfo = await api.rpc.payment.queryInfo(block.extrinsics[1].toHex(), blockHash);
console.log('queryInfo:', JSON.stringify(queryInfo.toHuman(), null, 2));

输出:

extrinsic: {
  "isSigned": true,
  "method": {
    "args": [
      {
        "Id": "FJLFJeKagZ8xkDxH4AneGN9tRe9NZM65CZFdbnhVz6Whm2u"
      },
      "12.9452 KSM"
    ],
    "method": "transferKeepAlive",
    "section": "balances"
  },
  "era": {
    "MortalEra": {
      "period": "1,024",
      "phase": "559"
    }
  },
  "nonce": "15,942",
  "signature": "0x63866b9d4dd0a5b2240c3a864bf1184ed764fb02eecc950a0c480e1a887c3643ea3625960a1e4e808a3171684c173a3eb099183433eaf54b4a94367406334d08",
  "signer": {
    "Id": "HmFYPT1btmi1T9qqs5WtuNJK93yNdnjjhReZh6emgNQvCHa"
  },
  "tip": "0"
}
queryFeeDetails: {
  "inclusionFee": {
    "baseFee": "166.6666 µKSM",
    "lenFee": "2.4666 mKSM",
    "adjustedWeightFee": "243.0000 pKSM"
  }
}
queryInfo: {
  "weight": "182,428,000",
  "class": "Normal",
  "partialFee": "2.6333 mKSM"
}

关于javascript - 使用 api.rpc.payment.queryFeeDetails 获取外部费用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66958231/

相关文章:

blockchain - 开发 key (Alice) 如何从 `chain_specs.rs` 添加到本地 keystore ?

rust - Substrate 中来自比特币的 OP_RETURN <data> 相当于什么?

cryptography - 如何从 polkadot 公钥获取 Polkadot 地址?波卡.js

javascript - 如何同步执行socket.io端点

javascript - promise 处理 - 更新数据库条目(如果存在)

javascript - 在 Javascript 中验证错误日期

substrate - 如何创建在 Polkadot JS 应用程序界面上显示的托盘错误?

javascript - 制作此 Flash Action 的 CSS 副本——灰色图片和悬停时弹出的元素

底物创世 block 不匹配

rust - extrinsics 选项卡中没有可用的 `kitties` 模块