Пример #1
0
        private static async Task AccountSubscriptionAsync(string[] args)
        {
            using var client = new SubstrateClient(new Uri(Websocketurl));
            client.RegisterTypeConverter(new GenericTypeConverter <MogwaiStruct>());
            await client.ConnectAsync(CancellationToken.None);

            Action <string, StorageChangeSet> callBackAccountChange = (subscriptionId, eventObject) =>
            {
                Console.WriteLine($"Subscription[{subscriptionId}]: {eventObject}");
                if (eventObject.Changes != null)
                {
                    try
                    {
                        var accountInfo = new AccountInfo();
                        accountInfo.Create(eventObject.Changes[0][1]);

                        Console.WriteLine(accountInfo);
                    }
                    catch (NotImplementedException e)
                    {
                        Console.WriteLine($"##### {e}");
                    }
                }
            };

            var subscriptionId = await client.SubscribeStorageKeyAsync("System", "Account",
                                                                       new[]
            {
                Utils.Bytes2HexString(Utils.GetPublicKeyFrom("5DotMog6fcsVhMPqniyopz5sEJ5SMhHpz7ymgubr56gDxXwH"))
            },
                                                                       callBackAccountChange, CancellationToken.None);

            Thread.Sleep(60000);

            var reqResultUnsubscribe =
                await client.State.UnsubscribeStorageAsync(subscriptionId, CancellationToken.None);
        }
Пример #2
0
        private static async Task AllMogwaisCountSubscriptionAsync(string[] args)
        {
            using var client = new SubstrateClient(new Uri(Websocketurl));
            client.RegisterTypeConverter(new GenericTypeConverter <MogwaiStruct>());
            await client.ConnectAsync(CancellationToken.None);

            var subscriptionId = await client.SubscribeStorageKeyAsync("DotMogModule", "AllMogwaisCount", null,
                                                                       (id, storageChangeSet) =>
            {
                foreach (var change in storageChangeSet.Changes)
                {
                    var p   = 0;
                    var u64 = new U64();
                    u64.Decode(Utils.HexToByteArray(change[1]), ref p);
                    var result = u64.Value;
                    Console.WriteLine($"AllMogwaisCount = {result}");
                }
            }, CancellationToken.None);

            Thread.Sleep(60000);

            var reqResultUnsubscribe =
                await client.State.UnsubscribeStorageAsync(subscriptionId, CancellationToken.None);
        }