Ejemplo n.º 1
0
        public async Task <RefundClaimStatus> TryClaimEarlyRefundAsync(DepositDetails deposit, Address refundTo)
        {
            var now = _timestamper.EpochSeconds;

            if (!deposit.CanClaimEarlyRefund(now))
            {
                return(RefundClaimStatus.Empty);
            }

            var latestBlock = await _blockchainBridge.GetLatestBlockAsync();

            now = (ulong)latestBlock.Timestamp;
            if (!deposit.CanClaimEarlyRefund(now))
            {
                return(RefundClaimStatus.Empty);
            }

            var depositId       = deposit.Deposit.Id;
            var transactionHash = deposit.ClaimedRefundTransaction?.Hash;

            if (transactionHash is null)
            {
                var provider         = deposit.DataAsset.Provider.Address;
                var ticket           = deposit.EarlyRefundTicket;
                var earlyRefundClaim = new EarlyRefundClaim(ticket.DepositId, deposit.DataAsset.Id,
                                                            deposit.Deposit.Units, deposit.Deposit.Value, deposit.Deposit.ExpiryTime, deposit.Pepper, provider,
                                                            ticket.ClaimableAfter, ticket.Signature, refundTo);
                var gasPrice = await _gasPriceService.GetCurrentAsync();

                transactionHash = await _refundService.ClaimEarlyRefundAsync(refundTo, earlyRefundClaim, gasPrice);

                if (transactionHash is null)
                {
                    if (_logger.IsError)
                    {
                        _logger.Error("There was an error when trying to claim early refund (no transaction hash returned).");
                    }
                    return(RefundClaimStatus.Empty);
                }

                deposit.SetClaimedRefundTransaction(new TransactionInfo(transactionHash, 0, gasPrice,
                                                                        _refundService.GasLimit, _timestamper.EpochSeconds));
                await _depositRepository.UpdateAsync(deposit);

                if (_logger.IsInfo)
                {
                    _logger.Info($"Claimed an early refund for deposit: '{depositId}', gas price: {gasPrice} wei, transaction hash: '{transactionHash}' (awaits a confirmation).");
                }
            }

            var confirmed = await TryConfirmClaimAsync(deposit, "early ");

            return(confirmed
                ? RefundClaimStatus.Confirmed(transactionHash)
                : RefundClaimStatus.Unconfirmed(transactionHash));
        }
Ejemplo n.º 2
0
        public void can_claim_early_refund_returns_correctly_true()
        {
            var earlyRefundTicket = new EarlyRefundTicket(Keccak.Zero,
                                                          claimableAfter: 10,
                                                          new Signature(1, 2, 37));

            _depositDetails.SetEarlyRefundTicket(earlyRefundTicket);
            _depositDetails.SetConfirmationTimestamp(10);

            bool canClaimEarlyRefund = _depositDetails.CanClaimEarlyRefund(currentBlockTimestamp: 20, depositTimestamp: 5);

            Assert.IsTrue(canClaimEarlyRefund);
        }
Ejemplo n.º 3
0
        public async Task TryClaimEarlyRefundAsync(DepositDetails deposit, Address refundTo)
        {
            var now = (ulong)_blockchainBridge.Head.Timestamp;

            if (!deposit.CanClaimEarlyRefund(now))
            {
                return;
            }

            var depositId       = deposit.Deposit.Id;
            var transactionHash = deposit.ClaimedRefundTransactionHash;

            if (transactionHash is null)
            {
                var provider         = deposit.DataAsset.Provider.Address;
                var ticket           = deposit.EarlyRefundTicket;
                var earlyRefundClaim = new EarlyRefundClaim(ticket.DepositId, deposit.DataAsset.Id,
                                                            deposit.Deposit.Units, deposit.Deposit.Value, deposit.Deposit.ExpiryTime, deposit.Pepper, provider,
                                                            ticket.ClaimableAfter, ticket.Signature, refundTo);
                transactionHash = _refundService.ClaimEarlyRefund(refundTo, earlyRefundClaim);
                deposit.SetClaimedRefundTransactionHash(transactionHash);
                await _depositRepository.UpdateAsync(deposit);

                if (_logger.IsInfo)
                {
                    _logger.Info($"Claimed an early refund for deposit: '{depositId}', transaction hash: '{transactionHash}' (awaits a confirmation).");
                }
            }

            await TryConfirmClaimAsync(deposit, "early ");
        }
Ejemplo n.º 4
0
        public async Task <RefundClaimStatus> TryClaimEarlyRefundAsync(DepositDetails deposit, Address refundTo)
        {
            ulong now = _timestamper.EpochSeconds;

            if (!deposit.CanClaimEarlyRefund(now, deposit.Timestamp))
            {
                return(RefundClaimStatus.Empty);
            }

            Block?latestBlock = await _blockchainBridge.GetLatestBlockAsync();

            if (latestBlock == null)
            {
                return(RefundClaimStatus.Empty);
            }

            now = (ulong)latestBlock.Timestamp;
            if (!deposit.CanClaimEarlyRefund(now, deposit.Timestamp))
            {
                return(RefundClaimStatus.Empty);
            }

            Keccak depositId       = deposit.Deposit.Id;
            Keccak?transactionHash = deposit.ClaimedRefundTransaction?.Hash;

            if (transactionHash is null)
            {
                Address provider = deposit.DataAsset.Provider.Address;
                if (deposit.EarlyRefundTicket == null)
                {
                    throw new InvalidDataException($"Early refund ticket is null on a claimable deposit {depositId}");
                }

                EarlyRefundTicket ticket           = deposit.EarlyRefundTicket;
                EarlyRefundClaim  earlyRefundClaim = new EarlyRefundClaim(ticket.DepositId, deposit.DataAsset.Id,
                                                                          deposit.Deposit.Units, deposit.Deposit.Value, deposit.Deposit.ExpiryTime, deposit.Pepper, provider,
                                                                          ticket.ClaimableAfter, ticket.Signature, refundTo);
                UInt256 gasPrice = await _gasPriceService.GetCurrentRefundAsync();

                transactionHash = await _refundService.ClaimEarlyRefundAsync(refundTo, earlyRefundClaim, gasPrice);

                if (transactionHash is null)
                {
                    if (_logger.IsError)
                    {
                        _logger.Error("There was an error when trying to claim early refund (no transaction hash returned).");
                    }
                    return(RefundClaimStatus.Empty);
                }

                deposit.AddClaimedRefundTransaction(TransactionInfo.Default(transactionHash, 0, gasPrice,
                                                                            _refundService.GasLimit, _timestamper.EpochSeconds));
                await _depositRepository.UpdateAsync(deposit);

                if (_logger.IsInfo)
                {
                    _logger.Info($"Claimed an early refund for deposit: '{depositId}', gas price: {gasPrice} wei, transaction hash: '{transactionHash}' (awaits a confirmation).");
                }
            }

            bool confirmed = await TryConfirmClaimAsync(deposit, "early ");

            return(confirmed
                ? RefundClaimStatus.Confirmed(transactionHash)
                : RefundClaimStatus.Unconfirmed(transactionHash));
        }