Beispiel #1
0
        private async Task UpdateBalances_Continuously(
            CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                var walletExternalServices = await
                                             _externalServiceManager.GetExternalServicesData(new ExternalServicesDataQuery()
                {
                    Type = new string[]
                    {
                        NBXplorerWalletService.NBXplorerWalletServiceType
                    }
                });

                var groupedWalletEService = walletExternalServices
                                            .Select(data => new NBXplorerWalletService(data, _nbXplorerPublicWalletProvider,
                                                                                       _derivationSchemeParser, _derivationStrategyFactoryProvider, _nbXplorerClientProvider))
                                            .Select(data => (data, data.GetData()))
                                            .GroupBy(
                    tuple => $"{tuple.Item2.CryptoCode}_{tuple.Item2.Address}_{tuple.Item2.DerivationStrategy}")
                                            .ToList();


                foreach (var valueTuples in groupedWalletEService)
                {
                    var first = valueTuples.First();

                    var wallet = await first.Item1.ConstructClient();

                    var explorerClient = _nbXplorerClientProvider.GetClient(first.Item2.CryptoCode);


                    var balance = await wallet.GetBalance();

                    await _triggerDispatcher.DispatchTrigger(new NBXplorerBalanceTrigger(explorerClient)
                    {
                        Data = new NBXplorerBalanceTriggerData()
                        {
                            Balance       = balance,
                            CryptoCode    = explorerClient.CryptoCode,
                            TrackedSource = wallet.TrackedSource
                        }
                    });
                }

                await Task.Delay(TimeSpan.FromMinutes(1),
                                 cancellationToken);
            }
        }
 private async Task Loop(CancellationToken cancellationToken)
 {
     while (!cancellationToken.IsCancellationRequested)
     {
         _ = _triggerDispatcher.DispatchTrigger(new TimerTrigger());
         await Task.Delay(TimeSpan.FromMinutes(1), cancellationToken);
     }
 }
        private async Task Selector(KeyValuePair <string, ImapService> service)
        {
            var client = await service.Value.CreateClientAndConnect();

            if (client == null)
            {
                return;
            }

            var data = service.Value.GetData();

            var inbox = client.Inbox;
            await inbox.OpenAsync(FolderAccess.ReadOnly);

            var emailIds =
                await inbox.SearchAsync(new DateSearchQuery(SearchTerm.SentAfter,
                                                            data.LastCheck.GetValueOrDefault(data.PairedDate)));


            foreach (var emailId in emailIds)
            {
                var email = await inbox.GetMessageAsync(emailId);

                var trigger = new ReceivedEmailTrigger()
                {
                    Data = new ReceivedEmailTriggerData()
                    {
                        Body              = email.GetTextBody(TextFormat.Plain),
                        Subject           = email.Subject,
                        FromEmail         = email.From.ToString(),
                        ExternalServiceId = service.Key
                    }
                };
                _ = _triggerDispatcher.DispatchTrigger(trigger);
            }

            data.LastCheck = DateTime.Now;
            service.Value.SetData(data);
            await _externalServiceManager.UpdateInternalData(service.Key, data);

            await client.DisconnectAsync(true);

            client.Dispose();
        }
