Bitcoin ABC  0.22.12
P2P Digital Currency
feerate.h
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 #ifndef BITCOIN_FEERATE_H
8 #define BITCOIN_FEERATE_H
9 
10 #include <amount.h>
11 #include <serialize.h>
12 
13 #include <cstdlib>
14 #include <ostream>
15 #include <string>
16 #include <type_traits>
17 
21 class CFeeRate {
22 private:
23  // unit is satoshis-per-1,000-bytes
25 
26 public:
30  constexpr CFeeRate() : nSatoshisPerK() {}
31  explicit constexpr CFeeRate(const Amount _nSatoshisPerK)
32  : nSatoshisPerK(_nSatoshisPerK) {}
33 
38  CFeeRate(const Amount nFeePaid, size_t nBytes);
39 
43  Amount GetFee(size_t nBytes) const;
44 
49  Amount GetFeeCeiling(size_t nBytes) const;
50 
54  Amount GetFeePerK() const { return GetFee(1000); }
55 
59  friend constexpr bool operator==(const CFeeRate a, const CFeeRate b) {
60  return a.nSatoshisPerK == b.nSatoshisPerK;
61  }
62  friend constexpr bool operator!=(const CFeeRate a, const CFeeRate b) {
63  return !(a == b);
64  }
65 
69  friend bool operator<(const CFeeRate &a, const CFeeRate &b) {
70  return a.nSatoshisPerK < b.nSatoshisPerK;
71  }
72  friend bool operator>(const CFeeRate &a, const CFeeRate &b) {
73  return a.nSatoshisPerK > b.nSatoshisPerK;
74  }
75  friend bool operator<=(const CFeeRate &a, const CFeeRate &b) {
76  return a.nSatoshisPerK <= b.nSatoshisPerK;
77  }
78  friend bool operator>=(const CFeeRate &a, const CFeeRate &b) {
79  return a.nSatoshisPerK >= b.nSatoshisPerK;
80  }
82  nSatoshisPerK += a.nSatoshisPerK;
83  return *this;
84  }
85  std::string ToString() const;
86 
88 
89  template <typename Stream, typename Operation>
90  inline void SerializationOp(Stream &s, Operation ser_action) {
91  READWRITE(nSatoshisPerK);
92  }
93 };
94 
95 #endif // BITCOIN_FEERATE_H
friend bool operator>=(const CFeeRate &a, const CFeeRate &b)
Definition: feerate.h:78
friend bool operator<=(const CFeeRate &a, const CFeeRate &b)
Definition: feerate.h:75
Definition: amount.h:17
Amount nSatoshisPerK
Definition: feerate.h:24
friend bool operator>(const CFeeRate &a, const CFeeRate &b)
Definition: feerate.h:72
ADD_SERIALIZE_METHODS
Definition: feerate.h:87
Amount GetFeePerK() const
Return the fee in satoshis for a size of 1000 bytes.
Definition: feerate.h:54
friend constexpr bool operator!=(const CFeeRate a, const CFeeRate b)
Definition: feerate.h:62
Amount GetFee(size_t nBytes) const
Return the fee in satoshis for the given size in bytes.
Definition: feerate.cpp:51
friend constexpr bool operator==(const CFeeRate a, const CFeeRate b)
Equality.
Definition: feerate.h:59
constexpr CFeeRate()
Fee rate of 0 satoshis per kB.
Definition: feerate.h:30
CFeeRate & operator+=(const CFeeRate &a)
Definition: feerate.h:81
void SerializationOp(Stream &s, Operation ser_action)
Definition: feerate.h:90
constexpr CFeeRate(const Amount _nSatoshisPerK)
Definition: feerate.h:31
friend bool operator<(const CFeeRate &a, const CFeeRate &b)
Comparison.
Definition: feerate.h:69
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
std::string ToString() const
Definition: feerate.cpp:59
#define READWRITE(...)
Definition: serialize.h:191
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