-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathexchange.lua
512 lines (417 loc) · 15.3 KB
/
exchange.lua
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
local function to_table_from_redis_hash(hash)
local tmp = {};
for k, v in pairs(hash) do
if k % 2 == 0 then
tmp[hash[k - 1]] = hash[k];
end
end
return tmp;
end
local function table_is_empty(t)
return _G.next(t) == nil;
end
local function get_json_from_redis_hash(key, field, value)
local hash_in_redis = redis.call('HGETALL', key);
if not hash_in_redis then
return '{}';
end
local table = to_table_from_redis_hash(hash_in_redis);
table[field] = value;
return cjson.encode(table);
end
-- user asset
-- user_asset:available:1:btc
-- user_asset:frozen:1:usdt
local function add_user_available(user_id, coin, amount)
redis.call('INCRBYFLOAT', 'user_asset:available:' .. user_id .. ':' .. coin, amount);
end
local function sub_user_available(user_id, coin, amount)
redis.call('INCRBYFLOAT', 'user_asset:available:' .. user_id .. ':' .. coin, -amount);
end
local function add_user_frozen(user_id, coin, amount)
redis.call('INCRBYFLOAT', 'user_asset:frozen:' .. user_id .. ':' .. coin, amount);
end
local function sub_user_frozen(user_id, coin, amount)
redis.call('INCRBYFLOAT', 'user_asset:frozen:' .. user_id .. ':' .. coin, -amount);
end
local function get_user_available_asset(user_id, coin)
local asset = redis.call('GET', 'user_asset:available:' .. user_id .. ':' .. coin);
if not asset then
redis.call('SET', 'user_asset:available:' .. user_id .. ':' .. coin, 0.0);
return 0.0;
end
return tonumber(asset);
end
local function get_user_frozen_asset(user_id, coin)
local asset = redis.call('GET', 'user_asset:frozen:' .. user_id .. ':' .. coin);
if not asset then
redis.call('SET', 'user_asset:frozen:' .. user_id .. ':' .. coin, 0.0);
return 0.0;
end
return tonumber(asset);
end
local function deposit_for_user(user_id, coin, amount)
add_user_available(user_id, coin, amount);
return true;
end
local function withdraw_apply_user(user_id, coin, amount)
sub_user_available(user_id, coin, amount);
add_user_frozen(user_id, coin, amount);
end
local function withdraw_success_user(user_id, coin, amount)
-- atomic
sub_user_frozen(user_id, coin, amount);
end
-- order
-- order:last
-- order:1
-- user_id
-- side
-- stock_id
-- money_id
-- price
-- amount
-- money
-- process_amount
-- process_money
-- create_time
-- update_time
-- status: create, part, cancel, finish
local function get_order_last_id()
-- atomic
return redis.call('INCRBY', 'order:last', 1);
end
local function save_order(user_id, stock_id, money_id, side, price, amount, money, timestamp)
local order_id = get_order_last_id();
local key = 'order:' .. order_id;
redis.call('HSET', key, 'user_id', user_id);
redis.call('HSET', key, 'side', side);
redis.call('HSET', key, 'stock_id', stock_id);
redis.call('HSET', key, 'money_id', money_id);
redis.call('HSET', key, 'price', price);
redis.call('HSET', key, 'amount', amount);
redis.call('HSET', key, 'money', money);
redis.call('HSET', key, 'create_time', timestamp);
redis.call('HSET', key, 'process_amount', 0.0);
redis.call('HSET', key, 'process_money', 0.0);
redis.call('HSET', key, 'status', 'create');
return order_id;
end
local function get_order_field(order_id, field)
local key = 'order:' .. order_id;
return redis.call('HGET', key, field);
end
local function get_order_remain_amount(order_id)
local amount = get_order_field(order_id, 'amount');
local process_amount = get_order_field(order_id, 'process_amount');
return tonumber(amount) - tonumber(process_amount);
end
local function modify_order_status(order_id, status)
local key = 'order:' .. order_id;
redis.call('HSET', key, 'status', status);
end
local function get_order_json(order_id)
local key = 'order:' .. order_id;
return get_json_from_redis_hash(key, 'id', order_id);
end
-- order_book:usdt:btc
local function update_depth_item(stock_id, money_id, side, price, amount)
local order_book = 'order_book:' .. money_id .. ':' .. stock_id .. ':' .. side;
local order_book_price = 'order_book_price:' .. money_id .. ':' .. stock_id .. ':' .. side;
local price_key = tostring(price);
local amount_value = redis.call('HINCRBYFLOAT', order_book, price_key, amount);
amount_value = tonumber(amount_value);
if amount_value <= 0.0 then
redis.call('HDEL', order_book, price_key);
redis.call('SREM', order_book_price, price_key);
else
redis.call('SADD', order_book_price, price_key);
end
end
local function get_single_depth(stock_id, money_id, side, limit)
local order_book = 'order_book:' .. money_id .. ':' .. stock_id .. ':' .. side;
local order_book_price = 'order_book_price:' .. money_id .. ':' .. stock_id .. ':' .. side;
local sort = '';
if side == 'buy' then
sort = 'DESC';
else
sort = 'ASC';
end
local single_depth = {};
local price_list = redis.call('SORT', order_book_price, sort, 'limit', 0, limit)
for i = 1, #price_list do
local price = price_list[i];
local amount = redis.call('HGET', order_book, price);
if not amount then
-- do nothing
else
single_depth[i] = { price, amount };
end
end
return single_depth;
end
local function get_depth_json(stock_id, money_id, limit)
local ask_depth = get_single_depth(stock_id, money_id, 'sell', limit);
local bid_depth = get_single_depth(stock_id, money_id, 'buy', limit);
local depth = {
stock_id = stock_id,
money_id = money_id
};
if not table_is_empty(ask_depth) then
depth['asks'] = ask_depth;
end
if not table_is_empty(bid_depth) then
depth['bids'] = bid_depth;
end
return cjson.encode(depth);
end
-- order_book_central:usdt:btc:buy
-- score: price
-- member: order_id
local function put_into_order_book_central(stock_id, money_id, side, order_id, price)
local key = 'order_book_central:' .. money_id .. ':' .. stock_id .. ':' .. side;
redis.call('ZADD', key, price, order_id); -- DESC sort
end
local function get_top_from_order_book_central(stock_id, money_id, side, limit)
-- limit > 0
local key = 'order_book_central:' .. money_id .. ':' .. stock_id .. ':' .. side;
local from = 0;
local to = 0;
if side == 'buy' then
from = -limit;
to = -1;
else
from = 0;
to = limit;
end
local order_book_range = redis.call('ZRANGE', key, from, to);
return order_book_range;
end
local function remove_order_from_order_book_central(stock_id, money_id, side, order_id)
local key = 'order_book_central:' .. money_id .. ':' .. stock_id .. ':' .. side;
redis.call('ZREM', key, order_id);
end
-- match
-- match:usdt:btc:1
-- id
-- price
-- side
-- stock_id
-- money_id
-- price
-- amount
-- money
-- create_time
local function get_match_last_id()
return redis.call('INCRBY', 'match:last', 1);
end
local function save_match(match)
local match_id = get_match_last_id();
match.id = match_id;
local key = 'match:' .. match_id;
redis.call('HSET', key, 'sell_order_id', match.sell_order_id);
redis.call('HSET', key, 'buy_order_id', match.buy_order_id);
redis.call('HSET', key, 'seller_id', match.seller_id);
redis.call('HSET', key, 'buyer_id', match.buyer_id);
redis.call('HSET', key, 'stock_id', match.stock_id);
redis.call('HSET', key, 'money_id', match.money_id);
redis.call('HSET', key, 'side', match.side);
redis.call('HSET', key, 'price', match.price);
redis.call('HSET', key, 'amount', match.amount);
redis.call('HSET', key, 'money', match.money);
redis.call('HSET', key, 'flag', match.flag);
redis.call('HSET', key, 'timestamp', match.timestamp);
return match_id;
end
local function get_match_json(match_id)
local key = 'match:' .. match_id;
return get_json_from_redis_hash(key, 'id', match_id);
end
local function put_last_price(stock_id, money_id, price)
local key = 'last_price:' .. money_id .. ':' .. stock_id;
redis.call('SET', key, price);
end
local function get_last_price(stock_id, money_id)
local key = 'last_price:' .. money_id .. ':' .. stock_id;
local last_price = redis.call('GET', key);
if not last_price then
return 0.0;
end
return tonumber(last_price);
end
local function add_order_process(order_id, side, match)
local key = 'order:' .. order_id;
local process_amount = redis.call('HINCRBYFLOAT', key, 'process_amount', match.amount);
local process_money = redis.call('HINCRBYFLOAT', key, 'process_money', match.money);
local order_amount = get_order_field(order_id, 'amount');
local remain_amount = order_amount - process_amount;
local remain_money = 0.0;
if remain_amount <= 0.0 then
remove_order_from_order_book_central(match.stock_id, match.money_id, side, order_id);
modify_order_status(order_id, 'finish');
local order_money = get_order_field(order_id, 'money');
remain_money = order_money - process_money;
else
modify_order_status(order_id, 'part');
end
if side == 'buy' then
add_user_available(match.buyer_id, match.stock_id, match.amount);
sub_user_frozen(match.buyer_id, match.money_id, match.money + remain_money);
else
add_user_available(match.seller_id, match.money_id, match.money);
sub_user_frozen(match.seller_id, match.stock_id, match.amount);
end
end
local function execute_order(match)
save_match(match);
add_order_process(match.buy_order_id, 'buy', match);
update_depth_item(match.stock_id, match.money_id, 'buy', match.bid1_price, -match.amount);
add_order_process(match.sell_order_id, 'sell', match);
update_depth_item(match.stock_id, match.money_id, 'sell', match.ask1_price, -match.amount);
put_last_price(match.stock_id, match.money_id, match.price);
end
local function submit_order(user_id, stock_id, money_id, side, price, amount, timestamp)
price = tonumber(price);
amount = tonumber(amount);
if price <= 0 then
return -2;
end
if amount <= 0 then
return -3;
end
local money = price * amount;
local user_available = 0.0;
local lock_asset_coin = '';
local lock_asset_coin_amount = 0.0;
if side == 'buy' then
lock_asset_coin = money_id;
lock_asset_coin_amount = money;
else
lock_asset_coin = stock_id;
lock_asset_coin_amount = amount;
end
user_available = get_user_available_asset(user_id, lock_asset_coin);
if lock_asset_coin_amount > user_available then
return -1;
end
sub_user_available(user_id, lock_asset_coin, lock_asset_coin_amount);
add_user_frozen(user_id, lock_asset_coin, lock_asset_coin_amount);
local order_id = save_order(user_id, stock_id, money_id, side, price, amount, money, timestamp);
put_into_order_book_central(stock_id, money_id, side, order_id, price);
update_depth_item(stock_id, money_id, side, price, amount);
return order_id;
end
local function cancel_order(order_id)
local order_status = get_order_field(order_id, 'status');
if order_status == 'cancel' or order_status == 'finish' then
return -1;
end
local side = get_order_field(order_id, 'side');
local unlock_asset_coin = '';
local unlock_asset_coin_amount = 0.0;
local user_id = get_order_field(order_id, 'user_id');
local price = get_order_field(order_id, 'price');
local stock_id = get_order_field(order_id, 'stock_id');
local money_id = get_order_field(order_id, 'money_id');
local money = get_order_field(order_id, 'money');
local process_money = get_order_field(order_id, 'process_money');
local amount = get_order_field(order_id, 'amount');
local process_amount = get_order_field(order_id, 'process_amount');
if side == 'buy' then
unlock_asset_coin = money_id;
unlock_asset_coin_amount = tonumber(money) - tonumber(process_money);
else
unlock_asset_coin = stock_id;
unlock_asset_coin_amount = tonumber(amount) - tonumber(process_amount);
end
modify_order_status(order_id, 'cancel');
remove_order_from_order_book_central(stock_id, money_id, side, order_id);
update_depth_item(stock_id, money_id, side, price, -unlock_asset_coin_amount);
add_user_available(user_id, unlock_asset_coin, unlock_asset_coin_amount);
sub_user_frozen(user_id, unlock_asset_coin, unlock_asset_coin_amount);
return 1;
end
local SELL_ORDER_FILL = 0;
local BUY_ORDER_FILL = 1;
local BUY_ORDER_AND_ORDER_SELL_FILL = 2;
local function match_order_once(stock_id, money_id, ask1_id, bid1_id, timestamp)
if not ask1_id or not bid1_id then
return false;
end
local ask1_price = tonumber(get_order_field(ask1_id, 'price'));
local bid1_price = tonumber(get_order_field(bid1_id, 'price'));
if ask1_price > bid1_price then
return false;
end
local seller_id = get_order_field(ask1_id, 'user_id');
local buyer_id = get_order_field(bid1_id, 'user_id');
local match = {};
match.sell_order_id = ask1_id;
match.buy_order_id = bid1_id;
match.seller_id = seller_id;
match.buyer_id = buyer_id;
match.stock_id = stock_id;
match.money_id = money_id;
match.timestamp = timestamp;
match.ask1_price = ask1_price;
match.bid1_price = bid1_price;
if ask1_id < bid1_id then
match.side = 'buy';
match.price = ask1_price;
else
match.side = 'sell';
match.price = bid1_price;
end
local ask_amount = get_order_remain_amount(ask1_id);
local bid_amount = get_order_remain_amount(bid1_id);
if ask_amount == bid_amount then
match.amount = ask_amount;
match.money = ask_amount * match.price;
match.flag = BUY_ORDER_AND_ORDER_SELL_FILL;
else
if ask_amount > bid_amount then
match.amount = bid_amount;
match.money = bid_amount * match.price;
match.flag = BUY_ORDER_FILL;
else
match.amount = ask_amount * 1;
match.money = ask_amount * match.price;
match.flag = SELL_ORDER_FILL;
end
end
execute_order(match);
return true;
end
local function match_order(stock_id, money_id, limit, timestamp)
local askList = get_top_from_order_book_central(stock_id, money_id, 'sell', limit);
local bidList = get_top_from_order_book_central(stock_id, money_id, 'buy', limit);
local match_count = 0;
local running = true;
repeat
if #askList == 0 or #bidList == 0 then
break ;
end
local ask1_id = table.remove(askList, 1);
local bid1_id = table.remove(bidList, 1);
running = match_order_once(stock_id, money_id, ask1_id, bid1_id, timestamp);
if running then
match_count = match_count + 1;
end
until not running;
return match_count;
end
local exchange = {
get_user_available_asset = get_user_available_asset,
get_user_frozen_asset = get_user_frozen_asset,
deposit_for_user = deposit_for_user,
withdraw_apply_user = withdraw_apply_user,
withdraw_success_user = withdraw_success_user,
submit_order = submit_order,
cancel_order = cancel_order,
match_order = match_order,
get_match_json = get_match_json,
get_order_json = get_order_json,
get_depth_json = get_depth_json,
get_last_price = get_last_price
}
return exchange;