Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the features of the PHP BnbTool package

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what are the features of the PHP BnbTool package". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The BnbTool package is suitable for rapidly increasing the support for currency chain / Binance Chain digital assets for PHP applications, that is, application scenarios that use their own deployment block chain nodes, and lightweight deployment scenarios that open API services based on third-party nodes.

1. Overview of the development package

BnbTool development kit is a complete PHP development kit for currency chain, which can greatly improve the efficiency of docking with currency chain in block chain wallet, trading platform or block chain application development. BnbTool mainly includes the following features:

Perfect RPC API and Rest API encapsulation of coin chain nodes

Perfect PHP implementation of Tendermint Amino Codec

Manage private keys offline, sign transactions offline, serialize / deserialize transactions offline

Transparent support for self-owned nodes or third-party nodes to access currency chains, such as querying data or broadcasting transactions

The BnbTool software package runs in the environment of * * Php 7.1 developers *. The current version 1.0.0 mainly implements classes / interfaces and their relationships as shown in the following figure:

2. Use the demo code

After downloading and unzipping the package, go to the demo directory and run the demo code.

2.1 CryotoDemo

CryptoDemo.php demonstrates how to use BnbTool's Crypto wrapper class to create and restore coin chain accounts, generate and verify signatures.

From the terminal, execute the following command to start CryptoDemo:

~ $php CryptoDemo.php

The running result is similar to the following figure:

2.2 RpcClientDemo

RpcClientDemo.php demonstrates how to use BnbTool's RpcClient to call the RPC API of a coin chain node.

From the terminal, execute the following command to start RpcClientDemo:

~ $php RpcClientDemo.php

The running result is similar to the following figure:

2.3 RestClientDemo

RestClientDemo.php demonstrates how to use BnbTool's RestClient to call the Rest API of the coin chain.

From the terminal, execute the following command to start RestClientDemo:

~ $php RestClientDemo.php

The running result is similar to the following figure:

2.4 ToolKitDemo

ToolKitDemo.php demonstrates how to use BnbTool's ToolKit entry class to create money chain accounts, transfer accounts, query balances and query historical transactions.

From the terminal, execute the following command to start ToolKitDemo:

~ $php ToolKitDemo.php

The running result is similar to the following figure:

3. Instructions for the use of the ToolKitclass

ToolKit is the entry class of BnbTool, which can be used to quickly complete the creation of currency chain account, transfer, DEX hanging order and other operations. The main properties and methods of ToolKit are as follows:

Attribute

KeyStore: KeyStore instance

RestClient: an instance of the Rest API client of Coin Security chain

Method

NewAddress (): create a new account and return the generated address. The new private key / address is automatically added to the KeyStore.

ImportKey (): imports the specified private key and returns the corresponding address, which is automatically added to the KeyStore

Transfer (): execute token transfer transaction

PlaceOrder (): hang the order in DEX entrustment

CancleOrder (): cancel the entrustment order on DEX

FreezeToken (): freezes a specified number of tokens

UnfreezeToken (): unfreeze a specified number of tokens

DexList (): listed trading pair on DEX

3.1Instantiation of ToolKit

To instantiate ToolKit, you need to pass in two parameters: the Rest API service address of the currency chain node and the network to which the node accesses. For example, use a native Rest API server to access Binance Chain's test network:

Use BnbTool\ ToolKit;$kit = new ToolKit ('http://localhost:8080', / / Rest API access base address' testnet' / / test chain, optional: mainnet | testnet)

ToolKit also provides two static methods to quickly create instances of access to the main network or test network:

$kit = ToolKit::mainnet (); / / use https://dex.binance.org to access the main network $kit = ToolKit::testnet (); / / use https://testnet-dex.binance.org to access the test network

Easy to understand, ToolKit::mainnet () is equivalent to the following code:

New ToolKit ('https://dex.binance.org','mainnet');3.2 account creation and recovery

