当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Solve the problem of database insert data garbled in PL / SQL developer
- Individual annual work summary and 2019 work plan (Internet)
- Swagger 3.0 brushes the screen every day. Does it really smell good?
- MeterSphere开发者手册
- Advanced Vue component pattern (3)
- Three Python tips for reading, creating and running multiple files
- Network security engineer Demo: the original * * is to get your computer administrator rights! [maintain]
- Vue.js Mobile end left slide delete component
- Introduction to the structure of PDF417 bar code system
- Use modelarts quickly, zero base white can also play AI!
猜你喜欢
TensorFlow中的Tensor是什么?
Ronglian completed US $125 million f round financing
只有1个字节的文件实际占用多少磁盘空间
The importance of big data application is reflected in all aspects
小游戏云开发入门
常用SQL语句总结
(1) ASP.NET Introduction to core3.1 Ocelot
Wow, elasticsearch multi field weight sorting can play like this
StickEngine-架构11-消息队列(MessageQueue)
What are manufacturing and new automation technologies?
随机推荐
Interpretation of Cocos creator source code: engine start and main loop
Wow, elasticsearch multi field weight sorting can play like this
文件过多时ls命令为什么会卡住?
It's time for your financial report to change to a more advanced style -- financial analysis cockpit
What if the front end doesn't use spa? - Hacker News
Introduction to the structure of PDF417 bar code system
Introduction to Google software testing
Who says cat can't do link tracking? Stand up for me
Construction of encoder decoder model with keras LSTM
python100例項
给字节的学姐讲如何准备“系统设计面试”
Named entity recognition in natural language processing: tanford core LP ner (1)
【自学unity2d传奇游戏开发】如何让角色动起来
Azure data factory (3) integrate azure Devops to realize CI / CD
Individual annual work summary and 2019 work plan (Internet)
(2) ASP.NET Core3.1 Ocelot routing
Analysis of serilog source code -- how to use it
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
用一个例子理解JS函数的底层处理机制
Read the advantages of Wi Fi 6 over Wi Fi 5 in 3 minutes