feerate.h raw
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 The Limenka 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 LIMENKA_POLICY_FEERATE_H
7 #define LIMENKA_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<std::integral I> // Disallow silent float -> int conversion
42 explicit CFeeRate(const I _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK) {
43 }
44
45 /**
46 * Construct a fee rate from a fee in satoshis and a vsize in vB.
47 */
48 CFeeRate(const CAmount& nFeePaid, uint32_t num_bytes);
49
50 /**
51 * Return the fee in satoshis for the given vsize in vbytes.
52 * If the calculated fee would have fractional satoshis, then the
53 * returned fee will always be rounded up to the nearest satoshi.
54 */
55 CAmount GetFee(uint32_t num_bytes) const;
56
57 /**
58 * Return the fee in satoshis for a vsize of 1000 vbytes
59 */
60 CAmount GetFeePerK() const { return nSatoshisPerK; }
61 friend bool operator<(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK < b.nSatoshisPerK; }
62 friend bool operator>(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK > b.nSatoshisPerK; }
63 friend bool operator==(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK == b.nSatoshisPerK; }
64 friend bool operator<=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK <= b.nSatoshisPerK; }
65 friend bool operator>=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK >= b.nSatoshisPerK; }
66 friend bool operator!=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK != b.nSatoshisPerK; }
67 CFeeRate& operator+=(const CFeeRate& a) { nSatoshisPerK += a.nSatoshisPerK; return *this; }
68 /** Return the fee rate in sat/vB or BTC/kvB, with units, as a string. */
69 std::string ToString(const FeeEstimateMode& fee_estimate_mode = FeeEstimateMode::BTC_KVB) const;
70 /** Return the fee rate in sat/vB, without units, as a string. */
71 std::string SatsToString() const;
72 friend CFeeRate operator*(const CFeeRate& f, int a) { return CFeeRate(a * f.nSatoshisPerK); }
73 friend CFeeRate operator*(int a, const CFeeRate& f) { return CFeeRate(a * f.nSatoshisPerK); }
74 friend CFeeRate operator/(const CFeeRate& f, int a) { return CFeeRate(f.nSatoshisPerK / a); }
75
76 SERIALIZE_METHODS(CFeeRate, obj) { READWRITE(obj.nSatoshisPerK); }
77 };
78
79 #endif // LIMENKA_POLICY_FEERATE_H
80