public async Task CompleteMonitoringTaskAsync(
     TransactionMonitoringTask task)
 {
     try
     {
         await _transactionMonitoringTaskRepository.CompleteAsync(task);
     }
     catch (Exception e)
     {
         _log.Warning("Failed to complete transaction monitoring task.", e);
     }
 }
        public async Task <bool> CheckAndUpdateStateAsync(
            TransactionMonitoringTask task)
        {
            try
            {
                var transaction = await _transactionRepository.TryGetAsync(task.TransactionId);

                if (transaction != null)
                {
                    var transactionChanged = false;

                    // ReSharper disable once ConvertIfStatementToSwitchStatement
                    if (transaction.State == TransactionState.InProgress)
                    {
                        transactionChanged = await CheckTransactionCompletionStateAsync(transaction);
                    }

                    if (transaction.State == TransactionState.Completed || transaction.State == TransactionState.Failed)
                    {
                        transactionChanged = await CheckTransactionConfirmationStateAsync(transaction);
                    }

                    if (transaction.State == TransactionState.Failed && transaction.IsConfirmed)
                    {
                        var isContract = await _blockchainService.IsContractAsync(transaction.To);

                        if (isContract)
                        {
                            await _addressService.AddAddressToBlacklistAsync
                            (
                                address : transaction.To,
                                reason : $"Transfer to contract failed in transaction [{transaction.TransactionId}]."
                            );
                        }
                    }

                    if (transactionChanged)
                    {
                        await _transactionRepository.UpdateAsync(transaction);
                    }

                    // ReSharper disable once SwitchStatementMissingSomeCases
                    switch (transaction.State)
                    {
                    case TransactionState.Built:
                        return(true);

                    case TransactionState.InProgress:
                        return(false);

                    case TransactionState.Completed:
                    case TransactionState.Failed:
                        return(transaction.IsConfirmed);

                    case TransactionState.Deleted:
                        return(true);

                    default:
                        throw new ArgumentException($"Transaction is in unknown state [{transaction.State}].");
                    }
                }
                else
                {
                    _log.Warning($"Transaction [{task.TransactionId}] does not exist.");

                    return(true);
                }
            }
            catch (Exception e)
            {
                _log.Warning($"Failed to check and update transaction [{task.TransactionId}] state.", e);

                return(false);
            }
        }