Bitcoin Core 29.99.0
P2P Digital Currency
feerate.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-present 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#include <consensus/amount.h>
7#include <policy/feerate.h>
8#include <tinyformat.h>
9
10
11CFeeRate::CFeeRate(const CAmount& nFeePaid, int32_t virtual_bytes)
12{
13 if (virtual_bytes > 0) {
14 m_feerate = FeePerVSize(nFeePaid, virtual_bytes);
15 } else {
17 }
18}
19
20CAmount CFeeRate::GetFee(int32_t virtual_bytes) const
21{
22 Assume(virtual_bytes >= 0);
23 if (m_feerate.IsEmpty()) { return CAmount(0);}
24 CAmount nFee = CAmount(m_feerate.EvaluateFeeUp(virtual_bytes));
25 if (nFee == 0 && virtual_bytes != 0 && m_feerate.fee < 0) return CAmount(-1);
26 return nFee;
27}
28
29std::string CFeeRate::ToString(const FeeEstimateMode& fee_estimate_mode) const
30{
31 const CAmount feerate_per_kvb = GetFeePerK();
32 switch (fee_estimate_mode) {
33 case FeeEstimateMode::SAT_VB: return strprintf("%d.%03d %s/vB", feerate_per_kvb / 1000, feerate_per_kvb % 1000, CURRENCY_ATOM);
34 default: return strprintf("%d.%08d %s/kvB", feerate_per_kvb / COIN, feerate_per_kvb % COIN, CURRENCY_UNIT);
35 }
36}
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
#define Assume(val)
Assume is the identity function.
Definition: check.h:118
std::string ToString(const FeeEstimateMode &fee_estimate_mode=FeeEstimateMode::BTC_KVB) const
Definition: feerate.cpp:29
CFeeRate()=default
Fee rate of 0 satoshis per 0 vB.
FeePerVSize m_feerate
Fee rate in sats/vB (satoshis per N virtualbytes)
Definition: feerate.h:38
CAmount GetFeePerK() const
Return the fee in satoshis for a vsize of 1000 vbytes.
Definition: feerate.h:63
CAmount GetFee(int32_t virtual_bytes) const
Return the fee in satoshis for the given vsize in vbytes.
Definition: feerate.cpp:20
FeePerUnit< VSizeTag > FeePerVSize
Definition: feefrac.h:252
const std::string CURRENCY_ATOM
Definition: feerate.h:19
const std::string CURRENCY_UNIT
Definition: feerate.h:18
FeeEstimateMode
Definition: feerate.h:22
@ SAT_VB
Use sat/vB fee rate unit.
int64_t fee
Definition: feefrac.h:107
int64_t EvaluateFeeUp(int32_t at_size) const noexcept
Compute the fee for a given size at_size using this object's feerate, rounding up.
Definition: feefrac.h:223
bool IsEmpty() const noexcept
Check if this is empty (size and fee are 0).
Definition: feefrac.h:120
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172