aelf-sdk.go - AELF Go API¶
This Go library helps in the communication with an AElf node. You can find out more here.
Introduction¶
aelf-sdk.go is a collection of libraries which allow you to interact with a local or remote aelf node, using a HTTP connection.
The following documentation will guide you through installing and running aelf-sdk.go, as well as providing a API reference documentation with examples.
If you need more information you can check out the repo : aelf-sdk.go
Adding aelf-sdk.go package¶
First you need to get aelf-sdk.go:
> go get -u github.com/AElfProject/aelf-sdk.go
Examples¶
Create instance¶
Create a new instance of AElfClient, and set url of an AElf chain node.
import ("github.com/AElfProject/aelf-sdk.go/client")
var aelf = client.AElfClient{
Host: "http://127.0.0.1:8000",
Version: "1.0",
PrivateKey: "cd86ab6347d8e52bbbe8532141fc59ce596268143a308d1d40fedf385528b458",
}
Initiate a transfer transaction¶
// Get token contract address.
tokenContractAddress, _ := aelf.GetContractAddressByName("AElf.ContractNames.Token")
fromAddress := aelf.GetAddressFromPrivateKey(aelf.PrivateKey)
methodName := "Transfer"
toAddress, _ := util.Base58StringToAddress("7s4XoUHfPuqoZAwnTV7pHWZAaivMiL8aZrDSnY9brE1woa8vz")
params := &pb.TransferInput{
To: toAddress,
Symbol: "ELF",
Amount: 1000000000,
Memo: "transfer in demo",
}
paramsByte, _ := proto.Marshal(params)
// Generate a transfer transaction.
transaction, _ := aelf.CreateTransaction(fromAddress, tokenContractAddress, methodName, paramsByte)
signature, _ := aelf.SignTransaction(aelf.PrivateKey, transaction)
transaction.Signature = signature
// Send the transfer transaction to AElf chain node.
transactionByets, _ := proto.Marshal(transaction)
sendResult, _ := aelf.SendTransaction(hex.EncodeToString(transactionByets))
time.Sleep(time.Duration(4) * time.Second)
transactionResult, _ := aelf.GetTransactionResult(sendResult.TransactionID)
fmt.Println(transactionResult)
// Query account balance.
ownerAddress, _ := util.Base58StringToAddress(fromAddress)
getBalanceInput := &pb.GetBalanceInput{
Symbol: "ELF",
Owner: ownerAddress,
}
getBalanceInputByte, _ := proto.Marshal(getBalanceInput)
getBalanceTransaction, _ := aelf.CreateTransaction(fromAddress, tokenContractAddress, "GetBalance", getBalanceInputByte)
getBalanceTransaction.Params = getBalanceInputByte
getBalanceSignature, _ := aelf.SignTransaction(aelf.PrivateKey, getBalanceTransaction)
getBalanceTransaction.Signature = getBalanceSignature
getBalanceTransactionByets, _ := proto.Marshal(getBalanceTransaction)
getBalanceResult, _ := aelf.ExecuteTransaction(hex.EncodeToString(getBalanceTransactionByets))
balance := &pb.GetBalanceOutput{}
getBalanceResultBytes, _ := hex.DecodeString(getBalanceResult)
proto.Unmarshal(getBalanceResultBytes, balance)
fmt.Println(balance)
Web API¶
You can see how the Web Api of the node works in {chainAddress}/swagger/index.html
tip: for an example, my local address: ‘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:
import ("github.com/AElfProject/aelf-sdk.go/client")
var aelf = client.AElfClient{
Host: "http://127.0.0.1:8000",
Version: "1.0",
PrivateKey: "680afd630d82ae5c97942c4141d60b8a9fedfa5b2864fca84072c17ee1f72d9d",
}
GetChainStatus¶
Get the current status of the block chain.
Web API path
/api/blockChain/chainStatus
Parameters
Empty
Returns
ChainStatusDto
ChainId - stringBranches - map[string]interface{}NotLinkedBlocks - map[string]interface{}LongestChainHeight - int64LongestChainHash - stringGenesisBlockHash - stringGenesisContractAddress - stringLastIrreversibleBlockHash - stringLastIrreversibleBlockHeight - int64BestChainHash - stringBestChainHeight - int64
Example
chainStatus, err := aelf.GetChainStatus()
GetContractFileDescriptorSet¶
Get the protobuf definitions related to a contract.
Web API path
/api/blockChain/contractFileDescriptorSet
Parameters
contractAddress - stringaddress of a contract
Returns
byte[]
Example
contractFile, err := aelf.GetContractFileDescriptorSet("pykr77ft9UUKJZLVq15wCH8PinBSjVRQ12sD1Ayq92mKFsJ1i")
GetBlockHeight¶
Get current best height of the chain.
Web API path
/api/blockChain/blockHeight
Parameters
Empty
Returns
float64
Example
height, err := aelf.GetBlockHeight()
GetBlock¶
Get block information by block hash.
Web API path
/api/blockChain/block
Parameters
blockHash - stringincludeTransactions - bool:
truerequire transaction ids list in the blockfalseDoesn’t require transaction ids list in the block
Returns
BlockDto
BlockHash - stringHeader - BlockHeaderDtoPreviousBlockHash - stringMerkleTreeRootOfTransactions - stringMerkleTreeRootOfWorldState - stringExtra - stringHeight - int64Time - stringChainId - stringBloom - stringSignerPubkey - string
Body - BlockBodyDtoTransactionsCount - intTransactions - []string
Example
block, err := aelf.GetBlockByHash(blockHash, true)
GetBlockByHeight¶
Web API path
/api/blockChain/blockByHeight
Get block information by block height.
Parameters
blockHeight - int64includeTransactions - bool:
truerequire transaction ids list in the blockfalseDoesn’t require transaction ids list in the block
Returns
BlockDto
BlockHash - stringHeader - BlockHeaderDtoPreviousBlockHash - stringMerkleTreeRootOfTransactions - stringMerkleTreeRootOfWorldState - stringExtra - stringHeight - int64Time - stringChainId - stringBloom - stringSignerPubkey - string
Body - BlockBodyDtoTransactionsCount - intTransactions - []string
Example
block, err := aelf.GetBlockByHeight(100, true)
GetTransactionResult¶
Get the result of a transaction.
Web API path
/api/blockChain/transactionResult
Parameters
transactionId - string
Returns
TransactionResultDto
TransactionId - stringStatus - stringLogs - []LogEventDtoAddress - stringName - stringIndexed - []stringNonIndexed - string
Bloom - stringBlockNumber - int64BlockHash - stringTransaction - TransactionDtoFrom - stringTo - stringRefBlockNumber - int64RefBlockPrefix - stringMethodName - stringParams - stringSignature - string
ReturnValue - stringError - string
Example
transactionResult, err := aelf.GetTransactionResult(transactionID)
GetTransactionResults¶
Get multiple transaction results in a block.
Web API path
/api/blockChain/transactionResults
Parameters
blockHash - stringoffset - intlimit - int
Returns
[]TransactionResultDto - The array of transaction result:
- the transaction result object
Example
transactionResults, err := aelf.GetTransactionResults(blockHash, 0, 10)
GetTransactionPoolStatus¶
Get the transaction pool status.
Web API path
/api/blockChain/transactionPoolStatus
Parameters
Empty
Returns
TransactionPoolStatusOutput
Queued- intValidated- int
Example
poolStatus, err := aelf.GetTransactionPoolStatus()
SendTransaction¶
Broadcast a transaction.
Web API path
/api/blockChain/sendTransaction
POST
Parameters
SendTransactionInput - Serialization of data into protobuf data:
RawTransaction - string
Returns
SendTransactionOutput
TransactionId - string
Example
sendResult, err := aelf.SendTransaction(input)
SendRawTransaction¶
Broadcast a transaction.
Web API path
/api/blockChain/sendTransaction
POST
Parameters
SendRawTransactionInput - Serialization of data into protobuf data:
Transaction - stringSignature - stringReturnTransaction - bool
Returns
SendRawTransactionOutput
TransactionId - stringTransaction - TransactionDto
Example
sendRawResult, err := aelf.SendRawTransaction(input)
SendTransactions¶
Broadcast multiple transactions.
Web API path
/api/blockChain/sendTransactions
POST
Parameters
rawTransactions - string - Serialization of data into protobuf data:
Returns
[]interface{}
Example
results, err := aelf.SendTransactions(transactions)
CreateRawTransaction¶
Creates an unsigned serialized transaction.
Web API path
/api/blockChain/rawTransaction
POST
Parameters
CreateRawTransactionInput
From - stringTo - stringRefBlockNumber - int64RefBlockHash - stringMethodName - stringParams - string
Returns
CreateRawTransactionOutput- Serialization of data into protobuf data:
RawTransactions - string
Example
result, err := aelf.CreateRawTransaction(input)
ExecuteTransaction¶
Call a read-only method on a contract.
Web API path
/api/blockChain/executeTransaction
POST
Parameters
rawTransaction - string
Returns
string
Example
executeresult, err := aelf.ExecuteTransaction(rawTransaction)
ExecuteRawTransaction¶
Call a read-only method on a contract.
Web API path
/api/blockChain/executeRawTransaction
POST
Parameters
ExecuteRawTransactionDto - Serialization of data into protobuf data:
RawTransaction - stringSignature - string
Returns
string
Example
executeRawresult, err := aelf.ExecuteRawTransaction(executeRawinput)
GetPeers¶
Get peer info about the connected network nodes.
Web API path
/api/net/peers
Parameters
withMetrics - bool
Returns
[]PeerDto
IpAddress - stringProtocolVersion - intConnectionTime - int64ConnectionStatus - stringInbound - boolBufferedTransactionsCount - intBufferedBlocksCount - intBufferedAnnouncementsCount - intRequestMetrics - []RequestMetricRoundTripTime - int64MethodName - stringInfo - stringRequestTime - string
Example
peers, err := aelf.GetPeers(false);
AddPeer¶
Attempts to add a node to the connected network nodes.
Web API path
/api/net/peer
POST
Parameters
ipAddress - string
Returns
bool
Example
addResult, err := aelf.AddPeer("127.0.0.1:7001");
RemovePeer¶
Attempts to remove a node from the connected network nodes.
Web API path
/api/net/peer
DELETE
Parameters
ipAddress - string
Returns
bool
Example
removeResult, err := aelf.RemovePeer("127.0.0.1:7001");
GetNetworkInfo¶
Get the network information of the node.
Web API path
/api/net/networkInfo
Parameters
Empty
Returns
NetworkInfoOutput
Version - stringProtocolVersion - intConnections - int
Example
networkInfo, err := aelf.GetNetworkInfo()
AElf Client¶
IsConnected¶
Verify whether this sdk successfully connects the chain.
Parameters
Empty
Returns
bool
Example
isConnected := aelf.IsConnected()
GetGenesisContractAddress¶
Get the address of genesis contract.
Parameters
Empty
Returns
string
Example
contractAddress, err := aelf.GetGenesisContractAddress()
GetContractAddressByName¶
Get address of a contract by given contractNameHash.
Parameters
contractNameHash - string
Returns
Address
Example
contractAddress, err := aelf.GetContractAddressByName("AElf.ContractNames.Token")
CreateTransaction¶
Build a transaction from the input parameters.
Parameters
from - stringto - stringmethodName - stringparams - []byte
Returns
Transaction
Example
transaction, err := aelf.CreateTransaction(fromAddress, toAddress, methodName, param)
GetFormattedAddress¶
Convert the Address to the displayed string:symbol_base58-string_base58-string-chain-id.
Parameters
address - string
Returns
string
Example
formattedAddress, err := aelf.GetFormattedAddress(address);
SignTransaction¶
Sign a transaction using private key.
Parameters
privateKey - stringtransaction - Transaction
Returns
[]byte
Example
signature, err := aelf.SignTransaction(privateKey, transaction)
GetAddressFromPubKey¶
Get the account address through the public key.
Parameters
pubKey - string
Returns
string
Example
address := aelf.GetAddressFromPubKey(pubKey);
GetAddressFromPrivateKey¶
Get the account address through the private key.
Parameters
privateKey - string
Returns
string
Example
address := aelf.GetAddressFromPrivateKey(privateKey)
GenerateKeyPairInfo¶
Generate a new account key pair.
Parameters
Empty
Returns
KeyPairInfo
PrivateKey - stringPublicKey - stringAddress - string
Example
keyPair := aelf.GenerateKeyPairInfo()
Supports¶
Go 1.13