Algorand Account Operations with .NET: Create, Check Balance, and Send Transactions

·

Developing on the Algorand blockchain using .NET offers a powerful and efficient way to build decentralized applications. This guide dives into core account operations—creating accounts, checking balances, and sending transactions—using the Algorand .NET SDK. Whether you're new to blockchain development or expanding your skills into Algorand, this tutorial provides actionable insights and clean code examples to get you started quickly.

Before proceeding, ensure your development environment is set up. If not, refer to the first part of this series for complete setup instructions.

👉 Get started with blockchain development using powerful tools and resources


Connecting to Algorand and Retrieving Network Information

To interact with the Algorand network, you must first establish a connection using the Algod API client. Once connected, you can retrieve essential network data such as total token supply and consensus parameters.

The following steps are integrated into the Main method of your Program.cs file. For clarity, we’ll break them down into logical sections.

Connect to Algorand and Query Total Token Supply

This section initializes the Algod API instance and fetches the total Algo supply on the network. The GetSupply() method returns both total and online (actively staked) supply values.

string ALGOD_API_ADDR = "ALGOD_API_ADDR"; // Replace with your Algorand Testnet API address
string ALGOD_API_TOKEN = "ALGOD_API_TOKEN"; // Replace with your Algod API key

AlgodApi algodApiInstance = new AlgodApi(ALGOD_API_ADDR, ALGOD_API_TOKEN);

try
{
    Supply supply = algodApiInstance.GetSupply();
    Console.WriteLine("Total Algorand Supply: " + supply.TotalMoney);
    Console.WriteLine("Online Algorand Supply: " + supply.OnlineMoney);
}
catch (ApiException e)
{
    Console.WriteLine("Exception when calling algod#getSupply: " + e.Message);
}
Note: Always use secure methods to store your API keys—never hardcode them in production environments.

Retrieve Network Transaction Parameters

Algorand's .NET SDK encapsulates network transaction parameters in the TransactionParams class. These include fee suggestions, genesis ID, and round information—all critical for constructing valid transactions.

ulong? feePerByte;
string genesisID;
Digest genesisHash;
ulong? firstRound = 0;

try
{
    TransactionParams transParams = algodApiInstance.TransactionParams();
    feePerByte = transParams.Fee;
    genesisHash = new Digest(Convert.FromBase64String(transParams.Genesishashb64));
    genesisID = transParams.GenesisID;

    Console.WriteLine("Suggested Fee: " + feePerByte);

    NodeStatus s = algodApiInstance.GetStatus();
    firstRound = s.LastRound;
    Console.WriteLine("Current Round: " + firstRound);
}
catch (ApiException e)
{
    throw new Exception("Could not get params", e);
}

These values are essential for building compliant transactions that the network will accept.


Working with Algorand Accounts

Understanding account structure is fundamental to blockchain development. In Algorand, every interaction starts with an account.

Key Concepts

🔐 Security Tip: Never expose your mnemonic or private key. If compromised, your assets are at risk.

Account Creation and Key Management

Below is a practical example demonstrating how to create accounts from mnemonics, extract keys, and generate new random accounts.

string mnemonicStr = "typical permit hurdle hat song detail cattle merge oxygen crowd arctic cargo smooth fly rice vacuum lounge yard frown predict west wife latin absent cup";

// Create account from mnemonic
Account src = new Account(mnemonicStr);

// Get public address
var publicKey = src.Address;
Console.WriteLine("Account Address: " + publicKey);

// Convert mnemonic to master key
var masterKey = Mnemonic.ToKey(mnemonicStr);

// Recreate account from master key
Account scr2 = new Account(masterKey);

// Convert master key back to mnemonic
var recoveredMnemonic = Mnemonic.FromKey(masterKey);
Console.WriteLine("Recovered Mnemonic: " + recoveredMnemonic);

// Generate a new random account
var randomAccount = new Account();
var randomMnemonic = randomAccount.ToMnemonic();
Console.WriteLine("New Account Mnemonic: " + randomMnemonic);

This flexibility allows developers to manage accounts securely across different application layers.


Sending Transactions on Algorand

Transferring ALGO tokens is one of the most common operations in any dApp. Let’s walk through creating, signing, and broadcasting a transaction.

Transaction Workflow

  1. Connect to the network.
  2. Retrieve transaction parameters (genesisID, genesisHash, fees).
  3. Construct the transaction.
  4. Sign it with the sender’s private key.
  5. Broadcast it to the network.
ulong amount = 100000; // 0.1 ALGO (in microAlgos)
ulong lastRound = firstRound + 1000; // Validity window (max 1000 rounds)
string destAddr = "KV2XGKMXGYJ6PWYQA5374BYIQBL3ONRMSIARPCFCJEAMAHQEVYPB7PL3KU";

Account src = new Account(mnemonicStr);
Console.WriteLine("Sender Address: " + src.Address.ToString());

Transaction tx = new Transaction(
    src.Address,
    new Address(destAddr),
    amount,
    firstRound,
    lastRound,
    genesisID,
    genesisHash
);

// Sign transaction with dynamic fee
SignedTransaction signedTx = src.SignTransactionWithFeePerByte(tx, (ulong)feePerByte);
Console.WriteLine("Signed transaction ID: " + signedTx.transactionID);

// Encode and broadcast
try
{
    byte[] encodedMsg = Algorand.Encoder.EncodeToMsgPack(signedTx);
    TransactionID txId = algodApiInstance.RawTransaction(encodedMsg);
    Console.WriteLine("Transaction successful! TXID: " + txId.TxId);
}
catch (ApiException e)
{
    Console.WriteLine("Error sending transaction: " + e.Message);
}

💡 Pro Tip: Use Utils.SubmitTransaction(algodApiInstance, signedTx) to simplify encoding and submission in one step.

👉 Explore advanced blockchain tools to enhance your development workflow


Signing Transactions

All transactions must be signed before being accepted by the network. The .NET SDK provides two primary methods:

You can also sign raw transactions or multi-signature operations—advanced topics covered in later tutorials.


Frequently Asked Questions

Q: How do I protect my Algorand account’s private key?
A: Always store mnemonics and master keys securely—preferably in encrypted vaults or hardware wallets. Never commit them to version control.

Q: What units does Algorand use for transactions?
A: All amounts are in microAlgos (1 ALGO = 1,000,000 microAlgos). Use Utils.AlgosToMicroalgos() and Utils.MicroalgosToAlgos() for conversion.

Q: Can I reuse a transaction after signing?
A: No. Each transaction has a unique round validity window. Reusing it outside that range will result in rejection.

Q: Why do I need genesis ID and hash?
A: They ensure your transaction targets the correct blockchain (Mainnet, Testnet, or BetaNet), preventing replay attacks across networks.

Q: Is there a faster way to send transactions?
A: Yes! Use Utils.SubmitTransaction() to combine signing and broadcasting in a single call.

Q: Where can I find sample code for these operations?
A: Full working examples are available in the official GitHub repository for reference and contribution.


Final Thoughts

This tutorial covered foundational Algorand account operations using .NET: connecting to the network, managing accounts, retrieving balances, and sending transactions. With these building blocks, you're well-equipped to develop robust blockchain applications.

As you progress, explore smart contracts, ASA (Algorand Standard Asset) creation, and off-chain computations to unlock more advanced capabilities.

👉 Accelerate your blockchain journey with cutting-edge resources and platforms

By mastering these core concepts and integrating best practices in security and efficiency, you position yourself at the forefront of decentralized innovation on the Algorand network.