当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
- 2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办
- With the advent of tensorflow 2.0, can pytoch still shake the status of big brother?
- C語言I部落格作業03
- Interface pressure test: installation, use and instruction of siege pressure test
- How to get started with new HTML5 (2)
- For a while, a dynamic thread pool was created, and the source code was put into GitHub
- Staying up late summarizes the key points of report automation, data visualization and mining, which is different from what you think
- 零基础打造一款属于自己的网页搜索引擎
- Use modelarts quickly, zero base white can also play AI!
猜你喜欢
前端未來趨勢之原生API:Web Components
Jetcache buried some of the operation, you can't accept it
DRF JWT authentication module and self customization
How to hide part of barcode text in barcode generation software
Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
For a while, a dynamic thread pool was created, and the source code was put into GitHub
【自学unity2d传奇游戏开发】如何让角色动起来
MongoDB与SQL常用语法对应表
Who says cat can't do link tracking? Stand up for me
With the advent of tensorflow 2.0, can pytoch still shake the status of big brother?
随机推荐
Three Python tips for reading, creating and running multiple files
Python基础数据类型——tuple浅析
零基础打造一款属于自己的网页搜索引擎
How to customize sorting for pandas dataframe
Individual annual work summary and 2019 work plan (Internet)
华为Mate 40 系列搭载HMS有什么亮点?
Analysis of ThreadLocal principle
美团内部讲座|周烜:华东师范大学的数据库系统研究
每个大火的“线上狼人杀”平台,都离不开这个新功能
FastThreadLocal 是什么鬼?吊打 ThreadLocal 的存在!!
Interpretation of Cocos creator source code: engine start and main loop
Humor: hacker programming is actually similar to machine learning!
Get twice the result with half the effort: automation without cabinet
如何对数据库账号权限进行精细化管理?
GUI engine evaluation index
[actual combat of flutter] pubspec.yaml Configuration file details
Gather in Beijing! The countdown to openi 2020
Isn't data product just a report? absolutely wrong! There are university questions in this category
文件过多时ls命令为什么会卡住?
nacos、ribbon和feign的簡明教程