private async Task <IBitcoinBasedTransaction> CreatePaymentTxAsync(
            Swap swap,
            string refundAddress,
            DateTimeOffset lockTime)
        {
            var currency = Currencies.Get <BitcoinBasedConfig>(swap.SoldCurrency);

            Log.Debug("Create swap payment {@currency} tx for swap {@swapId}",
                      currency.Name,
                      swap.Id);

            var amountInSatoshi = currency.CoinToSatoshi(
                AmountHelper.QtyToSellAmount(
                    swap.Side,
                    swap.Qty,
                    swap.Price,
                    currency.DigitsMultiplier));

            // maker network fee
            if (swap.MakerNetworkFee > 0)
            {
                var makerNetworkFeeInSatoshi = currency.CoinToSatoshi(swap.MakerNetworkFee);

                if (makerNetworkFeeInSatoshi < amountInSatoshi) // network fee size check
                {
                    amountInSatoshi += makerNetworkFeeInSatoshi;
                }
            }

            var tx = await _transactionFactory
                     .CreateSwapPaymentTxAsync(
                fromOutputs : swap.FromOutputs,
                amount : amountInSatoshi,
                refundAddress : refundAddress,
                toAddress : swap.PartyAddress,
                lockTime : lockTime,
                secretHash : swap.SecretHash,
                secretSize : DefaultSecretSize,
                currencyConfig : currency)
                     .ConfigureAwait(false);

            if (tx == null)
            {
                throw new InternalException(
                          code: Errors.TransactionCreationError,
                          description: $"Payment tx creation error for swap {swap.Id}");
            }

            tx.Type = BlockchainTransactionType.Output | BlockchainTransactionType.SwapPayment;

            Log.Debug("Payment tx successfully created for swap {@swapId}", swap.Id);

            return(tx);
        }
Exemplo n.º 2
0
        private async Task <(IBitcoinBasedTransaction, byte[])> CreatePaymentTxAsync(
            Swap swap,
            string refundAddress,
            DateTimeOffset lockTime)
        {
            var currency = Currencies.Get <BitcoinBasedCurrency>(swap.SoldCurrency);

            Log.Debug("Create swap payment {@currency} tx for swap {@swapId}",
                      currency.Name,
                      swap.Id);

            var unspentAddresses = (await _account
                                    .GetUnspentAddressesAsync()
                                    .ConfigureAwait(false))
                                   .ToList()
                                   .SortList(new AvailableBalanceAscending())
                                   .Select(a => a.Address);

            var amountInSatoshi = currency.CoinToSatoshi(AmountHelper.QtyToAmount(swap.Side, swap.Qty, swap.Price, currency.DigitsMultiplier));

            var(tx, redeemScript) = await _transactionFactory
                                    .CreateSwapPaymentTxAsync(
                currency : currency,
                amount : amountInSatoshi,
                fromWallets : unspentAddresses,
                refundAddress : refundAddress,
                toAddress : swap.PartyAddress,
                lockTime : lockTime,
                secretHash : swap.SecretHash,
                secretSize : DefaultSecretSize,
                outputsSource : new LocalTxOutputSource(_account))
                                    .ConfigureAwait(false);

            if (tx == null)
            {
                throw new InternalException(
                          code: Errors.TransactionCreationError,
                          description: $"Payment tx creation error for swap {swap.Id}");
            }

            tx.Type = BlockchainTransactionType.Output | BlockchainTransactionType.SwapPayment;

            Log.Debug("Payment tx successfully created for swap {@swapId}", swap.Id);

            return(tx, redeemScript);
        }
        private async Task <IBitcoinBasedTransaction> CreatePaymentTxAsync(
            ClientSwap swap,
            string refundAddress,
            DateTimeOffset lockTime)
        {
            var currency = (BitcoinBasedCurrency)swap.SoldCurrency;

            Log.Debug("Create swap payment {@currency} tx for swap {@swapId}",
                      currency.Name,
                      swap.Id);

            var unspentAddresses = (await Account
                                    .GetUnspentAddressesAsync(currency)
                                    .ConfigureAwait(false))
                                   .ToList()
                                   .SortList((a, b) => a.AvailableBalance().CompareTo(b.AvailableBalance()))
                                   .Select(a => a.Address);

            var tx = await _transactionFactory
                     .CreateSwapPaymentTxAsync(
                currency : currency,
                swap : swap,
                fromWallets : unspentAddresses,
                refundAddress : refundAddress,
                toAddress : swap.PartyAddress,
                lockTime : lockTime,
                secretHash : swap.SecretHash,
                secretSize : DefaultSecretSize,
                outputsSource : new LocalTxOutputSource(Account))
                     .ConfigureAwait(false);

            if (tx == null)
            {
                throw new InternalException(
                          code: Errors.TransactionCreationError,
                          description: $"Payment tx creation error for swap {swap.Id}");
            }

            Log.Debug("Payment tx successfully created for swap {@swapId}", swap.Id);

            return(tx);
        }