Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use TronTool.Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use TronTool.Java". In daily operation, I believe many people have doubts about how to use TronTool.Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use TronTool.Java"! Next, please follow the editor to study!

The TronTool.Java package is suitable for rapidly increasing the support for Tron/USDT-TRC20 digital assets for Java applications, that is, application scenarios that use their own Tron block chain nodes, as well as lightweight deployment scenarios based on Tron official public API services.

1. Overview of TronTool.Java development package

The TronTool.Java development package mainly includes the following features:

Support for native Trx transactions in Tron blockchain

Support Tron smart contracts and TRC20 tokens, such as USDT-TRC20

Support offline signature of transactions to avoid divulging private keys

Complete API encapsulation of Tron nodes to support API provided by full nodes, Solidity nodes and event nodes

Support the use of self-owned nodes or third-party nodes, such as public nodes officially provided by Tron

The TronTool.Java package runs in the Java 8 environment, and the current version 1.0.0. The main classes / interfaces and relationships are shown in the following figure:

The main code files for the TronTool.Java package are listed below:

Code file description trontool/TronTool library project code directory trontool/build.gradleTronTool project Gradle configuration trontool/src/main/java/trontool/TronKit.javaTron development package entry class trontool/src/main/java/trontool/Trc20.javaTron TRC20 smart contract wrapper class trontool/src/main/java/trontool/Contract.javaTron smart contract wrapper class trontool/src/main/java/trontool/Credential.javaTron block chain identity class Used for transaction signing trontool/src/main/java/trontool/Address.javaTron address representation class trontool/src/main/java/trontool/Base58.javaBase58 codec trontool/src/main/java/trontool/TronApi.javaTron node API aggregation wrapper class trontool/src/main/java/trontool/NodeClient.javaHTTP protocol wrapper class trontool/src/main/java/trontool/api/Tron API data type definition directory demo/ demo project code directory demo/build.gradle demonstration item The demo code of the Gradle configuration file demo/src/main/java/demo/NewAddressDemo.java Create a new Tron blockchain address demo/src/main/java/demo/TrxDemo.java demo code, Trx transfer transaction and balance query demo/src/main/java/demo/Trc20Demo.java demo code, Trc20 token transfer, balance query, event monitoring and other build.gradle root project profile settings.gradle root project configuration file

2. Create a new Tron account using the sample code 2.1of TronTool.Java

Enter the demo code directory at the terminal and execute the following command:

~ $cd ~ / trontool.java/demo~/trontool.java/demo$ gradle NewAddressDemo

The implementation results are as follows:

2.2 Trx transfer and balance inquiry

Enter the demo code directory at the terminal and execute the following command:

~ $cd ~ / trontool.java/demo~/trontool.java/demo$ gradle TrxDemo

The implementation results are as follows:

2.3 Trc20 token transfer, balance inquiry and event monitoring

Enter the demo code directory at the terminal and execute the following command:

~ $cd ~ / trontool.java/demo~/trontool.java/demo$ gradle Trc20Demo

The implementation results are as follows:

2. Use TronKit

TronKit is the entrance to the development package, and using this class, you can quickly implement the following functions:

Trx transfer and balance query

Trc20 token transfer, authorization, balance inquiry, etc.

2.1 instantiate TronKit

To instantiate TronKit, you need to pass in a TronApi object and a Credential object, which encapsulate the API provided by the Tron node and the user identity information for signing the transaction.

For example, the following code creates an instance of TronKit connected to the main chain of Tron and signs the transaction with the specified private key:

/ / import trontool.TronKit;//import trontool.TronApi;//import trontool.Credential;TronKit kit = new TronKit (TronApi.mainNet (), / / access the main chain Credential.fromPrivateKey ("87c12d....d435") / / use the specified private key); 2.2 Trx transfer and balance query

Use the sendTrx () method of TronKit to transfer money to Trx, such as sending 1000 TRX:

