Bitcoin Core  31.0.0
P2P Digital Currency
feerate.cpp
Go to the documentation of this file.
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 
11 CFeeRate::CFeeRate(const CAmount& nFeePaid, int32_t virtual_bytes)
12 {
13  if (virtual_bytes > 0) {
14  m_feerate = FeePerVSize(nFeePaid, virtual_bytes);
15  } else {
17  }
18 }
19 
20 CAmount CFeeRate::GetFee(int32_t virtual_bytes) const
21 {
22  Assume(virtual_bytes >= 0);
23  if (m_feerate.IsEmpty()) { return CAmount(0);}
24  CAmount nFee = CAmount(m_feerate.EvaluateFeeUp(virtual_bytes));
25  if (nFee == 0 && virtual_bytes != 0 && m_feerate.fee < 0) return CAmount(-1);
26  return nFee;
27 }
28 
29 std::string CFeeRate::ToString(FeeRateFormat fee_rate_format) const
30 {
31  const CAmount feerate_per_kvb{GetFeePerK()};
32  switch (fee_rate_format) {
33  case FeeRateFormat::BTC_KVB: return strprintf("%d.%08d %s/kvB", feerate_per_kvb / COIN, feerate_per_kvb % COIN, CURRENCY_UNIT);
34  case FeeRateFormat::SAT_VB: return strprintf("%d.%03d %s/vB", feerate_per_kvb / 1000, feerate_per_kvb % 1000, CURRENCY_ATOM);
35  } // no default case, so the compiler can warn about missing cases
36  assert(false);
37 }
int64_t fee
Definition: feefrac.h:107
assert(!tx.IsCoinBase())
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
Use BTC/kvB fee rate unit.
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
FeePerUnit< VSizeTag > FeePerVSize
Definition: feefrac.h:252
FeeRateFormat
Definition: feerate.h:22
const std::string CURRENCY_ATOM
Definition: feerate.h:20
int64_t EvaluateFeeUp(int32_t at_size) const noexcept
Compute the fee for a given size at_size using this object&#39;s feerate, rounding up.
Definition: feefrac.h:223
const std::string CURRENCY_UNIT
Definition: feerate.h:19
bool IsEmpty() const noexcept
Check if this is empty (size and fee are 0).
Definition: feefrac.h:120
#define Assume(val)
Assume is the identity function.
Definition: check.h:125
std::string ToString(FeeRateFormat fee_rate_format=FeeRateFormat::BTC_KVB) const
Definition: feerate.cpp:29
FeePerVSize m_feerate
Fee rate in sats/vB (satoshis per N virtualbytes)
Definition: feerate.h:35
Use sat/vB fee rate unit.
CFeeRate()=default
Fee rate of 0 satoshis per 0 vB.
CAmount GetFee(int32_t virtual_bytes) const
Return the fee in satoshis for the given vsize in vbytes.
Definition: feerate.cpp:20
CAmount GetFeePerK() const
Return the fee in satoshis for a vsize of 1000 vbytes.
Definition: feerate.h:62
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15