Bitcoin Core  27.99.0
P2P Digital Currency
feerate.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 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 #ifndef BITCOIN_POLICY_FEERATE_H
7 #define BITCOIN_POLICY_FEERATE_H
8 
9 #include <consensus/amount.h>
10 #include <serialize.h>
11 
12 
13 #include <cstdint>
14 #include <string>
15 #include <type_traits>
16 
17 const std::string CURRENCY_UNIT = "BTC"; // One formatted unit
18 const std::string CURRENCY_ATOM = "sat"; // One indivisible minimum value unit
19 
20 /* Used to determine type of fee estimation requested */
21 enum class FeeEstimateMode {
22  UNSET,
23  ECONOMICAL,
24  CONSERVATIVE,
25  BTC_KVB,
26  SAT_VB,
27 };
28 
32 class CFeeRate
33 {
34 private:
37 
38 public:
41  template<typename I>
42  explicit CFeeRate(const I _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK) {
43  // We've previously had bugs creep in from silent double->int conversion...
44  static_assert(std::is_integral<I>::value, "CFeeRate should be used without floats");
45  }
46 
53  CFeeRate(const CAmount& nFeePaid, uint32_t num_bytes);
54 
60  CAmount GetFee(uint32_t num_bytes) const;
61 
65  CAmount GetFeePerK() const { return nSatoshisPerK; }
66  friend bool operator<(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK < b.nSatoshisPerK; }
67  friend bool operator>(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK > b.nSatoshisPerK; }
68  friend bool operator==(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK == b.nSatoshisPerK; }
69  friend bool operator<=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK <= b.nSatoshisPerK; }
70  friend bool operator>=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK >= b.nSatoshisPerK; }
71  friend bool operator!=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK != b.nSatoshisPerK; }
72  CFeeRate& operator+=(const CFeeRate& a) { nSatoshisPerK += a.nSatoshisPerK; return *this; }
73  std::string ToString(const FeeEstimateMode& fee_estimate_mode = FeeEstimateMode::BTC_KVB) const;
74  friend CFeeRate operator*(const CFeeRate& f, int a) { return CFeeRate(a * f.nSatoshisPerK); }
75  friend CFeeRate operator*(int a, const CFeeRate& f) { return CFeeRate(a * f.nSatoshisPerK); }
76 
77  SERIALIZE_METHODS(CFeeRate, obj) { READWRITE(obj.nSatoshisPerK); }
78 };
79 
80 #endif // BITCOIN_POLICY_FEERATE_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:33
std::string ToString(const FeeEstimateMode &fee_estimate_mode=FeeEstimateMode::BTC_KVB) const
Definition: feerate.cpp:39
CFeeRate & operator+=(const CFeeRate &a)
Definition: feerate.h:72
friend bool operator>=(const CFeeRate &a, const CFeeRate &b)
Definition: feerate.h:70
CAmount GetFee(uint32_t num_bytes) const
Return the fee in satoshis for the given vsize in vbytes.
Definition: feerate.cpp:23
friend CFeeRate operator*(int a, const CFeeRate &f)
Definition: feerate.h:75
CAmount nSatoshisPerK
Fee rate in sat/kvB (satoshis per 1000 virtualbytes)
Definition: feerate.h:36
SERIALIZE_METHODS(CFeeRate, obj)
Definition: feerate.h:77
friend CFeeRate operator*(const CFeeRate &f, int a)
Definition: feerate.h:74
friend bool operator==(const CFeeRate &a, const CFeeRate &b)
Definition: feerate.h:68
friend bool operator>(const CFeeRate &a, const CFeeRate &b)
Definition: feerate.h:67
friend bool operator<(const CFeeRate &a, const CFeeRate &b)
Definition: feerate.h:66
CAmount GetFeePerK() const
Return the fee in satoshis for a vsize of 1000 vbytes.
Definition: feerate.h:65
friend bool operator!=(const CFeeRate &a, const CFeeRate &b)
Definition: feerate.h:71
CFeeRate(const I _nSatoshisPerK)
Definition: feerate.h:42
CFeeRate()
Fee rate of 0 satoshis per kvB.
Definition: feerate.h:40
friend bool operator<=(const CFeeRate &a, const CFeeRate &b)
Definition: feerate.h:69
const std::string CURRENCY_ATOM
Definition: feerate.h:18
const std::string CURRENCY_UNIT
Definition: feerate.h:17
FeeEstimateMode
Definition: feerate.h:21
@ CONSERVATIVE
Force estimateSmartFee to use conservative estimates.
@ UNSET
Use default settings based on other criteria.
@ BTC_KVB
Use BTC/kvB fee rate unit.
@ ECONOMICAL
Force estimateSmartFee to use non-conservative estimates.
@ SAT_VB
Use sat/vB fee rate unit.
#define READWRITE(...)
Definition: serialize.h:156