forked from Bitcoin-ABC/bitcoin-abc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamount.h
173 lines (148 loc) · 4.81 KB
/
amount.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2016 The Bitcoin Core developers
// Copyright (c) 2017-2018 The Bitcoin developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_AMOUNT_H
#define BITCOIN_AMOUNT_H
#include <serialize.h>
#include <cstdlib>
#include <ostream>
#include <string>
#include <type_traits>
struct Amount {
private:
int64_t amount;
explicit constexpr Amount(int64_t _amount) : amount(_amount) {}
public:
constexpr Amount() : amount(0) {}
constexpr Amount(const Amount &other) : amount(other.amount) {}
/**
* Assignement operator.
*/
constexpr Amount &operator=(const Amount &other) {
amount = other.amount;
return *this;
}
static constexpr Amount zero() { return Amount(0); }
static constexpr Amount satoshi() { return Amount(1); }
/**
* Implement standard operators
*/
Amount &operator+=(const Amount a) {
amount += a.amount;
return *this;
}
Amount &operator-=(const Amount a) {
amount -= a.amount;
return *this;
}
/**
* Equality
*/
friend constexpr bool operator==(const Amount a, const Amount b) {
return a.amount == b.amount;
}
friend constexpr bool operator!=(const Amount a, const Amount b) {
return !(a == b);
}
/**
* Comparison
*/
friend constexpr bool operator<(const Amount a, const Amount b) {
return a.amount < b.amount;
}
friend constexpr bool operator>(const Amount a, const Amount b) {
return b < a;
}
friend constexpr bool operator<=(const Amount a, const Amount b) {
return !(a > b);
}
friend constexpr bool operator>=(const Amount a, const Amount b) {
return !(a < b);
}
/**
* Unary minus
*/
constexpr Amount operator-() const { return Amount(-amount); }
/**
* Addition and subtraction.
*/
friend constexpr Amount operator+(const Amount a, const Amount b) {
return Amount(a.amount + b.amount);
}
friend constexpr Amount operator-(const Amount a, const Amount b) {
return a + -b;
}
/**
* Multiplication
*/
friend constexpr Amount operator*(const int64_t a, const Amount b) {
return Amount(a * b.amount);
}
friend constexpr Amount operator*(const int a, const Amount b) {
return Amount(a * b.amount);
}
/**
* Division
*/
constexpr int64_t operator/(const Amount b) const {
return amount / b.amount;
}
constexpr Amount operator/(const int64_t b) const {
return Amount(amount / b);
}
constexpr Amount operator/(const int b) const { return Amount(amount / b); }
Amount &operator/=(const int64_t n) {
amount /= n;
return *this;
}
/**
* Modulus
*/
constexpr Amount operator%(const Amount b) const {
return Amount(amount % b.amount);
}
constexpr Amount operator%(const int64_t b) const {
return Amount(amount % b);
}
constexpr Amount operator%(const int b) const { return Amount(amount % b); }
/**
* Do not implement double ops to get an error with double and ensure
* casting to integer is explicit.
*/
friend constexpr Amount operator*(const double a, const Amount b) = delete;
constexpr Amount operator/(const double b) const = delete;
constexpr Amount operator%(const double b) const = delete;
// ostream support
friend std::ostream &operator<<(std::ostream &stream, const Amount &ca) {
return stream << ca.amount;
}
std::string ToString() const;
// serialization support
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream &s, Operation ser_action) {
READWRITE(amount);
}
};
static constexpr Amount SATOSHI = Amount::satoshi();
static constexpr Amount CASH = 100 * SATOSHI;
static constexpr Amount COIN = 100000000 * SATOSHI;
static constexpr Amount CENT = COIN / 100;
extern const std::string CURRENCY_UNIT;
/**
* No amount larger than this (in satoshi) is valid.
*
* Note that this constant is *not* the total money supply, which in Bitcoin
* currently happens to be less than 21,000,000 BCH for various reasons, but
* rather a sanity check. As this sanity check is used by consensus-critical
* validation code, the exact value of the MAX_MONEY constant is consensus
* critical; in unusual circumstances like a(nother) overflow bug that allowed
* for the creation of coins out of thin air modification could lead to a fork.
*/
static const Amount MAX_MONEY = 21000000 * COIN;
inline bool MoneyRange(const Amount nValue) {
return nValue >= Amount::zero() && nValue <= MAX_MONEY;
}
#endif // BITCOIN_AMOUNT_H