Skip to content

Commit

Permalink
Adding get block height method (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoakbuilds committed Jun 3, 2021
1 parent bc66669 commit c22e863
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/Solnet.Examples/SolnetRpcTester.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Microsoft.Extensions.Logging;
using NBitcoin.Logging;
using Solnet.Rpc;
using Solnet.Rpc.Core.Sockets;
using Solnet.Rpc.Models;
Expand All @@ -20,6 +22,9 @@ static void Main(string[] args)

var cn = c.GetClusterNodes();

var bh = c.GetBlockHeight();
Console.WriteLine(bh.Result);

/* Large accounts for Token Mint PubKey
var largeAccounts = c.GetTokenLargestAccounts("7ugkvt26sFjMdiFQFP5AQX8m8UkxWaW7rk2nBk4R6Gf2");
Expand Down
2 changes: 1 addition & 1 deletion src/Solnet.Rpc/ClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class ClientFactory
/// <summary>
/// The test net cluster.
/// </summary>
private const string RpcTestNet = "https://api.testnet.solana.com";
private const string RpcTestNet = "https://testnet.solana.com";

/// <summary>
/// The main net cluster.
Expand Down
16 changes: 14 additions & 2 deletions src/Solnet.Rpc/IRpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

namespace Solnet.Rpc
{
/// <summary>
/// Specifies the methods to interact with the JSON RPC API.
/// </summary>
public interface IRpcClient
{
/// <summary>
Expand All @@ -31,7 +34,7 @@ public interface IRpcClient
/// <summary>
/// Gets the block commitment of a certain block, identified by slot.
/// </summary>
/// <param name="block">The block.</param>
/// <param name="slot">The slot.</param>
/// <returns>A task which may return a request result and block commitment information.</returns>
Task<RequestResult<BlockCommitment>> GetBlockCommitmentAsync(ulong slot);

Expand All @@ -41,13 +44,22 @@ public interface IRpcClient
/// <summary>
/// Gets the estimated production time for a certain block, identified by slot.
/// </summary>
/// <param name="block">The block.</param>
/// <param name="slot">The slot.</param>
/// <returns>A task which may return a request result and block production time as Unix timestamp (seconds since Epoch).</returns>
Task<RequestResult<ulong>> GetBlockTimeAsync(ulong slot);

/// <inheritdoc cref="SolanaRpcClient.GetBlockTimeAsync"/>
RequestResult<ulong> GetBlockTime(ulong slot);

/// <summary>
/// Gets the current block height of the node.
/// </summary>
/// <returns>A task which may return a request result and a block height.</returns>
Task<RequestResult<ulong>> GetBlockHeightAsync();

/// <inheritdoc cref="SolanaRpcClient.GetBlockHeightAsync"/>
RequestResult<ulong> GetBlockHeight();

/// <summary>
/// Gets the cluster nodes.
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions src/Solnet.Rpc/SolanaRpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ public async Task<RequestResult<ResponseValue<ulong>>> GetBalanceAsync(string pu
public RequestResult<ResponseValue<ulong>> GetBalance(string pubKey)
=> GetBalanceAsync(pubKey).Result;

/// <summary>
/// Gets the current block height of the node.
/// </summary>
/// <returns>A task which may return a request result and a block height.</returns>
public async Task<RequestResult<ulong>> GetBlockHeightAsync()
{
return await SendRequestAsync<ulong>("getBlockHeight");
}
/// <inheritdoc cref="GetBlockHeightAsync"/>
public RequestResult<ulong> GetBlockHeight()
=> GetBlockHeightAsync().Result;

/// <summary>
/// Gets the block commitment of a certain block, identified by slot.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"method":"getBlockHeight","jsonrpc":"2.0","id":0}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"jsonrpc":"2.0","result":1233,"id":0}
26 changes: 26 additions & 0 deletions test/Solnet.Rpc.Test/SolanaRpcClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,32 @@ public void TestGetAccountInfo()

FinishTest(messageHandlerMock, TestnetUri);
}

[TestMethod]
public void TestGetBlockHeight()
{
var responseData = File.ReadAllText("Resources/Http/GetBlockHeightResponse.json");
var requestData = File.ReadAllText("Resources/Http/GetBlockHeightRequest.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 = sut.GetBlockHeight();
Assert.AreEqual(requestData, sentMessage);
Assert.IsNotNull(result);
Assert.IsTrue(result.WasSuccessful);
Assert.AreEqual(1233UL, result.Result);

FinishTest(messageHandlerMock, TestnetUri);
}

[TestMethod]
public void TestGetBlockCommitment()
Expand Down
6 changes: 6 additions & 0 deletions test/Solnet.Rpc.Test/Solnet.Rpc.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@
<None Update="Resources\Http\EmptyPayloadRequest.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Http\GetBlockHeightRequest.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Http\GetBlockHeightResponse.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>

0 comments on commit c22e863

Please sign in to comment.