AElf php SDK
$ composer require AElf/AElf-sdk
You can also see full examples in ./example
;
-
Create a new instance of AElf, connect to an AElf chain node.
<?php require_once 'vendor/autoload.php'; use AElf\AElf; $url = '127.0.0.1:8000'; $AElf = new AElf($url);
-
Create or load a wallet with
AElf.wallet
;use use AElf\AElfECDSA\AElfECDSA; // create a new wallet $AElfECDSA = new AElfECDSA(); // load a wallet by private key $private_key = 'be3abe5c1439899ac2efd0001e15715fd989a3ae11f09e1cb95d320cd4993e2a'; $AElfECDSA->setPrivateKey($private_key); // To obtain the public key $public_key = $AElfECDSA->getUncompressedPubKey();
-
Get a system contract address, take
AElf.ContractNames.Token
as an example$tokenContractName = 'AElf.ContractNames.Token';
$tokenContractAddress = $AElf->getContractAddressByName($private_key,hex2bin(hash('sha256',$tokenContractName)));
4. Get a contract instance by contract address
php
$tokenContractName = 'AElf.ContractNames.Token';
$tokenContract = $AElf->getTransactionResults($tokenContractAddress);
```
-
How to use contract instance
A contract instance consists of several contract methods and methods can be called in two ways: read-only and send transaction
$params = hex2bin(hash('sha256','AElf.ContractNames.Vote')); $transactionObj = $this->AElf->generateTransaction($this->address,$AElf->getGenesisContractAddress(),'GetContractAddressByName',$params); $signature = $AElf->signTransaction($private_key,$transactionObj); $transactionObj->setSignature(hex2bin($signature)); $executeTransactionDtoObj =['RawTransaction'=>bin2hex($transaction->serializeToString())]; $result = $AElf->sendTransaction($executeTransactionDtoObj); print_r($result);
This module contains tests for all services provided by AElf. You can see how to properly use services provided by AElf here.
You need to firstly set necessary parameters to make sure tests can run successfully.
-
Set baseUrl to your target url.
$url = "Http://127.0.0.1:8001";
?>
-
Give a valid privateKey of a node.
$this->private_key = 'be3abe5c1439899ac2efd0001e15715fd989a3ae11f09e1cb95d320cd4993e2a';
You need to run a local or remote AElf node to run the unit test successfully. If you're not familiar with how to run a node or multiple nodes, please see Running a node / Running multiple nodes for more information.