Skip to content

Commit af49063

Browse files
authored
Updated GetRecentBlockHash to GetLatestBlockHash (#56)
* Updated GetRecentBlockHash to GetLatestBlockHash instances on the Solana RPC Client. Included core code and tests, but left interface-instances for convenience. * Explicitly awaiting GetLatestBlockHash everywhere * Changed explicit variabled typing to implicit, on request of magicblocks team.
1 parent 2fd9825 commit af49063

16 files changed

+174
-157
lines changed

src/Solana.Unity.Dex/Orca/Orca/OrcaDex.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using Solana.Unity.Dex.Quotes;
2323
using Solana.Unity.Dex.Ticks;
2424
using System.Linq;
25+
using Solana.Unity.Rpc.Core.Http;
2526

2627
namespace Orca
2728
{
@@ -1069,7 +1070,8 @@ Commitment commitment
10691070
)
10701071
{
10711072
tb.SetFeePayer(feePayer);
1072-
tb.SetRecentBlockHash((await rpcClient.GetRecentBlockHashAsync(commitment)).Result.Value.Blockhash);
1073+
var latestBlockHash = await rpcClient.GetLatestBlockHashAsync();
1074+
tb.SetRecentBlockHash(latestBlockHash.Result.Value.Blockhash);
10731075

10741076
return Transaction.Deserialize(tb.Serialize());
10751077
}

src/Solana.Unity.Dex/Orca/SolanaApi/SystemUtils.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Solana.Unity.Rpc.Types;
88
using Solana.Unity.Rpc.Core.Http;
99
using Solana.Unity.Programs;
10+
using Solana.Unity.Rpc.Models;
1011

1112
namespace Solana.Unity.Dex.Orca.SolanaApi
1213
{
@@ -55,10 +56,9 @@ public static async Task<RequestResult<string>> TransferSolAsync(
5556
Commitment commitment = Commitment.Finalized
5657
)
5758
{
58-
var recentHash = await ctx.RpcClient.GetRecentBlockHashAsync();
59-
59+
var latestBlockHashItem = await ctx.RpcClient.GetLatestBlockHashAsync();
6060
var tb = new TransactionBuilder()
61-
.SetRecentBlockHash(recentHash.Result.Value.Blockhash)
61+
.SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
6262
.SetFeePayer(from.PublicKey)
6363
.AddInstruction(SystemProgram.Transfer(from.PublicKey, to, lamports));
6464

@@ -84,10 +84,9 @@ public static async Task<RequestResult<string>> CreateAccountAsync(
8484
Commitment commitment = Commitment.Finalized
8585
)
8686
{
87-
var recentHash = await ctx.RpcClient.GetRecentBlockHashAsync();
88-
87+
var latestBlockHashItem = await ctx.RpcClient.GetLatestBlockHashAsync();
8988
var tx = new TransactionBuilder()
90-
.SetRecentBlockHash(recentHash.Result.Value.Blockhash)
89+
.SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
9190
.SetFeePayer(feePayer)
9291
.AddInstruction(SystemProgram.CreateAccount(
9392
fromAccount: fromAccount,

src/Solana.Unity.Dex/Orca/SolanaApi/TokenUtilsTransaction.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Solana.Unity.Rpc;
1111
using Solana.Unity.Rpc.Models;
1212
using Solana.Unity.Rpc.Types;
13+
using Solana.Unity.Rpc.Core.Http;
1314

1415
namespace Solana.Unity.Dex.Orca.SolanaApi
1516
{
@@ -33,9 +34,9 @@ public static async Task<Transaction> CreateMint(
3334
{
3435
ulong minBalanceForExemptionMint =
3536
(await rpc.GetMinimumBalanceForRentExemptionAsync(TokenProgram.TokenAccountDataSize)).Result;
36-
37-
var blockHash = await rpc.GetRecentBlockHashAsync();
38-
byte[] tx = new TransactionBuilder().SetRecentBlockHash(blockHash.Result.Value.Blockhash)
37+
38+
var latestBlockHashItem = await rpc.GetLatestBlockHashAsync();
39+
byte[] tx = new TransactionBuilder().SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
3940
.SetFeePayer(authority)
4041
.AddInstruction(SystemProgram.CreateAccount(
4142
fromAccount: authority,
@@ -79,12 +80,12 @@ public static async Task<Transaction> CreateAndMintToAssociatedTokenAccount (
7980
ulong rentExemptionMin =
8081
(await rpc.GetMinimumBalanceForRentExemptionAsync(TokenProgram.TokenAccountDataSize)).Result;
8182

82-
var blockHash = await rpc.GetRecentBlockHashAsync();
8383
var tempAccount = new Account();
8484
var sta = AssociatedTokenAccountProgram.DeriveAssociatedTokenAccount(tempAccount, mint);
85+
var latestBlockHashItem = await rpc.GetLatestBlockHashAsync();
8586
var trxBuild= new TransactionBuilder()
8687
.SetFeePayer(feePayer.PublicKey)
87-
.SetRecentBlockHash(blockHash.Result.Value.Blockhash)
88+
.SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
8889
.AddInstruction(SystemProgram.CreateAccount(
8990
feePayer,
9091
tempAccount,
@@ -123,9 +124,9 @@ public static async Task<Transaction> CreateAndMintToAssociatedTokenAccount (
123124
}
124125
else
125126
{
126-
var blockHash = await rpc.GetRecentBlockHashAsync();
127+
var latestBlockHashItem = await rpc.GetLatestBlockHashAsync();
127128
var txBuilder = new TransactionBuilder()
128-
.SetRecentBlockHash(blockHash.Result.Value.Blockhash)
129+
.SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
129130
.SetFeePayer(feePayer.PublicKey)
130131
.AddInstruction(
131132
AssociatedTokenAccountProgram.CreateAssociatedTokenAccount(
@@ -167,9 +168,9 @@ public static async Task<Transaction> MintToByAuthority(
167168
Account feePayer
168169
)
169170
{
170-
var blockHash = await rpc.GetRecentBlockHashAsync();
171+
var latestBlockHashItem = await rpc.GetLatestBlockHashAsync();
171172
byte[] tx = new TransactionBuilder()
172-
.SetRecentBlockHash(blockHash.Result.Value.Blockhash)
173+
.SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash)
173174
.SetFeePayer(feePayer.PublicKey)
174175
.AddInstruction(TokenProgram.MintTo(
175176
mint,

src/Solana.Unity.Examples/AssociatedTokenAccountsExample.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ public async void Run()
3333
#region Create and Initialize a token Mint Account
3434

3535

36-
RequestResult<ResponseValue<BlockHash>> blockHash = await RpcClient.GetRecentBlockHashAsync();
37-
3836
ulong minBalanceForExemptionAcc =
3937
(await RpcClient.GetMinimumBalanceForRentExemptionAsync(TokenProgram.TokenAccountDataSize)).Result;
4038
ulong minBalanceForExemptionMint =
@@ -50,8 +48,11 @@ public async void Run()
5048
Console.WriteLine($"MintAccount: {mintAccount}");
5149
Console.WriteLine($"InitialAccount: {initialAccount}");
5250

51+
var latestBlockHashItem = await RpcClient.GetLatestBlockHashAsync();
52+
string latestBlockHash = latestBlockHashItem.Result.Value.Blockhash;
53+
5354
byte[] createAndInitializeMintToTx = new TransactionBuilder().
54-
SetRecentBlockHash(blockHash.Result.Value.Blockhash).
55+
SetRecentBlockHash(latestBlockHash).
5556
SetFeePayer(ownerAccount).
5657
AddInstruction(SystemProgram.CreateAccount(
5758
ownerAccount,
@@ -104,7 +105,7 @@ public async void Run()
104105
Console.WriteLine($"AssociatedTokenAccount: {associatedTokenAccount}");
105106

106107
byte[] createAssociatedTokenAccountTx = new TransactionBuilder().
107-
SetRecentBlockHash(blockHash.Result.Value.Blockhash).
108+
SetRecentBlockHash(latestBlockHash).
108109
SetFeePayer(ownerAccount).
109110
AddInstruction(AssociatedTokenAccountProgram.CreateAssociatedTokenAccount(
110111
ownerAccount,

src/Solana.Unity.Examples/HelloWorldExample.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using Solana.Unity.Programs;
22
using Solana.Unity.Rpc;
33
using Solana.Unity.Rpc.Builders;
4+
using Solana.Unity.Rpc.Core.Http;
5+
using Solana.Unity.Rpc.Models;
46
using Solana.Unity.Wallet;
57
using Solana.Unity.Wallet.Bip39;
68
using System;
@@ -43,10 +45,10 @@ public async void Run()
4345

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

46-
var recentHash = await rpcClient.GetRecentBlockHashAsync();
48+
var latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync();
4749

4850
var tx = new TransactionBuilder().AddInstruction(memoInstruction).SetFeePayer(wallet.Account)
49-
.SetRecentBlockHash(recentHash.Result.Value.Blockhash).Build(wallet.Account);
51+
.SetRecentBlockHash(latestBlockHashItem.Result.Value.Blockhash).Build(wallet.Account);
5052

5153
var txHash = await rpcClient.SendTransactionAsync(tx);
5254

src/Solana.Unity.Examples/InstructionDecoderExample.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Solana.Unity.Programs;
22
using Solana.Unity.Rpc;
33
using Solana.Unity.Rpc.Builders;
4+
using Solana.Unity.Rpc.Core.Http;
45
using Solana.Unity.Rpc.Models;
56
using System;
67
using System.IO;
@@ -24,11 +25,12 @@ public async void Run()
2425
var fromAccount = wallet.GetAccount(10);
2526
var toAccount = wallet.GetAccount(8);
2627

27-
var blockHash = await rpcClient.GetRecentBlockHashAsync();
28-
Console.WriteLine($"BlockHash >> {blockHash.Result.Value.Blockhash}");
28+
var latestBlockHashItem = await rpcClient.GetLatestBlockHashAsync();
29+
string latestBlockHash = latestBlockHashItem.Result.Value.Blockhash;
30+
Console.WriteLine($"BlockHash >> {latestBlockHash}");
2931

3032
var msgBytes = new TransactionBuilder()
31-
.SetRecentBlockHash(blockHash.Result.Value.Blockhash)
33+
.SetRecentBlockHash(latestBlockHash)
3234
.SetFeePayer(fromAccount)
3335
.AddInstruction(SystemProgram.Transfer(fromAccount.PublicKey, toAccount.PublicKey, 10000000))
3436
.AddInstruction(MemoProgram.NewMemo(fromAccount.PublicKey, "Hello from Sol.Net :)"))

0 commit comments

Comments
 (0)