Bitcoin ABC  0.24.7
P2P Digital Currency
feerate.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Bitcoin developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include <feerate.h>
8 
9 #include <network.h>
10 
11 #include <tinyformat.h>
12 
13 CFeeRate::CFeeRate(const Amount nFeePaid, size_t nBytes_) {
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 = 1000 * nFeePaid / nSize;
19  } else {
21  }
22 }
23 
24 template <bool ceil>
25 static Amount GetFee(size_t nBytes_, Amount nSatoshisPerK) {
26  assert(nBytes_ <= uint64_t(std::numeric_limits<int64_t>::max()));
27  int64_t nSize = int64_t(nBytes_);
28 
29  // Ensure fee is rounded up when truncated if ceil is true.
30  Amount nFee = Amount::zero();
31  if (ceil) {
32  nFee = Amount(nSize * nSatoshisPerK % 1000 > Amount::zero()
33  ? nSize * nSatoshisPerK / 1000 + SATOSHI
34  : nSize * nSatoshisPerK / 1000);
35  } else {
36  nFee = nSize * nSatoshisPerK / 1000;
37  }
38 
39  if (nFee == Amount::zero() && nSize != 0) {
40  if (nSatoshisPerK > Amount::zero()) {
41  nFee = SATOSHI;
42  }
43  if (nSatoshisPerK < Amount::zero()) {
44  nFee = -SATOSHI;
45  }
46  }
47 
48  return nFee;
49 }
50 
51 Amount CFeeRate::GetFee(size_t nBytes) const {
52  return ::GetFee<false>(nBytes, nSatoshisPerK);
53 }
54 
55 Amount CFeeRate::GetFeeCeiling(size_t nBytes) const {
56  return ::GetFee<true>(nBytes, nSatoshisPerK);
57 }
58 
59 std::string CFeeRate::ToString() const {
60  const auto currency = Currency::get();
61  return strprintf("%d.%0*d %s/kB", nSatoshisPerK / currency.baseunit,
62  currency.decimals,
63  (nSatoshisPerK % currency.baseunit) / currency.subunit,
64  currency.ticker);
65 }
feerate.h
CFeeRate::nSatoshisPerK
Amount nSatoshisPerK
Definition: feerate.h:24
GetFee
static Amount GetFee(size_t nBytes_, Amount nSatoshisPerK)
Definition: feerate.cpp:25
tinyformat.h
Amount::zero
static constexpr Amount zero()
Definition: amount.h:42
SATOSHI
static constexpr Amount SATOSHI
Definition: amount.h:153
CFeeRate::CFeeRate
constexpr CFeeRate()
Fee rate of 0 satoshis per kB.
Definition: feerate.h:30
CFeeRate::GetFee
Amount GetFee(size_t nBytes) const
Return the fee in satoshis for the given size in bytes.
Definition: feerate.cpp:51
Currency::get
static const Currency & get()
Definition: amount.cpp:19
Amount
Definition: amount.h:19
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
network.h
CFeeRate::ToString
std::string ToString() const
Definition: feerate.cpp:59
CFeeRate::GetFeeCeiling
Amount GetFeeCeiling(size_t nBytes) const
Return the ceiling of a fee calculation in satoshis for the given size in bytes.
Definition: feerate.cpp:55