Java SDK
aelf-sdk.java - aelf Java API
Introduction#
aelf-sdk.java is a set of libraries that allow interaction with a local or remote aelf node using an HTTP connection. This documentation guides you through installing and running aelf-sdk.java, along with providing API reference documentation and examples.
For more information, you can check out the repository.
Adding aelf-sdk.java Package#
To add the aelf-sdk.java package to your project, use the following Maven dependency:
1<!-- https://mvnrepository.com/artifact/io.aelf/aelf-sdk -->2<dependency>3<groupId>io.aelf</groupId>4<artifactId>aelf-sdk</artifactId>5<version>0.X.X</version>6</dependency>
Examples#
Create Instance#
Create a new instance of AElfClient, and set the URL of an aelf chain node.
1import AElf.Client.Service;23// Create a new instance of AElfClient, change the URL if needed4AElfClient client = new AElfClient("http://127.0.0.1:1235");
Test Connection#
Check if the aelf chain node is connectable.
1boolean isConnected = client.isConnected();
Initiate a Transfer Transaction#
1// Get token contract address.2String tokenContractAddress = client.getContractAddressByName(privateKey, Sha256.getBytesSha256("AElf.ContractNames.Token"));34Client.Address.Builder to = Client.Address.newBuilder();5to.setValue(ByteString.copyFrom(Base58.decodeChecked("7s4XoUHfPuqoZAwnTV7pHWZAaivMiL8aZrDSnY9brE1woa8vz")));6Client.Address toObj = to.build();78TokenContract.TransferInput.Builder paramTransfer = TokenContract.TransferInput.newBuilder();9paramTransfer.setTo(toObj);10paramTransfer.setSymbol("ELF");11paramTransfer.setAmount(1000000000);12paramTransfer.setMemo("transfer in demo");13TokenContract.TransferInput paramTransferObj = paramTransfer.build();1415String ownerAddress = client.getAddressFromPrivateKey(privateKey);1617Transaction.Builder transactionTransfer = client.generateTransaction(ownerAddress, tokenContractAddress, "Transfer", paramTransferObj.toByteArray());18Transaction transactionTransferObj = transactionTransfer.build();19transactionTransfer.setSignature(ByteString.copyFrom(ByteArrayHelper.hexToByteArray(client.signTransaction(privateKey, transactionTransferObj))));20transactionTransferObj = transactionTransfer.build();2122// Send the transfer transaction to aelf chain node.23SendTransactionInput sendTransactionInputObj = new SendTransactionInput();24sendTransactionInputObj.setRawTransaction(Hex.toHexString(transactionTransferObj.toByteArray()));25SendTransactionOutput sendResult = client.sendTransaction(sendTransactionInputObj);2627Thread.sleep(4000);28// After the transaction is mined, query the execution results.29TransactionResultDto transactionResult = client.getTransactionResult(sendResult.getTransactionId());30System.out.println(transactionResult.getStatus());3132// Query account balance.33Client.Address.Builder owner = Client.Address.newBuilder();34owner.setValue(ByteString.copyFrom(Base58.decodeChecked(ownerAddress)));35Client.Address ownerObj = owner.build();3637TokenContract.GetBalanceInput.Builder paramGetBalance = TokenContract.GetBalanceInput.newBuilder();38paramGetBalance.setSymbol("ELF");39paramGetBalance.setOwner(ownerObj);40TokenContract.GetBalanceInput paramGetBalanceObj = paramGetBalance.build();4142Transaction.Builder transactionGetBalance = client.generateTransaction(ownerAddress, tokenContractAddress, "GetBalance", paramGetBalanceObj.toByteArray());43Transaction transactionGetBalanceObj = transactionGetBalance.build();44String signature = client.signTransaction(privateKey, transactionGetBalanceObj);45transactionGetBalance.setSignature(ByteString.copyFrom(ByteArrayHelper.hexToByteArray(signature)));46transactionGetBalanceObj = transactionGetBalance.build();4748ExecuteTransactionDto executeTransactionDto = new ExecuteTransactionDto();49executeTransactionDto.setRawTransaction(Hex.toHexString(transactionGetBalanceObj.toByteArray()));50String transactionGetBalanceResult = client.executeTransaction(executeTransactionDto);5152TokenContract.GetBalanceOutput balance = TokenContract.GetBalanceOutput.getDefaultInstance().parseFrom(ByteArrayHelper.hexToByteArray(transactionGetBalanceResult));53System.out.println(balance.getBalance());
This guide provides basic steps to interact with an aelf node using the aelf-sdk.java library. For more detailed information and advanced usage, please refer to the repository documentation.
Web API#
You can see how the Web API of the node works at {chainAddress}/swagger/index.html. For example, if you are using a local address, it would be: http://127.0.0.1:1235/swagger/index.html.
The usage of these methods is based on the AElfClient instance. So, if you don’t have one, please create it:
1import AElf.Client.Service;23// Create a new instance of AElfClient, change the URL if needed4AElfClient client = new AElfClient("http://127.0.0.1:1235");
GetChainStatus#
Get the current status of the blockchain.
Web API path: /api/blockChain/chainStatus
Parameters: None
Returns: ChainStatusDto
Example:
1client.getChainStatus();
GetContractFileDescriptorSet#
Get the protobuf definitions related to a contract.
Web API path: /api/blockChain/contractFileDescriptorSet
Parameters:
Returns: byte[]
Example:
1client.getContractFileDescriptorSet(address);
GetBlockHeight#
Get the current best height of the chain.
Web API path: /api/blockChain/blockHeight
Parameters:: None
Returns: long
Example:
1client.getBlockHeight();
GetBlock#
Get block information by block hash.
Web API path: /api/blockChain/block
Parameters:
Returns:
Example:
1client.getBlockByHash(blockHash);
GetBlockByHeight#
Web API path: /api/blockChain/blockByHeight
Parameters:
Returns:
Example:
1client.getBlockByHeight(height);
GetTransactionResult#
Web API path: /api/blockChain/transactionResult
Parameters:
Returns: TransactionResultDto
Example:
1client.getTransactionResult(transactionId);
GetTransactionResults#
Web API path: /api/blockChain/transactionResults
Parameters:
Returns: List<TransactionResultDto> - The array of transaction results
Example:
1client.getTransactionResults(blockHash, 0, 10);
GetTransactionPoolStatus#
Web API path: /api/blockChain/transactionPoolStatus
Parameters:: None
Returns:
Example:
1client.getTransactionPoolStatus();
SendTransaction#
Web API path: /api/blockChain/sendTransaction
Method: POST
Parameters:
Returns:
Example:
1client.sendTransaction(input);
SendRawTransaction#
Web API path: /api/blockChain/sendTransaction
Method: POST
Parameters:
Returns:
Example:
1client.sendRawTransaction(input);
SendTransactions#
Broadcast multiple transactions.
Web API path: /api/blockChain/sendTransactions
Method: POST
Parameters:
Returns: List<String>
Example:
1client.sendTransactions(input);
CreateRawTransaction#
Create an unsigned serialized transaction.
Web API path: /api/blockChain/rawTransaction
Method: POST
Parameters:
Returns:
Example:
1client.createRawTransaction(input);
ExecuteTransaction#
Web API path: /api/blockChain/executeTransaction
Method: POST
Parameters:
Returns: String
Example:
1client.executeTransaction(input);
ExecuteRawTransaction#
Web API path: /api/blockChain/executeRawTransaction
Method: POST
Parameters:
Returns: String
Example:
1client.executeRawTransaction(input);
GetPeers#
Get peer information about the connected network nodes.
Web API path: /api/net/peers
Parameters:
Returns:
Example:
1client.getPeers(false);
AddPeer#
Attempts to add a node to the connected network nodes.
Web API path: /api/net/peer
Method: POST
Parameters:
Returns: boolean
Example:
1client.addPeer("127.0.0.1:7001");
RemovePeer#
Attempts to remove a node from the connected network nodes.
Web API path: /api/net/peer
Method: DELETE
Parameters:
Returns: boolean
Example:
1client.removePeer("127.0.0.1:7001");
CalculateTransactionFee#
Estimate transaction fee.
Web API path: /api/blockChain/calculateTransactionFee
Method: POST
Parameters:
Returns:
Example:
1CalculateTransactionFeeOutput output = client.calculateTransactionFee(input);
GetNetworkInfo#
Web API path: /api/net/networkInfo
Parameters: None
Returns:
Example:
1client.getNetworkInfo();
AElf Client#
IsConnected#
Verify whether this SDK successfully connects to the chain.
Parameters: None
Returns: boolean
Example:
1client.isConnected();
GetGenesisContractAddress#
Parameters: None
Returns: String
Example:
1client.getGenesisContractAddress();
GetContractAddressByName#
Get the address of a contract by the given contract name hash.
Parameters::
Returns: String
Example:
1client.getContractAddressByName(privateKey, contractNameHash);
GenerateTransaction#
Build a transaction from the input parameters.
Parameters:
Returns: Transaction
Example:
1client.generateTransaction(from, to, methodName, input);
GetFormattedAddress#
Convert the Address to the displayed string: symbol_base58-string_base58-String-chain-id.
Parameters:
Returns: String
Example:
1client.getFormattedAddress(privateKey, address);
SignTransaction#
Parameters:
Returns: String
Example:
1client.signTransaction(privateKeyHex, transaction);
GetAddressFromPubKey#
Parameters:
Returns: String
Example:
1client.getAddressFromPubKey(pubKey);
GetAddressFromPrivateKey#
Parameters:
Returns: String
Example:
1client.getAddressFromPrivateKey(privateKey);
GenerateKeyPairInfo#
Parameters: None
Returns:
Example:
1client.generateKeyPairInfo();
Supports#
Edited on: 16 July 2024 04:44:34 GMT+0