Bitcoin ABC 0.32.5
P2P Digital Currency
amount.h
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#ifndef BITCOIN_CONSENSUS_AMOUNT_H
8#define BITCOIN_CONSENSUS_AMOUNT_H
9
10#include <serialize.h>
11#include <util/overflow.h>
12
13#include <cstdlib>
14#include <optional>
15#include <ostream>
16#include <string>
17#include <type_traits>
18
19class UniValue;
20
21struct Amount {
22private:
23 int64_t amount;
24
25 explicit constexpr Amount(int64_t _amount) : amount(_amount) {}
26
27public:
28 constexpr Amount() noexcept : amount(0) {}
32 operator UniValue() const;
33
34 static constexpr Amount zero() noexcept { return Amount(0); }
35 static constexpr Amount satoshi() noexcept { return Amount(1); }
36
41 amount += a.amount;
42 return *this;
43 }
45 amount -= a.amount;
46 return *this;
47 }
48
52 friend constexpr bool operator==(const Amount a, const Amount b) {
53 return a.amount == b.amount;
54 }
55 friend constexpr bool operator!=(const Amount a, const Amount b) {
56 return !(a == b);
57 }
58
62 friend constexpr bool operator<(const Amount a, const Amount b) {
63 return a.amount < b.amount;
64 }
65 friend constexpr bool operator>(const Amount a, const Amount b) {
66 return b < a;
67 }
68 friend constexpr bool operator<=(const Amount a, const Amount b) {
69 return !(a > b);
70 }
71 friend constexpr bool operator>=(const Amount a, const Amount b) {
72 return !(a < b);
73 }
74
78 constexpr Amount operator-() const { return Amount(-amount); }
79
83 friend constexpr Amount operator+(const Amount a, const Amount b) {
84 return Amount(a.amount + b.amount);
85 }
86 friend constexpr Amount operator-(const Amount a, const Amount b) {
87 return a + -b;
88 }
90 [[nodiscard]] std::optional<Amount>
91 CheckedAdd(const Amount &other) const noexcept;
92
96 friend constexpr Amount operator*(const int64_t a, const Amount b) {
97 return Amount(a * b.amount);
98 }
99 friend constexpr Amount operator*(const int a, const Amount b) {
100 return Amount(a * b.amount);
101 }
102
106 constexpr int64_t operator/(const Amount b) const {
107 return amount / b.amount;
108 }
109 constexpr Amount operator/(const int64_t b) const {
110 return Amount(amount / b);
111 }
112 constexpr Amount operator/(const int b) const { return Amount(amount / b); }
113 Amount &operator/=(const int64_t n) {
114 amount /= n;
115 return *this;
116 }
117
121 constexpr Amount operator%(const Amount b) const {
122 return Amount(amount % b.amount);
123 }
124 constexpr Amount operator%(const int64_t b) const {
125 return Amount(amount % b);
126 }
127 constexpr Amount operator%(const int b) const { return Amount(amount % b); }
128
133 friend constexpr Amount operator*(const double a, const Amount b) = delete;
134 constexpr Amount operator/(const double b) const = delete;
135 constexpr Amount operator%(const double b) const = delete;
136
137 // ostream support
138 friend std::ostream &operator<<(std::ostream &stream, const Amount &ca) {
139 return stream << ca.amount;
140 }
141
142 std::string ToString() const;
143
144 // serialization support
145 SERIALIZE_METHODS(Amount, obj) { READWRITE(obj.amount); }
146};
147
148static constexpr Amount SATOSHI = Amount::satoshi();
149static constexpr Amount COIN = 100000000 * SATOSHI;
150
151struct Currency {
154 uint8_t decimals;
155 std::string ticker;
156
157 static const Currency &get();
158};
159
170static constexpr Amount MAX_MONEY = 21000000 * COIN;
171inline bool MoneyRange(const Amount nValue) {
172 return nValue >= Amount::zero() && nValue <= MAX_MONEY;
173}
174
175#endif // BITCOIN_CONSENSUS_AMOUNT_H
bool MoneyRange(const Amount nValue)
Definition: amount.h:171
static constexpr Amount SATOSHI
Definition: amount.h:148
static constexpr Amount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:170
static constexpr Amount COIN
Definition: amount.h:149
#define READWRITE(...)
Definition: serialize.h:189
Definition: amount.h:21
Amount & operator/=(const int64_t n)
Definition: amount.h:113
friend constexpr bool operator>=(const Amount a, const Amount b)
Definition: amount.h:71
constexpr Amount operator%(const double b) const =delete
friend constexpr bool operator<(const Amount a, const Amount b)
Comparison.
Definition: amount.h:62
SERIALIZE_METHODS(Amount, obj)
Definition: amount.h:145
static constexpr Amount zero() noexcept
Definition: amount.h:34
constexpr Amount operator%(const int64_t b) const
Definition: amount.h:124
friend constexpr Amount operator*(const double a, const Amount b)=delete
Do not implement double ops to get an error with double and ensure casting to integer is explicit.
constexpr int64_t operator/(const Amount b) const
Division.
Definition: amount.h:106
friend constexpr bool operator!=(const Amount a, const Amount b)
Definition: amount.h:55
friend constexpr bool operator>(const Amount a, const Amount b)
Definition: amount.h:65
int64_t amount
Definition: amount.h:23
friend std::ostream & operator<<(std::ostream &stream, const Amount &ca)
Definition: amount.h:138
constexpr Amount operator/(const double b) const =delete
Amount & operator-=(const Amount a)
Definition: amount.h:44
friend constexpr Amount operator-(const Amount a, const Amount b)
Definition: amount.h:86
friend constexpr Amount operator+(const Amount a, const Amount b)
Addition and subtraction.
Definition: amount.h:83
constexpr Amount operator-() const
Unary minus.
Definition: amount.h:78
constexpr Amount operator/(const int64_t b) const
Definition: amount.h:109
Amount & operator+=(const Amount a)
Implement standard operators.
Definition: amount.h:40
constexpr Amount operator%(const Amount b) const
Modulus.
Definition: amount.h:121
friend constexpr Amount operator*(const int a, const Amount b)
Definition: amount.h:99
constexpr Amount() noexcept
Definition: amount.h:28
std::string ToString() const
Definition: amount.cpp:22
constexpr Amount operator%(const int b) const
Definition: amount.h:127
constexpr Amount(int64_t _amount)
Definition: amount.h:25
static constexpr Amount satoshi() noexcept
Definition: amount.h:35
constexpr Amount operator/(const int b) const
Definition: amount.h:112
friend constexpr Amount operator*(const int64_t a, const Amount b)
Multiplication.
Definition: amount.h:96
std::optional< Amount > CheckedAdd(const Amount &other) const noexcept
Amount addition with integer overflow check.
Definition: amount.cpp:40
friend constexpr bool operator<=(const Amount a, const Amount b)
Definition: amount.h:68
friend constexpr bool operator==(const Amount a, const Amount b)
Equality.
Definition: amount.h:52
Amount baseunit
Definition: amount.h:152
static const Currency & get()
Definition: amount.cpp:18
std::string ticker
Definition: amount.h:155
uint8_t decimals
Definition: amount.h:154
Amount subunit
Definition: amount.h:153