/ / import trontool.api.TransactionResult;String to = "TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx"; / / transfer destination address long amount = 10000000000; / / transfer amount in SUNTransactionResult ret = kit.sendTrx (to,amount); / / submit Trx transfer transaction System.out.printf ("tx id:% s\ n", ret.txId) / / Show transaction ID System.out.printf ("tx state:% b\ n", ret.state); / / Show transaction result

Note: you need to convert the amount unit to SUN,1 TRX = 1000000 SUN.

Use the getTrxBalance () method to query the Trx balance at the specified address, for example:

String addr = "TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx"; / / the Tron address to be queried long balance = kit.getTrxBlanace (addr); / / query the Trx balance in SUNSystem.out.printf ("balance:% d\ n", balance); / / display the balance 2.3TRC20 token transfer

Use the trc20 () method of the TronKit object to get the specified TRC20 token contract instance, and then call the contract's transfer () method to make the TRC20 token transfer. For example, the following code specifies that 1315300 minimum units of USDT-TRC20 tokens, 1.3153 USDT, are transferred between addresses:

/ / import trontool.Trc20;//import trontool.api.TransactionResult;//import java.math.BigInteger;String to = "TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx"; / / transfer destination address BigInteger value = new BigInteger ("1315300"); / / transfer Trc20 token quantity String contractAddress = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"; / / deployment address of USDT-TRC20 token contract Trc20 usdt = kit.trc20 (contractAddress) / / create Trc20 token contract instance TransactionResult ret = usdt.transfer (to,value); / / transfer Trc20 token System.out.printf ("tx id:% s\ n", ret.txId); / / display transfer transaction IDSystem.out.printf ("tx state:% b\ n", ret.state) / / display the result of the transfer transaction 2.4 TRC20 token balance query

Use the trc20 () method of the TronKit object to get the specified TRC20 token contract instance, and then call the balanceOf () method of the contract to query the TRC20 token balance at the specified address. For example, the following code queries the USDT token balance at the specified address:

/ / import trontool.Trc20;//impot java.math.BigInteger;Trc20 usdt = kit.trc20 ("TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"); / / create USDT-TRC20 token contract instance BigInteger balance = usdt.balanceOf ("TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx"); / / query Trc20 token balance System.out.printf ("balance:% s\ n", balance); / / Show token balance 2.5 TRC20 token event query

Use the trc20 () method of the TronKit object to get the specified TRC20 token contract instance, and then call the contract's events () method to query the specified contract trigger event.

For example, query the events of the last 10 seconds of a USDT token contract:

/ / import trontool.Trc20;//import trontool.api.ContractEvent;Trc20 usdt = kit.trc20 ("TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"); / / create a Trc20 token contract instance long since = System.currentTimeMillis ()-10000; / / calculate the check time point ContractEvent [] events = usdt.events (since) / / extract contract event 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); / / Show block height triggered by event}

The result returned by events () is an array of event objects, and the main fields of each member object are described as follows:

CallerContractAddress: call contract address in base58 format

TransactionId: the transaction ID,16 string that triggered the contract event

Result: contract event parameter list, array

ResultType: list of contract event parameter types, array

BlockTimestamp: timestamp of the block in which the event is located, integer

BlockNumber: block number of the event, integer

EventName: event name, string

ContractAddress: contract address, base58 format

EventIndex: event index serial number, integer

For example, the following is the JSON representation of a Transfer event object for a TRC20 token contract, with the event name given in the event_name field and event parameters in two indexed forms in the result field:

{"caller_contract_address": "TS2Hzo6KpAc8Ym2nGb3idpMtUpM2GiK2gL", "transaction_id": "265cf378f4943b7c77b7a294f533d4b8c718c297dd28a664848d77cd3f3a0af0", "result": {"0": "0x2539ef4f3eb733c105a957eebb20fd60ad8c9a43", / / event parameter 0 "1": "0x6f6794f3904ff51f9fa81e928afdec91f6744a50", / / event parameter 1 "2": "8" / / event parameter 2 "_ from": "0x2539ef4f3eb733c105a957eebb20fd60ad8c9a43", / / event parameter _ from "_ value": "8", / / event parameter _ value "_ to": "0x6f6794f3904ff51f9fa81e928afdec91f6744a50" / / event parameter _ to} "result_type": {"_ from": "address", "_ value": "uint256", "_ to": "address"}, "block_timestamp": 1586263455000, "block_number": 3539438, "event_name": "Transfer" / / event name "contract_address": "TS2Hzo6KpAc8Ym2nGb3idpMtUpM2GiK2gL", "event_index": 0} 3, Tron block chain identity and address representation

