2014-10-22 21:05:11 -03:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2020-12-31 05:48:25 -03:00
|
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
2014-10-22 21:05:11 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <policy/feerate.h>
|
2014-10-22 21:05:11 -03:00
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <tinyformat.h>
|
2014-10-22 21:05:11 -03:00
|
|
|
|
2016-04-03 08:44:01 -03:00
|
|
|
CFeeRate::CFeeRate(const CAmount& nFeePaid, size_t nBytes_)
|
2014-10-22 21:05:11 -03:00
|
|
|
{
|
2016-04-03 08:44:01 -03:00
|
|
|
assert(nBytes_ <= uint64_t(std::numeric_limits<int64_t>::max()));
|
|
|
|
int64_t nSize = int64_t(nBytes_);
|
|
|
|
|
2014-10-22 21:05:11 -03:00
|
|
|
if (nSize > 0)
|
2016-04-03 08:44:01 -03:00
|
|
|
nSatoshisPerK = nFeePaid * 1000 / nSize;
|
2014-10-22 21:05:11 -03:00
|
|
|
else
|
|
|
|
nSatoshisPerK = 0;
|
|
|
|
}
|
|
|
|
|
2016-04-03 08:44:01 -03:00
|
|
|
CAmount CFeeRate::GetFee(size_t nBytes_) const
|
2014-10-22 21:05:11 -03:00
|
|
|
{
|
2016-04-03 08:44:01 -03:00
|
|
|
assert(nBytes_ <= uint64_t(std::numeric_limits<int64_t>::max()));
|
|
|
|
int64_t nSize = int64_t(nBytes_);
|
|
|
|
|
2016-03-09 08:54:55 -03:00
|
|
|
CAmount nFee = nSatoshisPerK * nSize / 1000;
|
2014-10-22 21:05:11 -03:00
|
|
|
|
2016-04-03 08:44:01 -03:00
|
|
|
if (nFee == 0 && nSize != 0) {
|
|
|
|
if (nSatoshisPerK > 0)
|
|
|
|
nFee = CAmount(1);
|
|
|
|
if (nSatoshisPerK < 0)
|
|
|
|
nFee = CAmount(-1);
|
|
|
|
}
|
2014-10-22 21:05:11 -03:00
|
|
|
|
|
|
|
return nFee;
|
|
|
|
}
|
|
|
|
|
2020-03-03 23:28:08 -03:00
|
|
|
std::string CFeeRate::ToString(const FeeEstimateMode& fee_estimate_mode) const
|
2014-10-22 21:05:11 -03:00
|
|
|
{
|
2020-03-03 23:28:08 -03:00
|
|
|
switch (fee_estimate_mode) {
|
2020-11-08 15:57:35 -03: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-03 23:28:08 -03:00
|
|
|
}
|
2014-10-22 21:05:11 -03:00
|
|
|
}
|