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