Beispiel #4
0
        private async Task Selector(KeyValuePair <string, Pop3Service> service)
        {
            var pop3Client = await service.Value.CreateClientAndConnect();

            if (pop3Client == null)
            {
                return;
            }

            var data = service.Value.GetData();



            var validEmails = pop3Client.Select((message, i) => (message, i)).Where(tuple =>
                                                                                    !data.LastCheck.HasValue ||
                                                                                    tuple.Item1.Date >= data.LastCheck.Value).Select(tuple => tuple.Item2).ToList();



            var emails = await pop3Client.GetMessagesAsync(validEmails);

            foreach (var email in emails)
            {
                var trigger = new ReceivedEmailTrigger()
                {
                    Data = new ReceivedEmailTriggerData()
                    {
                        Body              = email.GetTextBody(TextFormat.Plain),
                        Subject           = email.Subject,
                        FromEmail         = email.From.ToString(),
                        ExternalServiceId = service.Key
                    }
                };
                _ = _triggerDispatcher.DispatchTrigger(trigger);
            }

            data.LastCheck = DateTime.Now;
            service.Value.SetData(data);
            await _externalServiceManager.UpdateInternalData(service.Key, data);

            await pop3Client.DisconnectAsync(true);

            pop3Client.Dispose();
        }
        private async Task ListenForPayment(CancellationToken cancellationToken, KeyValuePair <string, LightningNodeService> lightningNodeService)
        {
            var linkedCancellation = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);

            _cancellationTokens.AddOrReplace(lightningNodeService.Key, linkedCancellation);
            _ = (await lightningNodeService.Value.ConstructClient().Listen(linkedCancellation.Token))
                .WaitInvoice(linkedCancellation.Token).ContinueWith(
                task =>
            {
                _ = _triggerDispatcher.DispatchTrigger(new ReceivedLightningPaymentTrigger()
                {
                    Data = new ReceivedLightningPaymentTriggerData()
                    {
                        LightningInvoice  = task.Result,
                        ExternalServiceId = lightningNodeService.Key
                    }
                });
                _ = ListenForPayment(cancellationToken, lightningNodeService);
            }, TaskScheduler.Default);
        }
Beispiel #6
0
        private async Task UpdateBalances_Continuously(
            CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                var exchangeExternalServices = await
                                               _externalServiceManager.GetExternalServicesData(new ExternalServicesDataQuery()
                {
                    Type = new string[]
                    {
                        ExchangeService.ExchangeServiceType
                    }
                });


                await Task.WhenAll(exchangeExternalServices.Select(async data =>
                {
                    var exchangeService = new ExchangeService(data);
                    var client          = exchangeService.ConstructClient();
                    var amounts         = await client.GetAmountsAsync();
                    foreach (var keyValuePair in amounts)
                    {
                        _ = _triggerDispatcher.DispatchTrigger(new CheckExchangeBalanceTrigger()
                        {
                            Data = new CheckExchangeBalanceTriggerData()
                            {
                                Balance           = keyValuePair.Value,
                                Asset             = keyValuePair.Key,
                                ExternalServiceId = data.Id
                            }
                        });
                    }
                }));

                await Task.Delay(TimeSpan.FromMinutes(1),
                                 cancellationToken);
            }
        }
        private async Task CheckInvoiceChangeInService(KeyValuePair <string, BtcPayServerService> pair)
        {
            try
            {
                var key     = pair.Key;
                var service = pair.Value;
                if (!await service.CheckAccess())
                {
                    return;
                }

                var data = service.GetData();
                data.LastCheck = DateTime.Now;
                if (data.MonitoredInvoiceStatuses == null)
                {
                    data.MonitoredInvoiceStatuses = new Dictionary <string, string>();
                }

                var client = service.ConstructClient();

                var invoices = await client.GetInvoicesAsync <BtcPayInvoice>(data.PairedDate);

                foreach (var invoice in invoices)
                {
                    //do not trigger on first run
                    if (data.LastCheck.HasValue)
                    {
                        if (data.MonitoredInvoiceStatuses.ContainsKey(invoice.Id))
                        {
                            if (data.MonitoredInvoiceStatuses[invoice.Id] != invoice.Status)
                            {
                                await _triggerDispatcher.DispatchTrigger(new InvoiceStatusChangedTrigger()
                                {
                                    Data = new InvoiceStatusChangedTriggerData()
                                    {
                                        Invoice           = invoice,
                                        ExternalServiceId = key
                                    }
                                });
                            }
                        }
                        else
                        {
                            await _triggerDispatcher.DispatchTrigger(new InvoiceStatusChangedTrigger()
                            {
                                Data = new InvoiceStatusChangedTriggerData()
                                {
                                    Invoice           = invoice,
                                    ExternalServiceId = key
                                }
                            });
                        }
                    }

                    data.MonitoredInvoiceStatuses.AddOrReplace(invoice.Id, invoice.Status);
                }


                service.SetData(data);
                await _externalServiceManager.UpdateInternalData(key, data);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }