2016-03-31 23:30:17 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2021-12-30 19:36:57 +02:00
|
|
|
// Copyright (c) 2009-2021 The Bitcoin Core developers
|
2016-03-31 23:30:17 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_POLICY_FEERATE_H
|
|
|
|
#define BITCOIN_POLICY_FEERATE_H
|
|
|
|
|
2021-09-11 10:29:00 +08:00
|
|
|
#include <consensus/amount.h>
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <serialize.h>
|
2016-03-31 23:30:17 +02:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2019-08-07 11:32:51 +09:00
|
|
|
const std::string CURRENCY_UNIT = "BTC"; // One formatted unit
|
|
|
|
const std::string CURRENCY_ATOM = "sat"; // One indivisible minimum value unit
|
2016-03-31 23:30:17 +02:00
|
|
|
|
2020-03-04 11:26:06 +09:00
|
|
|
/* Used to determine type of fee estimation requested */
|
|
|
|
enum class FeeEstimateMode {
|
|
|
|
UNSET, //!< Use default settings based on other criteria
|
|
|
|
ECONOMICAL, //!< Force estimateSmartFee to use non-conservative estimates
|
|
|
|
CONSERVATIVE, //!< Force estimateSmartFee to use conservative estimates
|
2020-11-08 19:57:35 +01:00
|
|
|
BTC_KVB, //!< Use BTC/kvB fee rate unit
|
|
|
|
SAT_VB, //!< Use sat/vB fee rate unit
|
2020-03-04 11:26:06 +09:00
|
|
|
};
|
|
|
|
|
2016-03-31 23:30:17 +02:00
|
|
|
/**
|
2022-02-09 15:41:53 -05:00
|
|
|
* Fee rate in satoshis per kilovirtualbyte: CAmount / kvB
|
2016-03-31 23:30:17 +02:00
|
|
|
*/
|
|
|
|
class CFeeRate
|
|
|
|
{
|
|
|
|
private:
|
2022-02-09 15:41:53 -05:00
|
|
|
/** Fee rate in sat/kvB (satoshis per 1000 virtualbytes) */
|
|
|
|
CAmount nSatoshisPerK;
|
2017-09-11 15:47:09 -04:00
|
|
|
|
2016-03-31 23:30:17 +02:00
|
|
|
public:
|
2022-02-09 15:41:53 -05:00
|
|
|
/** Fee rate of 0 satoshis per kvB */
|
2016-03-31 23:30:17 +02:00
|
|
|
CFeeRate() : nSatoshisPerK(0) { }
|
2017-09-11 15:47:09 -04:00
|
|
|
template<typename I>
|
2019-08-20 09:58:35 -04:00
|
|
|
explicit CFeeRate(const I _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK) {
|
2017-09-11 15:47:09 -04:00
|
|
|
// We've previously had bugs creep in from silent double->int conversion...
|
|
|
|
static_assert(std::is_integral<I>::value, "CFeeRate should be used without floats");
|
|
|
|
}
|
2022-02-09 15:41:53 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a fee rate from a fee in satoshis and a vsize in vB.
|
2020-11-10 19:32:50 +01:00
|
|
|
*
|
2022-02-09 15:41:53 -05:00
|
|
|
* param@[in] nFeePaid The fee paid by a transaction, in satoshis
|
|
|
|
* param@[in] num_bytes The vsize of a transaction, in vbytes
|
2020-11-10 19:32:50 +01:00
|
|
|
*/
|
2021-05-04 10:14:12 +02:00
|
|
|
CFeeRate(const CAmount& nFeePaid, uint32_t num_bytes);
|
2022-02-09 15:41:53 -05:00
|
|
|
|
2016-03-31 23:30:17 +02:00
|
|
|
/**
|
2022-02-09 15:41:53 -05:00
|
|
|
* Return the fee in satoshis for the given vsize in vbytes.
|
|
|
|
* If the calculated fee would have fractional satoshis, then the
|
|
|
|
* returned fee will always be rounded up to the nearest satoshi.
|
2016-03-31 23:30:17 +02:00
|
|
|
*/
|
2021-05-04 10:14:12 +02:00
|
|
|
CAmount GetFee(uint32_t num_bytes) const;
|
2022-02-09 15:41:53 -05:00
|
|
|
|
2016-03-31 23:30:17 +02:00
|
|
|
/**
|
2022-02-09 15:41:53 -05:00
|
|
|
* Return the fee in satoshis for a vsize of 1000 vbytes
|
2016-03-31 23:30:17 +02:00
|
|
|
*/
|
|
|
|
CAmount GetFeePerK() const { return GetFee(1000); }
|
|
|
|
friend bool operator<(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK < b.nSatoshisPerK; }
|
|
|
|
friend bool operator>(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK > b.nSatoshisPerK; }
|
|
|
|
friend bool operator==(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK == b.nSatoshisPerK; }
|
|
|
|
friend bool operator<=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK <= b.nSatoshisPerK; }
|
|
|
|
friend bool operator>=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK >= b.nSatoshisPerK; }
|
2017-06-06 13:08:27 -04:00
|
|
|
friend bool operator!=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK != b.nSatoshisPerK; }
|
2016-03-31 23:30:17 +02:00
|
|
|
CFeeRate& operator+=(const CFeeRate& a) { nSatoshisPerK += a.nSatoshisPerK; return *this; }
|
2020-11-08 19:57:35 +01:00
|
|
|
std::string ToString(const FeeEstimateMode& fee_estimate_mode = FeeEstimateMode::BTC_KVB) const;
|
2016-03-31 23:30:17 +02:00
|
|
|
|
2020-03-11 09:35:50 -07:00
|
|
|
SERIALIZE_METHODS(CFeeRate, obj) { READWRITE(obj.nSatoshisPerK); }
|
2016-03-31 23:30:17 +02:00
|
|
|
};
|
|
|
|
|
2021-11-12 11:19:44 +01:00
|
|
|
#endif // BITCOIN_POLICY_FEERATE_H
|