如何查询 RSK 交易的内部交易?

时间:2021-07-29 09:30:58

标签: web3js rsk

如果我有 RSK 交易的交易哈希,我如何获取其内部交易 - 即智能合约何时调用其他合约的函数或进行 RBTC 转账? 我可以使用 web3.js 获取主事务,但是一旦获得它, 我无法解析它以提取发生的内部事务。 我尝试过的另一件事是使用 web3.js 查询发生交易的块,但是无法解析它以获取内部交易。

1 个答案:

答案 0 :(得分:3)

重申我原来的评论:

<块引用>

RSK 虚拟机(如 EVM)没有定义“内部交易”,因此没有 RPC 来查询它们。您将需要“调试”事务执行以重建这些内部结构——这很难做到。区块浏览器通常会为您执行此操作。

幸运的是 RSK Block Explorer 公开一个 API,因此可以通过编程方式查询。 因此,虽然您将无法为此使用 web3.js, 正如你在问题中所要求的那样, 尽管如此,您仍将能够获得内部交易。

让我们举个例子,下面的交易 0x01fbd670ea2455d38e83316129765376a693852eca296b3469f18d2a8dde35d8 恰好有很多的内部交易。

curl \
  -X GET \
  -H  "accept: application/json" \
  "https://backend.explorer.rsk.co/api?module=internalTransactions&action=getInternalTransactionsByTxHash&hash=0x01fbd670ea2455d38e83316129765376a693852eca296b3469f18d2a8dde35d8"

上述命令检索此特定交易的内部交易。 如果您希望为不同的交易执行此操作, 只需更改请求 URL 中 hash 查询参数的值。

这会给你一个相当大的 JSON 响应, 我不会在这里完整复制。 然后您可以使用您的 JS 代码解析它(因为您已经在使用 web3.js)。

在命令行上,您可以使用 jq 命令行实用程序中可用的响应过滤器:

curl \
  -X GET \
  -H  "accept: application/json" \
  "https://backend.explorer.rsk.co/api?module=internalTransactions&action=getInternalTransactionsByTxHash&hash=0x01fbd670ea2455d38e83316129765376a693852eca296b3469f18d2a8dde35d8" \
  | jq -c '.data[].action.callType'

以上将 curl 命令的输出通过管道传输到 jq,然后 应用过滤器:

  • 查看 data 属性,并返回数组中的所有项
  • 在每个项目中深入到 action 对象,并返回其 callType

结果如下:

"delegatecall"
"staticcall"
"delegatecall"
"staticcall"
"delegatecall"
"staticcall"
"delegatecall"
"staticcall"
"delegatecall"
"staticcall"
"delegatecall"
"staticcall"
"delegatecall"
"staticcall"
"delegatecall"
"staticcall"
"delegatecall"
"call"

所以这个交易包含18个内部交易, 混合使用 delegatecallstaticcallcall... 确实是相当复杂的交易!

现在让我们使用 jq 命令来使用不同的过滤器, 这样我们才能获得有关最终内部交易的完整详细信息, 这恰好是唯一的 call 内部交易:

curl \
  -X GET \
  -H  "accept: application/json" \
  "https://backend.explorer.rsk.co/api?module=internalTransactions&action=getInternalTransactionsByTxHash&hash=0x01fbd670ea2455d38e83316129765376a693852eca296b3469f18d2a8dde35d8" \
  | jq -c '.data[17].action'

请注意,与上一个命令的唯一区别是现在过滤器 是 .data[17].action。 这将导致以下输出:

{
  "callType": "call",
  "from": "0x3f7ec3a190661db67c4907c839d8f1b0c18f2fc4",
  "to": "0xa288319ecb63301e21963e21ef3ca8fb720d2672",
  "gas": "0x20529",
  "input": "0xcbf83a040000000000000000000000000000000000000000000000000000000000000003425443555344000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000086f36650548d5c400000000000000000000000000003f7ec3a190661db67c4907c839d8f1b0c18f2fc4000000000000000000000000000000000000000000000000000000000036430c000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000001b000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000001b000000000000000000000000000000000000000000000000000000000000001b000000000000000000000000000000000000000000000000000000000000001b0000000000000000000000000000000000000000000000000000000000000005d6328b4db96469d968348a852e6978d18b7dc9bda776727991b83f171abe4a4040ebab67dee8e9711683af91e05c3970bcb6a29502f9b35b14b7a9225d43f6e3e0cf4ae577be626ae350d8e103df88f55205167eaad7267fdbf247e4b35ec674457ac87e13451d2fa9985c854b2f84982e3b611c3b48f5045f2cdc3c6acff44d1735d2771581dc2cc7477fc846767ad088182fc317424d468477cf3a54724543000000000000000000000000000000000000000000000000000000000000000516a3d4cf7e73d17e2230c87f6ef48f38d82885c64d47fef646987f8d6fbb86405515760c786315cac84d7df048e2ba054868f2b9e2afeec0b63ebf2dcac59c8848f254382abf73cf6ce2d5134b5bc065c0706fb7a2f7886a15e79a8953ed11006c5a7d14b4fbf1bb6ff8d687a82a548dcdbd823ebec4b10e331bee332df1a7ae0e45fdac4f6648e093b90a6b56f33e31f36d4079526f871f51cafa710cdde4c3",
  "value": "0x0"
}