logo

C# SDK

aelf-sdk.cs - aelf C# API

aelf-sdk.cs is a C# library that facilitates communication with an aelf node over HTTP. Below is a comprehensive guide on how to install and use the aelf-sdk.cs package, along with some example usages.

Introduction#

aelf-sdk.cs is a collection of libraries designed for interaction with both local and remote aelf nodes via HTTP connections. This documentation provides instructions on how to install and run aelf-sdk.cs, and includes API reference documentation with examples. aelf-sdk.cs

Adding aelf-sdk.cs package#

To use aelf-sdk.cs, you need to add the AElf.Client package to your project. This can be done using various methods:

Using Package Manager#

Open the Package Manager Console in Visual Studio and run:

1
PM> Install-Package AElf.Client

Using .NET CLI#

Run the following command in your terminal:

1
dotnet add package AElf.Client

Using PackageReference#

Add the following line to your .csproj file:

1
<PackageReference Include="AElf.Client" Version="X.X.X" />

Replace X.X.X with the desired version of the AElf.Client package.

Examples#

Create Instance#

Create a new instance of AElfClient and set the URL of an aelf chain node.

1
using AElf.Client.Service;
2
3
// Create a new instance of AElfClient
4
AElfClient client = new AElfClient("http://127.0.0.1:1235");

Test Connection#

Check that the aelf chain node is connectable.

1
var isConnected = await client.IsConnectedAsync();
2
Console.WriteLine($"Connected: {isConnected}");

Initiate a Transfer Transaction#

