当前位置:网站首页>Tron smart wallet PHP development kit [zero TRX collection]
Tron smart wallet PHP development kit [zero TRX collection]
2020-11-06 20:21:00 【Blockchain tutorial】
TronSmartWallet The development package is suitable for the platform side to efficiently complete the Trx/TRC20 The collection of tokens , There is no need to inject Trx Can finish Trx/TRC20 The collection of tokens . Official download address :TronSmartWallet PHP Development kit .
1、 Development package Overview
TronSmartWallet The main features of the development package are as follows :
- Real platform unmanaged wallets , The platform does not need to manage a large number of user address keys, and there is no loss of security
- There is no need to inject Trx Can finish Trx/TRC20 The collection of tokens , The process is simpler , More efficient
- Support the collection of multiple user addresses in a single transaction
TronSmartWallet To run on PHP 7.1+ In the environment , The main classes and their relationships are shown in the figure below :
TronSmartWallet See the official website for a list of the main code files :http://sc.hubwiz.com/codebag/tron-smartwallet/ .
2、 Use sample code
2.1 Deploy plant contracts
TronSmartWallet Development packages use factory contracts SmartWalletFacotry Manage the generation and collection of user addresses . So first you need to deploy the factory contract .
Sample code demo/deploy-contracts.php
Shows how to deploy SmartWalletFactory Contract and a demo TRC20 The token contract . Execute the following command to run the sample code :
php deploy-contracts.php
The operation results are as follows :
After the contract is deployed, it will be generated in the current directory addresses.json file , The document records SmartWalletFactory Contract and HappyToken The deployment address of the contract , The information recorded in this file will be used in other demo code .
2.2 Generate user address
Sample code demo/generate-user-address.php
Shows how to use TronSmartWallet The development package generates platform addresses for users or orders .
Execute the following command to run the sample code , For three different ID Generate the corresponding address respectively :
php generate-user-wallet.php
The operation results are as follows :
notes : There is no handling charge for generating user address .
2.3 User top-up
Sample code demo/fund-user-address.php
Simulation of the user to the platform address recharge behavior .
Execute the following command to run the sample code , Assign to three ID The corresponding address is transferred to Trx and token:
php fund-user-wallet.php
The operation results are as follows :
2.4 Check user address balance
Sample code demo/get-user-balance.php
It shows how to query the user's address Trx/TRC20 Token balance .
Execute the following command to run the sample code , Display three corresponding to the specified ID The balance information of the address of :
php get-user-balance.php
The operation results are as follows :
2.5 Collect user address balance
Sample code demo/sweep-user-address.php
Shows how to use the Trx and TRC20 Token to designated cold ( heat ) Wallet address .
Execute the following command to run the sample code :
php sweep-user-wallet.php
The operation results are as follows :
3、Tron Identity and address
TronSmartWallet Development packages use Credential
Object to represent a specific Tron identity certificate , This object contains the key and address information of the account .
3.1 Instantiation Credential
Use static methods create()
Create a new Ethereum account , for example :
//use TronSmartWallet\Credential;
$credential = Credential::create(); // Create a new account
You can also use static methods fromPrivateKey()
Import an existing private key to instantiate Credential object , for example :
$credential = Credential::fromPrivateKey(
'4f3edf983ac6......b113bce9c46' // Private key to import
);
3.2 Check your account key and address
Credential Class provides the following methods to obtain the private key of the current account 、 Public key and address :
- privateKey() : Return the private key 16 Base string
- publicKey() : Return public key 16 Base string
- address() : return Address object
for example , The following code creates a new Tron ID card and show its address :
$credential = Credential::new();
echo 'address => ' . $credential->address() . PHP_EOL; // Show account address
3.3 Tron address translation
stay Tron In blockchain , There are two expressions for an address :16 Into the system and base58 Express , For example, here are two representations of the same address :
- Base58 :TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx
- 16 Base number :412539EF4F3EB733C105A957EEBB20FD60AD8C9A43
Address
Class contains the corresponding encoding and decoding logic , The address format can be easily converted . for example :
//use TronSmartWallet\Address;
$a1 = Address::decode('TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx'); // decode Base58 Address
echo $a1 . PHP_EOL; // Output :412539EF4F3EB733C105A957EEBB20FD60AD8C9A43
$a2 = Address::encode('412539EF4F3EB733C105A957EEBB20FD60AD8C9A43'); // code 16 Base address
echo $a2 . PHP_EOL; // Output :TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx
4、 Use TronApi
TronApi A variety of Tron Nodes provide API, for example tron All the nodes 、solidity Node and event service node API,TronSmartWallet utilize TronApi visit Tron Blockchain .
Instantiation TronApi when , It can be divided into different types of Tron Nodes specify different connections URL, for example :
//use TronSmartWallet\TronApi;
$api = new TronApi(
'https://api.trongrid.io', // All the nodes URL
'https://api.trongrid.io', // Contract node URL
'https://api.trongrid.io' // Event node URL
);
When the above three nodes URL Phase at the same time , I could just write it as :
$api = new TronApi('https://api.trongrid.io');
If I use theta Tron Official TronGrid node , So you can use it directly TronApi Two static functions provided mainNet() and testNet(), Access the main chain and shasta Test chain .
for example , The following code is equivalent :
$api = new TronApi('https://api.trongrid.io');
$api = TronApi::mainNet(); // Equivalent to the above
$api = new TronApi('https://api.shasta.trongrid.io');
$api = TronApi::testNet(); // Equivalent to the above
5、SmartWalletKit Class
3.1 Instantiation SmartWalletKit
SmartWalletKit yes TronSmartWallet The entry class of the development package , When instantiating, you need to pass in TronApi object 、Credential Object and factory contract address . for example :
//use TronSmartWallet\TronApi;
//use TronSmartWallet\Credential;
//use TronSmartWallet\SmartWalletKit;
$kit = new SmartWalletKit(
TronApi::mainNet(), // Access Tron Main network
Credential::fromPrivateKey('......'), // Ethereum account object
'TGuQLmDSmYEfFcQaKBqEJWNGtD4RontQBm' // Factory contract address
);
3.2 Generate user address
Use SmartWalletKit Of getUserWallet()
Method to generate the platform address for the specified user , for example :
$userId = 'u010203'; // User's platform ID
$userAddress = $kit->generateUserWallet($userId); // Return user address
echo 'user address => ' . $userWallet . PHP_EOL; // Show user address
3.3 Aggregate individual user address balance
Use SmartWalletKit Of sweepUserWallet()
Method to collect the specified user address Trx/TRC20 Token balance . for example :
$userId = 'u010203'; // User's platform ID
$txid = $kit->sweepUserWallet(
'u010203', // user ID
'TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx' // Receiving address
);
echo 'sweep txid => ' . $txid . PHP_EOL; // Show pooled transactions ID
$success = $kit->waitForConfirmation($txid); // Waiting for transaction confirmation
echo 'success => ' . $success . PHP_EOL; // Show execution results
3.4 Batch collection of user address balance
Use SmartWalletKit Of sweepUserWallets()
Method to collect a set of user addresses Trx/TRC20 Token balance . for example :
$txid = $kit->sweepUserWallets(
['u010203', 'u030405', 'u050607'], // user ID Array
'TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx' // Receiving address
);
echo 'sweep txid => ' . $txid . PHP_EOL; // Show pooled transactions ID
$success = $kit->waitForConfirmation($txid); // Waiting for transaction confirmation
echo 'success => ' . $success . PHP_EOL; // Show execution results
3.5 operation TRC20 Tokens,
Use SmartWalletKit Of trc20()
Method to get the specified address TRC20 Token instance , Call standard TRC20 Interface can operate token . For example, query USDT Balance and transfer :
$somebody = 'TDN3QY85Jft3RwgyatjRNmrwRmwkn8qwqx'; // Receiving account number
$token = $kit->trc20('TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t');
echo 'balance => ' . $token->balanceOf($somebody) . PHP_EOL; // Inquire about USDT balance
$txid = $kit->transfer($somebody, hex('100000000')); // TRC20 Transfer accounts
echo 'transfer token txid => ' . $txid . PHP_EOL; // Show transactions ID
$success = $kit->waitForConfirmation($txid); // Waiting for transaction confirmation
echo 'success => ' . $success . PHP_EOL; // Show execution results
hex($numstr)
yes SmartWalletKit Auxiliary methods provided , It's convenient to put 10 A large number represented by a decimal string is converted to 16 Hexadecimal said .
TronSmartWallet Development package official download address :http://sc.hubwiz.com/codebag/tron-smartwallet/
版权声明
本文为[Blockchain tutorial]所创,转载请带上原文链接,感谢
边栏推荐
- use Asponse.Words Working with word templates
- Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
- Advanced Vue component pattern (3)
- What to do if you are squeezed by old programmers? I don't want to quit
- Uncle Bob: the software architecture is similar to a house. Object oriented is the structure of the house, and the water pipe is functional programming
- Pattern matching: The gestalt approach一种序列的文本相似度方法
- Behind the first lane level navigation in the industry
- It's easy to operate. ThreadLocal can also be used as a cache
- 给字节的学姐讲如何准备“系统设计面试”
- A brief history of neural networks
猜你喜欢
C + + and C + + programmers are about to be eliminated from the market
Network security engineer Demo: the original * * is to get your computer administrator rights! [maintain]
前端未來趨勢之原生API:Web Components
给字节的学姐讲如何准备“系统设计面试”
Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
A course on word embedding
StickEngine-架构11-消息队列(MessageQueue)
每个大火的“线上狼人杀”平台,都离不开这个新功能
Free patent download tutorial (HowNet, Espacenet)
Flink's datasource Trilogy 2: built in connector
随机推荐
常用SQL语句总结
What course of artificial intelligence? Will it replace human work?
Pattern matching: The gestalt approach一种序列的文本相似度方法
如何在终端启动Coda 2中隐藏的首选项?
Solve the problem of database insert data garbled in PL / SQL developer
Try to build my mall from scratch (2): use JWT to protect our information security and perfect swagger configuration
For a while, a dynamic thread pool was created, and the source code was put into GitHub
Advanced Vue component pattern (3)
Uncle Bob: the software architecture is similar to a house. Object oriented is the structure of the house, and the water pipe is functional programming
这个项目可以让你在几分钟快速了解某个编程语言
What are manufacturing and new automation technologies?
Use modelarts quickly, zero base white can also play AI!
Digital city responds to relevant national policies and vigorously develops the construction of digital twin platform
If PPT is drawn like this, can the defense of work report be passed?
Unity性能优化整理
開源一套極簡的前後端分離專案腳手架
How to turn data into assets? Attracting data scientists
WeihanLi.Npoi 1.11.0/1.12.0 Release Notes
What if the front end doesn't use spa? - Hacker News
Individual annual work summary and 2019 work plan (Internet)