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
        //TODO: Get rif of that method?
        public async Task <int> WalletSubscriptionsCount(string blockchainType, string address, MonitoringSubscriptionType subscriptionType)
        {
            var partitionKey = GetPartitionKey(blockchainType, subscriptionType);
            var rowKey       = GetRowKey(address);

            var count = (await _table.GetDataAsync(partitionKey, rowKey));

            return(count != null ? 1 : 0);
        }
예제 #4
0
        public async Task <bool> WalletIsSubscribedAsync(string blockchainType, string address, MonitoringSubscriptionType subscriptionType)
        {
            var partitionKey = GetPartitionKey(blockchainType, subscriptionType);
            var rowKey       = GetRowKey(address);

            return(await _table.GetDataAsync(partitionKey, rowKey) != null);
        }
예제 #5
0
        public async Task UnregisterWalletSubscriptionAsync(string blockchainType, string address, MonitoringSubscriptionType subscriptionType)
        {
            var partitionKey = GetPartitionKey(blockchainType, subscriptionType);
            var rowKey       = GetRowKey(address);

            await _table.DeleteIfExistAsync(partitionKey, rowKey);
        }
예제 #6
0
        public async Task RegisterWalletSubscriptionAsync(string blockchainType, string address, MonitoringSubscriptionType subscriptionType)
        {
            var entity = new MonitoringSubscriptionEntity
            {
                Address          = address,
                BlockchainType   = blockchainType,
                SubscriptionType = subscriptionType,

                PartitionKey = GetPartitionKey(blockchainType, subscriptionType),
                RowKey       = GetRowKey(address)
            };

            await _table.InsertOrReplaceAsync(entity);
        }
예제 #7
0
 private static string GetPartitionKey(string blockchainType, MonitoringSubscriptionType subscriptionType)
 {
     return($"{subscriptionType.ToString()}-{blockchainType}");
 }
예제 #8
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());
        }