Skip to content

Commit

Permalink
➕ Add GetLatestBlockHash method (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielePicco authored Jul 18, 2023
1 parent d1ddd4e commit 4fac5e0
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Solana.Unity.Rpc/IRpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,13 @@ Task<RequestResult<List<AccountKeyPair>>> GetProgramAccountsAsync(string pubKey,
/// <returns>Returns a task that holds the asynchronous operation result and state.</returns>
Task<RequestResult<ResponseValue<BlockHash>>> GetRecentBlockHashAsync(Commitment commitment = default);

/// <summary>
/// Gets the latest block hash.
/// </summary>
/// <param name="commitment">The state commitment to consider when querying the ledger state.</param>
/// <returns>Returns a task that holds the asynchronous operation result and state.</returns>
Task<RequestResult<ResponseValue<LatestBlockHash>>> GetLatestBlockHashAsync(Commitment commitment = Commitment.Finalized);

/// <summary>
/// Gets a list of recent performance samples.
/// <remarks>
Expand Down
17 changes: 17 additions & 0 deletions src/Solana.Unity.Rpc/Models/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,21 @@ public class BlockHash
/// </summary>
public FeeCalculator FeeCalculator { get; set; }
}


/// <summary>
/// Represents the latest block hash info.
/// </summary>
public class LatestBlockHash
{
/// <summary>
/// A base-58 encoded string representing the block hash.
/// </summary>
public string Blockhash { get; set; }

/// <summary>
/// The last block height at which the blockhash will be valid.
/// </summary>
public ulong LastValidBlockHeight { get; set; }
}
}
9 changes: 8 additions & 1 deletion src/Solana.Unity.Rpc/SolanaRpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ private JsonRpcRequest BuildRequest<T>(string method, IList<object> parameters)
private async Task<RequestResult<T>> SendRequestAsync<T>(string method)
{
JsonRpcRequest req = BuildRequest<T>(method, null);

return await SendRequest<T>(req);
}

Expand Down Expand Up @@ -564,6 +563,14 @@ public async Task<RequestResult<ResponseValue<BlockHash>>> GetRecentBlockHashAsy
/// <inheritdoc cref="IRpcClient.GetRecentBlockHash"/>
public RequestResult<ResponseValue<BlockHash>> GetRecentBlockHash(Commitment commitment = default)
=> GetRecentBlockHashAsync(commitment).Result;

/// <inheritdoc cref="IRpcClient.GetLatestBlockHashAsync"/>
public async Task<RequestResult<ResponseValue<LatestBlockHash>>> GetLatestBlockHashAsync(
Commitment commitment = Commitment.Finalized)
{
return await SendRequestAsync<ResponseValue<LatestBlockHash>>("getLatestBlockhash",
Parameters.Create(ConfigObject.Create(HandleCommitment(commitment))));
}

/// <inheritdoc cref="IRpcClient.GetMaxRetransmitSlotAsync"/>
public async Task<RequestResult<ulong>> GetMaxRetransmitSlotAsync()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"method":"getLatestBlockhash","jsonrpc":"2.0","id":0}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"jsonrpc":"2.0","result":{"context":{"slot":127140942},"value":{"blockhash":"DDFfxGAsEVcqNbCLRgvDtzcc2ZxNnqJfQJfMTRhEEPwW","lastValidBlockHeight":115143990}},"id":0}
6 changes: 6 additions & 0 deletions test/Solana.Unity.Rpc.Test/Solana.Unity.Rpc.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,12 @@
<None Update="Resources\Http\GetRecentBlockhashRequest.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Http\Blocks\GetLatestBlockhashRequest.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Http\Blocks\GetLatestBlockhashResponse.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Http\Transaction\SimulateTransactionRequest.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
27 changes: 27 additions & 0 deletions test/Solana.Unity.Rpc.Test/SolanaRpcClientBlockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,33 @@ public void TestGetRecentBlockHash()

FinishTest(messageHandlerMock, TestnetUri);
}

[TestMethod]
public async Task TestGetLatestBlockHash()
{
var responseData = File.ReadAllText("Resources/Http/Blocks/GetLatestBlockhashResponse.json");
var requestData = File.ReadAllText("Resources/Http/Blocks/GetLatestBlockhashRequest.json");
var sentMessage = string.Empty;
var messageHandlerMock = SetupTest(
(s => sentMessage = s), responseData);

var httpClient = new HttpClient(messageHandlerMock.Object)
{
BaseAddress = TestnetUri,
};

var sut = new SolanaRpcClient(TestnetUrl, null, httpClient);
var result = await sut.GetLatestBlockHashAsync();

Assert.AreEqual(requestData, sentMessage);
Assert.IsNotNull(result.Result);
Assert.IsTrue(result.WasSuccessful);
Assert.AreEqual(127140942UL, result.Result.Context.Slot);
Assert.AreEqual("DDFfxGAsEVcqNbCLRgvDtzcc2ZxNnqJfQJfMTRhEEPwW", result.Result.Value.Blockhash);
Assert.AreEqual(115143990UL, result.Result.Value.LastValidBlockHeight);

FinishTest(messageHandlerMock, TestnetUri);
}

[TestMethod]
public void TestGetRecentBlockHashProcessed()
Expand Down

0 comments on commit 4fac5e0

Please sign in to comment.