/tmp/bitcoin/src/policy/feerate.cpp
Line | Count | Source |
1 | | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | | // Copyright (c) 2009-present The Bitcoin Core developers |
3 | | // Distributed under the MIT software license, see the accompanying |
4 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | | |
6 | | #include <consensus/amount.h> |
7 | | #include <policy/feerate.h> |
8 | | #include <tinyformat.h> |
9 | | |
10 | | #include <cstdlib> |
11 | | |
12 | | CFeeRate::CFeeRate(const CAmount& nFeePaid, int32_t virtual_bytes) |
13 | 140k | { |
14 | 140k | if (virtual_bytes > 0) { |
15 | 140k | m_feerate = FeePerVSize(nFeePaid, virtual_bytes); |
16 | 140k | } else { |
17 | 4 | m_feerate = FeePerVSize(); |
18 | 4 | } |
19 | 140k | } |
20 | | |
21 | | CAmount CFeeRate::GetFee(int32_t virtual_bytes) const |
22 | 1.93M | { |
23 | 1.93M | Assume(virtual_bytes >= 0); |
24 | 1.93M | if (m_feerate.IsEmpty()) { return CAmount(0);} |
25 | 1.93M | CAmount nFee = CAmount(m_feerate.EvaluateFeeUp(virtual_bytes)); |
26 | 1.93M | if (nFee == 0 && virtual_bytes != 0 && m_feerate.fee < 0) return CAmount(-1); |
27 | 1.93M | return nFee; |
28 | 1.93M | } |
29 | | |
30 | | std::string CFeeRate::ToString(FeeRateFormat fee_rate_format) const |
31 | 3.09k | { |
32 | 3.09k | const CAmount feerate_per_kvb{GetFeePerK()}; |
33 | 3.09k | const auto format_feerate = [](const CAmount fee_rate, const CAmount divisor, const int decimals, const std::string& currency_unit, const std::string& size_unit) { |
34 | 3.09k | Assert(divisor > 0); |
35 | 3.09k | const char* sign{fee_rate < 0 ? "-" : ""}; |
36 | 3.09k | const CAmount quotient{std::abs(fee_rate / divisor)}; |
37 | 3.09k | const CAmount remainder{std::abs(fee_rate % divisor)}; |
38 | 3.09k | return strprintf("%s%d.%0*d %s/%s", sign, quotient, decimals, remainder, currency_unit, size_unit); |
39 | 3.09k | }; |
40 | 3.09k | switch (fee_rate_format) { |
41 | 1.13k | case FeeRateFormat::BTC_KVB: return format_feerate(feerate_per_kvb, COIN, /*decimals=*/8, CURRENCY_UNIT, "kvB"); |
42 | 1.95k | case FeeRateFormat::SAT_VB: return format_feerate(feerate_per_kvb, 1000, /*decimals=*/3, CURRENCY_ATOM, "vB"); |
43 | 3.09k | } // no default case, so the compiler can warn about missing cases |
44 | 3.09k | assert(false); |
45 | 0 | } |