2014-10-23 02:05:11 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2020-12-31 09:48:25 +01:00
|
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
2014-10-23 02:05:11 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <policy/feerate.h>
|
2014-10-23 02:05:11 +02:00
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <tinyformat.h>
|
2014-10-23 02:05:11 +02:00
|
|
|
|
2021-05-04 10:14:12 +02:00
|
|
|
CFeeRate::CFeeRate(const CAmount& nFeePaid, uint32_t num_bytes)
|
2014-10-23 02:05:11 +02:00
|
|
|
{
|
2021-05-04 10:14:12 +02:00
|
|
|
const int64_t nSize{num_bytes};
|
2016-04-03 13:44:01 +02:00
|
|
|
|
2021-05-04 10:14:12 +02:00
|
|
|
if (nSize > 0) {
|
2016-04-03 13:44:01 +02:00
|
|
|
nSatoshisPerK = nFeePaid * 1000 / nSize;
|
2021-05-04 10:14:12 +02:00
|
|
|
} else {
|
2014-10-23 02:05:11 +02:00
|
|
|
nSatoshisPerK = 0;
|
2021-05-04 10:14:12 +02:00
|
|
|
}
|
2014-10-23 02:05:11 +02:00
|
|
|
}
|
|
|
|
|
2021-05-04 10:14:12 +02:00
|
|
|
CAmount CFeeRate::GetFee(uint32_t num_bytes) const
|
2014-10-23 02:05:11 +02:00
|
|
|
{
|
2021-05-04 10:14:12 +02:00
|
|
|
const int64_t nSize{num_bytes};
|
2016-04-03 13:44:01 +02:00
|
|
|
|
2016-03-09 12:54:55 +01:00
|
|
|
CAmount nFee = nSatoshisPerK * nSize / 1000;
|
2014-10-23 02:05:11 +02:00
|
|
|
|
2016-04-03 13:44:01 +02:00
|
|
|
if (nFee == 0 && nSize != 0) {
|
2021-05-04 10:14:12 +02:00
|
|
|
if (nSatoshisPerK > 0) nFee = CAmount(1);
|
|
|
|
if (nSatoshisPerK < 0) nFee = CAmount(-1);
|
2016-04-03 13:44:01 +02:00
|
|
|
}
|
2014-10-23 02:05:11 +02:00
|
|
|
|
|
|
|
return nFee;
|
|
|
|
}
|
|
|
|
|
2020-03-04 11:28:08 +09:00
|
|
|
std::string CFeeRate::ToString(const FeeEstimateMode& fee_estimate_mode) const
|
2014-10-23 02:05:11 +02:00
|
|
|
{
|
2020-03-04 11:28:08 +09:00
|
|
|
switch (fee_estimate_mode) {
|
2020-11-08 19:57:35 +01:00
|
|
|
case FeeEstimateMode::SAT_VB: return strprintf("%d.%03d %s/vB", nSatoshisPerK / 1000, nSatoshisPerK % 1000, CURRENCY_ATOM);
|
|
|
|
default: return strprintf("%d.%08d %s/kvB", nSatoshisPerK / COIN, nSatoshisPerK % COIN, CURRENCY_UNIT);
|
2020-03-04 11:28:08 +09:00
|
|
|
}
|
2014-10-23 02:05:11 +02:00
|
|
|
}
|