Go SDK
aelf-sdk.go - aelf Go API Introduction#
This document provides information on how to use the aelf Go SDK (aelf-sdk.go) to interact with an aelf node. The SDK allows you to communicate with a local or remote aelf node using HTTP. Here you will find instructions for setting up the SDK, examples of how to use it, and a brief description of its main functions.
For additional information, please visit the repository: aelf-sdk.go
Installation#
To install the aelf-sdk.go package, run the following command:
1go get -u github.com/AElfProject/aelf-sdk.go
Examples#
Create instance#
Create a new instance of AElfClient and set the URL of an AElf chain node:
1import ("github.com/AElfProject/aelf-sdk.go/client")23var aelf = client.AElfClient{4Host: "http://127.0.0.1:8000",5Version: "1.0",6PrivateKey: "cd86ab6347d8e52bbbe8532141fc59ce596268143a308d1d40fedf385528b458",7}
Initiating a Transfer Transaction#
Here is an example of how to initiate a transfer transaction using the aelf Go SDK:
1// Get token contract address.2tokenContractAddress, _ := aelf.GetContractAddressByName("AElf.ContractNames.Token")3fromAddress := aelf.GetAddressFromPrivateKey(aelf.PrivateKey)4methodName := "Transfer"5toAddress, _ := util.Base58StringToAddress("7s4XoUHfPuqoZAwnTV7pHWZAaivMiL8aZrDSnY9brE1woa8vz")67params := &pb.TransferInput{8To: toAddress,9Symbol: "ELF",10Amount: 1000000000,11Memo: "transfer in demo",12}13paramsByte, _ := proto.Marshal(params)1415// Generate a transfer transaction.16transaction, _ := aelf.CreateTransaction(fromAddress, tokenContractAddress, methodName, paramsByte)17signature, _ := aelf.SignTransaction(aelf.PrivateKey, transaction)18transaction.Signature = signature1920// Send the transfer transaction to aelf chain node.21transactionByets, _ := proto.Marshal(transaction)22sendResult, _ := aelf.SendTransaction(hex.EncodeToString(transactionByets))2324time.Sleep(time.Duration(4) * time.Second)25transactionResult, _ := aelf.GetTransactionResult(sendResult.TransactionID)26fmt.Println(transactionResult)2728// Query account balance.29ownerAddress, _ := util.Base58StringToAddress(fromAddress)30getBalanceInput := &pb.GetBalanceInput{31Symbol: "ELF",32Owner: ownerAddress,33}34getBalanceInputByte, _ := proto.Marshal(getBalanceInput)3536getBalanceTransaction, _ := aelf.CreateTransaction(fromAddress, tokenContractAddress, "GetBalance", getBalanceInputByte)37getBalanceTransaction.Params = getBalanceInputByte38getBalanceSignature, _ := aelf.SignTransaction(aelf.PrivateKey, getBalanceTransaction)39getBalanceTransaction.Signature = getBalanceSignature4041getBalanceTransactionByets, _ := proto.Marshal(getBalanceTransaction)42getBalanceResult, _ := aelf.ExecuteTransaction(hex.EncodeToString(getBalanceTransactionByets))43balance := &pb.GetBalanceOutput{}44getBalanceResultBytes, _ := hex.DecodeString(getBalanceResult)45proto.Unmarshal(getBalanceResultBytes, balance)46fmt.Println(balance)
Web API#
You can see how the Web API of the node works at {chainAddress}/swagger/index.html. For example, on a local address: http://127.0.0.1:1235/swagger/index.html.
The usage of these methods is based on the AElfClient instance. If you don’t have one, please create it:
1import ("github.com/AElfProject/aelf-sdk.go/client")23var aelf = client.AElfClient{4Host: "http://127.0.0.1:8000",5Version: "1.0",6PrivateKey: "680afd630d82ae5c97942c4141d60b8a9fedfa5b2864fca84072c17ee1f72d9d",7}
GetChainStatus#
Example:
1chainStatus, err := aelf.GetChainStatus()
GetContractFileDescriptorSet#
Get the protobuf definitions related to a contract.
Example:
1contractFile, err := aelf.GetContractFileDescriptorSet("pykr77ft9UUKJZLVq15wCH8PinBSjVRQ12sD1Ayq92mKFsJ1i")
GetBlockHeight#
Get the current best height of the chain.
Example:
1height, err := aelf.GetBlockHeight()
GetBlock#
Get block information by block hash.
Example:
1block, err := aelf.GetBlockByHash(blockHash, true)
GetBlockByHeight#
Example:
1block, err := aelf.GetBlockByHeight(100, true)
GetTransactionResult#
Example:
1transactionResult, err := aelf.GetTransactionResult(transactionID)
GetTransactionResults#
Get multiple transaction results in a block.
Example:
1transactionResults, err := aelf.GetTransactionResults(blockHash, 0, 10)
GetTransactionPoolStatus#
Example:
1poolStatus, err := aelf.GetTransactionPoolStatus();
SendTransaction#
Example:
1sendResult, err := aelf.SendTransaction(input)
SendRawTransaction#
Example:
1sendRawResult, err := aelf.SendRawTransaction(input)
SendTransactions#
Example:
1results, err := aelf.SendTransactions(transactions)
CreateRawTransaction#
Creates an unsigned serialized transaction.
Example:
1result, err := aelf.CreateRawTransaction(input)
ExecuteTransaction#
Call a read-only method on a contract.
Example:
1executeresult, err := aelf.ExecuteTransaction(rawTransaction)
ExecuteRawTransaction#
Call a read-only method on a contract.
Example:
1executeRawresult, err := aelf.ExecuteRawTransaction(executeRawinput)
GetPeers#
Get peer info about the connected network nodes.
Example:
1peers, err := aelf.GetPeers(false)
AddPeer#
Attempts to add a node to the connected network nodes.
Example:
1addResult, err := aelf.AddPeer("127.0.0.1:7001")
RemovePeer#
Attempts to remove a node from the connected network nodes.
Example:
1removeResult, err := aelf.RemovePeer("127.0.0.1:7001")
CalculateTransactionFee#
Estimate transaction fee.
Example:
1calculateTransactionFee, err := aelf.CalculateTransactionFee(transactionFeeInput)
GetNetworkInfo#
Get the network information of the node.
Example:
1networkInfo, err := aelf.GetNetworkInfo()
aelf Client#
IsConnected#
Check if the SDK is successfully connected to the blockchain.
Example:
1isConnected := aelf.IsConnected()
GetGenesisContractAddress#
Example:
1contractAddress, err := aelf.GetGenesisContractAddress()
GetContractAddressByName#
Get the address of a contract by its name hash.
Example:
1contractAddress, err := aelf.GetContractAddressByName("AElf.ContractNames.Token")
CreateTransaction#
Build a transaction with the provided parameters.
Example:
1transaction, err := aelf.CreateTransaction(fromAddress, toAddress, methodName, param)
GetFormattedAddress#
Convert an address to a displayable string format: symbol_base58-string_base58-string-chain-id.
Example:
1formattedAddress, err := aelf.GetFormattedAddress(address)
SignTransaction#
Sign a transaction using a private key.
Example:
1signature, err := aelf.SignTransaction(privateKey, transaction)
GetAddressFromPubKey#
Example:
1address := aelf.GetAddressFromPubKey(pubKey)
GetAddressFromPrivateKey#
Example:
1address := aelf.GetAddressFromPrivateKey(privateKey)
GenerateKeyPairInfo#
Example:
1keyPair := aelf.GenerateKeyPairInfo()
Supported Go Version#
Edited on: 15 July 2024 05:49:28 GMT+0