Skip to content

Instantly share code, notes, and snippets.

@hoakbuilds
Created July 20, 2021 16:44
Show Gist options
  • Save hoakbuilds/eaecc6b9a7f2dfe667fe1d0d1c2cf393 to your computer and use it in GitHub Desktop.
Save hoakbuilds/eaecc6b9a7f2dfe667fe1d0d1c2cf393 to your computer and use it in GitHub Desktop.
Solana - Create Nonce Account using Solnet
public class CreateNonceAccountExample : 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()
{
var wallet = new Wallet.Wallet(MnemonicWords);
var blockHash = rpcClient.GetRecentBlockHash();
var minBalanceForExemptionAcc =
rpcClient.GetMinimumBalanceForRentExemption(SystemProgram.AccountDataSize).Result;
var ownerAccount = wallet.GetAccount(10);
Console.WriteLine($"OwnerAccount: {ownerAccount.PublicKey.Key}");
var nonceAccount = wallet.GetAccount(1111);
Console.WriteLine($"NonceAccount: {nonceAccount.PublicKey.Key}");
var tx = new TransactionBuilder()
.SetRecentBlockHash(blockHash.Result.Value.Blockhash)
.SetFeePayer(ownerAccount)
.AddInstruction(SystemProgram.CreateAccount(
ownerAccount,
nonceAccount,
minBalanceForExemptionAcc,
SystemProgram.AccountDataSize,
SystemProgram.ProgramIdKey
))
.AddInstruction(SystemProgram.InitializeNonceAccount(
nonceAccount,
ownerAccount))
.Build(new List<Account>{ ownerAccount, nonceAccount });
Console.WriteLine($"Tx: {Convert.ToBase64String(tx)}");
var txSim = rpcClient.SimulateTransaction(tx);
var logs = Examples.PrettyPrintTransactionSimulationLogs(txSim.Result.Value.Logs);
Console.WriteLine($"Transaction Simulation:\n\tError: {txSim.Result.Value.Error}\n\tLogs: \n" + logs);
/*
var txReq = rpcClient.SendTransaction(tx);
Console.WriteLine($"Tx Signature: {txReq.Result}");*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment