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:
1PM> Install-Package AElf.Client
Using .NET CLI#
Run the following command in your terminal:
1dotnet 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.
1using AElf.Client.Service;23// Create a new instance of AElfClient4AElfClient client = new AElfClient("http://127.0.0.1:1235");
Test Connection#
Check that the aelf chain node is connectable.
1var isConnected = await client.IsConnectedAsync();2Console.WriteLine($"Connected: {isConnected}");
Initiate a Transfer Transaction#
1// Get token contract address.2var tokenContractAddress = await client.GetContractAddressByNameAsync(HashHelper.ComputeFrom("AElf.ContractNames.Token"));34var methodName = "Transfer";5var param = new TransferInput6{7To = new Address {Value = Address.FromBase58("7s4XoUHfPuqoZAwnTV7pHWZAaivMiL8aZrDSnY9brE1woa8vz").Value},8Symbol = "ELF",9Amount = 1000000000,10Memo = "transfer in demo"11};12var ownerAddress = client.GetAddressFromPrivateKey(PrivateKey);1314// Generate a transfer transaction.15var transaction = await client.GenerateTransaction(ownerAddress, tokenContractAddress.ToBase58(), methodName, param);16var txWithSign = client.SignTransaction(PrivateKey, transaction);1718// Send the transfer transaction to AElf chain node.19var result = await client.SendTransactionAsync(new SendTransactionInput20{21RawTransaction = txWithSign.ToByteArray().ToHex()22});2324await Task.Delay(4000);25// After the transaction is mined, query the execution results.26var transactionResult = await client.GetTransactionResultAsync(result.TransactionId);27Console.WriteLine(transactionResult.Status);2829// Query account balance.30var paramGetBalance = new GetBalanceInput31{32Symbol = "ELF",33Owner = new Address {Value = Address.FromBase58(ownerAddress).Value}34};35var transactionGetBalance =await client.GenerateTransaction(ownerAddress, tokenContractAddress.ToBase58(), "GetBalance", paramGetBalance);36var txWithSignGetBalance = client.SignTransaction(PrivateKey, transactionGetBalance);3738var transactionGetBalanceResult = await client.ExecuteTransactionAsync(new ExecuteTransactionDto39{40RawTransaction = txWithSignGetBalance.ToByteArray().ToHex()41});4243var balance = GetBalanceOutput.Parser.ParseFrom(ByteArrayHelper.HexstringToByteArray(transactionGetBalanceResult));44Console.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.
1using AElf.Client.Service;23// Create a new instance of AElfClient, change the URL if needed4AElfClient client = new AElfClient("http://127.0.0.1:1235");
Get Chain Status#
Example:
1await client.GetChainStatusAsync();
Get Contract File Descriptor Set#
Example:
1await client.GetContractFileDescriptorSetAsync(address);
Get Block Height#
Example:
1await client.GetBlockHeightAsync();
Get Block Information by Block Hash#
Example:
1await client.GetBlockByHashAsync(blockHash);
Get Block Information by Block Height#
Example:
1await client.GetBlockByHeightAsync(height);
Get Transaction Result#
Example:
1await client.GetTransactionResultAsync(transactionId);
Get Multiple Transaction Results in a Block#
Example:
1await client.GetTransactionResultsAsync(blockHash, 0, 10);
Get Transaction Pool Status#
Example:
1var transactionPoolStatus = await client.GetTransactionPoolStatusAsync();
Send Transaction#
Example:
1var sendTransactionOutput = await client.SendTransactionAsync(sendTransactionInput);
Send Raw Transaction#
Example:
1var sendRawTransactionInput = new SendRawTransactionInput2{3Transaction = "YOUR_RAW_TRANSACTION",4Signature = "YOUR_SIGNATURE",5ReturnTransaction = true6};7var sendRawTransactionOutput = await client.SendRawTransactionAsync(sendRawTransactionInput);8Console.WriteLine($"Transaction ID: {sendRawTransactionOutput.TransactionId}");
Send Multiple Transactions#
Example:
1await client.SendTransactionsAsync(input);
Create Raw Transaction#
Example:
1await client.CreateRawTransactionAsync(input);
Execute Transaction#
Example:
1await client.ExecuteRawTransactionAsync(input);
Execute Raw Transaction#
Example:
1await client.ExecuteRawTransactionAsync(input);
Get Peers#
Example:
1await client.GetPeersAsync(false);
Add Peer#
Attempts to remove a node from the connected network nodes.
Example:
1await client.AddPeerAsync("127.0.0.1:7001");
Remove Peer#
Attempts to remove a node from the connected network nodes.
1await client.RemovePeerAsync("127.0.0.1:7001");
Calculate Transaction Fee#
Example:
1var input = new CalculateTransactionFeeInput{2RawTransaction = RawTransaction3};4await Client.CalculateTransactionFeeAsync(input);
Get Network Information#
Example:
1await 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.
Example:
1bool isConnected = await client.IsConnectedAsync();2Console.WriteLine($"Is Connected: {isConnected}");
GetGenesisContractAddress#
Get the address of the genesis contract.
Example:
1await client.GetGenesisContractAddressAsync();
GetContractAddressByName#
Get the address of a contract by the given contract name hash.
Example:
1await client.GetContractAddressByNameAsync(contractNameHash);
GenerateTransaction#
Build a transaction from the input parameters.
Example:
1await client.GenerateTransactionAsync(from, to, methodName, input);
GetFormattedAddress#
Convert the Address to the displayed string format: symbol_base58-string_base58-string_chain-id.
Example:
1await client.GetFormattedAddressAsync(address);
SignTransaction#
Example:
1client.SignTransaction(privateKeyHex, transaction);
GetAddressFromPubKey#
Get the account address through the public key.
Example:
1client.GetAddressFromPubKey(pubKey);
GetAddressFromPrivateKey#
Get the account address through the private key.
Example:
1client.GetAddressFromPrivateKey(privateKeyHex);
GenerateKeyPairInfo#
Generate a new account key pair.
Example:
1client.GenerateKeyPairInfo();
Supports#
.NET Standard 2.0
Edited on: 15 July 2024 05:39:19 GMT+0