Search in sources :

Example 1 with CoinSelection

use of org.bitcoinj.wallet.CoinSelection in project bisq-core by bisq-network.

the class BtcCompensationRequestFeeCoinSelector method select.

private CoinSelection select(Coin target, List<TransactionOutput> candidates) throws InsufficientMoneyException, ChangeBelowDustException {
    ArrayList<TransactionOutput> selected = new ArrayList<>();
    // Sort the inputs by value so we get the lowest values first.
    ArrayList<TransactionOutput> sortedOutputs = new ArrayList<>(candidates);
    // them in order to improve performance.
    if (!target.equals(NetworkParameters.MAX_MONEY))
        sortOutputsByValue(sortedOutputs);
    // Now iterate over the sorted outputs until we have got as close to the target as possible or a little
    // bit over (excessive value will be change).
    long total = 0;
    long dust = Restrictions.getMinNonDustOutput().value;
    long targetValue = target.value;
    for (TransactionOutput output : sortedOutputs) {
        if (total >= targetValue) {
            long change = total - targetValue;
            // If we get a change it must not be smaller than dust
            if (change >= dust || change == 0)
                break;
        }
        // Only pick chain-included transactions, or transactions that are ours and pending.
        if (!isSelectable(output.getParentTransaction()))
            continue;
        selected.add(output);
        total += output.getValue().value;
    }
    long missing = targetValue - total;
    if (missing > 0)
        throw new InsufficientMoneyException(Coin.valueOf(missing));
    long change = total - targetValue;
    if (change > 0 && change < dust)
        throw new ChangeBelowDustException(Coin.valueOf(change));
    return new CoinSelection(Coin.valueOf(total), selected);
}
Also used : TransactionOutput(org.bitcoinj.core.TransactionOutput) ArrayList(java.util.ArrayList) InsufficientMoneyException(org.bitcoinj.core.InsufficientMoneyException) CoinSelection(org.bitcoinj.wallet.CoinSelection)

Example 2 with CoinSelection

use of org.bitcoinj.wallet.CoinSelection in project bisq-core by bisq-network.

the class BsqWalletService method getPreparedBurnFeeTx.

// /////////////////////////////////////////////////////////////////////////////////////////
// Burn fee tx
// /////////////////////////////////////////////////////////////////////////////////////////
public Transaction getPreparedBurnFeeTx(Coin fee) throws InsufficientBsqException, ChangeBelowDustException {
    final Transaction tx = new Transaction(params);
    CoinSelection coinSelection = bsqCoinSelector.select(fee, wallet.calculateAllSpendCandidates());
    coinSelection.gathered.forEach(tx::addInput);
    try {
        Coin change = bsqCoinSelector.getChangeExcludingFee(fee, coinSelection);
        if (!Restrictions.isAboveDust(change))
            throw new ChangeBelowDustException(change);
        if (change.isPositive())
            tx.addOutput(change, getUnusedAddress());
    } catch (InsufficientMoneyException e) {
        throw new InsufficientBsqException(e.missing);
    }
    printTx("getPreparedFeeTx", tx);
    return tx;
}
Also used : Coin(org.bitcoinj.core.Coin) Transaction(org.bitcoinj.core.Transaction) InsufficientMoneyException(org.bitcoinj.core.InsufficientMoneyException) CoinSelection(org.bitcoinj.wallet.CoinSelection)

Example 3 with CoinSelection

use of org.bitcoinj.wallet.CoinSelection in project bisq-core by bisq-network.

the class BsqWalletService method getPreparedBlindVoteTx.

// /////////////////////////////////////////////////////////////////////////////////////////
// Blind vote tx
// /////////////////////////////////////////////////////////////////////////////////////////
public Transaction getPreparedBlindVoteTx(Coin fee, Coin stake) throws InsufficientBsqException, ChangeBelowDustException {
    Transaction tx = new Transaction(params);
    tx.addOutput(new TransactionOutput(params, tx, stake, getUnusedAddress()));
    final Coin sum = fee.add(stake);
    CoinSelection coinSelection = bsqCoinSelector.select(sum, wallet.calculateAllSpendCandidates());
    coinSelection.gathered.forEach(tx::addInput);
    try {
        // We add here stake as we have that output already added
        Coin change = bsqCoinSelector.getChangeExcludingFee(sum, coinSelection);
        if (!Restrictions.isAboveDust(change))
            throw new ChangeBelowDustException(change);
        if (change.isPositive())
            tx.addOutput(change, getUnusedAddress());
    } catch (InsufficientMoneyException e) {
        throw new InsufficientBsqException(e.missing);
    }
    printTx("getPreparedBlindVoteTx", tx);
    return tx;
}
Also used : Coin(org.bitcoinj.core.Coin) TransactionOutput(org.bitcoinj.core.TransactionOutput) Transaction(org.bitcoinj.core.Transaction) InsufficientMoneyException(org.bitcoinj.core.InsufficientMoneyException) CoinSelection(org.bitcoinj.wallet.CoinSelection)

Example 4 with CoinSelection

use of org.bitcoinj.wallet.CoinSelection in project bisq-core by bisq-network.

the class BisqDefaultCoinSelector method select.

@Override
public CoinSelection select(Coin target, List<TransactionOutput> candidates) {
    ArrayList<TransactionOutput> selected = new ArrayList<>();
    // Sort the inputs by age*value so we get the highest "coin days" spent.
    ArrayList<TransactionOutput> sortedOutputs = new ArrayList<>(candidates);
    // If we spend all we don't need to sort
    if (!target.equals(NetworkParameters.MAX_MONEY))
        sortOutputs(sortedOutputs);
    // Now iterate over the sorted outputs until we have got as close to the target as possible or a little
    // bit over (excessive value will be change).
    long total = 0;
    long targetValue = target.value;
    for (TransactionOutput output : sortedOutputs) {
        if (total >= targetValue) {
            long change = total - targetValue;
            if (change == 0 || change >= Restrictions.getMinNonDustOutput().value)
                break;
        }
        if (output.getParentTransaction() != null && isTxSpendable(output.getParentTransaction()) && isTxOutputSpendable(output)) {
            selected.add(output);
            total += output.getValue().value;
        }
    }
    // transaction.
    return new CoinSelection(Coin.valueOf(total), selected);
}
Also used : TransactionOutput(org.bitcoinj.core.TransactionOutput) ArrayList(java.util.ArrayList) CoinSelection(org.bitcoinj.wallet.CoinSelection)

Aggregations

CoinSelection (org.bitcoinj.wallet.CoinSelection)4 InsufficientMoneyException (org.bitcoinj.core.InsufficientMoneyException)3 TransactionOutput (org.bitcoinj.core.TransactionOutput)3 ArrayList (java.util.ArrayList)2 Coin (org.bitcoinj.core.Coin)2 Transaction (org.bitcoinj.core.Transaction)2