Bitcoin Core  22.99.0
P2P Digital Currency
feerate.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 <tinyformat.h>
9 
10 CFeeRate::CFeeRate(const CAmount& nFeePaid, uint32_t num_bytes)
11 {
12  const int64_t nSize{num_bytes};
13 
14  if (nSize > 0) {
15  nSatoshisPerK = nFeePaid * 1000 / nSize;
16  } else {
17  nSatoshisPerK = 0;
18  }
19 }
20 
21 CAmount CFeeRate::GetFee(uint32_t num_bytes) const
22 {
23  const int64_t nSize{num_bytes};
24 
25  CAmount nFee = nSatoshisPerK * nSize / 1000;
26 
27  if (nFee == 0 && nSize != 0) {
28  if (nSatoshisPerK > 0) nFee = CAmount(1);
29  if (nSatoshisPerK < 0) nFee = CAmount(-1);
30  }
31 
32  return nFee;
33 }
34 
35 std::string CFeeRate::ToString(const FeeEstimateMode& fee_estimate_mode) const
36 {
37  switch (fee_estimate_mode) {
38  case FeeEstimateMode::SAT_VB: return strprintf("%d.%03d %s/vB", nSatoshisPerK / 1000, nSatoshisPerK % 1000, CURRENCY_ATOM);
39  default: return strprintf("%d.%08d %s/kvB", nSatoshisPerK / COIN, nSatoshisPerK % COIN, CURRENCY_UNIT);
40  }
41 }
feerate.h
CFeeRate::nSatoshisPerK
CAmount nSatoshisPerK
Definition: feerate.h:32
tinyformat.h
CURRENCY_UNIT
const std::string CURRENCY_UNIT
Definition: feerate.h:14
FeeEstimateMode::SAT_VB
@ SAT_VB
Use sat/vB fee rate unit.
FeeEstimateMode
FeeEstimateMode
Definition: feerate.h:18
CURRENCY_ATOM
const std::string CURRENCY_ATOM
Definition: feerate.h:15
CAmount
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
CFeeRate::ToString
std::string ToString(const FeeEstimateMode &fee_estimate_mode=FeeEstimateMode::BTC_KVB) const
Definition: feerate.cpp:35
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
COIN
static const CAmount COIN
Definition: amount.h:14
CFeeRate::CFeeRate
CFeeRate()
Fee rate of 0 satoshis per kB.
Definition: feerate.h:36
CFeeRate::GetFee
CAmount GetFee(uint32_t num_bytes) const
Return the fee in satoshis for the given size in bytes.
Definition: feerate.cpp:21