PHP SDK
aelf-sdk.php - aelf PHP API#
Introduction#
aelf-sdk.php for aelf is similar to web.js for Ethereum. It consists of libraries that enable interaction with a local or remote aelf node via HTTP.
This documentation will guide you through the installation and usage of aelf-sdk.php, with examples included. For more information, visit the aelf-sdk.php repository.
Adding aelf PHP SDK#
To install the library via Composer, run the following commands in your console:
1composer require aelf/aelf-sdk dev-dev2composer require curl/curl
If you cloned the SDK directly, you must install Composer and run it in the root directory:
1{2"require": {3"aelf/aelf-sdk": "dev-dev"4}5}
Examples#
Create an Instance#
Create a new instance of AElf and connect to an AELF chain node. Using this instance, you can call the AElf APIs.
1require_once 'vendor/autoload.php';2use AElf\AElf;34$url = 'http://127.0.0.1:8000';5$aelf = new AElf($url);
Get a System Contract Address#
Get a system contract address. For example, to get the address of AElf.ContractNames.Token:
1require_once 'vendor/autoload.php';2use AElf\AElf;3use AElf\Protobuf\Generated\Hash;45$url = 'http://127.0.0.1:8000';6$aelf = new AElf($url);78$privateKey = 'cd86ab6347d8e52bbbe8532141fc59ce596268143a308d1d40fedf385528b458';9$bytes = new Hash();10$bytes->setValue(hex2bin(hash('sha256', 'AElf.ContractNames.Token')));11$contractAddress = $aelf->GetContractAddressByName($privateKey, $bytes);
Send a Transaction#
Get the contract address and then send a transaction.
1require_once 'vendor/autoload.php';2use AElf\AElf;3use BitWasp\Bitcoin\Key\PrivateKeyFactory;45$url = 'http://127.0.0.1:8000';6$aelf = new AElf($url);78$privateKey = 'cd86ab6347d8e52bbbe8532141fc59ce596268143a308d1d40fedf385528b458';9$aelfEcdsa = new BitcoinECDSA();10$aelfEcdsa->setPrivateKey($privateKey);11$publicKey = $aelfEcdsa->getUncompressedPubKey();12$address = $aelfEcdsa->hash256(hex2bin($publicKey));13$address = $address . substr($aelfEcdsa->hash256(hex2bin($address)), 0, 8);14$base58Address = $aelfEcdsa->base58_encode($address);1516$params = new Hash();17$params->setValue(hex2bin(hash('sha256', 'AElf.ContractNames.Vote')));18$methodName = "GetContractAddressByName";19$toAddress = $aelf->getGenesisContractAddress();2021$transactionObj = $aelf->generateTransaction($base58Address, $toAddress, $methodName, $params);22$signature = $aelf->signTransaction($privateKey, $transactionObj);23$transactionObj->setSignature(hex2bin($signature));2425$executeTransactionDtoObj = ['RawTransaction' => bin2hex($transactionObj->serializeToString())];26$result = $aelf->sendTransaction($executeTransactionDtoObj);27print_r($result);
Web API#
You can access the Web API of your aelf node at:
1{chainAddress}/swagger/index.html
Example: For a local address: http://127.0.0.1:1235/swagger/index.html
Before using the methods, make sure you have an instance of AElf:
1require_once 'vendor/autoload.php';2use AElf\AElf;3// create a new instance of AElf4$url = '127.0.0.1:8000';5$aelf = new AElf($url);
Get Chain Status#
1// create a new instance of AElf2$aelf = new AElf($url);34$chainStatus = $aelf->getChainStatus();5print_r($chainStatus);
Get Block Height#
1$aelf = new AElf($url);23$height = $aelf->getBlockHeight();4print($height);
getBlock#
1$aelf = new AElf($url);23$block = $aelf->getBlockByHeight(1, true);4$block2 = $aelf->getBlockByHash($block['BlockHash'], false);5print_r($block2);
Get Block by Height#
1$aelf = new AElf($url);23$block = $aelf->getBlockByHeight(1, true);4print_r($block);
Get Transaction Result#
1$aelf = new AElf($url);23$block = $aelf->getBlockByHeight(1, true);4$transactionResult = $aelf->getTransactionResult($block['Body']['Transactions'][0]);5print_r($transactionResult);
Get Multiple Transaction Results#
1$aelf = new AElf($url);23$block = $aelf->getBlockByHeight(1, true);4$transactionResults = $aelf->getTransactionResults($block['Body']);5print_r($transactionResults);
Get Transaction Pool Status#
1$aelf = new AElf($url);23$status = $aelf->getTransactionPoolStatus();4print_r($status);
Send Transaction#
1$params = new Hash();2$params->setValue(hex2bin(hash('sha256', 'AElf.ContractNames.Vote')));3$transaction = buildTransaction($aelf->getGenesisContractAddress(), 'GetContractAddressByName', $params);4$executeTransactionDtoObj = ['RawTransaction' => bin2hex($transaction->serializeToString())];5$result = $aelf->sendTransaction($executeTransactionDtoObj);6print_r($result);
Send Multiple Transactions#
1$aelf = new AElf($url);23$paramsList = [$params1, $params2];4$rawTransactionsList = [];5foreach ($paramsList as $param) {6$transactionObj = buildTransaction($toAddress, $methodName, $param);7$rawTransactions = bin2hex($transactionObj->serializeToString());8array_push($rawTransactionsList, $rawTransactions);9}10$sendTransactionsInputs = ['RawTransactions' => implode(',', $rawTransactionsList)];11$listString = $aelf->sendTransactions($sendTransactionsInputs);12print_r($listString);
Get Peers#
1print_r($aelf->getPeers(true));
Add Peer#
1$aelf->addPeer($url);
Remove Peer#
1$aelf->removePeer($url);
Create Raw Transaction#
1$aelf = new AElf($url);23$status = $aelf->getChainStatus();4$params = base64_encode(hex2bin(hash('sha256', 'AElf.ContractNames.Consensus')));5$param = array('value' => $params);6$transaction = [7"from" => $aelf->getAddressFromPrivateKey($privateKey),8"to" => $aelf->getGenesisContractAddress(),9"refBlockNumber" => $status['BestChainHeight'],10"refBlockHash" => $status['BestChainHash'],11"methodName" => "GetContractAddressByName",12"params" => json_encode($param)13];14$rawTransaction = $aelf->createRawTransaction($transaction);15print_r($rawTransaction);
Send Raw Transaction#
1$aelf = new AElf($url);23$rawTransaction = $aelf->createRawTransaction($transaction);4$transactionId = hash('sha256', hex2bin($rawTransaction['RawTransaction']));5$sign = $aelf->getSignatureWithPrivateKey($privateKey, $transactionId);6$transaction = array('Transaction' => $rawTransaction['RawTransaction'], 'signature' => $sign, 'returnTransaction' => true);7$execute = $aelf->sendRawTransaction($transaction);8print_r($execute);
Execute Raw Transaction#
1$aelf = new AElf($url);23$rawTransaction = $aelf->createRawTransaction($transaction);4$transactionId = hash('sha256', hex2bin($rawTransaction['RawTransaction']));5$sign = $aelf->getSignatureWithPrivateKey($privateKey, $transactionId);6$transaction = array('RawTransaction' => $rawTransaction['RawTransaction'], 'signature' => $sign);7$execute = $aelf->executeRawTransaction($transaction);8print_r($execute);
Get Merkle Path by Transaction ID#
1$aelf = new AElf($url);23$block = $aelf->getBlockByHeight(1, true);4$merklePath = $aelf->getMerklePathByTransactionId($block['Body']['Transactions'][0]);5print_r($merklePath);
Calculate Transaction Fee#
1$aelf = new AElf($url);23$calculateTransactionFeeInputParam = [4"rawTransaction" => $rawTransactionInput,5];6$result = $aelf->calculateTransactionFee($calculateTransactionFeeInputParam);7print_r($result);
Get Network Info#
1$aelf = new AElf($url);23print_r($aelf->getNetworkInfo());
Get Contract File Descriptor Set#
1$aelf = new AElf($url);23$blockDto = $aelf->getBlockByHeight($blockHeight, false);4$transactionResultDtoList = $aelf->getTransactionResults($blockDto['BlockHash'], 0, 10);5foreach ($transactionResultDtoList as $v) {6$request = $aelf->getContractFileDescriptorSet($v['Transaction']['To']);7print_r($request);8}
Get Task Queue Status#
1$aelf = new AElf($url);23$taskQueueStatus = $aelf->getTaskQueueStatus();4print_r($taskQueueStatus);
Execute Transaction#
1$aelf = new AElf($url);23$methodName = "GetNativeTokenInfo";4$bytes = new Hash();5$bytes->setValue(hex2bin(hash('sha256', 'AElf.ContractNames.Token')));6$toAddress = $aelf->GetContractAddressByName($privateKey, $bytes);7$param = new Hash();8$param->setValue('');9$transaction = $aelf->generateTransaction($fromAddress, $toAddress, $methodName, $param);10$signature = $aelf->signTransaction($privateKey, $transaction);11$transaction->setSignature(hex2bin($signature));12$executeTransactionDtoObj = ['RawTransaction' => bin2hex($transaction->serializeToString())];13$response = $aelf->executeTransaction($executeTransactionDtoObj);14$tokenInfo = new TokenInfo();15$tokenInfo->mergeFromString(hex2bin($response));
Other Tool Kit#
aelf supplies some APIs to simplify development.
Get Chain Id#
1$aelf = new AElf($url);23$chainId = $aelf->getChainId();4print_r($chainId);
Generate Transaction#
1$aelf = new AElf($url);23$param = new Hash();4$param->setValue('');5$transaction = $aelf->generateTransaction($fromAddress, $toAddress, $methodName, $param);
Sign Transaction#
1$aelf = new AElf($url);23$transaction = $aelf->generateTransaction($fromAddress, $toAddress, $methodName, $param);4$signature = $aelf->signTransaction($privateKey, $transaction);
Get Genesis Contract Address#
1$aelf = new AElf($url);23$genesisContractAddress = $aelf->getGenesisContractAddress();4print_r($genesisContractAddress);
Get Address From PubKey#
Calculate the account address according to the public key.
1$aelf = new AElf($url);23$pubKeyAddress = $aelf->getAddressFromPubKey('04166cf4be901dee1c21f3d97b9e4818f229bec72a5ecd56b5c4d6ce7abfc3c87e25c36fd279db721acf4258fb489b4a4406e6e6e467935d06990be9d134e5741c');4print_r($pubKeyAddress);
Get Formatted Address#
Convert the address to the displayed string: symbol_base58-string_base58-string_chain_id.
1$aelf = new AElf($url);23$addressVal = $aelf->getFormattedAddress($privateKey, $base58Address);4print_r($addressVal);
Generate Key Pair Info#
Generate a new key pair using ECDSA.
1$aelf = new AElf($url);23$pairInfo = $aelf->generateKeyPairInfo();4print_r($pairInfo);
Get Contract Address By Name#
1$aelf = new AElf($url);23$bytes = new Hash();4$bytes->setValue(hex2bin(hash('sha256', 'AElf.ContractNames.Token')));5$contractAddress = $aelf->GetContractAddressByName($privateKey, $bytes);6print_r($contractAddress);
Get Address From Private Key#
1$aelf = new AElf($url);23$address = $aelf->getAddressFromPrivateKey($privateKey);4print_r($address);
Get Signature With Private Key#
1$aelf = new AElf($url);23$sign = $aelf->getSignatureWithPrivateKey($privateKey, $transactionId);4print_r($sign);
Is Connected#
1$aelf = new AElf($url);23$isConnected = $this->aelf->isConnected();4print_r($isConnected);
Get Transaction Fees#
Get the transaction fee from the transaction result.
1$aelf = new AElf($url);23$block = $aelf->getBlockByHeight(1, true);4$transactionResult = $aelf->getTransactionResult($block['Body']['Transactions'][0]);5$transactionFees = $aelf->getTransactionFees($transactionResult);6print_r($transactionFees);
AElf.version#
1Copy code2$aelf = new AElf($url);34$version = $aelf->version;
Requirements#
About contributing#
Read out [contributing guide]
About Version#
Edited on: 16 July 2024 04:54:24 GMT+0