logo

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:

1
composer require aelf/aelf-sdk dev-dev
2
composer 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.

1
require_once 'vendor/autoload.php';
2
use AElf\AElf;
3
4
$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:

1
require_once 'vendor/autoload.php';
2
use AElf\AElf;
3
use AElf\Protobuf\Generated\Hash;
4
5
$url = 'http://127.0.0.1:8000';
6
$aelf = new AElf($url);
7
8
$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.

1
require_once 'vendor/autoload.php';
2
use AElf\AElf;
3
use BitWasp\Bitcoin\Key\PrivateKeyFactory;
4
5
$url = 'http://127.0.0.1:8000';
6
$aelf = new AElf($url);
7
8
$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);
15
16
$params = new Hash();
17
$params->setValue(hex2bin(hash('sha256', 'AElf.ContractNames.Vote')));
18
$methodName = "GetContractAddressByName";
19
$toAddress = $aelf->getGenesisContractAddress();
20
21
$transactionObj = $aelf->generateTransaction($base58Address, $toAddress, $methodName, $params);
22
$signature = $aelf->signTransaction($privateKey, $transactionObj);
23
$transactionObj->setSignature(hex2bin($signature));
24
25
$executeTransactionDtoObj = ['RawTransaction' => bin2hex($transactionObj->serializeToString())];
26
$result = $aelf->sendTransaction($executeTransactionDtoObj);
27
print_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:

1
require_once 'vendor/autoload.php';
2
use AElf\AElf;
3
// create a new instance of AElf
4
$url = '127.0.0.1:8000';
5
$aelf = new AElf($url);