Create a new account using ToolKit's newAddress () method, which generates a random private key and returns the address corresponding to that private key, while storing the private key information in ToolKit's KeyStore. For example, the following code creates a new currency chain account and displays the address of the account:

Address = $kit- > newAddress (); / / create a new account and return the address echo "new address: $address". PHP_EOL; / / display the address of the new account

You can also use the importKey () method to import an existing private key, which returns the address corresponding to the imported private key. For example, the following code imports the specified private key and displays the address of the account:

$prv = '49bd38a8.. 5af84709670fdkeys; / / the private key to import ToolKit $address = $kit- > importKey ($prv); / / import the private key and return the address echo "restored: $address". PHP_EOL; / / display the recovered address 3.3.Use the KeyStore

ToolKit uses the KeyStore to store account information. The KeyStore follows the four methods of the IKeyStore API:

Add (): add account information

GetAll (): returns a list of all accounts

GetByKey (): query the account with the specified private key

GetByAddress (): query the account at the specified address

The currently used KeyStore object can be accessed using the keyStore property of ToolKit. For example, the following code list shows all accounts in the current KeyStore:

$items = $kit- > keyStore- > getAll (); / return KeyStore all accounts foreach ($items as $item) {/ / display the account private key and address echo "key: {$item- > key}, address: {$item- > address}" line by line. PHP_EOL;}

You can use the getByKey () method of the key to query the account address of the specified key, for example:

$prv = '49bd38a8.. 5af84709670fdspell; / / the private key to be queried $item = $kit- > keyStore- > getByKey ($prv); / / return the account record if (! is_null ($item)) / / if found, echo "address = > {$item- > address}\ n"; / display the account address 3.4 transfer transaction

Use the tranfer () method of ToolKit to perform a token transfer transaction. For example, the following code transfers 1.234 BNB tokens between two specified accounts, with note information:

$kit- > importKey ('....') / / Import the private key of the transfer initiating account $ret = $kit- > transfer ('tbnb1hfw...x3kh9d7p5ryya', / / transfer initiating account' tbnb1l5f...xcyt0ec40avsp', / / transfer receiving account 'BNB', / / token 1.234 / / number of tokens' rent' / / remarks) echo "tx hash = > {$ret [0]-> hash}\ n" / / Show transaction hash

Note that since the transaction requires the initiating account to be signed, make sure that the private key of the initiating account already exists in ToolKit's KeyStore before calling the transfer () method.

3.5 query account balance

Because the Rest API of the coin chain is easier to use, ToolKit uses RestClient instead of RpcClient to interact with the coin chain, and the pre-created RestClient instance can be accessed through the restClient property of the ToolKit.

For example, the following code uses the getAccount () method of RestClient to query the balance of all kinds of tokens for a specified account and displays them line by line:

$ret = $kit- > restClient- > getAccount ('tbnb1hfw...x3kh9d7p5ryya'); / / query account information foreach ($ret- > balances as $b) {/ / traverse all currencies echo "symbol: {$b-> symbol}, free: {$b-> free}\ n"; / / Show currency symbols and available balances} 3.6 query the account's historical transactions

Similarly, using the getTransactions () method of RestClient, you can query the historical transactions of a specified account. For example, the following code:

$ret = $kit- > restClient- > getTransactions ('tbnb1hfw...x37p5ryya'); / / query historical transaction foreach ($ret- > tx as $tx) {/ / traverse all transaction echo json_encode ($tx). PHP_EOL; / / Show transaction content}

The getTransactions () method supports paging processing. For example, the following code uses the offset and limit options to declare that 20 transactions starting at 10 minutes need to be returned:

$ret = $kit- > restClient- > getTransactions ('tbnb1hfw...x3kh9d7p5ryya', / / account address to be queried [' offset'= > 100, / record start location 'limit'= > 20 / / number of records returned]); this is the end of the content of "what are the features of the PHP BnbTool package". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report