forked from breadwallet/breadwallet-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBRCryptoUnit.c
More file actions
151 lines (126 loc) · 4.02 KB
/
BRCryptoUnit.c
File metadata and controls
151 lines (126 loc) · 4.02 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
//
// BRCryptoUnit.c
// 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.
#include "BRCryptoUnit.h"
#include "support/BRArray.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
struct BRCryptoUnitRecord {
BRCryptoCurrency currency;
char *uids;
char *name;
char *symbol;
BRCryptoUnit base;
uint8_t decimals;
BRCryptoRef ref;
};
IMPLEMENT_CRYPTO_GIVE_TAKE (BRCryptoUnit, cryptoUnit)
static BRCryptoUnit
cryptoUnitCreateInternal (BRCryptoCurrency currency,
const char *code,
const char *name,
const char *symbol) {
BRCryptoUnit unit = malloc (sizeof (struct BRCryptoUnitRecord));
unit->currency = cryptoCurrencyTake (currency);
unit->name = strdup (name);
unit->symbol = strdup (symbol);
unit->uids = malloc (strlen (cryptoCurrencyGetUids(currency)) + 1 + strlen(code) + 1);
sprintf (unit->uids, "%s:%s", cryptoCurrencyGetUids(currency), code);
unit->ref = CRYPTO_REF_ASSIGN (cryptoUnitRelease);
return unit;
}
extern BRCryptoUnit
cryptoUnitCreateAsBase (BRCryptoCurrency currency,
const char *code,
const char *name,
const char *symbol) {
BRCryptoUnit unit = cryptoUnitCreateInternal (currency, code, name, symbol);
unit->base = NULL;
unit->decimals = 0;
return unit;
}
extern BRCryptoUnit
cryptoUnitCreate (BRCryptoCurrency currency,
const char *code,
const char *name,
const char *symbol,
BRCryptoUnit baseUnit,
uint8_t powerOffset) {
assert (NULL != baseUnit);
BRCryptoUnit unit = cryptoUnitCreateInternal (currency, code, name, symbol);
unit->base = cryptoUnitTake (baseUnit);
unit->decimals = powerOffset;
return unit;
}
static void
cryptoUnitRelease (BRCryptoUnit unit) {
if (NULL != unit->base) cryptoUnitGive (unit->base);
cryptoCurrencyGive (unit->currency);
free (unit->uids);
free (unit->name);
free (unit->symbol);
memset (unit, 0, sizeof(*unit));
free (unit);
}
private_extern BRArrayOf(BRCryptoUnit)
cryptoUnitTakeAll (BRArrayOf(BRCryptoUnit) units) {
if (NULL != units)
for (size_t index = 0; index < array_count (units); index++)
cryptoUnitTake(units[index]);
return units;
}
private_extern BRArrayOf(BRCryptoUnit)
cryptoUnitGiveAll (BRArrayOf(BRCryptoUnit) units) {
for (size_t index = 0; index < array_count (units); index++)
cryptoUnitGive(units[index]);
return units;
}
extern const char *
cryptoUnitGetUids(BRCryptoUnit unit) {
return unit->uids;
}
extern const char *
cryptoUnitGetName (BRCryptoUnit unit) {
return unit->name;
}
extern const char *
cryptoUnitGetSymbol (BRCryptoUnit unit) {
return unit->symbol;
}
extern BRCryptoCurrency
cryptoUnitGetCurrency (BRCryptoUnit unit) {
return cryptoCurrencyTake (unit->currency);
}
extern BRCryptoBoolean
cryptoUnitHasCurrency (BRCryptoUnit unit,
BRCryptoCurrency currency) {
return AS_CRYPTO_BOOLEAN (unit->currency == currency);
}
extern BRCryptoUnit
cryptoUnitGetBaseUnit (BRCryptoUnit unit) {
return cryptoUnitTake (NULL == unit->base ? unit : unit->base);
}
extern uint8_t
cryptoUnitGetBaseDecimalOffset (BRCryptoUnit unit) {
return NULL == unit->base ? 0 : unit->decimals;
}
extern BRCryptoBoolean
cryptoUnitIsCompatible (BRCryptoUnit u1,
BRCryptoUnit u2) {
return cryptoCurrencyIsIdentical (u1->currency, u2->currency);
}
extern BRCryptoBoolean
cryptoUnitIsIdentical (BRCryptoUnit u1,
BRCryptoUnit u2) {
return AS_CRYPTO_BOOLEAN (u1 == u2
|| u1->uids == u2->uids
|| 0 == strcmp (u1->uids, u2->uids));
}