-
Notifications
You must be signed in to change notification settings - Fork 2
/
ExampleLogsERC20Subscriptions.cs
91 lines (77 loc) · 3.39 KB
/
ExampleLogsERC20Subscriptions.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
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
89
90
91
using Nethereum.Contracts;
using Nethereum.Contracts.Standards.ERC20.ContractDefinition;
using Nethereum.JsonRpc.WebSocketStreamingClient;
using Nethereum.RPC.Reactive.Eth.Subscriptions;
using System;
using System.Reactive.Linq;
using System.Threading.Tasks;
namespace Nethereum.Templates.WebSocketStreaming
{
internal class ExampleLogsERC20Subscriptions
{
private readonly string url;
StreamingWebSocketClient client;
public ExampleLogsERC20Subscriptions(string url)
{
this.url = url;
}
public async Task SubscribeAndRunAsync()
{
if (client == null)
{
client = new StreamingWebSocketClient(url);
client.Error += Client_Error;
}
//Subscribing to blockHeaders to keep it alive
var blockHeaderSubscription = new EthNewBlockHeadersObservableSubscription(client);
blockHeaderSubscription.GetSubscribeResponseAsObservable().Subscribe(subscriptionId =>
Console.WriteLine("Block Header subscription Id: " + subscriptionId));
blockHeaderSubscription.GetSubscriptionDataResponsesAsObservable().Subscribe(block =>
Console.WriteLine("New Block: " + block.BlockHash)
, exception =>
{
Console.WriteLine("BlockHeaderSubscription error info:" + exception.Message);
});
blockHeaderSubscription.GetUnsubscribeResponseAsObservable().Subscribe(response =>
Console.WriteLine("Block Header unsubscribe result: " + response));
//DAI contract address
var filterTransfers = Event<TransferEventDTO>.GetEventABI().CreateFilterInput("0x6B175474E89094C44Da98b954EedeAC495271d0F");
var ethLogsTokenTransfer = new EthLogsObservableSubscription(client);
ethLogsTokenTransfer.GetSubscriptionDataResponsesAsObservable().Subscribe(log =>
{
try
{
var decoded = Event<TransferEventDTO>.DecodeEvent(log);
if (decoded != null)
{
Console.WriteLine("Contract address: " + log.Address + " Log Transfer from:" + decoded.Event.From);
}
else
{
Console.WriteLine("Found not standard transfer log");
}
}
catch (Exception ex)
{
Console.WriteLine("Log Address: " + log.Address + " is not a standard transfer log:", ex.Message);
}
}, exception =>
{
Console.WriteLine("Logs error info:" + exception.Message);
});
await client.StartAsync();
await blockHeaderSubscription.SubscribeAsync();
//don't pass any parameter if you want all
await ethLogsTokenTransfer.SubscribeAsync(filterTransfers);
Console.ReadLine();
}
private async void Client_Error(object sender, Exception ex)
{
Console.WriteLine("Client Error restarting...");
// ((StreamingWebSocketClient)sender).Error -= Client_Error;
((StreamingWebSocketClient)sender).StopAsync().Wait();
//Restart everything
await SubscribeAndRunAsync();
}
}
}