Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2021 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 = "DASH"; // One formatted unit 18 : const std::string CURRENCY_ATOM = "duff"; // 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 : DASH_KB, //!< Use DASH/kB fee rate unit 26 : DUFF_B, //!< Use duff/B 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 99886 : CFeeRate() : nSatoshisPerK(0) { } 41 : template<typename I> 42 1484086 : 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 1484086 : } 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 size in vbytes. 57 : * If the calculated fee would have fractional satoshis, then the returned fee will always be rounded up to the nearest satoshi. 58 : */ 59 : CAmount GetFee(uint32_t num_bytes) const; 60 : 61 : /** 62 : * Return the fee in satoshis for a vsize of 1000 vbytes 63 : */ 64 148323 : CAmount GetFeePerK() const { return nSatoshisPerK; } 65 596 : friend bool operator<(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK < b.nSatoshisPerK; } 66 1154 : friend bool operator>(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK > b.nSatoshisPerK; } 67 293 : friend bool operator==(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK == b.nSatoshisPerK; } 68 2 : friend bool operator<=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK <= b.nSatoshisPerK; } 69 2 : friend bool operator>=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK >= b.nSatoshisPerK; } 70 20 : friend bool operator!=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK != b.nSatoshisPerK; } 71 7 : CFeeRate& operator+=(const CFeeRate& a) { nSatoshisPerK += a.nSatoshisPerK; return *this; } 72 : std::string ToString(const FeeEstimateMode& fee_estimate_mode = FeeEstimateMode::DASH_KB) const; 73 7 : friend CFeeRate operator*(const CFeeRate& f, int a) { return CFeeRate(a * f.nSatoshisPerK); } 74 7 : friend CFeeRate operator*(int a, const CFeeRate& f) { return CFeeRate(a * f.nSatoshisPerK); } 75 : 76 : SERIALIZE_METHODS(CFeeRate, obj) { READWRITE(obj.nSatoshisPerK); } 77 : }; 78 : 79 : #endif // BITCOIN_POLICY_FEERATE_H