当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Analysis of etcd core mechanism
- 01. SSH Remote terminal and websocket of go language
- Network security engineer Demo: the original * * is to get your computer administrator rights! [maintain]
- Introduction to the structure of PDF417 bar code system
- Use modelarts quickly, zero base white can also play AI!
- 华为云微认证考试简介
- The dynamic thread pool in Kitty supports Nacos and Apollo multi configuration centers
- Interface pressure test: installation, use and instruction of siege pressure test
- Pattern matching: The gestalt approach一种序列的文本相似度方法
- How to get started with new HTML5 (2)
猜你喜欢
华为Mate 40 系列搭载HMS有什么亮点?
TensorFlow中的Tensor是什么?
【转发】查看lua中userdata的方法
Using NLP and ml to extract and construct web data
01. SSH Remote terminal and websocket of go language
消息队列(MessageQueue)-分析
有了这个神器,快速告别垃圾短信邮件
Flink's datasource Trilogy 2: built in connector
小游戏云开发入门
Three Python tips for reading, creating and running multiple files
随机推荐
一篇文章教会你使用Python网络爬虫下载酷狗音乐
How to understand Python iterators and generators?
Application of restful API based on MVC
理解格式化原理
Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
这个项目可以让你在几分钟快速了解某个编程语言
Asp.Net Core learning notes: Introduction
Construction of encoder decoder model with keras LSTM
For a while, a dynamic thread pool was created, and the source code was put into GitHub
Use modelarts quickly, zero base white can also play AI!
How to get started with new HTML5 (2)
use Asponse.Words Working with word templates
How to hide part of barcode text in barcode generation software
【ElasticSearch搜索引擎】
Custom function form of pychar shortcut key
What are the common problems of DTU connection
前端未來趨勢之原生API:Web Components
Jetcache buried some of the operation, you can't accept it
只有1个字节的文件实际占用多少磁盘空间
Interpretation of Cocos creator source code: engine start and main loop