Bitcoin ABC  0.26.3
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 <tinyformat.h>
10 
11 CFeeRate::CFeeRate(const Amount nFeePaid, size_t nBytes_) {
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 = 1000 * nFeePaid / nSize;
17  } else {
19  }
20 }
21 
22 template <bool ceil>
23 static Amount GetFee(size_t nBytes_, Amount nSatoshisPerK) {
24  assert(nBytes_ <= uint64_t(std::numeric_limits<int64_t>::max()));
25  int64_t nSize = int64_t(nBytes_);
26 
27  // Ensure fee is rounded up when truncated if ceil is true.
28  Amount nFee = Amount::zero();
29  if (ceil) {
30  nFee = Amount(nSize * nSatoshisPerK % 1000 > Amount::zero()
31  ? nSize * nSatoshisPerK / 1000 + SATOSHI
32  : nSize * nSatoshisPerK / 1000);
33  } else {
34  nFee = nSize * nSatoshisPerK / 1000;
35  }
36 
37  if (nFee == Amount::zero() && nSize != 0) {
38  if (nSatoshisPerK > Amount::zero()) {
39  nFee = SATOSHI;
40  }
41  if (nSatoshisPerK < Amount::zero()) {
42  nFee = -SATOSHI;
43  }
44  }
45 
46  return nFee;
47 }
48 
49 Amount CFeeRate::GetFee(size_t nBytes) const {
50  return ::GetFee<false>(nBytes, nSatoshisPerK);
51 }
52 
53 Amount CFeeRate::GetFeeCeiling(size_t nBytes) const {
54  return ::GetFee<true>(nBytes, nSatoshisPerK);
55 }
56 
57 std::string CFeeRate::ToString() const {
58  const auto currency = Currency::get();
59  return strprintf("%d.%0*d %s/kB", nSatoshisPerK / currency.baseunit,
60  currency.decimals,
61  (nSatoshisPerK % currency.baseunit) / currency.subunit,
62  currency.ticker);
63 }
static constexpr Amount SATOSHI
Definition: amount.h:143
constexpr CFeeRate()
Fee rate of 0 satoshis per kB.
Definition: feerate.h:30
std::string ToString() const
Definition: feerate.cpp:57
Amount GetFee(size_t nBytes) const
Return the fee in satoshis for the given size in bytes.
Definition: feerate.cpp:49
Amount nSatoshisPerK
Definition: feerate.h:24
Amount GetFeeCeiling(size_t nBytes) const
Return the ceiling of a fee calculation in satoshis for the given size in bytes.
Definition: feerate.cpp:53
static Amount GetFee(size_t nBytes_, Amount nSatoshisPerK)
Definition: feerate.cpp:23
Definition: amount.h:19
static constexpr Amount zero() noexcept
Definition: amount.h:32
static const Currency & get()
Definition: amount.cpp:18
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
assert(!tx.IsCoinBase())