Bitcoin ABC 0.33.10
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 <algorithm>
14#include <cstdlib>
15#include <optional>
16#include <ostream>
17#include <string>
18#include <string_view>
19#include <type_traits>
20
21class UniValue;
22
23struct Amount {
24private:
25 int64_t amount;
26
27 explicit constexpr Amount(int64_t _amount) : amount(_amount) {}
28
29public:
30 constexpr Amount() noexcept : amount(0) {}
34 operator UniValue() const;
35
36 static constexpr Amount zero() noexcept { return Amount(0); }
37 static constexpr Amount satoshi() noexcept { return Amount(1); }
38
43 amount += a.amount;
44 return *this;
45 }
47 amount -= a.amount;
48 return *this;
49 }
50
54 friend constexpr bool operator==(const Amount a, const Amount b) {
55 return a.amount == b.amount;
56 }
57 friend constexpr bool operator!=(const Amount a, const Amount b) {
58 return !(a == b);
59 }
60
64 friend constexpr bool operator<(const Amount a, const Amount b) {
65 return a.amount < b.amount;
66 }
67 friend constexpr bool operator>(const Amount a, const Amount b) {
68 return b < a;
69 }
70 friend constexpr bool operator<=(const Amount a, const Amount b) {
71 return !(a > b);
72 }
73 friend constexpr bool operator>=(const Amount a, const Amount b) {
74 return !(a < b);
75 }
76
80 constexpr Amount operator-() const { return Amount(-amount); }
81
85 friend constexpr Amount operator+(const Amount a, const Amount b) {
86 return Amount(a.amount + b.amount);
87 }
88 friend constexpr Amount operator-(const Amount a, const Amount b) {
89 return a + -b;
90 }
92 [[nodiscard]] std::optional<Amount>
93 CheckedAdd(const Amount &other) const noexcept;
94
96 void SaturatingAdd(const Amount &other) noexcept;
97
101 friend constexpr Amount operator*(const int64_t a, const Amount b) {
102 return Amount(a * b.amount);
103 }
104 friend constexpr Amount operator*(const int a, const Amount b) {
105 return Amount(a * b.amount);
106 }
107
111 constexpr int64_t operator/(const Amount b) const {
112 return amount / b.amount;
113 }
114 constexpr Amount operator/(const int64_t b) const {
115 return Amount(amount / b);
116 }
117 constexpr Amount operator/(const int b) const { return Amount(amount / b); }
118 Amount &operator/=(const int64_t n) {
119 amount /= n;
120 return *this;
121 }
122
126 constexpr Amount operator%(const Amount b) const {
127 return Amount(amount % b.amount);
128 }
129 constexpr Amount operator%(const int64_t b) const {
130 return Amount(amount % b);
131 }
132 constexpr Amount operator%(const int b) const { return Amount(amount % b); }
133
138 friend constexpr Amount operator*(const double a, const Amount b) = delete;
139 constexpr Amount operator/(const double b) const = delete;
140 constexpr Amount operator%(const double b) const = delete;
141
142 // ostream support
143 friend std::ostream &operator<<(std::ostream &stream, const Amount &ca) {
144 return stream << ca.amount;
145 }
146
147 std::string ToString() const;
148
149 // serialization support
150 SERIALIZE_METHODS(Amount, obj) { READWRITE(obj.amount); }
151};
152
153static constexpr Amount SATOSHI = Amount::satoshi();
154static constexpr Amount COIN = 100000000 * SATOSHI;
155
156struct Currency {
159 uint8_t decimals;
160 std::string_view ticker;
161
162 static const Currency &get();
163 static std::string getTicker() { return std::string{get().ticker}; }
164};
165
176static constexpr Amount MAX_MONEY = 21000000 * COIN;
177inline bool MoneyRange(const Amount nValue) {
178 return nValue >= Amount::zero() && nValue <= MAX_MONEY;
179}
180
181inline Amount MoneyClamp(const Amount nValue) {
182 return std::clamp(nValue, Amount::zero(), MAX_MONEY);
183}
184
185#endif // BITCOIN_CONSENSUS_AMOUNT_H
bool MoneyRange(const Amount nValue)
Definition: amount.h:177
static constexpr Amount SATOSHI
Definition: amount.h:153
static constexpr Amount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:176
Amount MoneyClamp(const Amount nValue)
Definition: amount.h:181
static constexpr Amount COIN
Definition: amount.h:154
#define READWRITE(...)
Definition: serialize.h:176
Definition: amount.h:23
Amount & operator/=(const int64_t n)
Definition: amount.h:118
void SaturatingAdd(const Amount &other) noexcept
Amount addition with integer saturation.
Definition: amount.cpp:57
friend constexpr bool operator>=(const Amount a, const Amount b)
Definition: amount.h:73
constexpr Amount operator%(const double b) const =delete
friend constexpr bool operator<(const Amount a, const Amount b)
Comparison.
Definition: amount.h:64
SERIALIZE_METHODS(Amount, obj)
Definition: amount.h:150
static constexpr Amount zero() noexcept
Definition: amount.h:36
constexpr Amount operator%(const int64_t b) const
Definition: amount.h:129
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:111
friend constexpr bool operator!=(const Amount a, const Amount b)
Definition: amount.h:57
friend constexpr bool operator>(const Amount a, const Amount b)
Definition: amount.h:67
int64_t amount
Definition: amount.h:25
friend std::ostream & operator<<(std::ostream &stream, const Amount &ca)
Definition: amount.h:143
constexpr Amount operator/(const double b) const =delete
Amount & operator-=(const Amount a)
Definition: amount.h:46
friend constexpr Amount operator-(const Amount a, const Amount b)
Definition: amount.h:88
friend constexpr Amount operator+(const Amount a, const Amount b)
Addition and subtraction.
Definition: amount.h:85
constexpr Amount operator-() const
Unary minus.
Definition: amount.h:80
constexpr Amount operator/(const int64_t b) const
Definition: amount.h:114
Amount & operator+=(const Amount a)
Implement standard operators.
Definition: amount.h:42
constexpr Amount operator%(const Amount b) const
Modulus.
Definition: amount.h:126
friend constexpr Amount operator*(const int a, const Amount b)
Definition: amount.h:104
constexpr Amount() noexcept
Definition: amount.h:30
std::string ToString() const
Definition: amount.cpp:29
constexpr Amount operator%(const int b) const
Definition: amount.h:132
constexpr Amount(int64_t _amount)
Definition: amount.h:27
static constexpr Amount satoshi() noexcept
Definition: amount.h:37
constexpr Amount operator/(const int b) const
Definition: amount.h:117
friend constexpr Amount operator*(const int64_t a, const Amount b)
Multiplication.
Definition: amount.h:101
std::optional< Amount > CheckedAdd(const Amount &other) const noexcept
Amount addition with integer overflow check.
Definition: amount.cpp:49
friend constexpr bool operator<=(const Amount a, const Amount b)
Definition: amount.h:70
friend constexpr bool operator==(const Amount a, const Amount b)
Equality.
Definition: amount.h:54
Amount baseunit
Definition: amount.h:157
static const Currency & get()
Definition: amount.cpp:25
uint8_t decimals
Definition: amount.h:159
Amount subunit
Definition: amount.h:158
static std::string getTicker()
Definition: amount.h:163
std::string_view ticker
Definition: amount.h:160