Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto # 2 : : // Copyright (c) 2009-2020 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 <amount.h> # 10 : : #include <serialize.h> # 11 : : # 12 : : #include <string> # 13 : : # 14 : : const std::string CURRENCY_UNIT = "BTC"; // One formatted unit # 15 : : const std::string CURRENCY_ATOM = "sat"; // One indivisible minimum value unit # 16 : : # 17 : : /* Used to determine type of fee estimation requested */ # 18 : : enum class FeeEstimateMode { # 19 : : UNSET, //!< Use default settings based on other criteria # 20 : : ECONOMICAL, //!< Force estimateSmartFee to use non-conservative estimates # 21 : : CONSERVATIVE, //!< Force estimateSmartFee to use conservative estimates # 22 : : BTC_KVB, //!< Use BTC/kvB fee rate unit # 23 : : SAT_VB, //!< Use sat/vB fee rate unit # 24 : : }; # 25 : : # 26 : : /** # 27 : : * Fee rate in satoshis per kilobyte: CAmount / kB # 28 : : */ # 29 : : class CFeeRate # 30 : : { # 31 : : private: # 32 : : CAmount nSatoshisPerK; // unit is satoshis-per-1,000-bytes # 33 : : # 34 : : public: # 35 : : /** Fee rate of 0 satoshis per kB */ # 36 : 85442 : CFeeRate() : nSatoshisPerK(0) { } # 37 : : template<typename I> # 38 : 1206033 : explicit CFeeRate(const I _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK) { # 39 : : // We've previously had bugs creep in from silent double->int conversion... # 40 : 1206033 : static_assert(std::is_integral<I>::value, "CFeeRate should be used without floats"); # 41 : 1206033 : } # 42 : : /** Constructor for a fee rate in satoshis per kvB (sat/kvB). # 43 : : * # 44 : : * Passing a num_bytes value of COIN (1e8) returns a fee rate in satoshis per vB (sat/vB), # 45 : : * e.g. (nFeePaid * 1e8 / 1e3) == (nFeePaid / 1e5), # 46 : : * where 1e5 is the ratio to convert from BTC/kvB to sat/vB. # 47 : : */ # 48 : : CFeeRate(const CAmount& nFeePaid, uint32_t num_bytes); # 49 : : /** # 50 : : * Return the fee in satoshis for the given size in bytes. # 51 : : */ # 52 : : CAmount GetFee(uint32_t num_bytes) const; # 53 : : /** # 54 : : * Return the fee in satoshis for a size of 1000 bytes # 55 : : */ # 56 : 787900 : CAmount GetFeePerK() const { return GetFee(1000); } # 57 : 27566 : friend bool operator<(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK < b.nSatoshisPerK; } # 58 : 33877 : friend bool operator>(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK > b.nSatoshisPerK; } # 59 : 19076 : friend bool operator==(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK == b.nSatoshisPerK; } # 60 : 539 : friend bool operator<=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK <= b.nSatoshisPerK; } # 61 : 4 : friend bool operator>=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK >= b.nSatoshisPerK; } # 62 : 5046 : friend bool operator!=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK != b.nSatoshisPerK; } # 63 : 306 : CFeeRate& operator+=(const CFeeRate& a) { nSatoshisPerK += a.nSatoshisPerK; return *this; } # 64 : : std::string ToString(const FeeEstimateMode& fee_estimate_mode = FeeEstimateMode::BTC_KVB) const; # 65 : : # 66 : : SERIALIZE_METHODS(CFeeRate, obj) { READWRITE(obj.nSatoshisPerK); } # 67 : : }; # 68 : : # 69 : : #endif // BITCOIN_POLICY_FEERATE_H