/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 | 139k | { |
14 | 139k | if (virtual_bytes > 0) { |
15 | 139k | m_feerate = FeePerVSize(nFeePaid, virtual_bytes); |
16 | 139k | } else { |
17 | 7 | m_feerate = FeePerVSize(); |
18 | 7 | } |
19 | 139k | } |
20 | | |
21 | | CAmount CFeeRate::GetFeePerK() const |
22 | 648k | { |
23 | 648k | if (m_feerate.IsEmpty()) { return CAmount(0); } |
24 | 648k | return CAmount(m_feerate.EvaluateFeeDown(1000)); |
25 | 648k | } |
26 | | |
27 | | CAmount CFeeRate::GetFee(int32_t virtual_bytes) const |
28 | 1.90M | { |
29 | 1.90M | Assume(virtual_bytes >= 0); |
30 | 1.90M | if (m_feerate.IsEmpty()) { return CAmount(0);} |
31 | 1.90M | return CAmount{m_feerate.EvaluateFeeUp(virtual_bytes)}; |
32 | 1.90M | } |
33 | | |
34 | | std::string CFeeRate::ToString(FeeRateFormat fee_rate_format) const |
35 | 3.20k | { |
36 | 3.20k | const CAmount feerate_per_kvb{GetFeePerK()}; |
37 | 3.20k | const auto format_feerate = [](const CAmount fee_rate, const CAmount divisor, const int decimals, const std::string& currency_unit, const std::string& size_unit) { |
38 | 3.20k | Assert(divisor > 0); |
39 | 3.20k | const char* sign{fee_rate < 0 ? "-" : ""}; |
40 | 3.20k | const CAmount quotient{std::abs(fee_rate / divisor)}; |
41 | 3.20k | const CAmount remainder{std::abs(fee_rate % divisor)}; |
42 | 3.20k | return strprintf("%s%d.%0*d %s/%s", sign, quotient, decimals, remainder, currency_unit, size_unit); |
43 | 3.20k | }; |
44 | 3.20k | switch (fee_rate_format) { |
45 | 1.15k | case FeeRateFormat::BTC_KVB: return format_feerate(feerate_per_kvb, COIN, /*decimals=*/8, CURRENCY_UNIT, "kvB"); |
46 | 2.04k | case FeeRateFormat::SAT_VB: return format_feerate(feerate_per_kvb, 1000, /*decimals=*/3, CURRENCY_ATOM, "vB"); |
47 | 3.20k | } // no default case, so the compiler can warn about missing cases |
48 | 3.20k | assert(false); |
49 | 0 | } |