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