๐Ÿ“˜ Tronsell Wiki

Event Logs System โ€” TRON Smart Contract Events

Complete guide to the Event Logs System in TRON smart contracts. Learn how events work, log structure, indexed parameters, and how to query and listen to contract events.

๐Ÿ“‹ Event Logs at a Glance
PurposeTrack contract activity & state changes
StorageTransaction receipt (on-chain)
Max Indexed Params3 per event
Query MethodsTronWeb, TronScan API
Real-timeEvent listeners

๐Ÿ“ What are Event Logs in TRON?

Event Logs are a mechanism for smart contracts to emit structured data that is stored in the blockchain's transaction receipt. They allow off-chain applications to track contract activity, monitor state changes, and react to on-chain events efficiently without reading the entire contract state.

Events are defined in Solidity using the event keyword and emitted with the emit statement. When a transaction is executed, all emitted events are recorded in the receipt and can be retrieved later.

๐Ÿ’ก Why Events Matter

Events are the primary way for dApps and backends to be notified about what happened in a transaction. They are essential for building responsive UIs, tracking token transfers, auditing contract usage, and building analytics.

๐Ÿ”ฌ Log Structure & Topics

Each event log in TRON consists of several components. Understanding this structure is key to querying and decoding events:

ComponentDescriptionExample
AddressContract address that emitted the event0x41...
TopicsArray of up to 4 32-byte values (topic0 = event signature, topics 1-3 = indexed parameters)["0xddf252...", "0x0000..."]
DataHex-encoded non-indexed event parameters0x000000000000000000000000...
Block NumberBlock in which the log was created12345678
Transaction HashHash of the transaction that emitted the log0xabc...

Example: Transfer Event Log

// ERC-20 Transfer event definition
event Transfer(address indexed from, address indexed to, uint256 amount);

// Emitted log structure:
// topic0 = keccak256("Transfer(address,address,uint256)")
// topic1 = from address (indexed)
// topic2 = to address (indexed)
// data = amount (non-indexed, 32 bytes)

๐Ÿท๏ธ Indexed vs. Non-Indexed Parameters

One of the most important concepts in the event system is the distinction between indexed and non-indexed parameters:

  • Indexed parameters (up to 3 per event) are stored as topics and can be used for efficient filtering when querying logs. They are typically used for addresses, IDs, or other values you want to search by.
  • Non-indexed parameters are stored in the data field and cannot be filtered directly. They are used for values that don't need to be searchable.
๐Ÿ’ก Choosing What to Index

Index parameters that you will frequently filter by โ€” like sender address, recipient address, token ID, or order ID. Leave other parameters non-indexed to save gas and keep logs compact.

// Good use of indexed parameters
event OrderCreated(address indexed user, uint256 indexed orderId, uint256 amount, uint256 timestamp);

// Filter by user OR orderId โ†’ efficient

๐Ÿ”Ž Querying Event Logs

There are several ways to query event logs on TRON, depending on your needs:

1. Using TronWeb

// Query Transfer events for a specific contract
const events = await tronWeb.getEventResult(
  contractAddress,
  {
    eventName: "Transfer",
    blockNumber: "latest",
    pageSize: 10
  }
);

// Filter by indexed parameter (from address)
const fromEvents = await tronWeb.getEventResult(
  contractAddress,
  {
    eventName: "Transfer",
    filter: { from: "0x41..." },
    blockNumber: 10000000,
    pageSize: 20
  }
);

2. Using TronScan API

// TronScan API endpoint for contract events
GET https://api.tronscan.org/api/contract/events?
  contract={address}&
  eventName=Transfer&
  limit=20&
  start=0

3. Using TronGrid (Data Indexing)

For more complex queries, TronGrid provides a GraphQL interface that allows you to query events with advanced filtering, sorting, and pagination.

๐Ÿ“ก Listening to Events in Real-Time

TronWeb provides a powerful event listener API that lets you react to events as they happen:

// Set up a contract instance
const contract = await tronWeb.contract(abi, contractAddress);

// Listen to Transfer events
contract.events.Transfer()
  .on('data', (event) => {
    console.log('Transfer detected:', event);
    console.log('From:', event.returnValues.from);
    console.log('To:', event.returnValues.to);
    console.log('Amount:', event.returnValues.amount);
  })
  .on('error', (err) => {
    console.error('Event listener error:', err);
  });

// Filter by from address
contract.events.Transfer({ filter: { from: '0x41...' } })
  .on('data', (event) => { /* handle */ });
โš ๏ธ Event Listener Considerations

Event listeners rely on a persistent connection to a TRON node. For production use, consider using WebSocket connections and implementing reconnection logic. Also, be mindful of the volume of events โ€” use filters to reduce the data load.

๐Ÿš€ Common Use Cases for Events

  • Token transfer tracking: Monitor USDT TRC20 or other token transfers for your application.
  • Order book management: Track order creation, matching, and cancellation events in a DEX.
  • Audit trails: Keep a comprehensive log of all contract interactions for compliance.
  • Analytics dashboards: Build real-time dashboards showing on-chain activity.
  • User notifications: Notify users when their transactions are confirmed or when specific events occur.
  • Cross-chain bridges: Monitor deposit events to trigger cross-chain transfers.

๐Ÿ† Best Practices for Events

  • Keep events informative: Include all data needed for off-chain applications to understand the state change.
  • Use indexed parameters wisely: Index the parameters you will filter by; avoid indexing large strings or arrays.
  • Version your events: If you modify an event, consider adding a version number or creating a new event type to avoid breaking existing listeners.
  • Test event emission: Use TronBox or Hardhat to test that events are emitted correctly in your smart contract tests.
  • Monitor event sizes: Events that are too large can increase gas costs; keep the data payload minimal.
๐Ÿ’ก Pro Tip

For large applications, consider using a dedicated indexing service like TronGrid or The Graph to efficiently query historical events without scanning the blockchain directly.

โ“ Frequently Asked Questions

What are Event Logs in TRON?

Event Logs are a mechanism for smart contracts to emit structured data that is stored in the blockchain's transaction receipt. They allow off-chain applications to track contract activity, monitor state changes, and react to on-chain events efficiently without reading the entire contract state.

How do event logs work in TRON?

When a smart contract emits an event (using the 'emit' keyword in Solidity), the TRON Virtual Machine (TVM) creates a log entry in the transaction receipt. This log contains the event signature (topic0), up to three indexed parameters (topics 1-3), and the non-indexed data. Applications can query these logs by topic or by contract address.

What is the difference between indexed and non-indexed event parameters?

Indexed parameters (up to 3 per event) are stored as topics and can be used for efficient filtering when querying logs. Non-indexed parameters are stored in the 'data' field and cannot be filtered directly. Indexed parameters are typically used for addresses or IDs that you want to search by.

How can I query event logs on TRON?

You can query event logs using TronWeb's 'getEventResult' or 'getContractEvents' methods, or via TronScan's API. You can filter by contract address, event signature (topic0), indexed parameters, and block range.

How do I listen to events in real-time with TronWeb?

TronWeb provides event listeners via the 'contract.events' interface. You can use .on('data', callback) to listen for new events as they are emitted, or use .on('error', callback) to handle errors. You can also filter events by indexed parameters.

โšก Build with Tronsell Energy

Integrate Tronsell Energy into your zero-fee USDT transfer contracts. Simple API, instant delivery.