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

What are the features of the PHP TronTool package

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

Share

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

This article focuses on "what are the features of the PHP TronTool development package", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the features of the PHP TronTool package"?

The TronTool package is suitable for rapidly increasing the support for Tron/USDT-TRC20 digital assets for PHP 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 the development package

The TronTool 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 software package runs under the environment of * * Php 7.1 clients *. The current version 1.0.0. The main classes / interfaces and relationships are shown in the following figure:

For a list of the main code files of TronTool, please see the official website description: TronTool PHP.

2. Create a new address using sample code 2.1

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

~ $cd ~ / trontool/demo~/trontool/demo$ php NewAddressDemo.php

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/demo~/trontool/demo$ php TrxDemo.php

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/demo~/trontool/demo$ php Trc20Demo.php

The implementation results are as follows:

2.4 Tron Smart contract deployment

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

~ $cd ~ / trontool/demo~/trontool/demo$ php DeployContractDemo.php

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:

Use TronTool\ TronKit;use TronTool\ TronApi;use TronTool\ Credential;$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:

$to = 'TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx'; / / transfer destination address $amount = 10000000000; / / transfer amount in SUN$ret = $kit- > sendTrx ($to,$amount); / / submit Trx transfer transaction echo' txid = >'. $ret- > tx- > txID. PHP_EOL; / / shows the transaction IDecho 'result = >'. $ret- > result. PHP_EOL; / / display the result of the transaction

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:

$addr = 'TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx'; / / the Tron address to be queried $balance = $kit- > getTrxBlanace ($addr); / / query the Trx balance in SUNecho' trx balance = >'. $balance. PHP_EOL; / / display balance 2.3TRC20 token transfer

Use the Trc20 () method 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:

$to = 'TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx'; / / transfer destination address $amount = 1315300; / transfer Trc20 token quantity $contractAddress =' TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t' / / USDT-TRC20 token contract deployment address $usdt = $kit- > Trc20 ($contractAddress); / / create Trc20 token contract instance $ret = $usdt- > transfer ($to,$amount) / / transfer Trc20 token echo 'txid = >'. $ret- > tx- > txID. PHP_EOL; / / shows the transfer transaction IDecho 'result = >'. $ret- > result. PHP_EOL; / / display the result of transfer transaction 2.4 TRC20 token balance query

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

$usdt = $kit- > Trc20 ('TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'); / / create USDT-TRC20 token contract instance $balance = $usdt- > balanceOf (' TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx'); / / query Trc20 token balance echo 'usdt balance = >'. $balance. PHP_EOL; / / Show token balance 2.5 TRC20 token event query

Use the Trc20 () method 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:

$usdt = $kit- > Trc20 ('TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'); / / create Trc20 token contract instance $since = time ()-10000; / / calculate check time $events = $usdt- > events ($since); / / extract contract event foreach ($events as $event) {echo' block height = >'. $event- > block_number. PHP_EOL; / / displays the height of the block triggered by the event echo 'event name = >'. $event- > event_name. PHP_EOL; / / Show event name}

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

Caller_contract_address: call contract address in base58 format

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

Result: contract event parameter list, array

Result_type: list of contract event parameter types, array

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

Block_number: block number of the event, integer

Event_name: event name, string

Contract_address: contract address, base58 format

Event_index: 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 TronTool, Credential is used to represent a user's identity 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:

Use TronTool\ Credential;$credential = Credential::create (); / / create a new account echo 'private key = >'. $credential- > privateKey (). PHP_EOL; / / displays the private key echo 'public key = >'. $credential- > publicKey (). PHP_EOL; / / displays the public key echo 'address = >'. $credential- > address (). PHP_EOL; / / 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:

Use TronTool\ Credential;$credential = Credential::fromPrivateKey ('7889.. 023a'); / / Import the existing private key echo' address = >'. $credential- > address (). PHP_EOL; / / 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:

$A1 = Address::fromBase58 ('TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx'); echo $A1-> hex (). PHP_EOL; / / output: 412539EF4F3EB733C105A957EEBB20FD60AD8C9A43 $a2 = Address::fromHex ('412539EF4F3EB733C105A957EEBB20FD60AD8C9A43'); echo $a2-> base58 (). PHP_EOL; / / 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:

$A1 = Address::decode ('TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx'); echo $A1. PHP_EOL; / / output: 412539EF4F3EB733C105A957EEBB20FD60AD8C9A43 $a2 = Address::encode ('412539EF4F3EB733C105A957EEBB20FD60AD8C9A43'); echo $a2. PHP_EOL; / / 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:

Use TronTool\ TronApi;$tc = 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:

$tc = 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:

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

TronApi encapsulates the API provided by a variety of official nodes of Tron, and basically maintains the corresponding relationship to facilitate search and use. For example, query the TRX balance of an account:

$info = $tc- > getAccount ('TEgM5CPeqowkKUXoKrFrpvB7vcBgVkD4tP'); / / query account information echo' balance->'. $info- > balance. PHP_EOL; / / shows the account balance at this point. I believe you have a better understanding of "what are the features of the PHP TronTool package". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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