Exemplo n.º 1
0
        public async Task <UpdatedTransactionInfo> CancelDepositAsync(Keccak depositId)
        {
            if (_logger.IsWarn)
            {
                _logger.Warn($"Canceling transaction for deposit with id: '{depositId}'.");
            }
            (UpdatedTransactionStatus status, DepositDetails? deposit) = await TryGetDepositAsync(depositId);

            if (status != UpdatedTransactionStatus.Ok)
            {
                return(new UpdatedTransactionInfo(status));
            }

            if (deposit !.Transaction == null)
            {
                throw new InvalidDataException($"Maneged to retrieve deposit with id: '{depositId}' which has no Transaction set.");
            }

            if (deposit !.Confirmed)
            {
                if (_logger.IsError)
                {
                    _logger.Error($"Deposit with id: '{depositId}' was confirmed, transaction hash: '{deposit!.Transaction.Hash}'.");
                }
                return(new UpdatedTransactionInfo(UpdatedTransactionStatus.ResourceConfirmed));
            }

            if (deposit !.Transaction.State != TransactionState.Pending)
            {
                if (_logger.IsError)
                {
                    _logger.Error($"Cannot cancel transaction with hash: '{deposit!.Transaction.Hash}' for deposit with id: '{depositId}' (state: '{deposit.Transaction.State}').");
                }
                return(new UpdatedTransactionInfo(UpdatedTransactionStatus.AlreadyIncluded));
            }

            if (deposit !.Transaction.Hash == null)
            {
                if (_logger.IsError)
                {
                    _logger.Error($"Cannot cancel transaction with hash: '{deposit!.Transaction.Hash}' for deposit with id: '{depositId}' (state: '{deposit.Transaction.State}').");
                }
                return(new UpdatedTransactionInfo(UpdatedTransactionStatus.MissingTransaction));
            }

            CanceledTransactionInfo transaction = await _transactionService.CancelAsync(deposit !.Transaction.Hash);

            if (_logger.IsWarn)
            {
                _logger.Warn($"Canceled transaction for deposit with id: '{depositId}', transaction hash: '{transaction.Hash}'.");
            }
            TransactionInfo cancellingTransaction = TransactionInfo.Cancellation(transaction.Hash, transaction.GasPrice,
                                                                                 transaction.GasLimit, _timestamper.UnixTime.Seconds);

            deposit !.AddTransaction(cancellingTransaction);
            await _depositRepository.UpdateAsync(deposit);

            return(new UpdatedTransactionInfo(UpdatedTransactionStatus.Ok, transaction.Hash));
        }
Exemplo n.º 2
0
        public async Task <UpdatedTransactionInfo> CancelPaymentClaimAsync(Keccak paymentClaimId)
        {
            (UpdatedTransactionStatus status, PaymentClaim? paymentClaim) = await TryGetPaymentClaimAsync(paymentClaimId, "cancel");

            if (status != UpdatedTransactionStatus.Ok)
            {
                return(new UpdatedTransactionInfo(status));
            }

            TransactionInfo?currentTransaction = paymentClaim !.Transaction;

            if (currentTransaction == null)
            {
                if (_logger.IsError)
                {
                    _logger.Error($"Cannot cancel missing transaction for payment claim with id: '{paymentClaim!.Id}'.");
                }
                return(new UpdatedTransactionInfo(UpdatedTransactionStatus.MissingTransaction));
            }

            Keccak?hash = currentTransaction.Hash;

            if (hash == null)
            {
                if (_logger.IsError)
                {
                    _logger.Error($"Cannot cancel missing transaction for payment claim with id: '{paymentClaim!.Id}'.");
                }
                return(new UpdatedTransactionInfo(UpdatedTransactionStatus.MissingTransaction));
            }

            if (currentTransaction.State != TransactionState.Pending)
            {
                if (_logger.IsError)
                {
                    _logger.Error($"Cannot cancel transaction with hash: '{hash}' for payment claim with id: '{paymentClaim!.Id}' (state: '{currentTransaction.State}').");
                }
                return(new UpdatedTransactionInfo(UpdatedTransactionStatus.AlreadyIncluded));
            }

            if (_logger.IsWarn)
            {
                _logger.Warn($"Canceling transaction for payment claim with id: '{paymentClaimId}'.");
            }
            CanceledTransactionInfo transaction = await _transactionService.CancelAsync(hash);

            if (_logger.IsWarn)
            {
                _logger.Warn($"Canceled transaction for payment claim with id: '{paymentClaimId}', transaction hash: '{transaction.Hash}'.");
            }
            TransactionInfo cancellingTransaction = TransactionInfo.Cancellation(transaction.Hash, transaction.GasPrice,
                                                                                 transaction.GasLimit, _timestamper.UnixTime.Seconds);

            paymentClaim !.AddTransaction(cancellingTransaction);
            await _paymentClaimRepository.UpdateAsync(paymentClaim !);

            return(new UpdatedTransactionInfo(UpdatedTransactionStatus.Ok, transaction.Hash));
        }
        public async Task cancel_deposit_should_return_transaction_status()
        {
            var transactionHash = TestItem.KeccakA;
            var deposit         = GetDepositDetails(transactionHash);
            var info            = new CanceledTransactionInfo(TestItem.KeccakB, 10.GWei(), 1000);

            _depositRepository.GetAsync(deposit.Id).Returns(deposit);
            _transactionService.CancelAsync(transactionHash).Returns(info);
            var transactionInfo = await _consumerTransactionsService.CancelDepositAsync(deposit.Id);

            transactionInfo.Hash.Should().Be(info.Hash);
            transactionInfo.Status.Should().Be(UpdatedTransactionStatus.Ok);
            deposit.Transaction.Hash.Should().Be(transactionInfo.Hash);
            deposit.Transaction.Type.Should().Be(TransactionType.Cancellation);
            await _depositRepository.Received().GetAsync(deposit.Id);

            await _depositRepository.Received().UpdateAsync(deposit);

            await _transactionService.Received().CancelAsync(transactionHash);
        }
