Skip to content

Commit

Permalink
Explicitly awaiting GetLatestBlockHash everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
MCollector committed Jan 31, 2025
1 parent d2d424b commit dbb6660
Show file tree
Hide file tree
Showing 16 changed files with 151 additions and 63 deletions.
4 changes: 3 additions & 1 deletion src/Solana.Unity.Dex/Orca/Orca/OrcaDex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Solana.Unity.Dex.Quotes;
using Solana.Unity.Dex.Ticks;
using System.Linq;
using Solana.Unity.Rpc.Core.Http;

namespace Orca
{
Expand Down Expand Up @@ -1069,7 +1070,8 @@ Commitment commitment
)
{
tb.SetFeePayer(feePayer);
tb.SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash);
RequestResult<Solana.Unity.Rpc.Messages.ResponseValue<LatestBlockHash>> latestBlockHash = await rpcClient.GetLatestBlockHashAsync();
tb.SetRecentBlockHash(latestBlockHash.Result.Value.Blockhash);

return Transaction.Deserialize(tb.Serialize());
}
Expand Down
7 changes: 5 additions & 2 deletions src/Solana.Unity.Dex/Orca/SolanaApi/SystemUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Solana.Unity.Rpc.Types;
using Solana.Unity.Rpc.Core.Http;
using Solana.Unity.Programs;
using Solana.Unity.Rpc.Models;

