1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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.
6 #ifndef BITCOIN_AMOUNT_H
7 #define BITCOIN_AMOUNT_H
14 /** Amount in satoshis (Can be negative) */
15 typedef int64_t CAmount
;
17 static const CAmount COIN
= 100000000;
18 static const CAmount CENT
= 1000000;
20 extern const std::string CURRENCY_UNIT
;
22 /** No amount larger than this (in satoshi) is valid.
24 * Note that this constant is *not* the total money supply, which in Bitcoin
25 * currently happens to be less than 21,000,000 BTC for various reasons, but
26 * rather a sanity check. As this sanity check is used by consensus-critical
27 * validation code, the exact value of the MAX_MONEY constant is consensus
28 * critical; in unusual circumstances like a(nother) overflow bug that allowed
29 * for the creation of coins out of thin air modification could lead to a fork.
31 static const CAmount MAX_MONEY
= 21000000 * COIN
;
32 inline bool MoneyRange(const CAmount
& nValue
) { return (nValue
>= 0 && nValue
<= MAX_MONEY
); }
35 * Fee rate in satoshis per kilobyte: CAmount / kB
40 CAmount nSatoshisPerK
; // unit is satoshis-per-1,000-bytes
42 /** Fee rate of 0 satoshis per kB */
43 CFeeRate() : nSatoshisPerK(0) { }
44 explicit CFeeRate(const CAmount
& _nSatoshisPerK
): nSatoshisPerK(_nSatoshisPerK
) { }
45 /** Constructor for a fee rate in satoshis per kB. The size in bytes must not exceed (2^63 - 1)*/
46 CFeeRate(const CAmount
& nFeePaid
, size_t nBytes
);
47 CFeeRate(const CFeeRate
& other
) { nSatoshisPerK
= other
.nSatoshisPerK
; }
49 * Return the fee in satoshis for the given size in bytes.
51 CAmount
GetFee(size_t nBytes
) const;
53 * Return the fee in satoshis for a size of 1000 bytes
55 CAmount
GetFeePerK() const { return GetFee(1000); }
56 friend bool operator<(const CFeeRate
& a
, const CFeeRate
& b
) { return a
.nSatoshisPerK
< b
.nSatoshisPerK
; }
57 friend bool operator>(const CFeeRate
& a
, const CFeeRate
& b
) { return a
.nSatoshisPerK
> b
.nSatoshisPerK
; }
58 friend bool operator==(const CFeeRate
& a
, const CFeeRate
& b
) { return a
.nSatoshisPerK
== b
.nSatoshisPerK
; }
59 friend bool operator<=(const CFeeRate
& a
, const CFeeRate
& b
) { return a
.nSatoshisPerK
<= b
.nSatoshisPerK
; }
60 friend bool operator>=(const CFeeRate
& a
, const CFeeRate
& b
) { return a
.nSatoshisPerK
>= b
.nSatoshisPerK
; }
61 CFeeRate
& operator+=(const CFeeRate
& a
) { nSatoshisPerK
+= a
.nSatoshisPerK
; return *this; }
62 std::string
ToString() const;
64 ADD_SERIALIZE_METHODS
;
66 template <typename Stream
, typename Operation
>
67 inline void SerializationOp(Stream
& s
, Operation ser_action
, int nType
, int nVersion
) {
68 READWRITE(nSatoshisPerK
);
72 #endif // BITCOIN_AMOUNT_H