Get Chain Status#

  • API Path: /api/blockChain/chainStatus
  • Parameters: None
  • Returns:
  • Example :
  • 1
    // create a new instance of AElf
    2
    $aelf = new AElf($url);
    3
    4
    $chainStatus = $aelf->getChainStatus();
    5
    print_r($chainStatus);

    Get Block Height#

  • API Path: /api/blockChain/blockHeight
  • Parameters: None
  • Returns: Integer
  • Example :
  • 1
    $aelf = new AElf($url);
    2
    3
    $height = $aelf->getBlockHeight();
    4
    print($height);

    getBlock#

  • API Path: /api/blockChain/block
  • Parameters:
  • Returns:
  • Example :
  • 1
    $aelf = new AElf($url);
    2
    3
    $block = $aelf->getBlockByHeight(1, true);
    4
    $block2 = $aelf->getBlockByHash($block['BlockHash'], false);
    5
    print_r($block2);

    Get Block by Height#

  • API Path: /api/blockChain/blockByHeight
  • Parameters:
  • Returns:
  • Example :
  • 1
    $aelf = new AElf($url);
    2
    3
    $block = $aelf->getBlockByHeight(1, true);
    4
    print_r($block);

    Get Transaction Result#

  • API Path: /api/blockChain/transactionResult
  • Parameters:
  • Returns:
  • Example :
  • 1
    $aelf = new AElf($url);
    2
    3
    $block = $aelf->getBlockByHeight(1, true);
    4
    $transactionResult = $aelf->getTransactionResult($block['Body']['Transactions'][0]);
    5
    print_r($transactionResult);

    Get Multiple Transaction Results#

  • API Path: /api/blockChain/transactionResults
  • Parameters:
  • Returns:
  • Example :
  • 1
    $aelf = new AElf($url);
    2
    3
    $block = $aelf->getBlockByHeight(1, true);
    4
    $transactionResults = $aelf->getTransactionResults($block['Body']);
    5
    print_r($transactionResults);

    Get Transaction Pool Status#

  • API Path: /api/blockChain/transactionPoolStatus
  • Example :
  • 1
    $aelf = new AElf($url);
    2
    3
    $status = $aelf->getTransactionPoolStatus();
    4
    print_r($status);

    Send Transaction#

  • API Path: /api/blockChain/sendTransaction
  • Method: POST
  • Parameters:
  • Example :
  • 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);
    6
    print_r($result);

    Send Multiple Transactions#

  • API Path: /api/blockChain/sendTransactions
  • Method:POST
  • Parameters:
  • Example :
  • 1
    $aelf = new AElf($url);
    2
    3
    $paramsList = [$params1, $params2];
    4
    $rawTransactionsList = [];
    5
    foreach ($paramsList as $param) {
    6
    $transactionObj = buildTransaction($toAddress, $methodName, $param);
    7
    $rawTransactions = bin2hex($transactionObj->serializeToString());
    8
    array_push($rawTransactionsList, $rawTransactions);
    9
    }
    10
    $sendTransactionsInputs = ['RawTransactions' => implode(',', $rawTransactionsList)];
    11
    $listString = $aelf->sendTransactions($sendTransactionsInputs);
    12
    print_r($listString);

    Get Peers#

  • API Path: /api/net/peers
  • Example :
  • 1
    print_r($aelf->getPeers(true));

    Add Peer#

  • API Path: /api/net/peer
  • Method: POST
  • Parameters:
  • Example :
  • 1
    $aelf->addPeer($url);

    Remove Peer#

  • API Path: /api/net/peer
  • Parameters:
  • Example :
  • 1
    $aelf->removePeer($url);

    Create Raw Transaction#

  • API Path: /api/blockchain/rawTransaction
  • Method: POST
  • Parameters:
  • Returns:
  • Example :
  • 1
    $aelf = new AElf($url);
    2
    3
    $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);
    15
    print_r($rawTransaction);

    Send Raw Transaction#

  • API Path: /api/blockchain/sendRawTransaction
  • Parameters:
  • Example :
  • 1
    $aelf = new AElf($url);
    2
    3
    $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);
    8
    print_r($execute);

    Execute Raw Transaction#

  • API Path: /api/blockchain/executeRawTransaction
  • Method: POST
  • Parameters:
  • Example :
  • 1
    $aelf = new AElf($url);
    2
    3
    $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);
    8
    print_r($execute);

    Get Merkle Path by Transaction ID#

  • API Path: /api/blockchain/merklePathByTransactionId
  • Parameters:
  • Example :
  • 1
    $aelf = new AElf($url);
    2
    3
    $block = $aelf->getBlockByHeight(1, true);
    4
    $merklePath = $aelf->getMerklePathByTransactionId($block['Body']['Transactions'][0]);
    5
    print_r($merklePath);

    Calculate Transaction Fee#

  • API Path: /api/blockChain/calculateTransactionFee
  • Method: POST
  • Parameters:
  • Returns:
  • Example :
  • 1
    $aelf = new AElf($url);
    2
    3
    $calculateTransactionFeeInputParam = [
    4
    "rawTransaction" => $rawTransactionInput,
    5
    ];
    6
    $result = $aelf->calculateTransactionFee($calculateTransactionFeeInputParam);
    7
    print_r($result);

    Get Network Info#

  • API Path: /api/net/networkInfo
  • Example :
  • 1
    $aelf = new AElf($url);
    2
    3
    print_r($aelf->getNetworkInfo());

    Get Contract File Descriptor Set#

  • API Path: /api/blockchain/contractFileDescriptorSet
  • Example :
  • 1
    $aelf = new AElf($url);
    2
    3
    $blockDto = $aelf->getBlockByHeight($blockHeight, false);
    4
    $transactionResultDtoList = $aelf->getTransactionResults($blockDto['BlockHash'], 0, 10);
    5
    foreach ($transactionResultDtoList as $v) {
    6
    $request = $aelf->getContractFileDescriptorSet($v['Transaction']['To']);
    7
    print_r($request);
    8
    }

    Get Task Queue Status#

  • API Path: /api/blockchain/taskQueueStatus
  • Example :
  • 1
    $aelf = new AElf($url);
    2
    3
    $taskQueueStatus = $aelf->getTaskQueueStatus();
    4
    print_r($taskQueueStatus);

    Execute Transaction#

  • API Path: /api/blockchain/executeTransaction
  • Example :
  • 1
    $aelf = new AElf($url);
    2
    3
    $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);
    2
    3
    $chainId = $aelf->getChainId();
    4
    print_r($chainId);

    Generate Transaction#

    1
    $aelf = new AElf($url);
    2
    3
    $param = new Hash();
    4
    $param->setValue('');
    5
    $transaction = $aelf->generateTransaction($fromAddress, $toAddress, $methodName, $param);

    Sign Transaction#

    1
    $aelf = new AElf($url);
    2
    3
    $transaction = $aelf->generateTransaction($fromAddress, $toAddress, $methodName, $param);
    4
    $signature = $aelf->signTransaction($privateKey, $transaction);

    Get Genesis Contract Address#

    1
    $aelf = new AElf($url);
    2
    3
    $genesisContractAddress = $aelf->getGenesisContractAddress();
    4
    print_r($genesisContractAddress);

    Get Address From PubKey#

    Calculate the account address according to the public key.

    1
    $aelf = new AElf($url);
    2
    3
    $pubKeyAddress = $aelf->getAddressFromPubKey('04166cf4be901dee1c21f3d97b9e4818f229bec72a5ecd56b5c4d6ce7abfc3c87e25c36fd279db721acf4258fb489b4a4406e6e6e467935d06990be9d134e5741c');
    4
    print_r($pubKeyAddress);

    Get Formatted Address#

    Convert the address to the displayed string: symbol_base58-string_base58-string_chain_id.

    1
    $aelf = new AElf($url);
    2
    3
    $addressVal = $aelf->getFormattedAddress($privateKey, $base58Address);
    4
    print_r($addressVal);

    Generate Key Pair Info#

    Generate a new key pair using ECDSA.

    1
    $aelf = new AElf($url);
    2
    3
    $pairInfo = $aelf->generateKeyPairInfo();
    4
    print_r($pairInfo);

    Get Contract Address By Name#

    1
    $aelf = new AElf($url);
    2
    3
    $bytes = new Hash();
    4
    $bytes->setValue(hex2bin(hash('sha256', 'AElf.ContractNames.Token')));
    5
    $contractAddress = $aelf->GetContractAddressByName($privateKey, $bytes);
    6
    print_r($contractAddress);

    Get Address From Private Key#

    1
    $aelf = new AElf($url);
    2
    3
    $address = $aelf->getAddressFromPrivateKey($privateKey);
    4
    print_r($address);

    Get Signature With Private Key#

    1
    $aelf = new AElf($url);
    2
    3
    $sign = $aelf->getSignatureWithPrivateKey($privateKey, $transactionId);
    4
    print_r($sign);

    Is Connected#

    1
    $aelf = new AElf($url);
    2
    3
    $isConnected = $this->aelf->isConnected();
    4
    print_r($isConnected);

    Get Transaction Fees#

    Get the transaction fee from the transaction result.

    1
    $aelf = new AElf($url);
    2
    3
    $block = $aelf->getBlockByHeight(1, true);
    4
    $transactionResult = $aelf->getTransactionResult($block['Body']['Transactions'][0]);
    5
    $transactionFees = $aelf->getTransactionFees($transactionResult);
    6
    print_r($transactionFees);

    AElf.version#

    1
    Copy code
    2
    $aelf = new AElf($url);
    3
    4
    $version = $aelf->version;

    Requirements#

  • php
  • About contributing#

    Read out [contributing guide]

    About Version#

    https://semver.org/

    Edited on: 16 July 2024 04:54:24 GMT+0