TRON Developer Hub CN

TRON开发者中心

欢迎来到TRON开发者中心。您将找到全面的指南和文档,以帮助您尽快开始使用TRON Developer Hub,并在遇到困难时提供支持。让我们跳进去吧!

Get Started    

交易信息中的事件解码

介绍

事件是一种导入功能,可帮助开发人员确认,检查和搜索智能合约的特定状态。本文介绍如何解码事件并从transactionHistory数据库获取结果。

Solidity事件示例:

contract EventExampleContract {
    event Transfer(address indexed toAddress, uint256 amount);
    constructor() payable public{}
    function contractTransfer(address toAddress, uint256 amount){
        toAddress.transfer(amount);
        emit Transfer(toAddress, amount);
    }
}

事件结构

Solidity 使用 LOG 指令在事务中记录事件信息。 protobuf 中的结构如下:

message TransactionInfo {
  enum code {
    SUCESS = 0;
    FAILED = 1;
  }
  // LOG Structure represent single event
  message Log {
    bytes address = 1;
    repeated bytes topics = 2;
    bytes data = 3;
  }
  bytes id = 1;
  int64 fee = 2;
  int64 blockNumber = 3;
  int64 blockTimeStamp = 4;
  repeated bytes contractResult = 5;
  bytes contract_address = 6;
  ResourceReceipt receipt = 7;
  // A list of LOG represent list of events in a transaction
  repeated Log log = 8;
  code result = 9;
  bytes resMessage = 10;

  string assetIssueID = 14;
  int64 withdraw_amount = 15;
  int64 unfreeze_amount = 16;
  repeated InternalTransaction internal_transactions = 17;
  int64 exchange_received_amount = 18;
  int64 exchange_inject_another_amount = 19;
  int64 exchange_withdraw_another_amount = 20;
  int64 exchange_id = 21;
}

解码事件示例

上述稳健智能合约的智能合约ABI是:

{"anonymous":false,"inputs":[{"indexed":true,"name":"toAddress","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Transfer","type":"event"}

使用gettransactioninfobyid来获取交易结果:

LogList:
address:
289C4D540B32C7BC56953E55631F8B141EB86434                          // contract address
data:
0000000000000000000000000000000000000000000000000000000000000001
TopicsList
69ca02dd4edd7bf0a4abb9ed3b7af3f14778db5d61921c7dc7cd545266326de2  // topics[0]
000000000000000000000000E552F6487585C2B58BC2C9BB4492BC1F17132CD0  // topics[1]

与ABI一起检查:

  • 主题[0]是69ca02dd4edd7bf0a4abb9ed3b7af3f14778db5d61921c7dc7cd545266326de2。这是keccak的结果(“event(address,uint256)”)。

  • 主题[1]是000000000000000000000000E552F6487585C2B58BC2C9BB4492BC1F17132CD0。它是第一个带有类型地址的索引参数(toAddress)。

  • 数据为0000000000000000000000000000000000000000000000000000000000000001。它是没有使用类型uint256索引(金额)的参数。

交易信息中的事件解码


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.