Core Concepts
Transaction builder
Learn about Transactions on the official Anchor documentation
using Solana.Unity.Programs;
using Solana.Unity.Programs.Models;
using Solana.Unity.Rpc;
using Solana.Unity.Rpc.Builders;
using Solana.Unity.Rpc.Core.Http;
using Solana.Unity.Rpc.Messages;
using Solana.Unity.Rpc.Models;
using Solana.Unity.Wallet;
using System;
using System.Collections.Generic;
namespace Solana.Unity.Examples
{
public class TransactionBuilderExample : IExample
{
private static readonly IRpcClient rpcClient = ClientFactory.GetClient(Cluster.TestNet);
private const string MnemonicWords =
"route clerk disease box emerge airport loud waste attitude film army tray " +
"forward deal onion eight catalog surface unit card window walnut wealth medal";
public void Run()
{
Wallet.Wallet wallet = new Wallet.Wallet(MnemonicWords);
Account fromAccount = wallet.GetAccount(10);
Account toAccount = wallet.GetAccount(8);
RequestResult<ResponseValue<BlockHash>> blockHash = rpcClient.GetRecentBlockHash();
Console.WriteLine($"BlockHash >> {blockHash.Result.Value.Blockhash}");
byte[] tx = new TransactionBuilder()
.SetRecentBlockHash(blockHash.Result.Value.Blockhash)
.SetFeePayer(fromAccount)
.AddInstruction(SystemProgram.Transfer(fromAccount.PublicKey, toAccount.PublicKey, 10000000))
.AddInstruction(MemoProgram.NewMemo(fromAccount.PublicKey, "Hello from Sol.Net :)"))
.Build(fromAccount);
Console.WriteLine($"Tx base64: {Convert.ToBase64String(tx)}");
RequestResult<ResponseValue<SimulationLogs>> txSim = rpcClient.SimulateTransaction(tx);
string logs = Examples.PrettyPrintTransactionSimulationLogs(txSim.Result.Value.Logs);
Console.WriteLine($"Transaction Simulation:\n\tError: {txSim.Result.Value.Error}\n\tLogs: \n" + logs);
RequestResult<string> firstSig = rpcClient.SendTransaction(tx);
Console.WriteLine($"First Tx Signature: {firstSig.Result}");
}
}
}