namespace Solana.Unity.Dex.Orca.SolanaApi
{
Expand Down Expand Up @@ -55,8 +56,9 @@ public static async Task<RequestResult<string>> TransferSolAsync(
Commitment commitment = Commitment.Finalized
)
{
RequestResult<Solana.Unity.Rpc.Messages.ResponseValue<LatestBlockHash>> latestBlockHashItem = await ctx.RpcClient.GetLatestBlockHashAsync();
var tb = new TransactionBuilder()
.SetRecentBlockHash(ctx.RpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash)
.SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
.SetFeePayer(from.PublicKey)
.AddInstruction(SystemProgram.Transfer(from.PublicKey, to, lamports));

Expand All @@ -82,8 +84,9 @@ public static async Task<RequestResult<string>> CreateAccountAsync(
Commitment commitment = Commitment.Finalized
)
{
RequestResult<Solana.Unity.Rpc.Messages.ResponseValue<LatestBlockHash>> latestBlockHashItem = await ctx.RpcClient.GetLatestBlockHashAsync();
var tx = new TransactionBuilder()
.SetRecentBlockHash(ctx.RpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash)
.SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
.SetFeePayer(feePayer)
.AddInstruction(SystemProgram.CreateAccount(
fromAccount: fromAccount,
Expand Down
15 changes: 10 additions & 5 deletions src/Solana.Unity.Dex/Orca/SolanaApi/TokenUtilsTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Solana.Unity.Rpc;
using Solana.Unity.Rpc.Models;
using Solana.Unity.Rpc.Types;
using Solana.Unity.Rpc.Core.Http;

namespace Solana.Unity.Dex.Orca.SolanaApi
{
Expand All @@ -33,8 +34,9 @@ public static async Task<Transaction> CreateMint(
{
ulong minBalanceForExemptionMint =
(await rpc.GetMinimumBalanceForRentExemptionAsync(TokenProgram.TokenAccountDataSize)).Result;

byte[] tx = new TransactionBuilder().SetRecentBlockHash(rpc.GetLatestBlockHashAsync().Result.Result.Value.Blockhash)

RequestResult<Solana.Unity.Rpc.Messages.ResponseValue<LatestBlockHash>> latestBlockHashItem = await rpc.GetLatestBlockHashAsync();
byte[] tx = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
.SetFeePayer(authority)
.AddInstruction(SystemProgram.CreateAccount(
fromAccount: authority,
Expand Down Expand Up @@ -80,9 +82,10 @@ public static async Task<Transaction> CreateAndMintToAssociatedTokenAccount (

var tempAccount = new Account();
var sta = AssociatedTokenAccountProgram.DeriveAssociatedTokenAccount(tempAccount, mint);
RequestResult<Solana.Unity.Rpc.Messages.ResponseValue<LatestBlockHash>> latestBlockHashItem = await rpc.GetLatestBlockHashAsync();
var trxBuild= new TransactionBuilder()
.SetFeePayer(feePayer.PublicKey)
.SetRecentBlockHash(rpc.GetLatestBlockHashAsync().Result.Result.Value.Blockhash)
.SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
.AddInstruction(SystemProgram.CreateAccount(
feePayer,
tempAccount,
Expand Down Expand Up @@ -121,8 +124,9 @@ public static async Task<Transaction> CreateAndMintToAssociatedTokenAccount (
}
else
{
RequestResult<Solana.Unity.Rpc.Messages.ResponseValue<LatestBlockHash>> latestBlockHashItem = await rpc.GetLatestBlockHashAsync();
var txBuilder = new TransactionBuilder()
.SetRecentBlockHash(rpc.GetLatestBlockHashAsync().Result.Result.Value.Blockhash)
.SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
.SetFeePayer(feePayer.PublicKey)
.AddInstruction(
AssociatedTokenAccountProgram.CreateAssociatedTokenAccount(
Expand Down Expand Up @@ -164,8 +168,9 @@ public static async Task<Transaction> MintToByAuthority(
Account feePayer
)
{
RequestResult<Solana.Unity.Rpc.Messages.ResponseValue<LatestBlockHash>> latestBlockHashItem = await rpc.GetLatestBlockHashAsync();
byte[] tx = new TransactionBuilder()
.SetRecentBlockHash(rpc.GetLatestBlockHashAsync().Result.Result.Value.Blockhash)
.SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
.SetFeePayer(feePayer.PublicKey)
.AddInstruction(TokenProgram.MintTo(
mint,
Expand Down
3 changes: 2 additions & 1 deletion src/Solana.Unity.Examples/AssociatedTokenAccountsExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public async void Run()
Console.WriteLine($"MintAccount: {mintAccount}");
Console.WriteLine($"InitialAccount: {initialAccount}");

string latestBlockHash = RpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash;
RequestResult<Solana.Unity.Rpc.Messages.ResponseValue<LatestBlockHash>> latestBlockHashItem = await RpcClient.GetLatestBlockHashAsync();
string latestBlockHash = latestBlockHashItem.Result.Value.Blockhash;

byte[] createAndInitializeMintToTx = new TransactionBuilder().
SetRecentBlockHash(latestBlockHash).
Expand Down
6 changes: 5 additions & 1 deletion src/Solana.Unity.Examples/HelloWorldExample.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Solana.Unity.Programs;
using Solana.Unity.Rpc;
using Solana.Unity.Rpc.Builders;
using Solana.Unity.Rpc.Core.Http;
using Solana.Unity.Rpc.Models;
using Solana.Unity.Wallet;
using Solana.Unity.Wallet.Bip39;
using System;
Expand Down Expand Up @@ -43,8 +45,10 @@ public async void Run()

var memoInstruction = MemoProgram.NewMemoV2("Hello Solana World, using Solana.Unity :)");

RequestResult<Solana.Unity.Rpc.Messages.ResponseValue<LatestBlockHash>> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync();

var tx = new TransactionBuilder().AddInstruction(memoInstruction).SetFeePayer(wallet.Account)
.SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash).Build(wallet.Account);
.SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash).Build(wallet.Account);

var txHash = await rpcClient.SendTransactionAsync(tx);

Expand Down
4 changes: 3 additions & 1 deletion src/Solana.Unity.Examples/InstructionDecoderExample.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Solana.Unity.Programs;
using Solana.Unity.Rpc;
using Solana.Unity.Rpc.Builders;
using Solana.Unity.Rpc.Core.Http;
using Solana.Unity.Rpc.Models;
using System;
using System.IO;
Expand All @@ -24,7 +25,8 @@ public async void Run()
var fromAccount = wallet.GetAccount(10);
var toAccount = wallet.GetAccount(8);

string latestBlockHash = rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash;
RequestResult<Solana.Unity.Rpc.Messages.ResponseValue<LatestBlockHash>> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync();
string latestBlockHash = latestBlockHashItem.Result.Value.Blockhash;
Console.WriteLine($"BlockHash >> {latestBlockHash}");

var msgBytes = new TransactionBuilder()
Expand Down
45 changes: 30 additions & 15 deletions src/Solana.Unity.Examples/MultisigExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public async void Run()
Account signerAccount4 = wallet.GetAccount(25103);
Account signerAccount5 = wallet.GetAccount(25104);

byte[] msgData = new TransactionBuilder().SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash)
RequestResult<Solana.Unity.Rpc.Messages.ResponseValue<LatestBlockHash>> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync();
byte[] msgData = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
.SetFeePayer(ownerAccount)
.AddInstruction(SystemProgram.CreateAccount(
ownerAccount.PublicKey,
Expand Down Expand Up @@ -90,7 +91,8 @@ public async void Run()
string createMultiSigAndMintSignature = await Examples.SubmitTxSendAndLog(txBytes);
Examples.PollConfirmedTx(createMultiSigAndMintSignature);

msgData = new TransactionBuilder().SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash)
latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync();
msgData = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
.SetFeePayer(ownerAccount)
.AddInstruction(SystemProgram.CreateAccount(
ownerAccount,
Expand Down Expand Up @@ -171,7 +173,8 @@ public async void Run()
Account signerAccount2 = wallet.GetAccount(25101);
Account signerAccount4 = wallet.GetAccount(25103);

byte[] msgData = new TransactionBuilder().SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash)
RequestResult<Solana.Unity.Rpc.Messages.ResponseValue<LatestBlockHash>> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync();
byte[] msgData = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
.SetFeePayer(ownerAccount)
.AddInstruction(TokenProgram.MintToChecked(
mintAccount.PublicKey,
Expand Down Expand Up @@ -222,7 +225,8 @@ public async void Run()
{
Wallet.Wallet wallet = new Wallet.Wallet(MnemonicWords);

string latestBlockHash = rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash;
RequestResult<Solana.Unity.Rpc.Messages.ResponseValue<LatestBlockHash>> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync();
string latestBlockHash = latestBlockHashItem.Result.Value.Blockhash;

ulong minBalanceForExemptionMultiSig =
(await rpcClient.GetMinimumBalanceForRentExemptionAsync(TokenProgram.MultisigAccountDataSize)).Result;
Expand Down Expand Up @@ -385,7 +389,8 @@ public async void Run()
Account freezeSigner5 = wallet.GetAccount(25414);

// First we create a multi sig account to use as the token's freeze authority
byte[] msgData = new TransactionBuilder().SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash)
RequestResult<Solana.Unity.Rpc.Messages.ResponseValue<LatestBlockHash>> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync();
byte[] msgData = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
.SetFeePayer(ownerAccount)
.AddInstruction(SystemProgram.CreateAccount(
ownerAccount,
Expand Down Expand Up @@ -422,7 +427,8 @@ public async void Run()

// Then we create an account which will be the token's mint authority
// In this same transaction we initialize the token mint with said authorities
msgData = new TransactionBuilder().SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash)
latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync();
msgData = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
.SetFeePayer(ownerAccount)
.AddInstruction(SystemProgram.CreateAccount(
ownerAccount,
Expand Down Expand Up @@ -470,7 +476,8 @@ public async void Run()
Examples.PollConfirmedTx(signature);

// Here we mint tokens to an account using the mint authority multi sig
msgData = new TransactionBuilder().SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash)
latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync();
msgData = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
.SetFeePayer(ownerAccount)
.AddInstruction(SystemProgram.CreateAccount(
ownerAccount,
Expand Down Expand Up @@ -516,7 +523,8 @@ public async void Run()

// After doing this, we freeze the account to which we just minted tokens
// Notice how the signers used are different, because the `freezeAuthority` has different signers
msgData = new TransactionBuilder().SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash)
latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync();
msgData = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
.SetFeePayer(ownerAccount)
.AddInstruction(TokenProgram.FreezeAccount(
initialAccount,
Expand Down Expand Up @@ -550,7 +558,8 @@ public async void Run()
Examples.PollConfirmedTx(signature);

// Because we're actually cool people, we now thaw that same account and then set the authority to nothing
msgData = new TransactionBuilder().SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash)
latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync();
msgData = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
.SetFeePayer(ownerAccount)
.AddInstruction(TokenProgram.ThawAccount(
initialAccount,
Expand Down Expand Up @@ -648,7 +657,8 @@ public async void Run()
Account tokenAccountSigner4 = wallet.GetAccount(25493);
Account tokenAccountSigner5 = wallet.GetAccount(25494);

byte[] msgData = new TransactionBuilder().SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash)
RequestResult<Solana.Unity.Rpc.Messages.ResponseValue<LatestBlockHash>> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync();
byte[] msgData = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
.SetFeePayer(ownerAccount)
.AddInstruction(SystemProgram.CreateAccount(
ownerAccount.PublicKey,
Expand Down Expand Up @@ -685,7 +695,8 @@ public async void Run()
string signature = await Examples.SubmitTxSendAndLog(txBytes);
Examples.PollConfirmedTx(signature);

msgData = new TransactionBuilder().SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash)
latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync();
msgData = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
.SetFeePayer(ownerAccount)
.AddInstruction(SystemProgram.CreateAccount(
ownerAccount.PublicKey,
Expand Down Expand Up @@ -729,7 +740,8 @@ public async void Run()
signature = await Examples.SubmitTxSendAndLog(txBytes);
Examples.PollConfirmedTx(signature);

msgData = new TransactionBuilder().SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash)
latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync();
msgData = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
.SetFeePayer(ownerAccount)
.AddInstruction(TokenProgram.ApproveChecked(
tokenAccountWithMultisigOwner,
Expand Down Expand Up @@ -764,7 +776,8 @@ public async void Run()
signature = await Examples.SubmitTxSendAndLog(txBytes);
Examples.PollConfirmedTx(signature);

msgData = new TransactionBuilder().SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash)
latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync();
msgData = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
.SetFeePayer(ownerAccount)
.AddInstruction(TokenProgram.TransferChecked(
tokenAccountWithMultisigOwner,
Expand Down Expand Up @@ -852,7 +865,8 @@ public async void Run()
Account tokenAccountSigner4 = wallet.GetAccount(25493);
Account tokenAccountSigner5 = wallet.GetAccount(25494);

byte[] msgData = new TransactionBuilder().SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash)
RequestResult<Solana.Unity.Rpc.Messages.ResponseValue<LatestBlockHash>> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync();
byte[] msgData = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
.SetFeePayer(ownerAccount)
.AddInstruction(TokenProgram.MintToChecked(
mintAccount,
Expand Down Expand Up @@ -945,7 +959,8 @@ public async void Run()

Console.WriteLine($"Account Balance >> {balance.Result.Value.UiAmountString}");

byte[] msgData = new TransactionBuilder().SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash)
RequestResult<Solana.Unity.Rpc.Messages.ResponseValue<LatestBlockHash>> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync();
byte[] msgData = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
.SetFeePayer(ownerAccount)
.AddInstruction(TokenProgram.BurnChecked(
mintAccount,
Expand Down
5 changes: 4 additions & 1 deletion src/Solana.Unity.Examples/NameServiceProgramExamples.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Solana.Unity.Programs;
using Solana.Unity.Rpc;
using Solana.Unity.Rpc.Builders;
using Solana.Unity.Rpc.Core.Http;
using Solana.Unity.Rpc.Models;
using Solana.Unity.Wallet;
using System;
using System.Threading.Tasks;
Expand Down Expand Up @@ -75,7 +77,8 @@ public async void Run()
var reverseRegistry = GetReverseRegistryKey(ownerAccount.PublicKey.Key);
Console.WriteLine($"ReverseRegistryKey: {reverseRegistry.Key}");

var tx = new TransactionBuilder().SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash)
RequestResult<Solana.Unity.Rpc.Messages.ResponseValue<LatestBlockHash>> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync();
var tx = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
.SetFeePayer(payerAccount).AddInstruction(
NameServiceProgram.CreateNameRegistry(
twitterHandleRegistry,
Expand Down
Loading

0 comments on commit dbb6660

Please sign in to comment.