Skip to content

Commit

Permalink
Bootstrapping streaming client.
Browse files Browse the repository at this point in the history
Added all relevant models.
Started unit test project.
  • Loading branch information
tiago18c committed May 23, 2021
1 parent 7a4bbf4 commit 675736f
Show file tree
Hide file tree
Showing 17 changed files with 630 additions and 12 deletions.
1 change: 1 addition & 0 deletions Solnet.Rpc.Test/Resources/AccountSubscribe.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"method":"accountSubscribe","params":["CM78CPUeXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNH12",{"encoding":"base64"}],"jsonrpc":"2.0","id":0}
45 changes: 45 additions & 0 deletions Solnet.Rpc.Test/SolanaStreamingClientTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Solnet.Rpc.Core.Sockets;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Solnet.Rpc.Messages;
using Solnet.Rpc.Models;
using System;
using System.Threading;
using System.IO;
using System.Net.WebSockets;
using System.Text;

namespace Solnet.Rpc.Test
{
[TestClass]
public class SolanaStreamingClientTest
{
[TestMethod]
public void SubscribeAccountInfoTest()
{
var expected = File.ReadAllText("Resources/AccountSubscribe.json");
ReadOnlyMemory<byte> result = new ReadOnlyMemory<byte>();
var socket = new Mock<IWebSocket>();

SolanaStreamingClient sut = new SolanaStreamingClient("wss://api.mainnet-beta.solana.com/", socket.Object);

var pubKey = "CM78CPUeXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNH12";

var action = new Mock<Action<SubscriptionState, ResponseValue<AccountInfo>>>();

socket.Setup(_ => _.ConnectAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
.Callback(() => socket.SetupGet(s => s.State).Returns(WebSocketState.Open));

socket.Setup(_ => _.SendAsync(It.IsAny<ReadOnlyMemory<byte>>(), WebSocketMessageType.Text, true, It.IsAny<CancellationToken>()))
.Callback<ReadOnlyMemory<byte>, WebSocketMessageType, bool, CancellationToken>((mem, _, _, _) => result = mem);

sut.Init().Wait();
sut.SubscribeAccountInfo(pubKey, action.Object);

socket.Verify(s => s.SendAsync(It.IsAny<ReadOnlyMemory<byte>>(), WebSocketMessageType.Text, true, It.IsAny<CancellationToken>()));
var res = Encoding.UTF8.GetString(result.Span);
Assert.AreEqual(expected, res);
}

}
}
27 changes: 27 additions & 0 deletions Solnet.Rpc.Test/Solnet.Rpc.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.1" />
<PackageReference Include="coverlet.collector" Version="1.3.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\Solnet.Rpc\Solnet.Rpc.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="Resources\AccountSubscribe.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
31 changes: 21 additions & 10 deletions Solnet.sln
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Solnet.Rpc", "src\Solnet.Rpc\Solnet.Rpc.csproj", "{FE8D270F-30AE-4818-AC7B-030591820419}"
# Visual Studio Version 16
VisualStudioVersion = 16.0.31129.286
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Solnet.Rpc", "src\Solnet.Rpc\Solnet.Rpc.csproj", "{FE8D270F-30AE-4818-AC7B-030591820419}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Solnet.Models", "src\Solnet.Models\Solnet.Models.csproj", "{8B503BC5-F1C8-4F87-8C53-AD4D6CEFFCF4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Solnet.Models", "src\Solnet.Models\Solnet.Models.csproj", "{8B503BC5-F1C8-4F87-8C53-AD4D6CEFFCF4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Solnet.KeyStore", "src\Solnet.KeyStore\Solnet.KeyStore.csproj", "{BA5BFA52-8010-4438-BC87-153A34464CAA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Solnet.KeyStore", "src\Solnet.KeyStore\Solnet.KeyStore.csproj", "{BA5BFA52-8010-4438-BC87-153A34464CAA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Solnet.Wallet", "src\Solnet.Wallet\Solnet.Wallet.csproj", "{DA22B717-4FD7-4D08-A546-DFF3B21B7BAE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Solnet.Wallet", "src\Solnet.Wallet\Solnet.Wallet.csproj", "{DA22B717-4FD7-4D08-A546-DFF3B21B7BAE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Solnet.Examples", "src\Solnet.Examples\Solnet.Examples.csproj", "{1198ED67-B1F1-4DE9-BDE8-AE6C96DA33A6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Solnet.Examples", "src\Solnet.Examples\Solnet.Examples.csproj", "{1198ED67-B1F1-4DE9-BDE8-AE6C96DA33A6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Solnet.Util", "src\Solnet.Util\Solnet.Util.csproj", "{5B8896C9-4B06-41C9-B0F9-05B9F6190CA5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Solnet.Util", "src\Solnet.Util\Solnet.Util.csproj", "{5B8896C9-4B06-41C9-B0F9-05B9F6190CA5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Solnet.Rpc.Test", "Solnet.Rpc.Test\Solnet.Rpc.Test.csproj", "{E907F659-7FDE-4B98-85CD-1FBC6B1656CD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -42,9 +47,15 @@ Global
{5B8896C9-4B06-41C9-B0F9-05B9F6190CA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5B8896C9-4B06-41C9-B0F9-05B9F6190CA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5B8896C9-4B06-41C9-B0F9-05B9F6190CA5}.Release|Any CPU.Build.0 = Release|Any CPU
{3C534072-1F5F-4625-9831-ACBBA28E5C47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3C534072-1F5F-4625-9831-ACBBA28E5C47}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C534072-1F5F-4625-9831-ACBBA28E5C47}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C534072-1F5F-4625-9831-ACBBA28E5C47}.Release|Any CPU.Build.0 = Release|Any CPU
{E907F659-7FDE-4B98-85CD-1FBC6B1656CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E907F659-7FDE-4B98-85CD-1FBC6B1656CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E907F659-7FDE-4B98-85CD-1FBC6B1656CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E907F659-7FDE-4B98-85CD-1FBC6B1656CD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BE3287F3-8CBC-4793-8CC9-A1AE4B8266ED}
EndGlobalSection
EndGlobal
28 changes: 26 additions & 2 deletions src/Solnet.Examples/SolnetRpcTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Solnet.Rpc.Core.Sockets;
using Solnet.Rpc.Http;
using Solnet.Rpc.Messages;
using Solnet.Rpc.Models;

namespace Solnet.Examples
{
class SolnetRpcTester
{

static void Example(string[] args)
static void Main(string[] args)
{

SolanaJsonRpcClient c = new SolanaJsonRpcClient();
Expand All @@ -23,7 +27,27 @@ static void Example(string[] args)

//var blockCommitment = c.GetBlockCommitment(78561320);

var blockTime = c.GetBlockTime(78561320);
//var blockTime = c.GetBlockTime(78561320);

SolanaStreamingClient c2 = new SolanaStreamingClient("wss://api.mainnet-beta.solana.com/");

c2.Init().Wait();


var sub = c2.SubscribeAccountInfo("9UGxCidmZtU1PM7Tbhv2twQ8ChsS6S3HdL1xo56fSVWn", (s, data) =>
{
Console.WriteLine("received data " + data.Value.Lamports);
});

sub.SubscriptionChanged += Sub_SubscriptionChanged;

Console.ReadKey();
Console.ReadKey();
}

private static void Sub_SubscriptionChanged(object sender, SubscriptionEvent e)
{
Console.WriteLine("subcription changed to: " + e.Status);
}
}
}
17 changes: 17 additions & 0 deletions src/Solnet.Rpc/Core/Sockets/IWebSocket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;

namespace Solnet.Rpc.Core.Sockets
{
public interface IWebSocket : IDisposable
{
WebSocketState State { get; }

Task ConnectAsync(Uri uri, CancellationToken cancellationToken);
ValueTask SendAsync(ReadOnlyMemory<byte> buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken);
Task CloseAsync(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken);
ValueTask<ValueWebSocketReceiveResult> ReceiveAsync(Memory<byte> buffer, CancellationToken cancellationToken);
}
}
Loading

0 comments on commit 675736f

Please sign in to comment.