Blockchain Development Engineer 2022-05-14 03:56:49 阅读数:111
Polygon PHP The development package is suitable for PHP The application increases rapidly to Polygon The support ability of blockchain digital assets , That is to support the use of their own Polygon Application scenarios of blockchain nodes , Also support based on Polygon Official blockchain node API Service Lightweight deployment scenario . Official download address :Polygon PHP Development kit .
Polygon PHP The development package mainly includes the following features :
Polygon PHP The package is running in Php 7.1+ In the environment , current version 1.0.0, Main categories / The interface and relationship are shown in the figure below :
Polygon PHP The main code files of the development package are listed below :
Code file | explain |
---|---|
polygon.php/src/Kit.php | Polygon PHP Development package entry class |
polygon.php/src/Erc20.php | ERC20 Smart contract encapsulation class |
polygon.php/src/Erc721.php | ERC721 Smart contract encapsulation class |
polygon.php/src/Erc1155.php | ERC1155 Smart contract encapsulation class |
polygon.php/src/SmartContract.php | Polygon Smart contract encapsulation class |
polygon.php/src/Credential.php | Polygon Identity class on zone chain , Used to sign transactions |
polygon.php/src/NodeClient.php | Polygon Node protocol encapsulation class |
polygon.php/src/Callback.php | Php Callback auxiliary class |
polygon.php/src/Helper.php | Miscellaneous auxiliary function set |
contracts/WizToken.sol | Example ERC20 contract |
contracts/WizNFT.sol | Example ERC721 contract |
contracts/WizMT.sol | Example ERC1155 contract |
bin/build-contracts.sh | Contract compilation script tool |
demo/credential-demo.php | Show me how to create a new Polygon Account or import existing private key |
demo/matic-demo.php | demonstration MATIC Transfer and inquiry |
demo/erc20-demo.php | demonstration ERC20 Transfer and inquiry |
demo/erc20-event-demo.php | demonstration ERC20 Arrival monitoring |
demo/erc721-demo.php | demonstration ERC721 Transfer and inquiry |
demo/erc721-event-demo.php | demonstration ERC721 Arrival monitoring |
demo/erc1155-demo.php | demonstration ERC1155 Transfer and inquiry |
demo/erc1155-event-demo.php | demonstration ERC1155 Arrival monitoring |
demo/deploy-contracts-demo.php | Demo code , Deployment of smart contracts |
vendor | Third party dependent package directory |
composer.json | composer The configuration file |
Before using the sample code , Please set... According to the actual situation demo/config.php
The following configuration information in :
demo/credential-demo.php
Demonstrates how to use Polygon PHP The development package creates a new Polygon Chain address , Or import the existing private key to rebuild the account .
Enter the demo code directory at the terminal , Execute the following command :
1 2 | ~$ cd ~/polygon-php/demo ~/polygon-php/demo$ php credential-demo.php |
The results are as follows :
demo/matic-demo.php
Demonstrates how to use Polygon PHP Development package implementation MATIC Transfer and balance inquiry .
Enter the demo code directory at the terminal , Execute the following command :
1 2 | ~$ cd ~/polygon-php/demo ~/polygon-php/demo$ php matic-demo.php |
The results are as follows :
demo/deploy-contracts-demo.php
Demonstrates how to use Polygon PHP Development package deployment ERC20/ERC721/ERC1155 Intelligent contract .
Enter the demo code directory at the terminal , Execute the following command :
1 2 | ~$ cd ~/polygon-php/demo ~/polygon-php/demo$ php deploy-contracts-demo.php |
The results are as follows :
demo/erc20-demo.php
Demonstrates how to use Polygon PHP Development package implementation ERC20 Transfer, balance query and other operations .
Enter the demo code directory at the terminal , Execute the following command :
1 2 | ~$ cd ~/polygon-php/demo ~/polygon-php/demo$ php erc20-demo.php |
The results are as follows :
demo/erc20-event-demo.php
Demonstrates how to use Polygon PHP The contract event query function of the development package Realization ERC20 Receipt tracking of pass .
Enter the demo code directory at the terminal , Execute the following command :
1 2 | ~$ cd ~/polygon-php/demo ~/polygon-php/demo$ php erc20-event-demo.php |
The results are as follows :
demo/erc721-demo.php
Demonstrates how to use Polygon PHP Development package implementation ERC721 NFT Transfer, query and other operations .
Enter the demo code directory at the terminal , Execute the following command :
1 2 | ~$ cd ~/polygon-php/demo ~/polygon-php/demo$ php erc721-demo.php |
The results are as follows :
demo/erc721-event-demo.php
Demonstrates how to use Polygon PHP The contract event query function of the development package Realization ERC721 NFT Tracking the arrival of .
Enter the demo code directory at the terminal , Execute the following command :
1 2 | ~$ cd ~/polygon-php/demo ~/polygon-php/demo$ php erc721-event-demo.php |
The results are as follows :
demo/erc1155-demo.php
Demonstrates how to use Polygon PHP Development package implementation ERC1155 Transfer, query and other operations .
Enter the demo code directory at the terminal , Execute the following command :
1 2 | ~$ cd ~/polygon-php/demo ~/polygon-php/demo$ php erc1155-demo.php |
The results are as follows :
demo/erc1155-event-demo.php
Demonstrates how to use Polygon PHP The contract event query function of the development package Realization ERC1155 Tracking the arrival of .
Enter the demo code directory at the terminal , Execute the following command :
1 2 | ~$ cd ~/polygon-php/demo ~/polygon-php/demo$ php erc1155-event-demo.php |
The results are as follows :
Kit Class is Polygon PHP The entrance of the development package , Using this class, you can quickly implement the following functions :
Kit Instantiation needs to pass in NodeClient
Objects and Credential
object , these two items. Parameters encapsulate Polygon Nodes provide API、 And the identity of the user who signed the transaction .
for example , The following code creates an access Polygon Of the main chain Kit example , And use Sign the transaction with the specified private key :
1 2 3 4 5 6 7 8 | //use polygon\Kit; //use polygon\NodeClient; //use polygon\Credential; $kit = new Kit( NodeClient::mainNet(), // Access the main chain Credential::fromKey('0x87c12d....d435') // Use the specified private key ); |
Use Kit Object's transfer()
methods MATIC Transfer accounts , For example, send 0.1 MATIC:
1 2 3 4 5 6 | //use polygon\Kit; $to = '0x90F8bf6...0e7944Ea8c9C1'; // Transfer to address $amount = bn('100000000000000000'); // Transfer amount , In the smallest unit $txid = $kit->transfer($to,$amount); // Submit MATIC Transfer transaction echo 'txid => ' . $txid . PHP_EOL; // Show transactions ID |
Be careful : The amount needs to be converted to the smallest unit , because MATIC The number of decimal places is 18, therefore 0.1 MATIC = 100000000000000000 Smallest unit .
Use balanceOf()
Method to query the specified address MATIC balance , for example :
1 2 3 | $addr = '0x90F8bf6...0e7944Ea8c9C1'; // To query Polygon Chain address $balance = $kit->balanceOf($addr); // Inquire about MATIC balance , In the smallest unit echo 'balance => ' . $balance . PHP_EOL; // Show MATIC balance |
Use Kit Object's erc20()
Method to get the specified ERC20 Contract example , Then call the contract Of transfer()
methods ERC20 Transfer accounts . for example , The following code Transfer between designated addresses 123.4567 USDT( Contract address :0xc2132D05D31c914a87C6611C10748AEb04B58e8F):
1 2 3 4 5 6 7 8 | //use polygon\Kit; $to = '0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1'; // Transfer to address $amount = bn('123456700'); // Transfer accounts ERC20 Number $contractAddr = '0xc2132D05D31c914a87C6611C10748AEb04B58e8F' //USDT The deployment address of the contract $txid = $kit->erc20($contractAddr) ->transfer($to,$amount); // Transfer accounts ERC20 echo 'txid => ' . $txid . PHP_EOL; // Show transfer transactions ID |
Use erc20()
Method gets the specified ERC20 Contract example , Then call the contract Of balanceOf()
Method query ERC20 balance . for example , The following code queries the specified address Of USDT balance :
1 2 3 4 5 6 | //use polygon\Kit; $contractAddr = '0xc2132D05D31c914a87C6611C10748AEb04B58e8F' //USDT The deployment address of the contract $balance = $kit->erc20($contractAddr) ->balanceOf('0x90F8bf6...0e7944Ea8c9C1'); // Search address 0x90F8... Of USDT balance echo 'balance => ' . $balance . PHP_EOL; // Show balance |
Use erc20()
Method to get the specified ERC20 Contract example , And then call Example of contract getTransferEvents()
Method to query the transfer event with specified criteria .
have access to getTransferEvents()
Method to track the arrival status of the specified address . For example, query address 0x90F8… In the recent 1000 Block USDT Token receipt event :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | //use polygon\Kit; $height = $kit->getBlockHeight(); $contractAddr = '0xc2132D05D31c914a87C6611C10748AEb04B58e8F' //USDT The deployment address of the contract $events = $kit->erc20($contractAddr) ->getTransferEvents( // Query receipt event [], // Transfer out account number , An empty array indicates that no specific transfer out account is required ['0x90F8bf6...0e7944Ea8c9C1'], // Receiving account number , Query address only 0x90F8... Receipt event for $height-1000, // Query the starting block number $height // Query end block number ); foreach($events as $event){ echo 'block => ' . $event->blockNumber . PHP_EOL; // Event occurrence block number echo 'from => ' . $event->params['from'] . PHP_EOL; // Transfer out account number echo 'to => ' . $event->params['to'] . PHP_EOL; // Transfer to account number echo 'value => ' . $event->params['value'] . PHP_EOL; // Transfer amount } |
getTransferEvents()
Method returns an array of event objects , The main fields of each member object The explanation is as follows :
Use Kit Object's erc721()
Method to get the specified ERC721 Contract example , Then call the contract Of transferFrom()
methods ERC721 NFT Transfer accounts . for example , The following code The transfer number between designated addresses is 1278 Of ERC721 NFT( Contract address :0xc2132D05D31c914a87C6611C10748AEb04B58e8F):
1 2 3 4 5 6 7 8 | //use polygon\Kit; $to = '0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1'; // Destination address $tokenId = 1278; // Transferable NFT Number $contractAddr = '0xcc739b569624f82f9d7a4f614e47133f37482fed'; //ERC721 The deployment address of the contract $txid = $kit->erc721($contractAddr) ->transfer($to,$tokenId); // Transfer the designated ERC721 NFT echo 'txid => ' . $txid . PHP_EOL; // Show transactions ID |
Use Kit Object's erc721()
Method gets the specified ERC721 After the contract instance , You can call the method of the contract to query NFT Of Basic information and specific number NFT Information of the holder, etc . for example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | //use Polygon\Kit; $contractAddr = '0xcc739b569624f82f9d7a4f614e47133f37482fed'; //ERC721 The deployment address of the contract $nft = $kit->erc721($contractAddr); // return ERC721 Contract example echo 'name => ' . $nft->name() . PHP_EOL; echo 'symbol => ' . $nft->symbol() . PHP_EOL; echo 'total supply =>' . $nft->totalSupply() . PHP_EOL; $tokenId = 1278; //NFT Number echo 'owner => ' . $nft->ownerOf($tokenId); // Returns the specified NFT The holder of echo 'token uri => ' . $nft->tokenURI($tokenId) . PHP_EOL; // Returns the specified NFT Meta information of URI $user = '0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1'; // Address of the user echo 'total nfts => ' . $nft->balanceOf($user) . PHP_EOL; // Users hold NFT total |
Use erc721()
Method to get the specified ERC721 Contract example , And then call Example of contract getTransferEvents()
Method to query the transfer event with specified criteria .
have access to getTransferEvents()
Method to track the arrival status of the specified address . For example, query address 0x90F8… In the recent 1000 Designation of blocks NFT Contract receipt event :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | //use polygon\Kit; $height = $kit->getBlockHeight(); $contractAddr = '0xcc739b569624f82f9d7a4f614e47133f37482fed'; //ERC721 The deployment address of the contract $events = $kit->erc721($contractAddr) ->getTransferEvents( // Query receipt event [], // Transfer out account number , An empty array indicates that no specific transfer out account is required ['0x90F8bf6...0e7944Ea8c9C1'], // Receiving account number , Query address only 0x90F8... Receipt event for $height-1000, // Query the starting block number $height // Query end block number ); foreach($events as $event){ echo 'block => ' . $event->blockNumber . PHP_EOL; // Event occurrence block number echo 'from => ' . $event->params['from'] . PHP_EOL; // Transfer out account number echo 'to => ' . $event->params['to'] . PHP_EOL; // Transfer to account number echo 'tokenId => ' . $event->params['tokenId'] . PHP_EOL; // Transferable NFT Number } |
Use Kit Object's erc1155()
Method to get the specified ERC1155 Contract example , Then call the contract Of safeTransferFrom()
methods ERC1155 Transfer accounts . for example , The following code The transfer number between designated addresses is 1 Of ERC1155 NFT( Contract address :0xafefbbab1038a4fd475d685c11b07bd697e20e38):
1 2 3 4 5 6 7 8 9 10 | //use polygon\Kit; $from = $kit->getSender(); // Transfer out address $to = '0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1'; // Destination address $tokenId = 1; // The pass number of the transfer $value = 1000000; // Number of transfers $contractAddr = '0xafefbbab1038a4fd475d685c11b07bd697e20e38'; //ERC1155 The deployment address of the contract $txid = $kit->erc1155($contractAddr) ->safeTransferFrom($from, $to,$tokenId, $value); // Transfer the designated ERC1155 Pass card echo 'txid => ' . $txid . PHP_EOL; // Show transactions ID |
Use Kit Object's erc1155()
Method to get the specified ERC1155 After the contract instance , You can call the... Of the contract balanceOf()
Method Query the information held by the user ERC1155 Number of passes . for example :
1 2 3 4 5 6 7 8 | //use Polygon\Kit; $contractAddr = '0xafefbbab1038a4fd475d685c11b07bd697e20e38'; //ERC1155 The deployment address of the contract $mt = $kit->erc1155($contractAddr); // Return the contract instance $user = '0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1'; // Address of the user $tokenId = 1; // Pass No echo 'banalce => ' . $mt->balanceOf($user, $tokenId); // The number of passes with the specified number held by the user |
Use Kit Object's erc1155()
Method gets the specified ERC1155 Contract example , And then call Example of contract getTransferEvents()
Method to query the transfer event with specified criteria .
have access to getTransferEvents()
Method to track the arrival status of the specified address . For example, query address 0x90F8… In the recent 1000 Designation of blocks ERC1155 Voucher receipt event :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | //use polygon\Kit; $height = $kit->getBlockHeight(); $contractAddr = '0xafefbbab1038a4fd475d685c11b07bd697e20e38'; //ERC1155 The deployment address of the contract $events = $kit->erc1155($contractAddr) ->getTransferEvents( // Query receipt event [], // Transfer out account number , An empty array indicates that no specific transfer out account is required ['0x90F8bf6...0e7944Ea8c9C1'], // Receiving account number , Query address only 0x90F8... Receipt event for $height-1000, // Query the starting block number $height // Query end block number ); foreach($events as $event){ echo 'block => ' . $event->blockNumber . PHP_EOL; // Event occurrence block number echo 'from => ' . $event->params['from'] . PHP_EOL; // Transfer out account number echo 'to => ' . $event->params['to'] . PHP_EOL; // Transfer to account number echo 'id => ' . $event->params['id'] . PHP_EOL; // The pass number of the transfer echo 'value => ' . $event->params['value'] . PHP_EOL; // Number of passes transferred } |
stay Polygon PHP In the development package , Use Credential
Object represents the identity of a user on the blockchain , Use ordinary The string represents an address on the blockchain , The difference between the two is that Credential Includes users Private key information , Can be used to sign transactions , Therefore, it is necessary to protect .
Use Credential Class static methods create()
Create a new account . for example , The following code creates a Create a new account and display its private key 、 Public key and address :
1 2 3 4 5 6 | //use polygon\Credential; $credential = Credential::create(); // Create a new account echo 'private key => ' . $credential->getPrivateKey() . PHP_EOL; // Show private key echo 'public key => ' . $credential->getPublicKey() . PHP_EOL; // Show public key echo 'address => ' . $credential->getAddress() . PHP_EOL; // display address |
You can use static methods fromKey()
Import an existing private key to instantiate Credential. For example, the following code imports an existing private key and displays the address :
1 2 3 4 | //use polygon\Credential; $credential = Credential::fromKey('0x7889...023a'); // Import the existing private key echo 'address => ' . $credential->getAddress() . PHP_EOL; // Display the corresponding address |
NodeClient Class encapsulation Polygon Node RPC access protocol . Instantiation NodeClient when , Need to specify to connect The node of URL, For example, use local full nodes :
1 2 3 | //use polygon\NodeClient; $client = new NodeClient('http://localhost:8545'); |
When using Polygon When the official node ,NodeClient Class also provides two static functions mainNet()
and testNet()
, , respectively, It is used to access the main chain node and test chain node provided by the official .
for example , The following code is equivalent :
1 2 3 4 5 6 7 | //use polygon\NodeClient; $client = new NodeClient('https://rpc-mainnet.maticvigil.com'); $tc = NodeClient::mainNet(); // Equivalent to the above $tc = new NodeClient('https://rpc-mumbai.maticvigil.com'); $tc = NodeClient::testNet(); // Equivalent to the above |
版权声明:本文为[Blockchain Development Engineer]所创,转载请带上原文链接,感谢。 https://netfreeman.com/2022/134/202205140348123547.html