feerate.h
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, //!< Use default settings based on other criteria 23 ECONOMICAL, //!< Force estimateSmartFee to use non-conservative estimates 24 CONSERVATIVE, //!< Force estimateSmartFee to use conservative estimates 25 BTC_KVB, //!< Use BTC/kvB fee rate unit 26 SAT_VB, //!< Use sat/vB fee rate unit 27 }; 28 29 /** 30 * Fee rate in satoshis per kilovirtualbyte: CAmount / kvB 31 */ 32 class CFeeRate 33 { 34 private: 35 /** Fee rate in sat/kvB (satoshis per 1000 virtualbytes) */ 36 CAmount nSatoshisPerK; 37 38 public: 39 /** Fee rate of 0 satoshis per kvB */ 40 CFeeRate() : nSatoshisPerK(0) { } 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 47 /** 48 * Construct a fee rate from a fee in satoshis and a vsize in vB. 49 * 50 * param@[in] nFeePaid The fee paid by a transaction, in satoshis 51 * param@[in] num_bytes The vsize of a transaction, in vbytes 52 */ 53 CFeeRate(const CAmount& nFeePaid, uint32_t num_bytes); 54 55 /** 56 * Return the fee in satoshis for the given vsize in vbytes. 57 * If the calculated fee would have fractional satoshis, then the 58 * returned fee will always be rounded up to the nearest satoshi. 59 */ 60 CAmount GetFee(uint32_t num_bytes) const; 61 62 /** 63 * Return the fee in satoshis for a vsize of 1000 vbytes 64 */ 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