TronTool.Java: A Developer's Guide to the Tron Blockchain SDK

ยท

The TronTool.Java development kit is designed to empower Java applications with robust support for Tron and USDT-TRC20 digital assets. It caters to diverse deployment scenarios, whether you're utilizing your own private Tron blockchain nodes or leveraging the lightweight infrastructure of Tron's official public API services.

This comprehensive toolkit provides a seamless integration path for developers looking to interact with the Tron network efficiently.

Core Features of TronTool.Java

The development package offers a powerful suite of functionalities for blockchain interaction:

The SDK operates on Java 8 and is currently at version 1.0.0. Its architecture is built around several key classes and interfaces that work in concert to provide a streamlined developer experience.

Key Components and Project Structure

The TronTool.Java package is organized into a clear structure for easy navigation and implementation. Here's an overview of the main code files:

Code FileDescription
trontool/Main library project directory
trontool/build.gradleGradle configuration for the TronTool project
trontool/src/main/java/trontool/TronKit.javaPrimary entry point for the development kit
trontool/src/main/java/trontool/Trc20.javaEncapsulation class for TRC20 smart contracts
trontool/src/main/java/trontool/Contract.javaGeneral smart contract encapsulation class
trontool/src/main/java/trontool/Credential.javaBlockchain identity class for transaction signing
trontool/src/main/java/trontool/Address.javaTron address representation class
trontool/src/main/java/trontool/Base58.javaBase58 encoding/decoding utility
trontool/src/main/java/trontool/TronApi.javaAggregated API wrapper for Tron nodes
trontool/src/main/java/trontool/NodeClient.javaHTTP protocol encapsulation class
trontool/src/main/java/trontool/api/Directory for Tron API data type definitions
demo/Demonstration project code directory
demo/build.gradleGradle configuration for demo projects
demo/src/main/java/demo/NewAddressDemo.javaDemo: Creating new Tron blockchain addresses
demo/src/main/java/demo/TrxDemo.javaDemo: TRX transfer transactions and balance queries
demo/src/main/java/demo/Trc20Demo.javaDemo: TRC20 token transfers, balance checks, and event listening

Getting Started with TronKit

TronKit serves as the main gateway to the development kit's capabilities, offering streamlined access to essential functions like TRX transfers, balance inquiries, and TRC20 token operations including transfers, approvals, and balance checks.

Initializing TronKit

Creating a TronKit instance requires two fundamental components: a TronApi object that interfaces with Tron node APIs, and a Credential object that manages user identity for transaction signing.

The following example demonstrates how to create a TronKit instance connected to the Tron mainnet using a specific private key for signing:

TronKit kit = new TronKit(
  TronApi.mainNet("your-api-key-here"), // Connect to mainnet (API key required for TronGrid)
  Credential.fromPrivateKey("87c12d....d435") // Initialize with specified private key
);

Handling TRX Transactions and Balances

To execute TRX transfers, use the sendTrx() method. Remember to convert TRX amounts to SUN units (1 TRX = 1,000,000 SUN) as shown in this example transferring 1000 TRX:

String to = "TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx"; // Recipient address
long amount = 1000000000; // Transfer amount in SUN
TransactionResult ret = kit.sendTrx(to, amount); // Submit TRX transfer
System.out.printf("Transaction ID: %s\n", ret.txId); // Display transaction ID
System.out.printf("Transaction status: %b\n", ret.state); // Display transaction result

For balance inquiries, utilize the getTrxBalance() method:

String addr = "TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx"; // Tron address to query
long balance = kit.getTrxBalance(addr); // Query TRX balance (in SUN)
System.out.printf("Balance: %d\n", balance); // Display balance

Executing TRC20 Token Transfers

The process for TRC20 token transfers involves obtaining a contract instance through TronKit's trc20() method, then calling the contract's transfer() method. This example demonstrates transferring 1.3153 USDT (1,315,300 smallest units):

String to = "TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx"; // Recipient address
BigInteger value = new BigInteger("1315300"); // Amount in smallest token units
String contractAddress = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"; // USDT-TRC20 contract address
Trc20 usdt = kit.trc20(contractAddress); // Create TRC20 token contract instance
TransactionResult ret = usdt.transfer(to, value); // Execute token transfer
System.out.printf("Transaction ID: %s\n", ret.txId); // Display transfer ID
System.out.printf("Transaction status: %b\n", ret.state); // Display transfer result

Querying TRC20 Token Balances

To check TRC20 token balances, obtain the contract instance and call the balanceOf() method:

Trc20 usdt = kit.trc20("TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"); // USDT-TRC20 instance
BigInteger balance = usdt.balanceOf("TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx"); // Query token balance
System.out.printf("Token balance: %s\n", balance); // Display token balance

Monitoring TRC20 Token Events