Exemplo n.º 4
0
        public async Task <UpdatedTransactionInfo> CancelRefundAsync(Keccak depositId)
        {
            if (_logger.IsWarn)
            {
                _logger.Warn($"Canceling transaction for refund for deposit with id: '{depositId}'.");
            }
            (UpdatedTransactionStatus status, DepositDetails? deposit) = await TryGetDepositAsync(depositId);

            if (status != UpdatedTransactionStatus.Ok)
            {
                return(new UpdatedTransactionInfo(status));
            }

            if (deposit !.ClaimedRefundTransaction is null)
            {
                if (_logger.IsError)
                {
                    _logger.Error($"Deposit with id: '{depositId}' has no transaction for refund claim.");
                }
                return(new UpdatedTransactionInfo(UpdatedTransactionStatus.MissingTransaction));
            }

            Keccak?currentHash = deposit !.ClaimedRefundTransaction.Hash;

            if (deposit !.RefundClaimed)
            {
                if (_logger.IsError)
                {
                    _logger.Error($"Deposit with id: '{depositId}' has already claimed refund (transaction hash: '{currentHash}').");
                }
                return(new UpdatedTransactionInfo(UpdatedTransactionStatus.ResourceConfirmed));
            }

            if (currentHash is null)
            {
                if (_logger.IsError)
                {
                    _logger.Error($"Cannot cancel transaction with hash: '{null}' for refund for deposit with id: '{depositId}' (state: '{deposit.ClaimedRefundTransaction.State}').");
                }
                return(new UpdatedTransactionInfo(UpdatedTransactionStatus.MissingTransaction));
            }

            if (deposit !.ClaimedRefundTransaction.State != TransactionState.Pending)
            {
                if (_logger.IsError)
                {
                    _logger.Error($"Cannot cancel transaction with hash: '{deposit!.ClaimedRefundTransaction.Hash}' for refund for deposit with id: '{depositId}' (state: '{deposit.ClaimedRefundTransaction.State}').");
                }
                return(new UpdatedTransactionInfo(UpdatedTransactionStatus.AlreadyIncluded));
            }

            CanceledTransactionInfo transaction = await _transactionService.CancelAsync(currentHash);

            if (_logger.IsWarn)
            {
                _logger.Warn($"Canceled transaction for deposit with id: '{depositId}', transaction hash: '{transaction.Hash}'.");
            }
            TransactionInfo cancellingTransaction = TransactionInfo.Cancellation(transaction.Hash, transaction.GasPrice,
                                                                                 transaction.GasLimit, _timestamper.UnixTime.Seconds);

            deposit.AddClaimedRefundTransaction(cancellingTransaction);
            await _depositRepository.UpdateAsync(deposit);

            return(new UpdatedTransactionInfo(UpdatedTransactionStatus.Ok, transaction.Hash));
        }