public async Task <CommandHandlingResult> Handle(BeginBalanceMonitoringCommand command, IEventPublisher publisher)
        {
            var apiClient = _blockchainIntegrationService.TryGetApiClient(command.BlockchainType);

            if (apiClient == null)
            {
                throw new NotSupportedException($"Blockchain type [{command.BlockchainType}] is not supported.");
            }

            const MonitoringSubscriptionType subscriptionType = MonitoringSubscriptionType.Balance;

            var address        = command.Address;
            var blockchainType = command.BlockchainType;

            if (await _monitoringSubscriptionRepository.WalletSubscriptionsCount(blockchainType, address, subscriptionType) == 0)
            {
                try
                {
                    await apiClient.StartBalanceObservationAsync(address);
                }
                catch (ErrorResponseException e) when(e.StatusCode == HttpStatusCode.Conflict)
                {
                }
            }

            await _monitoringSubscriptionRepository.RegisterWalletSubscriptionAsync
            (
                blockchainType : blockchainType,
                address : address,
                subscriptionType : subscriptionType
            );

            return(CommandHandlingResult.Ok());
        }
Пример #2
0
        public async Task <CommandHandlingResult> Handle(EndTransactionHistoryMonitoringCommand command, IEventPublisher publisher)
        {
            var apiClient = _blockchainIntegrationService.TryGetApiClient(command.BlockchainType);

            if (apiClient == null)
            {
                throw new NotSupportedException($"Blockchain type [{command.BlockchainType}] is not supportedю");
            }


            const MonitoringSubscriptionType subscriptionType = MonitoringSubscriptionType.TransactionHistory;

            var address        = command.Address;
            var blockchainType = command.BlockchainType;


            await _monitoringSubscriptionRepository.UnregisterWalletSubscriptionAsync
            (
                blockchainType : blockchainType,
                address : address,
                subscriptionType : subscriptionType
            );

            // TODO: Fix potential issue with subscription/unsubscription race conditions
            // If we have no subscriptions for address-asset pairs for specified address...
            if (await _monitoringSubscriptionRepository.WalletSubscriptionsCount(blockchainType, address, subscriptionType) == 0)
            {
                try
                {
                    // Unsubscribe from address transactions observation (for all assets)
                    await apiClient.StopHistoryObservationOfIncomingTransactionsAsync(address);
                }
                catch (ErrorResponseException e) when(e.StatusCode == HttpStatusCode.NoContent)
                {
                }
            }

            return(CommandHandlingResult.Ok());
        }
Пример #3
0
        public async Task <CommandHandlingResult> Handle(BeginTransactionHistoryMonitoringCommand command, IEventPublisher publisher)
        {
            var apiClient = _blockchainIntegrationService.TryGetApiClient(command.BlockchainType);

            if (apiClient == null)
            {
                throw new NotSupportedException($"Blockchain type [{command.BlockchainType}] is not supported");
            }


            const MonitoringSubscriptionType subscriptionType = MonitoringSubscriptionType.TransactionHistory;

            var address        = command.Address;
            var blockchainType = command.BlockchainType;

            if (await _monitoringSubscriptionRepository.WalletSubscriptionsCount(blockchainType, address, subscriptionType) == 0)
            {
                try
                {
                    await apiClient.StartHistoryObservationOfIncomingTransactionsAsync(address);
                }
                catch (ErrorResponseException e) when(e.StatusCode == HttpStatusCode.Conflict)
                {
                }
                catch (ErrorResponseException e)
                {
                    string warningMessage;

                    // ReSharper disable once SwitchStatementMissingSomeCases
                    switch (e.StatusCode)
                    {
                    case HttpStatusCode.NotImplemented:
                        warningMessage =
                            $"Blockchain type [{blockchainType}] does not support transactions history.";
                        break;

                    case HttpStatusCode.NotFound:
                        warningMessage =
                            $"Blockchain type [{blockchainType}] either does not support transactions history, or not respond.";
                        break;

                    default:
                        throw;
                    }

                    _log.Warning(warningMessage, context: command);

                    return(CommandHandlingResult.Ok());
                }
            }

            // Register subscription of specified asset for specified wallet
            await _monitoringSubscriptionRepository.RegisterWalletSubscriptionAsync
            (
                blockchainType,
                address,
                subscriptionType
            );

            return(CommandHandlingResult.Ok());
        }