Search in sources :

Example 1 with CoinSelection

use of co.rsk.bitcoinj.wallet.CoinSelection in project rskj by rsksmart.

the class RskAllowUnconfirmedCoinSelector method select.

@Override
public CoinSelection select(Coin target, List<TransactionOutput> candidates) {
    ArrayList<TransactionOutput> selected = new ArrayList<TransactionOutput>();
    // Sort the inputs by age*value so we get the highest "coindays" spent.
    // TODO: Consider changing the wallets internal format to track just outputs and keep them ordered.
    ArrayList<TransactionOutput> sortedOutputs = new ArrayList<TransactionOutput>(candidates);
    // TODO: Take in network parameters when instanatiated, and then test against the current network. Or just have a boolean parameter for "give me everything"
    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;
    for (TransactionOutput output : sortedOutputs) {
        if (total >= target.value) {
            break;
        }
        selected.add(output);
        total += output.getValue().value;
    }
    // transaction.
    return new CoinSelection(Coin.valueOf(total), selected);
}
Also used : TransactionOutput(co.rsk.bitcoinj.core.TransactionOutput) ArrayList(java.util.ArrayList) CoinSelection(co.rsk.bitcoinj.wallet.CoinSelection)

Aggregations

TransactionOutput (co.rsk.bitcoinj.core.TransactionOutput)1 CoinSelection (co.rsk.bitcoinj.wallet.CoinSelection)1 ArrayList (java.util.ArrayList)1