forked from viabtc/viabtc_exchange_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathme_balance.c
364 lines (302 loc) · 9.13 KB
/
me_balance.c
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
* Description:
* History: [email protected], 2017/03/15, create
*/
# include "me_config.h"
# include "me_balance.h"
dict_t *dict_balance;
static dict_t *dict_asset;
struct asset_type {
int prec_save;
int prec_show;
};
static uint32_t asset_dict_hash_function(const void *key)
{
return dict_generic_hash_function(key, strlen(key));
}
static void *asset_dict_key_dup(const void *key)
{
return strdup(key);
}
static void *asset_dict_val_dup(const void *val)
{
struct asset_type *obj = malloc(sizeof(struct asset_type));
if (obj == NULL)
return NULL;
memcpy(obj, val, sizeof(struct asset_type));
return obj;
}
static int asset_dict_key_compare(const void *key1, const void *key2)
{
return strcmp(key1, key2);
}
static void asset_dict_key_free(void *key)
{
free(key);
}
static void asset_dict_val_free(void *val)
{
free(val);
}
static uint32_t balance_dict_hash_function(const void *key)
{
return dict_generic_hash_function(key, sizeof(struct balance_key));
}
static void *balance_dict_key_dup(const void *key)
{
struct balance_key *obj = malloc(sizeof(struct balance_key));
if (obj == NULL)
return NULL;
memcpy(obj, key, sizeof(struct balance_key));
return obj;
}
static void *balance_dict_val_dup(const void *val)
{
return mpd_qncopy(val);
}
static int balance_dict_key_compare(const void *key1, const void *key2)
{
return memcmp(key1, key2, sizeof(struct balance_key));
}
static void balance_dict_key_free(void *key)
{
free(key);
}
static void balance_dict_val_free(void *val)
{
mpd_del(val);
}
static int init_dict(void)
{
dict_types type;
memset(&type, 0, sizeof(type));
type.hash_function = asset_dict_hash_function;
type.key_compare = asset_dict_key_compare;
type.key_dup = asset_dict_key_dup;
type.key_destructor = asset_dict_key_free;
type.val_dup = asset_dict_val_dup;
type.val_destructor = asset_dict_val_free;
dict_asset = dict_create(&type, 64);
if (dict_asset == NULL)
return -__LINE__;
memset(&type, 0, sizeof(type));
type.hash_function = balance_dict_hash_function;
type.key_compare = balance_dict_key_compare;
type.key_dup = balance_dict_key_dup;
type.key_destructor = balance_dict_key_free;
type.val_dup = balance_dict_val_dup;
type.val_destructor = balance_dict_val_free;
dict_balance = dict_create(&type, 64);
if (dict_balance == NULL)
return -__LINE__;
return 0;
}
int init_balance()
{
ERR_RET(init_dict());
for (size_t i = 0; i < settings.asset_num; ++i) {
struct asset_type type;
type.prec_save = settings.assets[i].prec_save;
type.prec_show = settings.assets[i].prec_show;
if (dict_add(dict_asset, settings.assets[i].name, &type) == NULL)
return -__LINE__;
}
return 0;
}
static struct asset_type *get_asset_type(const char *asset)
{
dict_entry *entry = dict_find(dict_asset, asset);
if (entry == NULL)
return NULL;
return entry->val;
}
bool asset_exist(const char *asset)
{
struct asset_type *at = get_asset_type(asset);
return at ? true : false;
}
int asset_prec(const char *asset)
{
struct asset_type *at = get_asset_type(asset);
return at ? at->prec_save : -1;
}
int asset_prec_show(const char *asset)
{
struct asset_type *at = get_asset_type(asset);
return at ? at->prec_show: -1;
}
mpd_t *balance_get(uint32_t user_id, uint32_t type, const char *asset)
{
struct balance_key key;
key.user_id = user_id;
key.type = type;
strncpy(key.asset, asset, sizeof(key.asset));
dict_entry *entry = dict_find(dict_balance, &key);
if (entry) {
return entry->val;
}
return NULL;
}
void balance_del(uint32_t user_id, uint32_t type, const char *asset)
{
struct balance_key key;
key.user_id = user_id;
key.type = type;
strncpy(key.asset, asset, sizeof(key.asset));
dict_delete(dict_balance, &key);
}
mpd_t *balance_set(uint32_t user_id, uint32_t type, const char *asset, mpd_t *amount)
{
struct asset_type *at = get_asset_type(asset);
if (at == NULL)
return NULL;
int ret = mpd_cmp(amount, mpd_zero, &mpd_ctx);
if (ret < 0) {
return NULL;
} else if (ret == 0) {
balance_del(user_id, type, asset);
return mpd_zero;
}
struct balance_key key;
key.user_id = user_id;
key.type = type;
strncpy(key.asset, asset, sizeof(key.asset));
mpd_t *result;
dict_entry *entry;
entry = dict_find(dict_balance, &key);
if (entry) {
result = entry->val;
mpd_rescale(result, amount, -at->prec_save, &mpd_ctx);
return result;
}
entry = dict_add(dict_balance, &key, amount);
if (entry == NULL)
return NULL;
result = entry->val;
mpd_rescale(result, amount, -at->prec_save, &mpd_ctx);
return result;
}
mpd_t *balance_add(uint32_t user_id, uint32_t type, const char *asset, mpd_t *amount)
{
struct asset_type *at = get_asset_type(asset);
if (at == NULL)
return NULL;
if (mpd_cmp(amount, mpd_zero, &mpd_ctx) < 0)
return NULL;
struct balance_key key;
key.user_id = user_id;
key.type = type;
strncpy(key.asset, asset, sizeof(key.asset));
mpd_t *result;
dict_entry *entry = dict_find(dict_balance, &key);
if (entry) {
result = entry->val;
mpd_add(result, result, amount, &mpd_ctx);
mpd_rescale(result, result, -at->prec_save, &mpd_ctx);
return result;
}
return balance_set(user_id, type, asset, amount);
}
mpd_t *balance_sub(uint32_t user_id, uint32_t type, const char *asset, mpd_t *amount)
{
struct asset_type *at = get_asset_type(asset);
if (at == NULL)
return NULL;
if (mpd_cmp(amount, mpd_zero, &mpd_ctx) < 0)
return NULL;
mpd_t *result = balance_get(user_id, type, asset);
if (result == NULL)
return NULL;
if (mpd_cmp(result, amount, &mpd_ctx) < 0)
return NULL;
mpd_sub(result, result, amount, &mpd_ctx);
if (mpd_cmp(result, mpd_zero, &mpd_ctx) == 0) {
balance_del(user_id, type, asset);
return mpd_zero;
}
mpd_rescale(result, result, -at->prec_save, &mpd_ctx);
return result;
}
mpd_t *balance_freeze(uint32_t user_id, const char *asset, mpd_t *amount)
{
struct asset_type *at = get_asset_type(asset);
if (at == NULL)
return NULL;
if (mpd_cmp(amount, mpd_zero, &mpd_ctx) < 0)
return NULL;
mpd_t *available = balance_get(user_id, BALANCE_TYPE_AVAILABLE, asset);
if (available == NULL)
return NULL;
if (mpd_cmp(available, amount, &mpd_ctx) < 0)
return NULL;
if (balance_add(user_id, BALANCE_TYPE_FREEZE, asset, amount) == 0)
return NULL;
mpd_sub(available, available, amount, &mpd_ctx);
if (mpd_cmp(available, mpd_zero, &mpd_ctx) == 0) {
balance_del(user_id, BALANCE_TYPE_AVAILABLE, asset);
return mpd_zero;
}
mpd_rescale(available, available, -at->prec_save, &mpd_ctx);
return available;
}
mpd_t *balance_unfreeze(uint32_t user_id, const char *asset, mpd_t *amount)
{
struct asset_type *at = get_asset_type(asset);
if (at == NULL)
return NULL;
if (mpd_cmp(amount, mpd_zero, &mpd_ctx) < 0)
return NULL;
mpd_t *freeze = balance_get(user_id, BALANCE_TYPE_FREEZE, asset);
if (freeze == NULL)
return NULL;
if (mpd_cmp(freeze, amount, &mpd_ctx) < 0)
return NULL;
if (balance_add(user_id, BALANCE_TYPE_AVAILABLE, asset, amount) == 0)
return NULL;
mpd_sub(freeze, freeze, amount, &mpd_ctx);
if (mpd_cmp(freeze, mpd_zero, &mpd_ctx) == 0) {
balance_del(user_id, BALANCE_TYPE_FREEZE, asset);
return mpd_zero;
}
mpd_rescale(freeze, freeze, -at->prec_save, &mpd_ctx);
return freeze;
}
mpd_t *balance_total(uint32_t user_id, const char *asset)
{
mpd_t *balance = mpd_new(&mpd_ctx);
mpd_copy(balance, mpd_zero, &mpd_ctx);
mpd_t *available = balance_get(user_id, BALANCE_TYPE_AVAILABLE, asset);
if (available) {
mpd_add(balance, balance, available, &mpd_ctx);
}
mpd_t *freeze = balance_get(user_id, BALANCE_TYPE_FREEZE, asset);
if (freeze) {
mpd_add(balance, balance, freeze, &mpd_ctx);
}
return balance;
}
int balance_status(const char *asset, mpd_t *total, size_t *available_count, mpd_t *available, size_t *freeze_count, mpd_t *freeze)
{
*freeze_count = 0;
*available_count = 0;
mpd_copy(total, mpd_zero, &mpd_ctx);
mpd_copy(freeze, mpd_zero, &mpd_ctx);
mpd_copy(available, mpd_zero, &mpd_ctx);
dict_entry *entry;
dict_iterator *iter = dict_get_iterator(dict_balance);
while ((entry = dict_next(iter)) != NULL) {
struct balance_key *key = entry->key;
if (strcmp(key->asset, asset) != 0)
continue;
mpd_add(total, total, entry->val, &mpd_ctx);
if (key->type == BALANCE_TYPE_AVAILABLE) {
*available_count += 1;
mpd_add(available, available, entry->val, &mpd_ctx);
} else {
*freeze_count += 1;
mpd_add(freeze, freeze, entry->val, &mpd_ctx);
}
}
dict_release_iterator(iter);
return 0;
}