当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Introduction to the structure of PDF417 bar code system
- Jetcache buried some of the operation, you can't accept it
- Digital city responds to relevant national policies and vigorously develops the construction of digital twin platform
- Advanced Vue component pattern (3)
- In depth to uncover the bottom layer of garbage collection, this time let you understand her thoroughly
- Interpretation of Cocos creator source code: engine start and main loop
- 给字节的学姐讲如何准备“系统设计面试”
- Installing ns-3 on ubuntu18.04
- 【字节跳动 秋招岗位开放啦】Ohayoo!放学别走,我想约你做游戏!!!
- Use modelarts quickly, zero base white can also play AI!
猜你喜欢
Behind the record breaking Q2 revenue of Alibaba cloud, the cloud opening mode is reshaping
【:: 是什么语法?】
Three Python tips for reading, creating and running multiple files
It's time for your financial report to change to a more advanced style -- financial analysis cockpit
Markdown tricks
MeterSphere开发者手册
How to hide part of barcode text in barcode generation software
Pattern matching: The gestalt approach一种序列的文本相似度方法
【自学unity2d传奇游戏开发】地图编辑器
The difference between gbdt and XGB, and the mathematical derivation of gradient descent method and Newton method
随机推荐
Construction of encoder decoder model with keras LSTM
What course of artificial intelligence? Will it replace human work?
Behind the record breaking Q2 revenue of Alibaba cloud, the cloud opening mode is reshaping
如何对数据库账号权限进行精细化管理?
What are manufacturing and new automation technologies?
use Asponse.Words Working with word templates
这个项目可以让你在几分钟快速了解某个编程语言
【自学unity2d传奇游戏开发】如何让角色动起来
Individual annual work summary and 2019 work plan (Internet)
MeterSphere开发者手册
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:王涛
Basic usage of GDB debugging
美团内部讲座|周烜:华东师范大学的数据库系统研究
用一个例子理解JS函数的底层处理机制
Discussion on the technical scheme of text de duplication (1)
Interpretation of Cocos creator source code: engine start and main loop
百万年薪,国内工作6年的前辈想和你分享这四点
electron 實現檔案下載管理器
快速排序为什么这么快?
Elasticsearch Part 6: aggregate statistical query