-
Notifications
You must be signed in to change notification settings - Fork 15
/
Subscribe.cs
68 lines (56 loc) · 2.12 KB
/
Subscribe.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
using System;
using System.Text.Json;
using System.Threading.Tasks;
using Tatum.Core.Models;
using Tatum.Notifications.Models.Notifications;
using Tatum.Notifications.Models.Notifications.SupportedChains;
using Xunit;
namespace Tatum.Examples.Notifications.Examples;
[Collection("Notification")]
public class Subscribe : IDisposable, IAsyncDisposable
{
[Fact]
public async Task Subscribe_Example()
{
var tatumSdk = await TatumSdk.InitAsync(Network.Testnet, config => config.EnableDebugMode = true);
AddressBasedNotification<AddressEventChain> notification = new AddressBasedNotification<AddressEventChain>()
{
Chain = AddressEventChain.Ethereum,
Address = "0x2be3e0a7fc9c0d0592ea49b05dde7f28baf8e380",
Url = "https://webhook.site/0x2be3e0a7fc9c0d0592ea49b05dde7f28baf8e380"
};
var result = await tatumSdk.Notifications.Subscribe.AddressEvent(notification);
if (result.Success)
{
Console.WriteLine("Success - notification subscribed.");
Console.WriteLine(JsonSerializer.Serialize(result.Value, new JsonSerializerOptions() { WriteIndented = true }));
}
else
{
Console.WriteLine("Failure - there was a problem creating the subscription.");
Console.WriteLine(result.ErrorMessage);
}
Assert.True(result.Success);
}
public async ValueTask DisposeAsync()
{
var tatumSdk = await TatumSdk.InitAsync(Network.Testnet, config => config.EnableDebugMode = true);
var notifications = await tatumSdk.Notifications.GetAll();
if (!notifications.Success)
{
throw new Exception("Failed to get notifications.");
}
foreach (var notification in notifications.Value)
{
var unsubscribe = await tatumSdk.Notifications.Unsubscribe(notification.Id);
if (!unsubscribe.Success)
{
throw new Exception("Failed to unsubscribe.");
}
}
}
public void Dispose()
{
DisposeAsync().GetAwaiter().GetResult();
}
}