Bitcoin Core  0.18.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 const std::string CURRENCY_UNIT = "BTC";
11 
12 CFeeRate::CFeeRate(const CAmount& nFeePaid, size_t nBytes_)
13 {
14  assert(nBytes_ <= uint64_t(std::numeric_limits<int64_t>::max()));
15  int64_t nSize = int64_t(nBytes_);
16 
17  if (nSize > 0)
18  nSatoshisPerK = nFeePaid * 1000 / nSize;
19  else
20  nSatoshisPerK = 0;
21 }
22 
23 CAmount CFeeRate::GetFee(size_t nBytes_) const
24 {
25  assert(nBytes_ <= uint64_t(std::numeric_limits<int64_t>::max()));
26  int64_t nSize = int64_t(nBytes_);
27 
28  CAmount nFee = nSatoshisPerK * nSize / 1000;
29 
30  if (nFee == 0 && nSize != 0) {
31  if (nSatoshisPerK > 0)
32  nFee = CAmount(1);
33  if (nSatoshisPerK < 0)
34  nFee = CAmount(-1);
35  }
36 
37  return nFee;
38 }
39 
40 std::string CFeeRate::ToString() const
41 {
42  return strprintf("%d.%08d %s/kB", nSatoshisPerK / COIN, nSatoshisPerK % COIN, CURRENCY_UNIT);
43 }
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1067
static const CAmount COIN
Definition: amount.h:14
const std::string CURRENCY_UNIT
Definition: feerate.cpp:10
CAmount nSatoshisPerK
Definition: feerate.h:22
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
CFeeRate()
Fee rate of 0 satoshis per kB.
Definition: feerate.h:26
std::string ToString() const
Definition: feerate.cpp:40
CAmount GetFee(size_t nBytes) const
Return the fee in satoshis for the given size in bytes.
Definition: feerate.cpp:23