-
Notifications
You must be signed in to change notification settings - Fork 321
Expand file tree
/
Copy pathBRCryptoAmount.h
More file actions
189 lines (163 loc) Β· 5.13 KB
/
BRCryptoAmount.h
File metadata and controls
189 lines (163 loc) Β· 5.13 KB
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
//
// BRCryptoAmount.h
// BRCore
//
// Created by Ed Gamble on 3/19/19.
// Copyright Β© 2019 breadwallet. All rights reserved.
//
// See the LICENSE file at the project root for license information.
// See the CONTRIBUTORS file at the project root for a list of contributors.
#ifndef BRCryptoAmount_h
#define BRCryptoAmount_h
#include "BRCryptoBase.h"
#include "BRCryptoCurrency.h"
#include "BRCryptoUnit.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
CRYPTO_COMPARE_LT,
CRYPTO_COMPARE_EQ,
CRYPTO_COMPARE_GT
} BRCryptoComparison;
typedef struct BRCryptoAmountRecord *BRCryptoAmount;
/**
* Create an amount from `value` in `unit`. If the amount is "2 Bitcoin" then, for example,
* the amount can be created from either of {2.0, BTC} or {2e8, SAT}.
*
* @note The internal representation is always as a UInt256 in the currency's baseUnit (aka
* the 'integer unit'). This allows easy arithmetic operations w/o conversions. Conversions
* to the base unit happens on creation, and conversion from the base unit happens on display.
*
* @param value
* @param unit
*
* @return An amount
*/
extern BRCryptoAmount
cryptoAmountCreateDouble (double value,
BRCryptoUnit unit);
/**
* Create an amount form `value` in `unit`. See discusion on `cryptoAmountCreateDouble()`
*
* @param value
* @param unit
*
*@return An Amount
*/
extern BRCryptoAmount
cryptoAmountCreateInteger (int64_t value,
BRCryptoUnit unit);
/**
* Create an amount from `value` and `unit`. See discusion on `cryptoAmountCreateDouble()`
*
* @note there are some constraints on `const char *value` that need to be described.
*
* @param value
* @param isNegative
* @param unit
*
* @return An amount
*/
extern BRCryptoAmount
cryptoAmountCreateString (const char *value,
BRCryptoBoolean isNegative,
BRCryptoUnit unit);
/**
* Returns the amount's unit.
*
* @param amount The amount
*
* @return The amount's unit, typically use for default display w/ an incremented reference
* count (aka 'taken')
*/
extern BRCryptoUnit
cryptoAmountGetUnit (BRCryptoAmount amount);
/**
* Returns the amount's currency
*
* @param amount The amount
*
* @return The currency w/ an incremented reference count (aka 'taken')
*/
extern BRCryptoCurrency
cryptoAmountGetCurrency (BRCryptoAmount amount);
extern BRCryptoBoolean
cryptoAmountHasCurrency (BRCryptoAmount amount,
BRCryptoCurrency currency);
extern BRCryptoBoolean
cryptoAmountIsNegative (BRCryptoAmount amount);
/**
* Check of two amount's are compatible; they are compatible if they have the same currency and
* thus can be added.
*
* @param a1
* @param a2
*
* @return
*/
extern BRCryptoBoolean
cryptoAmountIsCompatible (BRCryptoAmount a1,
BRCryptoAmount a2);
extern BRCryptoBoolean
cryptoAmountIsZero (BRCryptoAmount amount);
extern BRCryptoComparison
cryptoAmountCompare (BRCryptoAmount a1,
BRCryptoAmount a2);
extern BRCryptoAmount
cryptoAmountAdd (BRCryptoAmount a1,
BRCryptoAmount a2);
extern BRCryptoAmount
cryptoAmountSub (BRCryptoAmount a1,
BRCryptoAmount a2);
extern BRCryptoAmount
cryptoAmountNegate (BRCryptoAmount amount);
/**
* Convert `amount` into `unit`
*
* @param amount
* @param unit
*
* @note: if `unit` is incompatible, then NULL is returned.
*
* @return An amount
*/
extern BRCryptoAmount
cryptoAmountConvertToUnit (BRCryptoAmount amount,
BRCryptoUnit unit);
/**
* Return `amount` as a double in `unit`. For example, if amount is "2 BTC" and unit is
* SAT then the returned value will be 2e8; if amount is "2e6 SAT" and unit is "BTC" then the
* return value will be 0.02.
*
* @param amount
* @param unit
* @param overflow
*
* @return A double
*/
extern double
cryptoAmountGetDouble (BRCryptoAmount amount,
BRCryptoUnit unit,
BRCryptoBoolean *overflow);
/**
* Return `amount` as a UInt64 if the representation of `amount` in the base unit is less than
* or equal to UINT64_MAX; otherwise set overflow.
*
* @param amount
* @param overflow
*
* @return
*/
extern uint64_t
cryptoAmountGetIntegerRaw (BRCryptoAmount amount,
BRCryptoBoolean *overflow);
extern char *
cryptoAmountGetStringPrefaced (BRCryptoAmount amount,
int base,
const char *preface);
DECLARE_CRYPTO_GIVE_TAKE (BRCryptoAmount, cryptoAmount);
#ifdef __cplusplus
}
#endif
#endif /* BRCryptoAmount_h */