Skip to content

Instantly share code, notes, and snippets.

@hoakbuilds
Created July 21, 2021 00:34
Show Gist options
  • Save hoakbuilds/65c67fba96ed9878bd533bffbfda28f6 to your computer and use it in GitHub Desktop.
Save hoakbuilds/65c67fba96ed9878bd533bffbfda28f6 to your computer and use it in GitHub Desktop.
Solana - GetAccountInfo, Decode NonceAccount to use Nonce instead of RecentBlockhash
public class TransactionBuilderTransferWithDurableNonceExample : 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 ownerAccount = wallet.GetAccount(10);
Console.WriteLine($"OwnerAccount: {ownerAccount.PublicKey.Key}");
var nonceAccount = wallet.GetAccount(1118);
Console.WriteLine($"NonceAccount: {nonceAccount.PublicKey.Key}");
var toAccount = wallet.GetAccount(1);
Console.WriteLine($"ToAccount: {toAccount.PublicKey.Key}");
// Get the Nonce Account to get the Nonce to use for the transaction
RequestResult<ResponseValue<AccountInfo>> nonceAccountInfo = rpcClient.GetAccountInfo(nonceAccount.PublicKey);
byte[] accountDataBytes = Convert.FromBase64String(nonceAccountInfo.Result.Value.Data[0]);
NonceAccount nonceAccountData = NonceAccount.Deserialize(accountDataBytes);
Console.WriteLine($"NonceAccount Authority: {nonceAccountData.Authorized.Key}");
Console.WriteLine($"NonceAccount Nonce: {nonceAccountData.Nonce.Key}");
// Initialize the nonce information to be used with the transaction
NonceInformation nonceInfo = new NonceInformation()
{
Nonce = nonceAccountData.Nonce,
Instruction = SystemProgram.AdvanceNonceAccount(
nonceAccount.PublicKey,
ownerAccount
)
};
var tx = new TransactionBuilder()
.SetFeePayer(ownerAccount)
.SetNonceInformation(nonceInfo)
.AddInstruction(
SystemProgram.Transfer(
ownerAccount,
toAccount,
1_000_000_000)
)
.Build(ownerAccount);
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