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.
5 
6 #ifndef BITCOIN_AMOUNT_H
7 #define BITCOIN_AMOUNT_H
8 
9 #include "serialize.h"
10 
11 #include <stdlib.h>
12 #include <string>
13 
14 /** Amount in satoshis (Can be negative) */
15 typedef int64_t CAmount;
16 
17 static const CAmount COIN = 100000000;
18 static const CAmount CENT = 1000000;
19 
20 extern const std::string CURRENCY_UNIT;
21 
22 /** No amount larger than this (in satoshi) is valid.
23  *
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.
30  * */
31 static const CAmount MAX_MONEY = 200000000 * COIN;
MoneyRange(const CAmount & nValue)32 inline bool MoneyRange(const CAmount& nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }
33 
34 /**
35  * Fee rate in satoshis per kilobyte: CAmount / kB
36  */
37 class CFeeRate
38 {
39 private:
40     CAmount nSatoshisPerK; // unit is satoshis-per-1,000-bytes
41 public:
42     /** Fee rate of 0 satoshis per kB */
CFeeRate()43     CFeeRate() : nSatoshisPerK(0) { }
CFeeRate(const CAmount & _nSatoshisPerK)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);
CFeeRate(const CFeeRate & other)47     CFeeRate(const CFeeRate& other) { nSatoshisPerK = other.nSatoshisPerK; }
48     /**
49      * Return the fee in satoshis for the given size in bytes.
50      */
51     CAmount GetFee(size_t nBytes) const;
52     /**
53      * Return the fee in satoshis for a size of 1000 bytes
54      */
GetFeePerK()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;
63 
64     ADD_SERIALIZE_METHODS;
65 
66     template <typename Stream, typename Operation>
SerializationOp(Stream & s,Operation ser_action,int nType,int nVersion)67     inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
68         READWRITE(nSatoshisPerK);
69     }
70 };
71 
72 #endif //  BITCOIN_AMOUNT_H
73