diff options
author | S3RK <1466284+S3RK@users.noreply.github.com> | 2022-07-06 09:00:26 +0200 |
---|---|---|
committer | S3RK <1466284+S3RK@users.noreply.github.com> | 2022-08-15 09:35:20 +0200 |
commit | 15e97a6886902ebb378829993a972dc52558aa92 (patch) | |
tree | 7d4ec454757eefa0fb795301c47bfbc6d8dd1402 /src/wallet/coinselection.cpp | |
parent | 72cad28da05cfce9e4950f2dc5a709da41d251f4 (diff) | |
download | bitcoin-15e97a6886902ebb378829993a972dc52558aa92.tar.xz |
wallet: add SelectionResult::GetChange
Diffstat (limited to 'src/wallet/coinselection.cpp')
-rw-r--r-- | src/wallet/coinselection.cpp | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/wallet/coinselection.cpp b/src/wallet/coinselection.cpp index a61921f69f..58d297724d 100644 --- a/src/wallet/coinselection.cpp +++ b/src/wallet/coinselection.cpp @@ -421,6 +421,11 @@ CAmount SelectionResult::GetSelectedValue() const return std::accumulate(m_selected_inputs.cbegin(), m_selected_inputs.cend(), CAmount{0}, [](CAmount sum, const auto& coin) { return sum + coin.txout.nValue; }); } +CAmount SelectionResult::GetSelectedEffectiveValue() const +{ + return std::accumulate(m_selected_inputs.cbegin(), m_selected_inputs.cend(), CAmount{0}, [](CAmount sum, const auto& coin) { return sum + coin.GetEffectiveValue(); }); +} + void SelectionResult::Clear() { m_selected_inputs.clear(); @@ -480,4 +485,24 @@ std::string GetAlgorithmName(const SelectionAlgorithm algo) } assert(false); } + +CAmount SelectionResult::GetChange(const CAmount min_viable_change, const CAmount change_fee) const +{ + // change = SUM(inputs) - SUM(outputs) - fees + // 1) With SFFO we don't pay any fees + // 2) Otherwise we pay all the fees: + // - input fees are covered by GetSelectedEffectiveValue() + // - non_input_fee is included in m_target + // - change_fee + const CAmount change = m_use_effective + ? GetSelectedEffectiveValue() - m_target - change_fee + : GetSelectedValue() - m_target; + + if (change < min_viable_change) { + return 0; + } + + return change; +} + } // namespace wallet |