|
6 | 6 | #ifndef BITCOIN_AMOUNT_H
|
7 | 7 | #define BITCOIN_AMOUNT_H
|
8 | 8 |
|
9 |
| -#include <stdint.h> |
| 9 | +#include "serialize.h" |
| 10 | + |
| 11 | +#include <stdlib.h> |
| 12 | +#include <string> |
10 | 13 |
|
11 | 14 | typedef int64_t CAmount;
|
12 | 15 |
|
| 16 | +static const CAmount COIN = 100000000; |
| 17 | +static const CAmount CENT = 1000000; |
| 18 | + |
| 19 | +/** No amount larger than this (in satoshi) is valid */ |
| 20 | +static const CAmount MAX_MONEY = 21000000 * COIN; |
| 21 | +inline bool MoneyRange(const CAmount& nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); } |
| 22 | + |
| 23 | +/** Type-safe wrapper class to for fee rates |
| 24 | + * (how much to pay based on transaction size) |
| 25 | + */ |
| 26 | +class CFeeRate |
| 27 | +{ |
| 28 | +private: |
| 29 | + CAmount nSatoshisPerK; // unit is satoshis-per-1,000-bytes |
| 30 | +public: |
| 31 | + CFeeRate() : nSatoshisPerK(0) { } |
| 32 | + explicit CFeeRate(const CAmount& _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK) { } |
| 33 | + CFeeRate(const CAmount& nFeePaid, size_t nSize); |
| 34 | + CFeeRate(const CFeeRate& other) { nSatoshisPerK = other.nSatoshisPerK; } |
| 35 | + |
| 36 | + CAmount GetFee(size_t size) const; // unit returned is satoshis |
| 37 | + CAmount GetFeePerK() const { return GetFee(1000); } // satoshis-per-1000-bytes |
| 38 | + |
| 39 | + friend bool operator<(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK < b.nSatoshisPerK; } |
| 40 | + friend bool operator>(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK > b.nSatoshisPerK; } |
| 41 | + friend bool operator==(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK == b.nSatoshisPerK; } |
| 42 | + friend bool operator<=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK <= b.nSatoshisPerK; } |
| 43 | + friend bool operator>=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK >= b.nSatoshisPerK; } |
| 44 | + std::string ToString() const; |
| 45 | + |
| 46 | + ADD_SERIALIZE_METHODS; |
| 47 | + |
| 48 | + template <typename Stream, typename Operation> |
| 49 | + inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { |
| 50 | + READWRITE(nSatoshisPerK); |
| 51 | + } |
| 52 | +}; |
| 53 | + |
13 | 54 | #endif // BITCOIN_AMOUNT_H
|
0 commit comments