1
// Copyright © 2017-2020 Trust Wallet.
2
//
3
// This file is part of Trust. The full Trust copyright notice, including
4
// terms governing use, modification, and redistribution, is contained in the
5
// file LICENSE at the root of the source code distribution tree.
6

7
#pragma once
8

9
#include <numeric>
10
#include <vector>
11

12
#include "FeeCalculator.h"
13
#include <TrustWalletCore/TWCoinType.h>
14
#include "../proto/Bitcoin.pb.h"
15

16
namespace TW::Bitcoin {
17

18
class UnspentSelector {
19
  public:
20
    /// Maximum allowable transaction dust.
21
    static const int64_t dustThreshold;
22

23
    /// Selects unspent transactions to use given a target transaction value.
24
    ///
25
    /// \returns the list of selected utxos or an empty list if there are
26
    /// insufficient funds.
27
    template <typename T>
28
    std::vector<Proto::UnspentTransaction> select(const T& utxos, int64_t targetValue,
29
                                                  int64_t byteFee, int64_t numOutputs = 2);
30

31
    /// Selects UTXOs for max amount; select all except those which would reduce output (dust).
32
    /// One output and no change is assumed.
33
    template <typename T>
34
    std::vector<Proto::UnspentTransaction> selectMaxAmount(const T& utxos, int64_t byteFee);
35

36
    /// Construct, using provided feeCalculator (see getFeeCalculator()).
37 1
    explicit UnspentSelector(const FeeCalculator& feeCalculator) : feeCalculator(feeCalculator) {}
38 1
    UnspentSelector() : UnspentSelector(getFeeCalculator(TWCoinTypeBitcoin)) {}
39

40
    template <typename T>
41 1
    static inline int64_t sum(const T& utxos) {
42 1
        int64_t sum = 0;
43 1
        for (auto& utxo : utxos) {
44 1
            sum += utxo.amount();
45
        }
46 1
        return sum;
47
    }
48

49
  private:
50
    const FeeCalculator& feeCalculator;
51
    template <typename T> std::vector<Proto::UnspentTransaction> filterDustInput(const T& selectedUtxos, int64_t byteFee);
52
};
53

54
} // namespace TW::Bitcoin

Read our documentation on viewing source code .

Loading