1
// Get token contract address.
2
var tokenContractAddress = await client.GetContractAddressByNameAsync(HashHelper.ComputeFrom("AElf.ContractNames.Token"));
3
4
var methodName = "Transfer";
5
var param = new TransferInput
6
{
7
To = new Address {Value = Address.FromBase58("7s4XoUHfPuqoZAwnTV7pHWZAaivMiL8aZrDSnY9brE1woa8vz").Value},
8
Symbol = "ELF",
9
Amount = 1000000000,
10
Memo = "transfer in demo"
11
};
12
var ownerAddress = client.GetAddressFromPrivateKey(PrivateKey);
13
14
// Generate a transfer transaction.
15
var transaction = await client.GenerateTransaction(ownerAddress, tokenContractAddress.ToBase58(), methodName, param);
16
var txWithSign = client.SignTransaction(PrivateKey, transaction);
17
18
// Send the transfer transaction to AElf chain node.
19
var result = await client.SendTransactionAsync(new SendTransactionInput
20
{
21
RawTransaction = txWithSign.ToByteArray().ToHex()
22
});
23
24
await Task.Delay(4000);
25
// After the transaction is mined, query the execution results.
26
var transactionResult = await client.GetTransactionResultAsync(result.TransactionId);
27
Console.WriteLine(transactionResult.Status);
28
29
// Query account balance.
30
var paramGetBalance = new GetBalanceInput
31
{
32
Symbol = "ELF",
33
Owner = new Address {Value = Address.FromBase58(ownerAddress).Value}
34
};
35
var transactionGetBalance =await client.GenerateTransaction(ownerAddress, tokenContractAddress.ToBase58(), "GetBalance", paramGetBalance);
36
var txWithSignGetBalance = client.SignTransaction(PrivateKey, transactionGetBalance);
37
38
var transactionGetBalanceResult = await client.ExecuteTransactionAsync(new ExecuteTransactionDto
39
{
40
RawTransaction = txWithSignGetBalance.ToByteArray().ToHex()
41
});
42
43
var balance = GetBalanceOutput.Parser.ParseFrom(ByteArrayHelper.HexstringToByteArray(transactionGetBalanceResult));
44
Console.WriteLine(balance.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.

Here are the examples and code snippets for interacting with the aelf Web API using the AElfClient instance.

Create Instance#

Create a new instance of AElfClient and set the URL of an aelf chain node.

1
using AElf.Client.Service;
2
3
// Create a new instance of AElfClient, change the URL if needed
4
AElfClient client = new AElfClient("http://127.0.0.1:1235");

Get Chain Status#

  • Web API path: /api/blockChain/chainStatus
  • Parameters : None
  • Returns: ChainStatusDto
  • Example:

    1
    await client.GetChainStatusAsync();

    Get Contract File Descriptor Set#

  • Web API path: /api/blockChain/contractFileDescriptorSet
  • Parameters :
  • Returns: []byte
  • Example:

    1
    await client.GetContractFileDescriptorSetAsync(address);

    Get Block Height#

  • Web API path: /api/blockChain/blockHeight
  • Parameters : None
  • Returns: long
  • Example:

    1
    await client.GetBlockHeightAsync();

    Get Block Information by Block Hash#

  • Web API path: /api/blockChain/block
  • Parameters :
  • Returns: BlockDto
  • Example:

    1
    await client.GetBlockByHashAsync(blockHash);

    Get Block Information by Block Height#

  • Web API path: /api/blockChain/blockByHeight
  • Parameters :
  • Returns: BlockDto
  • Example:

    1
    await client.GetBlockByHeightAsync(height);

    Get Transaction Result#

  • Web API path: /api/blockChain/transactionResult
  • Parameters :
  • Returns: TransactionResultDto
  • Example:

    1
    await client.GetTransactionResultAsync(transactionId);

    Get Multiple Transaction Results in a Block#

  • Web API path: /api/blockChain/transactionResults
  • Parameters :
  • Returns: List<TransactionResultDto> - The array of transaction result:
  • Example:

    1
    await client.GetTransactionResultsAsync(blockHash, 0, 10);

    Get Transaction Pool Status#

  • Web API path: /api/blockChain/transactionPoolStatus
  • Parameters : None
  • Returns: TransactionPoolStatusOutput
  • Example:

    1
    var transactionPoolStatus = await client.GetTransactionPoolStatusAsync();

    Send Transaction#

  • Web API path: /api/blockChain/sendTransaction (POST)
  • Parameters :
  • Returns: SendRawTransactionOutput
  • Example:

    1
    var sendTransactionOutput = await client.SendTransactionAsync(sendTransactionInput);

    Send Raw Transaction#

  • Web API path: /api/blockChain/sendTransaction (POST)
  • Parameters :
  • Returns: SendRawTransactionOutput
  • Example:

    1
    var sendRawTransactionInput = new SendRawTransactionInput
    2
    {
    3
    Transaction = "YOUR_RAW_TRANSACTION",
    4
    Signature = "YOUR_SIGNATURE",
    5
    ReturnTransaction = true
    6
    };
    7
    var sendRawTransactionOutput = await client.SendRawTransactionAsync(sendRawTransactionInput);
    8
    Console.WriteLine($"Transaction ID: {sendRawTransactionOutput.TransactionId}");

    Send Multiple Transactions#

  • Web API path: /api/blockChain/sendTransactions (POST)
  • Parameters :
  • Returns: string[]
  • Example:

    1
    await client.SendTransactionsAsync(input);

    Create Raw Transaction#

  • Web API path: /api/blockChain/rawTransaction (POST)
  • Parameters :
  • Returns:
  • Example:

    1
    await client.CreateRawTransactionAsync(input);

    Execute Transaction#

  • Web API path: /api/blockChain/executeTransaction (POST)
  • Parameters :
  • Returns: string
  • Example:

    1
    await client.ExecuteRawTransactionAsync(input);

    Execute Raw Transaction#

  • Web API path: /api/blockChain/executeRawTransaction (POST)
  • Parameters :
  • Returns: string
  • Example:

    1
    await client.ExecuteRawTransactionAsync(input);

    Get Peers#

  • Web API path: /api/net/peers
  • Parameters :
  • Returns: List<PeerDto>
  • Example:

    1
    await client.GetPeersAsync(false);

    Add Peer#

    Attempts to remove a node from the connected network nodes.

  • Web API path: /api/net/peer (POST)
  • Parameters :
  • Returns: bool
  • Example:

    1
    await client.AddPeerAsync("127.0.0.1:7001");

    Remove Peer#

    Attempts to remove a node from the connected network nodes.

  • Web API path: /api/net/peer (DELETE)
  • Parameters :
  • Returns: bool
  • 1
    await client.RemovePeerAsync("127.0.0.1:7001");

    Calculate Transaction Fee#

  • Web API path: /api/blockChain/calculateTransactionFee (POST)
  • Parameters :
  • Returns:
  • Example:

    1
    var input = new CalculateTransactionFeeInput{
    2
    RawTransaction = RawTransaction
    3
    };
    4
    await Client.CalculateTransactionFeeAsync(input);

    Get Network Information#

  • Web API path: /api/net/networkInfo
  • Parameters : Empty
  • Returns:
  • Example:

    1
    await client.GetNetworkInfoAsync();

    These examples demonstrate how to use the aelf Web API in C# using the AElfClient class to interact with the aelf blockchain, including checking chain status, handling transactions, and managing network peers.

    aelf Client#

    IsConnected#

    Verify whether this SDK successfully connects to the chain.

  • Parameters: None
  • Returns :
  • Example:

    1
    bool isConnected = await client.IsConnectedAsync();
    2
    Console.WriteLine($"Is Connected: {isConnected}");

    GetGenesisContractAddress#

    Get the address of the genesis contract.

  • Parameters: None
  • Returns :
  • Example:

    1
    await client.GetGenesisContractAddressAsync();

    GetContractAddressByName#

    Get the address of a contract by the given contract name hash.

  • Parameters:
  • Returns :
  • Example:

    1
    await client.GetContractAddressByNameAsync(contractNameHash);

    GenerateTransaction#

    Build a transaction from the input parameters.

  • Parameters:
  • Returns :
  • Example:

    1
    await client.GenerateTransactionAsync(from, to, methodName, input);

    GetFormattedAddress#

    Convert the Address to the displayed string format: symbol_base58-string_base58-string_chain-id.

  • Parameters:
  • Returns :
  • Example:

    1
    await client.GetFormattedAddressAsync(address);

    SignTransaction#

  • Parameters:
  • Returns :
  • Example:

    1
    client.SignTransaction(privateKeyHex, transaction);

    GetAddressFromPubKey#

    Get the account address through the public key.

  • Parameters:
  • Returns :
  • Example:

    1
    client.GetAddressFromPubKey(pubKey);

    GetAddressFromPrivateKey#

    Get the account address through the private key.

  • Parameters:
  • Returns :
  • Example:

    1
    client.GetAddressFromPrivateKey(privateKeyHex);

    GenerateKeyPairInfo#

    Generate a new account key pair.

  • Parameters: None
  • Returns :
  • Example:

    1
    client.GenerateKeyPairInfo();

    Supports#

    .NET Standard 2.0

    Edited on: 15 July 2024 05:39:19 GMT+0