public Task Start(Action <NanoListenerTransaction> transactionCallback, Action <NanoResult> errorCallback)
        {
            return(Task.Run(
                       async() =>
            {
                while (true)
                {
                    try
                    {
                        await Task.Delay(this.Interval);
                        PendingRequest pendingRequest = new PendingRequest();
                        pendingRequest.Account = this.Account;
                        pendingRequest.Count = this.Count.ToString();
                        NanoResult <PendingResponse> pendingResult = await this.Nano.SendAsync <PendingResponse>(pendingRequest);
                        if (pendingResult.IsError())
                        {
                            errorCallback?.Invoke(pendingResult);
                        }

                        BlocksInfoRequest blocksInfoRequest = new BlocksInfoRequest();
                        blocksInfoRequest.Hashes = pendingResult.Response.Blocks;
                        NanoResult <BlocksInfoResponse> blocksInfoResult = await this.Nano.SendAsync <BlocksInfoResponse>(blocksInfoRequest);
                        if (blocksInfoResult.IsError())
                        {
                            errorCallback?.Invoke(blocksInfoResult);
                        }

                        if (blocksInfoResult.Response.Blocks != null)
                        {
                            foreach ((string key, BlockInfo block) in blocksInfoResult.Response.Blocks)
                            {
                                lock (this.knownHashes)
                                {
                                    if (this.knownHashes.Contains(key))
                                    {
                                        continue;
                                    }

                                    if (block.Confirmed == "true")
                                    {
                                        this.knownHashes.Add(key);
                                        transactionCallback?.Invoke(new NanoListenerTransaction(key, block));
                                    }
                                }
                            }
                        }
                    }
                    catch
                    {
                        // Do Nothing (For Now)
                    }
                }
            }));
        }
Esempio n. 2
0
        public async Task <NanoResult <T> > SendAsync <T>(NanoRequest request) where T : NanoResponse, new()
        {
            NanoResult <T> result = new NanoResult <T>();

            try
            {
                HttpRequestMessage requestMessage = new HttpRequestMessage();
                requestMessage.RequestUri = this.Nodes[this.random.Next(0, this.Nodes.Count)];
                requestMessage.Method     = HttpMethod.Post;
                string requestJson = JsonSerializer.Serialize(request, request.GetType());
                requestMessage.Content = new StringContent(requestJson, Encoding.UTF8, "application/json");
                HttpResponseMessage responseMessage = await this.Http.SendAsync(requestMessage);

                result.StatusCode          = responseMessage.StatusCode;
                result.IsSuccessStatusCode = responseMessage.IsSuccessStatusCode;
                if (responseMessage.IsSuccessStatusCode)
                {
                    result.Json = await responseMessage.Content.ReadAsStringAsync();

                    try
                    {
                        result.Response = JsonSerializer.Deserialize <T>(result.Json);
                    }
                    catch
                    {
                        // Do Nothing
                    }
                }
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }

            return(result);
        }