Skip to content

Commit 09584f8

Browse files
Merge pull request #112 from make-software/ces-parser-example
Added example of using CES Parser with SSE stream
2 parents fe97e1a + 3fff852 commit 09584f8

4 files changed

Lines changed: 123 additions & 0 deletions

File tree

Casper.Network.SDK/CES/CESEvent.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class CESEvent
4040
/// The key in the global state that stores the event (dictionary item)
4141
/// Set by <see cref="CESParser.GetEvents"/>; <c>null</c> when parsed in isolation.
4242
/// </summary>
43+
[JsonConverter(typeof(GlobalStateKey.GlobalStateKeyConverter))]
4344
public GlobalStateKey TransformKey { get; init; }
4445

4546
/// <summary>

Docs/Articles/CasperEventStandard.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,7 @@ if (buyEvent != null)
244244
var json = JsonSerializer.Serialize(events);
245245
Console.WriteLine(json);
246246
```
247+
248+
## Parsing CES events in the SSE stream
249+
250+
See `Docs/Examples/CESParser/Program.cs` for a runnable sample that connects to the node SSE stream, listens to `TransactionProcessed` events, and pretty-prints parsed CES events.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\..\Casper.Network.SDK\Casper.Network.SDK.csproj" />
10+
</ItemGroup>
11+
12+
</Project>

Docs/Examples/CESParser/Program.cs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text.Json;
4+
using System.Threading.Tasks;
5+
using Casper.Network.SDK;
6+
using Casper.Network.SDK.CES;
7+
using Casper.Network.SDK.SSE;
8+
9+
namespace Casper.NET.SDK.Examples
10+
{
11+
public static class CESParserExample
12+
{
13+
private static string nodeAddress = "http://127.0.0.1:11101/rpc";
14+
private static string sseHost = "127.0.0.1";
15+
private static int ssePort = 18101;
16+
17+
private static NetCasperClient casperSdk;
18+
private static IReadOnlyList<CESContractSchema> watchedSchemas;
19+
20+
public static void ListenEvents(int startFrom)
21+
{
22+
var sse = new ServerEventsClient(sseHost, ssePort);
23+
24+
sse.AddEventCallback(
25+
EventType.TransactionProcessed,
26+
"ces-parser-cb",
27+
(SSEvent evt) =>
28+
{
29+
try
30+
{
31+
var transaction = evt.Parse<TransactionProcessed>();
32+
var events = CESParser.GetEvents(
33+
transaction.ExecutionResult.Effect,
34+
watchedSchemas);
35+
36+
Console.WriteLine("TransactionProcessed: " + transaction.TransactionHash);
37+
38+
if (events.Count == 0)
39+
return;
40+
41+
foreach (var parsedEvent in events)
42+
{
43+
Console.WriteLine("CES event:");
44+
Console.WriteLine(PrettyPrintEvent(parsedEvent));
45+
}
46+
}
47+
catch (Exception e)
48+
{
49+
Console.WriteLine(e);
50+
}
51+
},
52+
startFrom: startFrom);
53+
54+
sse.StartListening();
55+
56+
Console.WriteLine("Listening to TransactionProcessed events.");
57+
Console.WriteLine("Press Enter to stop listening.");
58+
Console.ReadLine();
59+
Console.WriteLine("Terminating...");
60+
61+
sse.StopListening().Wait();
62+
63+
Console.WriteLine("Terminated");
64+
}
65+
66+
public static async Task Main(string[] args)
67+
{
68+
var contractHash = args.Length > 0
69+
? args[0]
70+
: "hash-1262d06e53125ea098187fb4d1d5b10a7afed48e5e5eef182ed992fc5b100349";
71+
72+
try
73+
{
74+
casperSdk = new NetCasperClient(nodeAddress);
75+
76+
Console.WriteLine("Loading CES schema for contract: " + contractHash);
77+
var schema = await CESContractSchema.LoadAsync(casperSdk, contractHash);
78+
watchedSchemas = new List<CESContractSchema> { schema };
79+
80+
Console.WriteLine("Resolved contract hash : " + schema.ContractHash);
81+
Console.WriteLine("Schema events URef : " + schema.EventsURef);
82+
Console.WriteLine("Event types:");
83+
foreach (var eventSchema in schema.Events)
84+
{
85+
Console.WriteLine("- " + eventSchema.Key);
86+
}
87+
88+
ListenEvents(0);
89+
}
90+
catch (Exception e)
91+
{
92+
Console.WriteLine(e);
93+
}
94+
}
95+
96+
private static string PrettyPrintEvent(CESEvent evt)
97+
{
98+
var options = new JsonSerializerOptions()
99+
{
100+
WriteIndented = true,
101+
};
102+
103+
return JsonSerializer.Serialize(evt, options);
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)