Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
feerate.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 The Bitcoin Core developers
3// Copyright (c) 2017-2019 The Bitcoin developers
4// Distributed under the MIT software license, see the accompanying
5// file COPYING or http://www.opensource.org/licenses/mit-license.php.
6
7#include <feerate.h>
8
9#include <tinyformat.h>
10
12 assert(nBytes_ <= uint64_t(std::numeric_limits<int64_t>::max()));
13 int64_t nSize = int64_t(nBytes_);
14
15 if (nSize > 0) {
16 nSatoshisPerK = 1000 * nFeePaid / nSize;
17 } else {
19 }
20}
21
22template <bool ceil>
23static Amount GetFee(size_t nBytes_, Amount nSatoshisPerK) {
24 assert(nBytes_ <= uint64_t(std::numeric_limits<int64_t>::max()));
25 int64_t nSize = int64_t(nBytes_);
26
27 // Ensure fee is rounded up when truncated if ceil is true.
28 Amount nFee = Amount::zero();
29 if (ceil) {
30 nFee = Amount(nSize * nSatoshisPerK % 1000 > Amount::zero()
31 ? nSize * nSatoshisPerK / 1000 + SATOSHI
32 : nSize * nSatoshisPerK / 1000);
33 } else {
34 nFee = nSize * nSatoshisPerK / 1000;
35 }
36
37 if (nFee == Amount::zero() && nSize != 0) {
38 if (nSatoshisPerK > Amount::zero()) {
39 nFee = SATOSHI;
40 }
41 if (nSatoshisPerK < Amount::zero()) {
42 nFee = -SATOSHI;
43 }
44 }
45
46 return nFee;
47}
48
50 return ::GetFee<false>(nBytes, nSatoshisPerK);
51}
52
54 return ::GetFee<true>(nBytes, nSatoshisPerK);
55}
56
57std::string CFeeRate::ToString() const {
58 const auto currency = Currency::get();
59 return strprintf("%d.%0*d %s/kB", nSatoshisPerK / currency.baseunit,
60 currency.decimals,
61 (nSatoshisPerK % currency.baseunit) / currency.subunit,
62 currency.ticker);
63}
static constexpr Amount SATOSHI
Definition amount.h:143
constexpr CFeeRate()
Fee rate of 0 satoshis per kB.
Definition feerate.h:30
std::string ToString() const
Definition feerate.cpp:57
Amount GetFee(size_t nBytes) const
Return the fee in satoshis for the given size in bytes.
Definition feerate.cpp:49
Amount nSatoshisPerK
Definition feerate.h:24
Amount GetFeeCeiling(size_t nBytes) const
Return the ceiling of a fee calculation in satoshis for the given size in bytes.
Definition feerate.cpp:53
static Amount GetFee(size_t nBytes_, Amount nSatoshisPerK)
Definition feerate.cpp:23
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
static constexpr Amount zero() noexcept
Definition amount.h:32
static const Currency & get()
Definition amount.cpp:18
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
assert(!tx.IsCoinBase())