feerate.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto 2 // Copyright (c) 2009-present 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 #include <util/feefrac.h> 12 13 14 #include <cstdint> 15 #include <string> 16 #include <type_traits> 17 18 const std::string CURRENCY_UNIT = "BTC"; // One formatted unit 19 const std::string CURRENCY_ATOM = "sat"; // One indivisible minimum value unit 20 21 /* Used to determine type of fee estimation requested */ 22 enum class FeeEstimateMode { 23 UNSET, //!< Use default settings based on other criteria 24 ECONOMICAL, //!< Force estimateSmartFee to use non-conservative estimates 25 CONSERVATIVE, //!< Force estimateSmartFee to use conservative estimates 26 BTC_KVB, //!< Use BTC/kvB fee rate unit 27 SAT_VB, //!< Use sat/vB fee rate unit 28 }; 29 30 /** 31 * Fee rate in satoshis per virtualbyte: CAmount / vB 32 * the feerate is represented internally as FeeFrac 33 */ 34 class CFeeRate 35 { 36 private: 37 /** Fee rate in sats/vB (satoshis per N virtualbytes) */ 38 FeePerVSize m_feerate; 39 40 public: 41 /** Fee rate of 0 satoshis per 0 vB */ 42 CFeeRate() = default; 43 template<std::integral I> // Disallow silent float -> int conversion 44 explicit CFeeRate(const I m_feerate_kvb) : m_feerate(FeePerVSize(m_feerate_kvb, 1000)) {} 45 46 /** 47 * Construct a fee rate from a fee in satoshis and a vsize in vB. 48 * 49 * Passing any virtual_bytes less than or equal to 0 will result in 0 fee rate per 0 size. 50 */ 51 CFeeRate(const CAmount& nFeePaid, int32_t virtual_bytes); 52 53 /** 54 * Return the fee in satoshis for the given vsize in vbytes. 55 * If the calculated fee would have fractional satoshis, then the 56 * returned fee will always be rounded up to the nearest satoshi. 57 */ 58 CAmount GetFee(int32_t virtual_bytes) const; 59 60 FeePerVSize GetFeePerVSize() const { return m_feerate; } 61 62 /** 63 * Return the fee in satoshis for a vsize of 1000 vbytes 64 */ 65 CAmount GetFeePerK() const { return CAmount(m_feerate.EvaluateFeeDown(1000)); } 66 friend std::weak_ordering operator<=>(const CFeeRate& a, const CFeeRate& b) noexcept 67 { 68 return FeeRateCompare(a.m_feerate, b.m_feerate); 69 } 70 friend bool operator==(const CFeeRate& a, const CFeeRate& b) noexcept 71 { 72 return FeeRateCompare(a.m_feerate, b.m_feerate) == std::weak_ordering::equivalent; 73 } 74 CFeeRate& operator+=(const CFeeRate& a) { 75 m_feerate = FeePerVSize(GetFeePerK() + a.GetFeePerK(), 1000); 76 return *this; 77 } 78 std::string ToString(const FeeEstimateMode& fee_estimate_mode = FeeEstimateMode::BTC_KVB) const; 79 friend CFeeRate operator*(const CFeeRate& f, int a) { return CFeeRate(a * f.m_feerate.fee, f.m_feerate.size); } 80 friend CFeeRate operator*(int a, const CFeeRate& f) { return CFeeRate(a * f.m_feerate.fee, f.m_feerate.size); } 81 82 SERIALIZE_METHODS(CFeeRate, obj) { READWRITE(obj.m_feerate.fee, obj.m_feerate.size); } 83 }; 84 85 #endif // BITCOIN_POLICY_FEERATE_H