-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
89 lines (74 loc) · 2.91 KB
/
Program.cs
File metadata and controls
89 lines (74 loc) · 2.91 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using FrontierSharp.SuiClient;
using FrontierSharp.SuiClient.GraphQl;
using FrontierSharp.SuiClient.Models;
using Microsoft.Extensions.DependencyInjection;
using ZiggyCreatures.Caching.Fusion;
var services = new ServiceCollection();
services.AddLogging();
services.AddHttpClient("SuiGraphQl", httpClient => {
httpClient.Timeout = TimeSpan.FromSeconds(30);
});
services.AddFusionCache().AsHybridCache();
services.Configure<SuiClientOptions>(options => {
options.HttpClientName = "SuiGraphQl";
// Optional overrides:
// options.GraphQlEndpoint = "https://graphql.testnet.sui.io/graphql";
// options.WorldPackageAddress = "0x...";
});
services.AddSingleton<ISuiGraphQlClient, SuiGraphQlClient>();
services.AddSingleton<IWorldClient, WorldClient>();
using var serviceProvider = services.BuildServiceProvider();
var worldClient = serviceProvider.GetRequiredService<IWorldClient>();
var initialAssembliesResult = await worldClient.GetAllAssembliesAsync(first: 100);
if (initialAssembliesResult.IsFailed) {
Console.WriteLine("Failed to load the initial assembly snapshot:");
foreach (var error in initialAssembliesResult.Errors) {
Console.WriteLine($" - {error.Message}");
}
return;
}
var currentAssemblies = initialAssembliesResult.Value.ToList();
Console.WriteLine($"Loaded {currentAssemblies.Count} assemblies.");
Console.WriteLine("Watching for assembly changes. Press Ctrl+C to stop.");
var shutdown = new CancellationTokenSource();
Console.CancelKeyPress += (_, eventArgs) => {
eventArgs.Cancel = true;
shutdown.Cancel();
};
try {
var subscriptionResult = await worldClient.SubscribeToAssemblyUpdatesAsync(
currentAssemblies,
(batch, _) => {
foreach (var change in batch.Changes) {
var assembly = change.Current ?? change.Previous;
if (assembly == null)
continue;
var status = change.Current?.Status.ToString() ?? "Removed";
Console.WriteLine($"[{DateTimeOffset.UtcNow:O}] {change.ChangeType}: {assembly.Key.Tenant}/{assembly.Key.ItemId} ({status})");
}
return Task.CompletedTask;
},
new AssemblySubscriptionOptions {
PollInterval = TimeSpan.FromSeconds(5),
PageSize = 100
},
shutdown.Token);
if (subscriptionResult.IsFailed) {
Console.WriteLine("Failed to start assembly watcher:");
foreach (var error in subscriptionResult.Errors) {
Console.WriteLine($" - {error.Message}");
}
return;
}
using var subscription = subscriptionResult.Value;
try {
await Task.Delay(Timeout.InfiniteTimeSpan, shutdown.Token);
} catch (OperationCanceledException) {
// Expected when Ctrl+C is pressed.
}
subscription.Dispose();
await subscription.Completion;
Console.WriteLine("Assembly watcher stopped.");
} finally {
shutdown.Dispose();
}