-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcfdcore_amount.cpp
192 lines (173 loc) · 5.92 KB
/
cfdcore_amount.cpp
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2019 CryptoGarage
/**
* @file cfdcore_amount.cpp
*
* @brief Class to show amount.
*/
#include "cfdcore/cfdcore_amount.h"
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include "cfdcore/cfdcore_bytedata.h"
#include "cfdcore/cfdcore_exception.h"
#include "cfdcore/cfdcore_logger.h"
namespace cfd {
namespace core {
using logger::warn;
Amount::Amount() : amount_(0) {
// do nothing
}
Amount::Amount(int64_t amount) : amount_(amount), ignore_check_(false) {
CheckValidAmount(amount_);
}
Amount::Amount(int amount) : Amount(int64_t{amount}) {
// do nothing
}
Amount::Amount(uint32_t amount) : Amount(static_cast<int64_t>(amount)) {
// do nothing
}
Amount::Amount(double amount)
: Amount(static_cast<int64_t>(amount * kCoinBase)) {}
Amount::Amount(int64_t amount, bool ignore_check)
: amount_(amount), ignore_check_(ignore_check) {
if (!ignore_check_) {
CheckValidAmount(amount_);
}
}
void Amount::CheckValidAmount(int64_t satoshi_amount) {
if (!IsValidAmount(satoshi_amount)) {
warn(CFD_LOG_SOURCE, "Amount out of range. amount={}.", satoshi_amount);
throw CfdException(kCfdOutOfRangeError, "Amount out of range.");
}
}
Amount Amount::CreateBySatoshiAmount(int64_t satoshi_amount) {
CheckValidAmount(satoshi_amount);
return Amount(satoshi_amount);
}
Amount Amount::CreateByCoinAmount(double coin_amount) {
CheckValidAmount(coin_amount);
return Amount(coin_amount);
}
int64_t Amount::GetSatoshiValue() const { return amount_; }
double Amount::GetCoinValue() const {
return (static_cast<double>(amount_) / kCoinBase);
}
ByteData Amount::GetByteData() const {
std::vector<uint8_t> bytes(sizeof(amount_));
memcpy(bytes.data(), &amount_, bytes.size());
#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN)
#if __BYTE_ORDER == __BIG_ENDIAN
// big -> little
std::reverse(bytes.begin(), bytes.end());
#endif // __BYTE_ORDER == __BIG_ENDIAN
#else
#if defined(__BIG_ENDIAN__)
std::reverse(bytes.begin(), bytes.end());
#endif // __BIG_ENDIAN__
#endif
return ByteData(bytes);
}
// member operator overloading
bool Amount::operator==(const Amount& amount) const {
return amount_ == amount.amount_;
}
bool Amount::operator==(const int64_t satoshi_amount) const {
return amount_ == satoshi_amount;
}
bool Amount::operator!=(const Amount& amount) const {
return amount_ != amount.amount_;
}
bool Amount::operator!=(const int64_t satoshi_amount) const {
return amount_ != satoshi_amount;
}
Amount Amount::operator+=(const int64_t satoshi_amount) {
amount_ += satoshi_amount;
CheckValidAmount(amount_);
return *this;
}
Amount Amount::operator+=(const Amount& amount) {
amount_ += amount.amount_;
return *this;
}
Amount Amount::operator-=(const int64_t satoshi_amount) {
amount_ -= satoshi_amount;
CheckValidAmount(amount_);
return *this;
}
Amount Amount::operator-=(const Amount& amount) {
amount_ -= amount.amount_;
return *this;
}
Amount Amount::operator*=(const int64_t value) {
amount_ *= value;
CheckValidAmount(amount_);
return *this;
}
Amount Amount::operator/=(const int64_t value) {
double calc_amount =
static_cast<double>(amount_) / static_cast<double>(value);
amount_ = static_cast<int64_t>(std::round(calc_amount));
CheckValidAmount(amount_);
return *this;
}
// global operator overloading
bool operator==(int64_t satoshi_amount, const Amount& amount) {
return amount == satoshi_amount;
}
bool operator!=(int64_t satoshi_amount, const Amount& amount) {
return amount != satoshi_amount;
}
bool operator<(const Amount& lhs, const Amount& rhs) {
return (lhs.GetSatoshiValue() < rhs.GetSatoshiValue());
}
bool operator<(const int64_t lhs, const Amount& rhs) {
return (lhs < rhs.GetSatoshiValue());
}
bool operator<(const Amount& lhs, const int64_t rhs) {
return (lhs.GetSatoshiValue() < rhs);
}
bool operator>(const Amount& lhs, const Amount& rhs) { return (rhs < lhs); }
bool operator>(const int64_t lhs, const Amount& rhs) { return (rhs < lhs); }
bool operator>(const Amount& lhs, const int64_t rhs) { return (rhs < lhs); }
bool operator<=(const Amount& lhs, const Amount& rhs) { return !(lhs > rhs); }
bool operator<=(const int64_t lhs, const Amount& rhs) { return !(lhs > rhs); }
bool operator<=(const Amount& lhs, const int64_t rhs) { return !(lhs > rhs); }
bool operator>=(const Amount& lhs, const Amount& rhs) { return !(lhs < rhs); }
bool operator>=(const int64_t lhs, const Amount& rhs) { return !(lhs < rhs); }
bool operator>=(const Amount& lhs, const int64_t rhs) { return !(lhs < rhs); }
Amount operator+(const Amount& left_amount, const Amount& right_amount) {
return Amount::CreateBySatoshiAmount(
left_amount.GetSatoshiValue() + right_amount.GetSatoshiValue());
}
Amount operator+(const Amount& amount, const int64_t satoshi_amount) {
return Amount::CreateBySatoshiAmount(amount.GetSatoshiValue()) +=
satoshi_amount;
}
Amount operator+(const int64_t satoshi_amount, const Amount& amount) {
return Amount::CreateBySatoshiAmount(amount.GetSatoshiValue()) +=
satoshi_amount;
}
Amount operator-(const Amount& left_amount, const Amount& right_amount) {
return Amount::CreateBySatoshiAmount(
left_amount.GetSatoshiValue() - right_amount.GetSatoshiValue());
}
Amount operator-(const Amount& amount, const int64_t satoshi_amount) {
return Amount::CreateBySatoshiAmount(amount.GetSatoshiValue()) -=
satoshi_amount;
}
Amount operator-(const int64_t satoshi_amount, const Amount& amount) {
return Amount::CreateBySatoshiAmount(
satoshi_amount - amount.GetSatoshiValue());
}
Amount operator*(const Amount& amount, const int64_t value) {
return Amount::CreateBySatoshiAmount(amount.GetSatoshiValue()) *= value;
}
Amount operator*(const int64_t value, const Amount& amount) {
return Amount::CreateBySatoshiAmount(amount.GetSatoshiValue()) *= value;
}
Amount operator/(const Amount& amount, const int64_t value) {
return Amount::CreateBySatoshiAmount(amount.GetSatoshiValue()) /= value;
}
} // namespace core
} // namespace cfd