-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
57 lines (49 loc) · 1.63 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using Grpc.Net.Client;
using GrpcGeyser;
using Microsoft.Extensions.Configuration;
// Load configuration
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
var endpoint = configuration["GrpcService:Endpoint"];
var commitment = configuration["GrpcService:Commitment"];
var pingIntervalMs = int.Parse(configuration["Subscription:PingIntervalMs"] ?? "5000");
var pingId = int.Parse(configuration["Subscription:PingId"] ?? "1");
var accountToTrack = configuration["Subscription:AccountToTrack"];
using var channel = GrpcChannel.ForAddress(endpoint!);
var client = new Geyser.GeyserClient(channel);
var request = new SubscribeRequest
{
Commitment = Enum.Parse<CommitmentLevel>(commitment!)
};
request.Blocks.Add("block", new SubscribeRequestFilterBlocks
{
AccountInclude = { accountToTrack },
IncludeTransactions = true,
IncludeAccounts = false,
IncludeEntries = false
});
var pingRequest = new SubscribeRequest
{
Ping = new SubscribeRequestPing
{
Id = pingId
}
};
using var stream = client.Subscribe();
var cancellationToken = new CancellationToken();
await stream.RequestStream.WriteAsync(request);
Task responseTask = Task.Run(async () =>
{
while (await stream.ResponseStream.MoveNext(cancellationToken))
{
var message = stream.ResponseStream.Current;
Console.WriteLine(message.ToString());
}
});
Timer timer = new(async (state) =>
{
await stream.RequestStream.WriteAsync(pingRequest);
}, null, pingIntervalMs, pingIntervalMs);
await Task.WhenAny(responseTask, Task.Run(() => Console.ReadKey()));