For tracking contract activities, use the events() method on your TRC20 contract instance to retrieve emitted events. This example queries USDT contract events from the last 10 seconds:

Trc20 usdt = kit.trc20("TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"); // Create token instance
long since = System.currentTimeMillis() - 10000; // Calculate timestamp for query
ContractEvent[] events = usdt.events(since); // Retrieve contract events
for(ContractEvent e: events) {
  System.out.println("Event name: %s\n", e.eventName); // Display event name
  System.out.println("Block height: %d\n", e.blockNumber); // Display block number
}

The events() method returns an array of event objects containing valuable information about contract interactions. Key fields include:

Managing Blockchain Identities and Addresses

The SDK distinguishes between Credential objects, which contain private key information for transaction signing and require protection, and Address objects, which represent public address information.

To create a new account, use the create() static method of the Credential class:

Credential c = Credential.create(); // Create new account
System.out.printf("Private key: %s\n", c.getPrivateKey()); // Display private key
System.out.printf("Public key: %s\n", c.getPublicKey()); // Display public key
System.out.printf("Address: %s\n", c.getAddress()); // Display address

For importing existing private keys, use the fromPrivateKey() method:

Credential c = Credential.fromPrivateKey("7889...023a"); // Import existing private key
System.out.printf("Address: %s\n", c.getAddress()); // Display corresponding address

Tron addresses support both hexadecimal and Base58 representations. The Address class provides convenient encoding and decoding capabilities:

Address a1 = Address.fromBase58("TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx");
System.out.println(a1.hex); // Output: 412539EF4F3EB733C105A957EEBB20FD60AD8C9A43

Address a2 = Address.fromHex("412539EF4F3EB733C105A957EEBB20FD60AD8C9A43");
System.out.println(a2.base58); // Output: TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx

For direct conversion between formats without intermediate objects, use the static encode() and decode() methods:

String a1 = Address.decode("TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx");
System.out.println(a1); // Output: 412539EF4F3EB733C105A957EEBB20FD60AD8C9A43

String a2 = Address.encode("412539EF4F3EB733C105A957EEBB20FD60AD8C9A43");
System.out.println(a2); // Output: TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx

Interacting with Tron Nodes via TronApi

The TronApi class provides a unified interface for accessing various Tron node APIs, aggregating functionality from full nodes, Solidity nodes, and event service nodes.

When instantiating TronApi, you can specify different connection URLs for each node type:

TronApi api = new TronApi(
  "https://api.shasta.trongrid.io", // Full node URL
  "https://api.shasta.trongrid.io", // Contract node URL
  "https://api.shasta.trongrid.io"  // Event node URL
);

When all three nodes share the same URL, you can use the simplified constructor:

TronApi api = new TronApi("https://api.shasta.trongrid.io");

For TronGrid's official nodes, use the convenient static methods mainNet() and testNet() for mainnet and Shasta testnet connections respectively:

TronApi api = TronApi.mainNet("25f66928-0b70-48cd-9ac6-da6f8247c663"); // Mainnet with API key
TronApi api = TronApi.testNet(); // Testnet connection

TronApi encapsulates a subset of Tron's official node APIs, enabling applications to interact seamlessly with the blockchain. For example, querying a specific account's TRX balance using the node's getaccount endpoint:

Account account = api.getAccount("TEgM5CPeqow...7vcBgVkD4tP"); // Query account information
System.out.printf("Balance: %d\n", account.balance); // Display account balance

๐Ÿ‘‰ Explore advanced blockchain development tools

Frequently Asked Questions

What is the minimum Java version required for TronTool.Java?
The development kit requires Java 8 or later. This ensures compatibility with most enterprise environments while providing access to modern language features necessary for blockchain interactions.

Can I use TronTool.Java with test networks?
Yes, the SDK supports both mainnet and testnet environments. You can easily configure connections to Shasta testnet using the TronApi.testNet() method, allowing you to develop and test applications without spending real TRX.

How does offline signing enhance security?
Offline signing keeps private keys isolated from internet-connected systems. Transactions are prepared online but signed on secure offline devices, significantly reducing vulnerability to remote attacks and unauthorized access.

What TRC20 tokens are supported by the SDK?
TronTool.Java supports all TRC20 standard tokens deployed on the Tron blockchain, including popular stablecoins like USDT-TRC20. You simply need the contract address to interact with any compliant token.

Is it mandatory to use TronGrid's nodes?
No, the architecture is flexible. While TronGrid provides convenient access points, you can configure the SDK to work with any compatible Tron node, including your own privately operated nodes for increased decentralization.

How can I handle different address formats?
The SDK includes comprehensive address conversion utilities through the Address class, allowing seamless translation between Base58 and hexadecimal formats without losing precision or compatibility.

๐Ÿ‘‰ View real-time blockchain development resources