In the TronTool.Java development package, Credential is used to represent the identity of a user in the Tron block chain, and Address is used to represent an address in the Tron block chain. The difference between the two is that Credential contains the user's private key information, which can be used to sign the transaction, so it needs to be protected, while Address is the information that can be made public.

Create a new account using the static method create () of the Credential class. For example, the following code creates a new account and displays its private key, public key, and address:

/ / import trontool.Credential;Credential c = Credential.create (); / / create a 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

You can instantiate Credential by importing an existing private key using the static method fromPrivateKey (). For example, the following code imports an existing private key and displays the address:

/ / import trontool.Credential;Credential c = Credential.fromPrivateKey ("7889.023a"); / / Import the existing private key System.out.printf ("address:% s\ n", c.getAddress ()); / / display the corresponding address

In the Tron block chain, there are two representations of an address: hexadecimal and base58, for example, the following are two representations of the same address:

Base58:TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx

Hexadecimal: 412539EF4F3EB733C105A957EEBB20FD60AD8C9A43

The Address class contains the corresponding codec logic, and Address can be instantiated using different forms of addresses in aspects. For example:

/ / import trontool.Address;Address A1 = Address.fromBase58 ("TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx"); System.out.println (a1.hex); / / output: 412539EF4F3EB733C105A957EEBB20FD60AD8C9A43Address a2 = Address.fromHex ("412539EF4F3EB733C105A957EEBB20FD60AD8C9A43"); System.out.println (a2.base58); / / output: TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx

Sometimes we simply need to translate addresses between base58 and hexadecimal, and instead of needing an intermediate Address object, we can directly use the static methods encode () and decode (). For example:

/ / import trontool.Address;String A1 = Address.decode ("TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx"); System.out.println (A1); / / output: 412539EF4F3EB733C105A957EEBB20FD60AD8C9A43String a2 = Address.encode ("412539EF4F3EB733C105A957EEBB20FD60AD8C9A43"); System.out.println (a2); / / output: TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx4, access Tron node API using TronApi

Use TronApi to access the various nodes API of Tron. TronApi aggregates the API provided by a variety of Tron nodes, such as the API of tron full nodes, solidity nodes, and event service nodes.

When you instantiate a TronApi, you can specify different connection URL for different types of Tron nodes, for example:

/ / import trontool.TronApi;TronApi api = new TronApi ("https://api.trongrid.io", / / full node URL" https://api.trongrid.io", / / contract node URL "https://api.trongrid.io" / / event node URL)

When the URL of the above three nodes is the same, it can be abbreviated as:

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

If you are using the TronGrid node officially provided by Tron, you can directly use the two static functions mainNet () and testNet () provided by TronApi to access the main chain and the shasta test chain, respectively.

For example, the following code is equivalent:

TronApi api = new TronApi ("https://api.trongrid.io");TronApi api = TronApi.mainNet (); / / equivalent to the above TronApi api = new TronApi (" https://api.shasta.trongrid.io");TronApi api = TronApi.testNet (); / / equivalent to the above

TronApi encapsulates a subset of API provided by various official nodes of Tron, which can be used for applications to interact with Tron block chains. For example, to query the TRX balance of a specified account, you can use the getaccount interface of the Tron node, which corresponds to the getAccount () method in TronApi:

/ / import trontool.api.AccountAccount account = api.getAccount ("TEgM5CPeqow...7vcBgVkD4tP"); / / query account information System.out.printf ("balance:% d\ n", account.balance); / / display the account balance at this point, the study on "how to use TronTool.Java" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report