Пример #1
0
        private async Task <(List <Deposit>, List <WithdrawInfo>)> LoadOps(string blockNumberDeposits)
        {
            await client.WaitServerStartedAsync();

            session = await client.CreateWebsocketNotificationSessionAsync();

            session.ListenTrackedSources(new[] { oracleAddress });
            ProcessNotifications();

            var oldTx = (await client.GetTransactionsAsync(oracleAddress)).ConfirmedTransactions.Transactions;

            int heigth = 0;

            if (!string.IsNullOrEmpty(blockNumberDeposits))
            {
                heigth = int.Parse(blockNumberDeposits);
            }

            if (heigth > 0)
            {
                oldTx = oldTx.Where(t => t.Height > heigth).ToList();
                return(ProcessOperations(oldTx));
            }
            else
            {
                return(ProcessOperations(oldTx));
            }
        }
Пример #2
0
        private async Task MonitorClientForTriggers(ExplorerClient explorerClient, CancellationToken cancellationToken)
        {
            await explorerClient.WaitServerStartedAsync(cancellationToken);

            WebsocketNotificationSession notificationSession = null;

            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    var summary = _nbXplorerSummaryProvider.GetSummary(explorerClient.CryptoCode);
                    if (summary?.State != NBXplorerState.Ready)
                    {
                        await Task.Delay(TimeSpan.FromSeconds(10), cancellationToken);

                        continue;
                    }

                    notificationSession =
                        await explorerClient.CreateWebsocketNotificationSessionAsync(cancellationToken);

                    await notificationSession.ListenNewBlockAsync(cancellationToken);

                    await notificationSession.ListenAllTrackedSourceAsync(false, cancellationToken);

                    while (!cancellationToken.IsCancellationRequested)
                    {
                        var evt = await notificationSession.NextEventAsync(cancellationToken);

                        var factory =
                            _derivationStrategyFactoryProvider.GetDerivationStrategyFactory(
                                evt.CryptoCode);

                        switch (evt)
                        {
                        case NewBlockEvent newBlockEvent:
                            await _triggerDispatcher.DispatchTrigger(new NBXplorerNewBlockTrigger()
                            {
                                Data = new NBXplorerNewBlockTriggerData()
                                {
                                    CryptoCode = evt.CryptoCode,
                                    Event      = newBlockEvent
                                }
                            });

                            //we need to trigger transaction events for previous unconfirmed txs  so that they are checked again and trigger respective actions
                            var recipes = await _recipeManager.GetRecipes(new RecipesQuery()
                            {
                                Enabled   = true,
                                TriggerId = NBXplorerNewTransactionTrigger.Id
                            });

                            foreach (var recipe in recipes)
                            {
                                var triggerParameters =
                                    recipe.RecipeTrigger.Get <NBXplorerNewTransactionTriggerParameters>();
                                if (triggerParameters.Transactions == null || !triggerParameters.Transactions.Any())
                                {
                                    continue;
                                }

                                var tasks = triggerParameters.Transactions.Select(result =>
                                                                                  (result,
                                                                                   explorerClient.GetTransactionAsync(result.TransactionHash,
                                                                                                                      cancellationToken)));
                                await Task.WhenAll(tasks.Select(tuple => tuple.Item2));


                                foreach (var tx in tasks)
                                {
                                    if (tx.Item1.Confirmations != tx.Item2.Result.Confirmations)
                                    {
                                        var walletService = new NBXplorerWalletService(
                                            recipe.RecipeTrigger.ExternalService, _nbXplorerPublicWalletProvider,
                                            _derivationSchemeParser, _derivationStrategyFactoryProvider,
                                            _nbXplorerClientProvider);



                                        await _triggerDispatcher.DispatchTrigger(
                                            new NBXplorerNewTransactionTrigger(explorerClient)
                                        {
                                            Data = new NBXplorerNewTransactionTriggerData()
                                            {
                                                CryptoCode = evt.CryptoCode,
                                                Event      = new NewTransactionEvent()
                                                {
                                                    CryptoCode      = evt.CryptoCode,
                                                    BlockId         = newBlockEvent.Hash,
                                                    TransactionData = tx.Item2.Result,
                                                    TrackedSource   = await walletService.ConstructTrackedSource()
                                                }
                                            }
                                        });
                                    }
                                }
                            }

                            break;

                        case NewTransactionEvent newTransactionEvent:
                        {
                            await _triggerDispatcher.DispatchTrigger(
                                new NBXplorerNewTransactionTrigger(explorerClient)
                                {
                                    Data = new NBXplorerNewTransactionTriggerData()
                                    {
                                        CryptoCode = evt.CryptoCode,
                                        Event      = newTransactionEvent
                                    }
                                });

                            break;
                        }

                        case UnknownEvent unknownEvent:
                            _logger.LogWarning(
                                $"Received unknown message from NBXplorer ({unknownEvent.CryptoCode}), ID: {unknownEvent.EventId}");
                            break;
                        }
                    }
                }
                catch when(cancellationToken.IsCancellationRequested)
                {
                }
Пример #3
0
        private async Task <(List <Deposit>, List <WithdrawInfo>)> LoadOps(string blockNumberDeposits)
        {
            var listenerStarted = false;

            while (!listenerStarted)
            {
                try
                {
                    await client.WaitServerStartedAsync();

                    session = await client.CreateWebsocketNotificationSessionAsync();

                    session.ListenTrackedSources(new[] { oracleAddress });
                    ProcessNotifications();
                    listenerStarted = true;
                }
                catch (Exception e)
                {
                    Log.Error("Failed to stablish bitcoin websocket connector ");
                    Log.Error(e.ToString());
                    await Task.Delay(3000);
                }
            }


            var opLoaded = false;

            while (!opLoaded)
            {
                try
                {
                    var oldTx = (await client.GetTransactionsAsync(oracleAddress)).ConfirmedTransactions.Transactions;

                    opLoaded = true;
                    int heigth = 0;
                    if (!string.IsNullOrEmpty(blockNumberDeposits))
                    {
                        heigth = int.Parse(blockNumberDeposits);
                    }

                    if (heigth > 0)
                    {
                        oldTx = oldTx.Where(t => t.Height > heigth).ToList();
                        return(ProcessOperations(oldTx));
                    }
                    else
                    {
                        return(ProcessOperations(oldTx));
                    }
                }
                catch (Exception e)
                {
                    Log.Error("Failed to load transactions");
                    Log.Error(e.ToString());

                    if (opLoaded)
                    {
                        throw;
                    }

                    await Task.Delay(3000);
                }
            }

            return(new List <Deposit>(), new List <WithdrawInfo>());
        }