From dbb6660ba2a0ed5a3797a4761f43c1a1f856d24f Mon Sep 17 00:00:00 2001 From: MoneyCollector Date: Fri, 31 Jan 2025 10:54:05 +0100 Subject: [PATCH] Explicitly awaiting GetLatestBlockHash everywhere --- src/Solana.Unity.Dex/Orca/Orca/OrcaDex.cs | 4 +- .../Orca/SolanaApi/SystemUtils.cs | 7 ++- .../Orca/SolanaApi/TokenUtilsTransaction.cs | 15 ++++--- .../AssociatedTokenAccountsExample.cs | 3 +- .../HelloWorldExample.cs | 6 ++- .../InstructionDecoderExample.cs | 4 +- src/Solana.Unity.Examples/MultisigExamples.cs | 45 ++++++++++++------- .../NameServiceProgramExamples.cs | 5 ++- src/Solana.Unity.Examples/StakeExample.cs | 18 +++++--- src/Solana.Unity.Examples/TokenSwapExample.cs | 36 +++++++++++---- .../TransactionBuilderExample.cs | 30 +++++++++---- .../TransactionDecodingExample.cs | 4 +- .../Abstract/TransactionalBaseClient.cs | 4 +- .../Integration/IncreaseLiquidityTests.cs | 5 ++- .../Orca/Utils/LiquidityTestUtils.cs | 4 +- .../Orca/Utils/TokenUtils.cs | 24 ++++++---- 16 files changed, 151 insertions(+), 63 deletions(-) diff --git a/src/Solana.Unity.Dex/Orca/Orca/OrcaDex.cs b/src/Solana.Unity.Dex/Orca/Orca/OrcaDex.cs index 4dd7d49..40b10f1 100644 --- a/src/Solana.Unity.Dex/Orca/Orca/OrcaDex.cs +++ b/src/Solana.Unity.Dex/Orca/Orca/OrcaDex.cs @@ -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 { @@ -1069,7 +1070,8 @@ Commitment commitment ) { tb.SetFeePayer(feePayer); - tb.SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash); + RequestResult> latestBlockHash = await rpcClient.GetLatestBlockHashAsync(); + tb.SetRecentBlockHash(latestBlockHash.Result.Value.Blockhash); return Transaction.Deserialize(tb.Serialize()); } diff --git a/src/Solana.Unity.Dex/Orca/SolanaApi/SystemUtils.cs b/src/Solana.Unity.Dex/Orca/SolanaApi/SystemUtils.cs index 25e1415..cf39f0e 100644 --- a/src/Solana.Unity.Dex/Orca/SolanaApi/SystemUtils.cs +++ b/src/Solana.Unity.Dex/Orca/SolanaApi/SystemUtils.cs @@ -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 { @@ -55,8 +56,9 @@ public static async Task> TransferSolAsync( Commitment commitment = Commitment.Finalized ) { + RequestResult> 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)); @@ -82,8 +84,9 @@ public static async Task> CreateAccountAsync( Commitment commitment = Commitment.Finalized ) { + RequestResult> 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, diff --git a/src/Solana.Unity.Dex/Orca/SolanaApi/TokenUtilsTransaction.cs b/src/Solana.Unity.Dex/Orca/SolanaApi/TokenUtilsTransaction.cs index aadb678..de6eb21 100644 --- a/src/Solana.Unity.Dex/Orca/SolanaApi/TokenUtilsTransaction.cs +++ b/src/Solana.Unity.Dex/Orca/SolanaApi/TokenUtilsTransaction.cs @@ -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 { @@ -33,8 +34,9 @@ public static async Task CreateMint( { ulong minBalanceForExemptionMint = (await rpc.GetMinimumBalanceForRentExemptionAsync(TokenProgram.TokenAccountDataSize)).Result; - - byte[] tx = new TransactionBuilder().SetRecentBlockHash(rpc.GetLatestBlockHashAsync().Result.Result.Value.Blockhash) + + RequestResult> latestBlockHashItem = await rpc.GetLatestBlockHashAsync(); + byte[] tx = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(authority) .AddInstruction(SystemProgram.CreateAccount( fromAccount: authority, @@ -80,9 +82,10 @@ public static async Task CreateAndMintToAssociatedTokenAccount ( var tempAccount = new Account(); var sta = AssociatedTokenAccountProgram.DeriveAssociatedTokenAccount(tempAccount, mint); + RequestResult> 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, @@ -121,8 +124,9 @@ public static async Task CreateAndMintToAssociatedTokenAccount ( } else { + RequestResult> 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( @@ -164,8 +168,9 @@ public static async Task MintToByAuthority( Account feePayer ) { + RequestResult> 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, diff --git a/src/Solana.Unity.Examples/AssociatedTokenAccountsExample.cs b/src/Solana.Unity.Examples/AssociatedTokenAccountsExample.cs index d2ca1d4..dceb68f 100644 --- a/src/Solana.Unity.Examples/AssociatedTokenAccountsExample.cs +++ b/src/Solana.Unity.Examples/AssociatedTokenAccountsExample.cs @@ -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> latestBlockHashItem = await RpcClient.GetLatestBlockHashAsync(); + string latestBlockHash = latestBlockHashItem.Result.Value.Blockhash; byte[] createAndInitializeMintToTx = new TransactionBuilder(). SetRecentBlockHash(latestBlockHash). diff --git a/src/Solana.Unity.Examples/HelloWorldExample.cs b/src/Solana.Unity.Examples/HelloWorldExample.cs index 4bf5e47..39a36f9 100644 --- a/src/Solana.Unity.Examples/HelloWorldExample.cs +++ b/src/Solana.Unity.Examples/HelloWorldExample.cs @@ -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; @@ -43,8 +45,10 @@ public async void Run() var memoInstruction = MemoProgram.NewMemoV2("Hello Solana World, using Solana.Unity :)"); + RequestResult> 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); diff --git a/src/Solana.Unity.Examples/InstructionDecoderExample.cs b/src/Solana.Unity.Examples/InstructionDecoderExample.cs index 54151fb..10ebba2 100644 --- a/src/Solana.Unity.Examples/InstructionDecoderExample.cs +++ b/src/Solana.Unity.Examples/InstructionDecoderExample.cs @@ -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; @@ -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> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + string latestBlockHash = latestBlockHashItem.Result.Value.Blockhash; Console.WriteLine($"BlockHash >> {latestBlockHash}"); var msgBytes = new TransactionBuilder() diff --git a/src/Solana.Unity.Examples/MultisigExamples.cs b/src/Solana.Unity.Examples/MultisigExamples.cs index 3c381a7..2edb749 100644 --- a/src/Solana.Unity.Examples/MultisigExamples.cs +++ b/src/Solana.Unity.Examples/MultisigExamples.cs @@ -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> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + byte[] msgData = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(ownerAccount) .AddInstruction(SystemProgram.CreateAccount( ownerAccount.PublicKey, @@ -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, @@ -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> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + byte[] msgData = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(ownerAccount) .AddInstruction(TokenProgram.MintToChecked( mintAccount.PublicKey, @@ -222,7 +225,8 @@ public async void Run() { Wallet.Wallet wallet = new Wallet.Wallet(MnemonicWords); - string latestBlockHash = rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash; + RequestResult> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + string latestBlockHash = latestBlockHashItem.Result.Value.Blockhash; ulong minBalanceForExemptionMultiSig = (await rpcClient.GetMinimumBalanceForRentExemptionAsync(TokenProgram.MultisigAccountDataSize)).Result; @@ -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> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + byte[] msgData = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(ownerAccount) .AddInstruction(SystemProgram.CreateAccount( ownerAccount, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + byte[] msgData = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(ownerAccount) .AddInstruction(SystemProgram.CreateAccount( ownerAccount.PublicKey, @@ -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, @@ -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, @@ -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, @@ -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> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + byte[] msgData = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(ownerAccount) .AddInstruction(TokenProgram.MintToChecked( mintAccount, @@ -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> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + byte[] msgData = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(ownerAccount) .AddInstruction(TokenProgram.BurnChecked( mintAccount, diff --git a/src/Solana.Unity.Examples/NameServiceProgramExamples.cs b/src/Solana.Unity.Examples/NameServiceProgramExamples.cs index a072cb6..11c43d1 100644 --- a/src/Solana.Unity.Examples/NameServiceProgramExamples.cs +++ b/src/Solana.Unity.Examples/NameServiceProgramExamples.cs @@ -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; @@ -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> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + var tx = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(payerAccount).AddInstruction( NameServiceProgram.CreateNameRegistry( twitterHandleRegistry, diff --git a/src/Solana.Unity.Examples/StakeExample.cs b/src/Solana.Unity.Examples/StakeExample.cs index ac3fa4e..5b5f4b5 100644 --- a/src/Solana.Unity.Examples/StakeExample.cs +++ b/src/Solana.Unity.Examples/StakeExample.cs @@ -24,7 +24,8 @@ public async void Run() { var wallet = new Wallet.Wallet(new Mnemonic(MnemonicWords)); await rpcClient.RequestAirdropAsync(wallet.Account.PublicKey, 100_000_000); - string latestBlockHash = rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash; + RequestResult> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + string latestBlockHash = latestBlockHashItem.Result.Value.Blockhash; ulong minBalance = (await rpcClient.GetMinimumBalanceForRentExemptionAsync(StakeProgram.StakeAccountDataSize)).Result; Account fromAccount = wallet.Account; PublicKey.TryCreateWithSeed(fromAccount.PublicKey, "yrdy1", StakeProgram.ProgramIdKey, out PublicKey stakeAccount); @@ -64,7 +65,8 @@ public async void Run() var b58 = new Base58Encoder(); string f = b58.EncodeData(seed); rpcClient.RequestAirdropAsync(wallet.Account.PublicKey, 100_000_000); - string latestBlockHash = rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash; + RequestResult> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + string latestBlockHash = latestBlockHashItem.Result.Value.Blockhash; ulong minbalanceforexception = (await rpcClient.GetMinimumBalanceForRentExemptionAsync(StakeProgram.StakeAccountDataSize)).Result; Account fromAccount = wallet.Account; Account toAccount = wallet.GetAccount(1); @@ -105,7 +107,8 @@ public async void Run() { var wallet = new Wallet.Wallet(new Mnemonic(MnemonicWords)); rpcClient.RequestAirdropAsync(wallet.Account.PublicKey, 100_000_000); - string latestBlockHash = rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash; + RequestResult> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + string latestBlockHash = latestBlockHashItem.Result.Value.Blockhash; Account fromAccount = wallet.Account; Account toAccount = wallet.GetAccount(1); @@ -142,7 +145,8 @@ public async void Run() { var wallet = new Wallet.Wallet(new Mnemonic(MnemonicWords)); rpcClient.RequestAirdropAsync(wallet.Account.PublicKey, 100_000_000); - string latestBlockHash = rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash; + RequestResult> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + string latestBlockHash = latestBlockHashItem.Result.Value.Blockhash; ulong minbalanceforexception = (await rpcClient.GetMinimumBalanceForRentExemptionAsync(StakeProgram.StakeAccountDataSize)).Result; Account fromAccount = wallet.Account; PublicKey.TryCreateWithSeed(fromAccount.PublicKey, "dog5", StakeProgram.ProgramIdKey, out PublicKey stakeAccount); @@ -197,7 +201,8 @@ public async void Run() { var wallet = new Wallet.Wallet(new Mnemonic(MnemonicWords)); rpcClient.RequestAirdropAsync(wallet.Account.PublicKey, 100_000_000); - string latestBlockHash = rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash; + RequestResult> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + string latestBlockHash = latestBlockHashItem.Result.Value.Blockhash; ulong minbalanceforexception = (await rpcClient.GetMinimumBalanceForRentExemptionAsync(StakeProgram.StakeAccountDataSize)).Result; Account fromAccount = wallet.Account; Account stakeAccount = wallet.GetAccount(22); @@ -250,7 +255,8 @@ public async void Run() { var wallet = new Wallet.Wallet(new Mnemonic(MnemonicWords)); rpcClient.RequestAirdropAsync(wallet.Account.PublicKey, 100_000_000); - string latestBlockHash = rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash; + RequestResult> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + string latestBlockHash = latestBlockHashItem.Result.Value.Blockhash; ulong minBalance = (await rpcClient.GetMinimumBalanceForRentExemptionAsync(StakeProgram.StakeAccountDataSize)).Result; Account a6 = wallet.GetAccount(6); diff --git a/src/Solana.Unity.Examples/TokenSwapExample.cs b/src/Solana.Unity.Examples/TokenSwapExample.cs index 105ec1e..f992c42 100644 --- a/src/Solana.Unity.Examples/TokenSwapExample.cs +++ b/src/Solana.Unity.Examples/TokenSwapExample.cs @@ -35,9 +35,11 @@ public async void Run() var tokenBMint = new Account(); var tokenBUserAccount = new Account(); + RequestResult> latestBlockHashItem = await RpcClient.GetLatestBlockHashAsync(); + //setup some mints and tokens owned by wallet var tx = new TransactionBuilder() - .SetRecentBlockHash(RpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash) + .SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(wallet.Account) .AddInstruction(SystemProgram.CreateAccount( wallet.Account, @@ -110,9 +112,11 @@ public async void Run() var swapTokenAAccount= new Account(); var swapTokenBAccount = new Account(); + latestBlockHashItem = await RpcClient.GetLatestBlockHashAsync(); + //init the swap authority's token accounts tx = new TransactionBuilder() - .SetRecentBlockHash(RpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash) + .SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(wallet.Account) .AddInstruction(SystemProgram.CreateAccount( wallet.Account, @@ -158,9 +162,11 @@ public async void Run() var poolUserAccount = new Account(); var poolFeeAccount = new Account(); + latestBlockHashItem = await RpcClient.GetLatestBlockHashAsync(); + //create the pool mint and the user and fee pool token accounts tx = new TransactionBuilder() - .SetRecentBlockHash(RpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash) + .SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(wallet.Account) .AddInstruction(SystemProgram.CreateAccount( wallet.Account, @@ -202,9 +208,11 @@ public async void Run() txSig = await Examples.SubmitTxSendAndLog(tx); Examples.PollConfirmedTx(txSig); + latestBlockHashItem = await RpcClient.GetLatestBlockHashAsync(); + //create the swap tx = new TransactionBuilder() - .SetRecentBlockHash(RpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash) + .SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(wallet.Account) .AddInstruction(SystemProgram.CreateAccount( wallet.Account, @@ -242,9 +250,11 @@ public async void Run() txSig = await Examples.SubmitTxSendAndLog(tx); Examples.PollConfirmedTx(txSig); + latestBlockHashItem = await RpcClient.GetLatestBlockHashAsync(); + //now a user can swap in the pool tx = new TransactionBuilder() - .SetRecentBlockHash(RpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash) + .SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(wallet.Account) .AddInstruction(program.Swap( swap, @@ -262,9 +272,11 @@ public async void Run() txSig = await Examples.SubmitTxSendAndLog(tx); Examples.PollConfirmedTx(txSig); + latestBlockHashItem = await RpcClient.GetLatestBlockHashAsync(); + //user can add liq tx = new TransactionBuilder() - .SetRecentBlockHash(RpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash) + .SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(wallet.Account) .AddInstruction(program.DepositAllTokenTypes( swap, @@ -282,9 +294,11 @@ public async void Run() txSig = await Examples.SubmitTxSendAndLog(tx); Examples.PollConfirmedTx(txSig); + latestBlockHashItem = await RpcClient.GetLatestBlockHashAsync(); + //user can remove liq tx = new TransactionBuilder() - .SetRecentBlockHash(RpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash) + .SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(wallet.Account) .AddInstruction(program.WithdrawAllTokenTypes( swap, @@ -303,9 +317,11 @@ public async void Run() txSig = await Examples.SubmitTxSendAndLog(tx); Examples.PollConfirmedTx(txSig); + latestBlockHashItem = await RpcClient.GetLatestBlockHashAsync(); + //user can deposit single tx = new TransactionBuilder() - .SetRecentBlockHash(RpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash) + .SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(wallet.Account) .AddInstruction(program.DepositSingleTokenTypeExactAmountIn( swap, @@ -321,9 +337,11 @@ public async void Run() txSig = await Examples.SubmitTxSendAndLog(tx); Examples.PollConfirmedTx(txSig); + latestBlockHashItem = await RpcClient.GetLatestBlockHashAsync(); + //user can withdraw single tx = new TransactionBuilder() - .SetRecentBlockHash(RpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash) + .SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(wallet.Account) .AddInstruction(program.WithdrawSingleTokenTypeExactAmountOut( swap, diff --git a/src/Solana.Unity.Examples/TransactionBuilderExample.cs b/src/Solana.Unity.Examples/TransactionBuilderExample.cs index 872fbb8..19353bf 100644 --- a/src/Solana.Unity.Examples/TransactionBuilderExample.cs +++ b/src/Solana.Unity.Examples/TransactionBuilderExample.cs @@ -27,7 +27,8 @@ public async void Run() Account fromAccount = wallet.GetAccount(10); Account toAccount = wallet.GetAccount(8); - string latestBlockHash = rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash; + RequestResult> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + string latestBlockHash = latestBlockHashItem.Result.Value.Blockhash; Console.WriteLine($"BlockHash >> {latestBlockHash}"); byte[] tx = new TransactionBuilder() @@ -73,7 +74,9 @@ public async void Run() Account initialAccount = wallet.GetAccount(3333); Console.WriteLine($"InitialAccount: {initialAccount}"); - byte[] tx = new TransactionBuilder().SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash) + RequestResult> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + + byte[] tx = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(ownerAccount) .AddInstruction(SystemProgram.CreateAccount( ownerAccount.PublicKey, @@ -142,7 +145,9 @@ public async void Run() Account initialAccount = wallet.GetAccount(26); Console.WriteLine($"InitialAccount: {initialAccount}"); - byte[] tx = new TransactionBuilder().SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash) + RequestResult> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + + byte[] tx = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(ownerAccount) .AddInstruction(TokenProgram.MintTo( mintAccount.PublicKey, @@ -187,7 +192,9 @@ public async void Run() Account newAccount = wallet.GetAccount(33); Console.WriteLine($"NewAccount: {newAccount}"); - byte[] tx = new TransactionBuilder().SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash) + RequestResult> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + + byte[] tx = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(ownerAccount) .AddInstruction(SystemProgram.CreateAccount( ownerAccount.PublicKey, @@ -243,8 +250,10 @@ public async void Run() Account newAccount = wallet.GetAccount(27); Console.WriteLine($"NewAccount: {newAccount}"); + RequestResult> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + byte[] tx = new TransactionBuilder() - .SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash) + .SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(ownerAccount) .AddInstruction(SystemProgram.CreateAccount( ownerAccount.PublicKey, @@ -300,8 +309,10 @@ public async void Run() Account newAuthority = wallet.GetAccount(1129); Console.WriteLine($"NewAuthority: {newAuthority}"); + RequestResult> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + byte[] tx = new TransactionBuilder() - .SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash) + .SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(ownerAccount) .AddInstruction(SystemProgram.CreateAccount( ownerAccount.PublicKey, @@ -408,7 +419,9 @@ public async void Run() Account mintAccount = wallet.GetAccount(21); Account initialAccount = wallet.GetAccount(26); - byte[] msgData = new TransactionBuilder().SetRecentBlockHash(rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash) + RequestResult> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + + byte[] msgData = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(ownerAccount) .AddInstruction(TokenProgram.Burn( initialAccount.PublicKey, @@ -446,7 +459,8 @@ public async void Run() Account fromAccount = wallet.GetAccount(10); Account toAccount = wallet.GetAccount(8); - string latestBlockHash = rpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash; + RequestResult> latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync(); + string latestBlockHash = latestBlockHashItem.Result.Value.Blockhash; Console.WriteLine($"BlockHash >> {latestBlockHash}"); TransactionBuilder txBuilder = new TransactionBuilder() diff --git a/src/Solana.Unity.Examples/TransactionDecodingExample.cs b/src/Solana.Unity.Examples/TransactionDecodingExample.cs index 9b30670..80840bc 100644 --- a/src/Solana.Unity.Examples/TransactionDecodingExample.cs +++ b/src/Solana.Unity.Examples/TransactionDecodingExample.cs @@ -42,8 +42,10 @@ public async void Run() Console.WriteLine($"MintAccount: {mintAccount}"); Console.WriteLine($"InitialAccount: {initialAccount}"); + RequestResult> latestBlockHashItem = await RpcClient.GetLatestBlockHashAsync(); + byte[] msgData = new TransactionBuilder() - .SetRecentBlockHash(RpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash) + .SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(ownerAccount) .AddInstruction(TokenProgram.InitializeMint( mintAccount.PublicKey, diff --git a/src/Solana.Unity.Programs/Abstract/TransactionalBaseClient.cs b/src/Solana.Unity.Programs/Abstract/TransactionalBaseClient.cs index 80a2774..2723dbc 100644 --- a/src/Solana.Unity.Programs/Abstract/TransactionalBaseClient.cs +++ b/src/Solana.Unity.Programs/Abstract/TransactionalBaseClient.cs @@ -54,10 +54,10 @@ protected TransactionalBaseClient(IRpcClient rpcClient, IStreamingRpcClient stre protected async Task> SignAndSendTransaction(TransactionInstruction instruction, PublicKey feePayer, Func signingCallback, Commitment commitment = Commitment.Finalized) { + RequestResult> latestBlockHashItem = await RpcClient.GetLatestBlockHashAsync(); TransactionBuilder tb = new TransactionBuilder(); tb.AddInstruction(instruction); - - tb.SetRecentBlockHash(RpcClient.GetLatestBlockHashAsync().Result.Result.Value.Blockhash); + tb.SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash); tb.SetFeePayer(feePayer); var wireFmt = tb.CompileMessage(); diff --git a/test/Solana.Unity.Dex.Test/Orca/Integration/IncreaseLiquidityTests.cs b/test/Solana.Unity.Dex.Test/Orca/Integration/IncreaseLiquidityTests.cs index 82bee78..90b6014 100644 --- a/test/Solana.Unity.Dex.Test/Orca/Integration/IncreaseLiquidityTests.cs +++ b/test/Solana.Unity.Dex.Test/Orca/Integration/IncreaseLiquidityTests.cs @@ -16,6 +16,8 @@ using Solana.Unity.Dex.Orca.Core.Errors; using Solana.Unity.Dex.Orca.Core.Accounts; using Solana.Unity.Dex.Ticks; +using Solana.Unity.Rpc.Core.Http; +using Solana.Unity.Rpc.Models; namespace Solana.Unity.Dex.Test.Orca.Integration { @@ -287,7 +289,8 @@ public static async Task InitializeIncreaseLiquiditySingleTransaction() ); //initialize tick array, open position, and increase liquidity in one transaction - string latestBlockHash = _context.RpcClient.GetLatestBlockHashAsync(_context.WhirlpoolClient.DefaultCommitment).Result.Result.Value.Blockhash; + RequestResult> latestBlockHashItem = await _context.RpcClient.GetLatestBlockHashAsync(_context.WhirlpoolClient.DefaultCommitment); + string latestBlockHash = latestBlockHashItem.Result.Value.Blockhash; byte[] tx = new TransactionBuilder() .SetRecentBlockHash(latestBlockHash) .SetFeePayer(openPositionParams.Accounts.Funder) diff --git a/test/Solana.Unity.Dex.Test/Orca/Utils/LiquidityTestUtils.cs b/test/Solana.Unity.Dex.Test/Orca/Utils/LiquidityTestUtils.cs index 6a9114b..3f00500 100644 --- a/test/Solana.Unity.Dex.Test/Orca/Utils/LiquidityTestUtils.cs +++ b/test/Solana.Unity.Dex.Test/Orca/Utils/LiquidityTestUtils.cs @@ -12,6 +12,7 @@ using Solana.Unity.Rpc; using Solana.Unity.Rpc.Builders; using Solana.Unity.Rpc.Types; +using Solana.Unity.Rpc.Models; namespace Solana.Unity.Dex.Test.Orca.Utils { @@ -304,9 +305,10 @@ public static async Task CreateAssociatedTokenAccountInstructionIfNotExtant(Publ ); if (!exists) { + RequestResult> latestBlockHashItem = await rpc.GetLatestBlockHashAsync(); TransactionBuilder builder = new(); builder.SetFeePayer(feePayer); - builder.SetRecentBlockHash(rpc.GetLatestBlockHashAsync().Result.Result.Value.Blockhash); + builder.SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash); builder.AddInstruction( AssociatedTokenAccountProgram.CreateAssociatedTokenAccount( feePayer, owner, mintAddress, idempotent: true diff --git a/test/Solana.Unity.Dex.Test/Orca/Utils/TokenUtils.cs b/test/Solana.Unity.Dex.Test/Orca/Utils/TokenUtils.cs index d5d97ea..9348f3b 100644 --- a/test/Solana.Unity.Dex.Test/Orca/Utils/TokenUtils.cs +++ b/test/Solana.Unity.Dex.Test/Orca/Utils/TokenUtils.cs @@ -45,7 +45,8 @@ public static async Task CreateMintAsync( mintAccount = (mintAccount == null) ? new Account() : mintAccount; Account ownerAccount = authority; - byte[] tx = new TransactionBuilder().SetRecentBlockHash(ctx.RpcClient.GetLatestBlockHashAsync(commitment).Result.Result.Value.Blockhash) + RequestResult> latestBlockHashItem = await ctx.RpcClient.GetLatestBlockHashAsync(commitment); + byte[] tx = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(authority) .AddInstruction(SystemProgram.CreateAccount( fromAccount: ownerAccount.PublicKey, @@ -84,8 +85,9 @@ public static async Task CreateTokenAccountAsync( ulong minBalanceForExemption = (await ctx.RpcClient.GetMinimumBalanceForRentExemptionAsync(TokenProgram.TokenAccountDataSize)).Result; + RequestResult> latestBlockHashItem = await ctx.RpcClient.GetLatestBlockHashAsync(commitment); byte[] tx = new TransactionBuilder() - .SetRecentBlockHash(ctx.RpcClient.GetLatestBlockHashAsync(commitment).Result.Result.Value.Blockhash) + .SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(ownerAccount) .AddInstruction(SystemProgram.CreateAccount( fromAccount: ownerAccount, @@ -129,8 +131,9 @@ public static async Task CreateAndMintToTokenAccountAsync( ulong minBalanceForExemption = (await ctx.RpcClient.GetMinimumBalanceForRentExemptionAsync(TokenProgram.TokenAccountDataSize)).Result; + RequestResult> latestBlockHashItem = await ctx.RpcClient.GetLatestBlockHashAsync(); byte[] tx = new TransactionBuilder() - .SetRecentBlockHash(ctx.RpcClient.GetLatestBlockHashAsync(commitment).Result.Result.Value.Blockhash) + .SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(fromAccount) .AddInstruction(SystemProgram.CreateAccount( //create account fromAccount: fromAccount, @@ -176,7 +179,8 @@ public static async Task> ApproveTokenAsync( Commitment commitment = Commitment.Finalized ) { - byte[] tx = new TransactionBuilder().SetRecentBlockHash(ctx.RpcClient.GetLatestBlockHashAsync(commitment).Result.Result.Value.Blockhash) + RequestResult> latestBlockHashItem = await ctx.RpcClient.GetLatestBlockHashAsync(commitment); + byte[] tx = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(ownerAccount) .AddInstruction(TokenProgram.Approve( source: tokenAccount, @@ -207,7 +211,8 @@ public static async Task> SetAuthorityAsync( Commitment commitment = Commitment.Finalized ) { - byte[] tx = new TransactionBuilder().SetRecentBlockHash(ctx.RpcClient.GetLatestBlockHashAsync(commitment).Result.Result.Value.Blockhash) + RequestResult> latestBlockHashItem = await ctx.RpcClient.GetLatestBlockHashAsync(); + byte[] tx = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(authorityAccount) .AddInstruction(TokenProgram.SetAuthority( account: tokenAccount, @@ -241,8 +246,9 @@ public static async Task> MintToByAuthorityAsync( Commitment commitment = Commitment.Finalized ) { + RequestResult> latestBlockHashItem = await ctx.RpcClient.GetLatestBlockHashAsync(commitment); byte[] tx = new TransactionBuilder() - .SetRecentBlockHash(ctx.RpcClient.GetLatestBlockHashAsync(commitment).Result.Result.Value.Blockhash) + .SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(feePayer.PublicKey) .AddInstruction(TokenProgram.MintTo( mint, @@ -273,8 +279,9 @@ public static async Task> TransferTokensAsync( Commitment commitment = Commitment.Finalized ) { + RequestResult> latestBlockHashItem = await ctx.RpcClient.GetLatestBlockHashAsync(commitment); byte[] tx = new TransactionBuilder() - .SetRecentBlockHash(ctx.RpcClient.GetLatestBlockHashAsync(commitment).Result.Result.Value.Blockhash) + .SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash) .SetFeePayer(ctx.WalletPubKey) .AddInstruction( TokenProgram.Transfer( @@ -344,10 +351,11 @@ public static async Task CloseAta(IRpcClient rpc, PublicKey mint, Account author balance = (await rpc.GetTokenBalanceByOwnerAsync( authority.PublicKey, mint)).Result.Value.AmountUlong; } + RequestResult> latestBlockHashItem = await rpc.GetLatestBlockHashAsync(commitment: Commitment.Finalized); TransactionBuilder txb = new(); txb .SetFeePayer(authority) - .SetRecentBlockHash(rpc.GetLatestBlockHashAsync(commitment: Commitment.Finalized).Result.Result.Value.Blockhash); + .SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash); if (balance > 0) { // Send the balance to a random ATA, close fails if balance is not 0 for not native tokens