2016-03-31 18:30:17 -03:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2021-12-30 14:36:57 -03:00
|
|
|
// Copyright (c) 2009-2021 The Bitcoin Core developers
|
2016-03-31 18:30:17 -03: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-10 23:29:00 -03:00
|
|
|
#include <consensus/amount.h>
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <serialize.h>
|
2016-03-31 18:30:17 -03:00
|
|
|
|
2022-05-31 07:30:23 -04:00
|
|
|
|
|
|
|
#include <cstdint>
|
2016-03-31 18:30:17 -03:00
|
|
|
#include <string>
|
2022-05-31 07:30:23 -04:00
|
|
|
#include <type_traits>
|
2016-03-31 18:30:17 -03:00
|
|
|
|
2019-08-06 22:32:51 -04:00
|
|
|
const std::string CURRENCY_UNIT = "BTC"; // One formatted unit
|
|
|
|
const std::string CURRENCY_ATOM = "sat"; // One indivisible minimum value unit
|
2016-03-31 18:30:17 -03:00
|
|
|
|
2020-03-03 23:26:06 -03: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 15:57:35 -03:00
|
|
|
BTC_KVB, //!< Use BTC/kvB fee rate unit
|
|
|
|
SAT_VB, //!< Use sat/vB fee rate unit
|
2020-03-03 23:26:06 -03:00
|
|
|
};
|
|
|
|
|
2016-03-31 18:30:17 -03:00
|
|
|
/**
|
2022-02-09 17:41:53 -03:00
|
|
|
* Fee rate in satoshis per kilovirtualbyte: CAmount / kvB
|
2016-03-31 18:30:17 -03:00
|
|
|
*/
|
|
|
|
class CFeeRate
|
|
|
|
{
|
|
|
|
private:
|
2022-02-09 17:41:53 -03:00
|
|
|
/** Fee rate in sat/kvB (satoshis per 1000 virtualbytes) */
|
|
|
|
CAmount nSatoshisPerK;
|
2017-09-11 16:47:09 -03:00
|
|
|
|
2016-03-31 18:30:17 -03:00
|
|
|
public:
|
2022-02-09 17:41:53 -03:00
|
|
|
/** Fee rate of 0 satoshis per kvB */
|
2016-03-31 18:30:17 -03:00
|
|
|
CFeeRate() : nSatoshisPerK(0) { }
|
2017-09-11 16:47:09 -03:00
|
|
|
template<typename I>
|
2019-08-20 09:58:35 -04:00
|
|
|
explicit CFeeRate(const I _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK) {
|
2017-09-11 16:47:09 -03: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 17:41:53 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a fee rate from a fee in satoshis and a vsize in vB.
|
2020-11-10 15:32:50 -03:00
|
|
|
*
|
2022-02-09 17:41:53 -03: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 15:32:50 -03:00
|
|
|
*/
|
2021-05-04 04:14:12 -04:00
|
|
|
CFeeRate(const CAmount& nFeePaid, uint32_t num_bytes);
|
2022-02-09 17:41:53 -03:00
|
|
|
|
2016-03-31 18:30:17 -03:00
|
|
|
/**
|
2022-02-09 17:41:53 -03: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 18:30:17 -03:00
|
|
|
*/
|
2021-05-04 04:14:12 -04:00
|
|
|
CAmount GetFee(uint32_t num_bytes) const;
|
2022-02-09 17:41:53 -03:00
|
|
|
|
2016-03-31 18:30:17 -03:00
|
|
|
/**
|
2022-02-09 17:41:53 -03:00
|
|
|
* Return the fee in satoshis for a vsize of 1000 vbytes
|
2016-03-31 18:30:17 -03: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 18:30:17 -03:00
|
|
|
CFeeRate& operator+=(const CFeeRate& a) { nSatoshisPerK += a.nSatoshisPerK; return *this; }
|
2020-11-08 15:57:35 -03:00
|
|
|
std::string ToString(const FeeEstimateMode& fee_estimate_mode = FeeEstimateMode::BTC_KVB) const;
|
2016-03-31 18:30:17 -03:00
|
|
|
|
2020-03-11 13:35:50 -03:00
|
|
|
SERIALIZE_METHODS(CFeeRate, obj) { READWRITE(obj.nSatoshisPerK); }
|
2016-03-31 18:30:17 -03:00
|
|
|
};
|
|
|
|
|
2021-11-12 07:19:44 -03:00
|
|
|
#endif // BITCOIN_POLICY_FEERATE_H
|