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 <policy/feerate.h>
7 : :
8 : : #include <consensus/amount.h>
9 : : #include <tinyformat.h>
10 : : #include <util/check.h>
11 : :
12 : : #include <cstdlib>
13 : :
14 [ + + ]: 147052 : CFeeRate::CFeeRate(const CAmount& nFeePaid, int32_t virtual_bytes)
15 : : {
16 [ + + ]: 147052 : if (virtual_bytes > 0) {
17 : 147045 : m_feerate = FeePerVSize(nFeePaid, virtual_bytes);
18 : : } else {
19 : 7 : m_feerate = FeePerVSize();
20 : : }
21 : 147052 : }
22 : :
23 : 691037 : CAmount CFeeRate::GetFeePerK() const
24 : : {
25 [ + + ]: 691037 : if (m_feerate.IsEmpty()) { return CAmount(0); }
26 : 691034 : return CAmount(m_feerate.EvaluateFeeDown(1000));
27 : : }
28 : :
29 : 1917703 : CAmount CFeeRate::GetFee(int32_t virtual_bytes) const
30 : : {
31 [ + - ]: 1917703 : Assume(virtual_bytes >= 0);
32 [ + - ]: 1917703 : if (m_feerate.IsEmpty()) { return CAmount(0);}
33 : 1917703 : return CAmount{m_feerate.EvaluateFeeUp(virtual_bytes)};
34 : : }
35 : :
36 : 3309 : std::string CFeeRate::ToString(FeeRateFormat fee_rate_format) const
37 : : {
38 : 3309 : const CAmount feerate_per_kvb{GetFeePerK()};
39 : 6618 : const auto format_feerate = [](const CAmount fee_rate, const CAmount divisor, const int decimals, const std::string& currency_unit, const std::string& size_unit) {
40 : 3309 : Assert(divisor > 0);
41 [ + + ]: 3309 : const char* sign{fee_rate < 0 ? "-" : ""};
42 : 3309 : const CAmount quotient{std::abs(fee_rate / divisor)};
43 : 3309 : const CAmount remainder{std::abs(fee_rate % divisor)};
44 : 3309 : return strprintf("%s%d.%0*d %s/%s", sign, quotient, decimals, remainder, currency_unit, size_unit);
45 : : };
46 [ + + - ]: 3309 : switch (fee_rate_format) {
47 [ + - ]: 2472 : case FeeRateFormat::BTC_KVB: return format_feerate(feerate_per_kvb, COIN, /*decimals=*/8, CURRENCY_UNIT, "kvB");
48 [ + - ]: 4146 : case FeeRateFormat::SAT_VB: return format_feerate(feerate_per_kvb, 1000, /*decimals=*/3, CURRENCY_ATOM, "vB");
49 : : } // no default case, so the compiler can warn about missing cases
50 : 0 : AssertUnreachable();
51 : : }
|