-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonchain.sol
1193 lines (1030 loc) · 40.3 KB
/
onchain.sol
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
pragma solidity ^0.4.25;
contract SafeMath {
function Sub(uint128 a, uint128 b) pure public returns (uint128) {
assert(b <= a);
return a - b;
}
function Add(uint128 a, uint128 b) pure public returns (uint128) {
uint128 c = a + b;
assert(c>=a && c>=b);
return c;
}
}
contract Token {
function totalSupply() public view returns (uint256 supply);
function balanceOf(address _owner) public view returns (uint256 balance);
function transfer(address _to, uint256 _value) public returns (bool success);
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success);
function approve(address _spender, uint256 _value) public returns (bool success);
function allowance(address _owner, address _spender) public view returns (uint256 remaining);
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}
contract onchain is SafeMath
{
uint32 public lastTransferId = 1;
event NewDeposit(uint32 indexed prCode, uint32 indexed accountId, uint128 amount, uint64 timestamp, uint32 lastTransferId);
event NewWithdraw(uint32 indexed prCode, uint32 indexed accountId, uint128 amount, uint64 timestamp, uint32 lastTransferId);
uint32 public lastNewOrderId = 1;
event NewOrder(uint32 indexed prTrade, uint32 indexed prBase, uint32 indexed accountId, uint32 id, bool isSell, uint80 price, uint104 qty, uint32 lastNewOrderId);
event NewCancel(uint32 indexed prTrade, uint32 indexed prBase, uint32 indexed accountId, uint32 id, bool isSell, uint80 price, uint104 qt, uint32 lastNewOrderId);
event NewBestBidAsk(uint32 indexed prTrade, uint32 indexed prBase, bool isBid, uint80 price);
uint32 public lastTradeId = 1;
event NewTrade(uint32 indexed prTrade, uint32 prBase, uint32 indexed bidId, uint32 indexed askId, uint32 accountIdBid, uint32 accountIdAsk, bool isSell, uint80 price, uint104 qty, uint32 lastTradeId, uint64 timestamp);
uint256 public constant basePrice = 10000000000;
uint80 public constant maxPrice = 10000000000000000000001;
uint104 public constant maxQty = 1000000000000000000000000000001;
uint128 public constant maxBalance = 1000000000000000000000000000000000001;
bool public isContractUse;
constructor() public
{
owner = msg.sender;
operator = owner;
AddOwner();
AddProduct(18, true, 0x0);
//lastProductId = 1; // productId == 1 -> ETH 0x0
isContractUse = true;
}
address public owner;
// Functions with this modifier can only be executed by the owner
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
address public operator;
modifier onlyOperator() {
require(msg.sender == operator);
_;
}
function transferOperator(address _operator) onlyOwner public {
operator = _operator;
}
modifier onlyExOwner() {
require(owner_id[msg.sender] != 0);
_;
}
modifier onlyContractUse {
require(isContractUse == true);
_;
}
function SetIsContractUse(bool _isContractUse) onlyOperator public
{
isContractUse = _isContractUse;
}
uint32 public lastOwnerId;
uint256 public newOwnerFee;
mapping (uint32 => address) id_owner;
mapping (address => uint32) owner_id;
mapping (uint32 => uint8) ownerId_takerFeeRateLocal;
mapping (uint32 => uint32) ownerId_accountId;
function UpdateOwner(uint32 orderId, address newOwner) onlyOperator public
{
require(lastOwnerId >= orderId);
owner_id[newOwner] = orderId;
id_owner[orderId] = newOwner;
}
function AddOwner() public payable
{
require(msg.value >= newOwnerFee);
require(owner_id[msg.sender] == 0);
owner_id[msg.sender] = ++lastOwnerId;
id_owner[lastOwnerId] = msg.sender;
ownerId_accountId[lastOwnerId] = FindOrAddAccount();
prCode_AccountId_Balance[1][ownerId_accountId[1]].available += uint128(msg.value);
}
function SetOwnerFee(uint256 ownerFee) onlyOperator public
{
newOwnerFee = ownerFee;
}
function GetOnwerList() view public returns (address[] owners, uint32[] ownerIds)
{
owners = new address[](lastOwnerId);
ownerIds = new uint32[](lastOwnerId);
for (uint32 i = 1; i <= lastOwnerId; i++)
{
owners[i - 1] = id_owner[i];
ownerIds[i - 1] = i;
}
}
function setTakerFeeRateLocal(uint8 _takerFeeRate) public
{
require (_takerFeeRate <= 100);
uint32 ownerId = owner_id[msg.sender];
require(ownerId != 0);
ownerId_takerFeeRateLocal[ownerId] = _takerFeeRate;//bp
}
function getTakerFeeRateLocal(uint32 ownerId) public view returns (uint8)
{
return ownerId_takerFeeRateLocal[ownerId];//bp
}
function airDrop(uint32 prCode, uint32[] accountIds, uint104[] qtys) onlyExOwner public
{
uint32 accountId = ownerId_accountId[owner_id[msg.sender]];
uint256 n = accountIds.length;
require(accountId != 0 && n == qtys.length);// && n <= 1000000);
uint128 sum = 0;
for (uint32 i = 0; i < n; i++)
{
sum += qtys[i];
}
prCode_AccountId_Balance[prCode][accountId].available = Sub(prCode_AccountId_Balance[prCode][accountId].available, sum);
for (i = 0; i < n; i++)
{
prCode_AccountId_Balance[prCode][accountIds[i]].available += qtys[i];
}
}
struct ProductInfo
{
uint256 divider;
bool isTradeBid;
bool isTradeAsk;
bool isDeposit;
bool isWithdraw;
uint32 ownerId;
uint104 minQty;
}
uint32 public lastProductId;
uint256 public newProductFee;
mapping (uint32 => address) prCode_product;
mapping (address => uint32) product_prCode;
mapping (uint32 => ProductInfo) prCode_productInfo;
function AddProduct(uint256 decimals, bool isDeposit, address product) payable onlyExOwner public
{
require(msg.value >= newProductFee);
require(product_prCode[product] == 0);
require(decimals <= 18);
product_prCode[product] = ++lastProductId;
prCode_product[lastProductId] = product;
ProductInfo memory productInfo;
productInfo.divider = 10 ** decimals;
productInfo.ownerId = owner_id[msg.sender];
productInfo.isDeposit = isDeposit;
prCode_productInfo[lastProductId] = productInfo;
prCode_AccountId_Balance[1][ownerId_accountId[1]].available += uint128(msg.value);
}
function SetProductInfo(uint32 prCode, bool isTradeBid, bool isTradeAsk, bool isDeposit, bool isWithdraw) public
{
ProductInfo storage prInfo = prCode_productInfo[prCode];
require(msg.sender == operator || owner_id[msg.sender] == prInfo.ownerId );
prInfo.isTradeBid = isTradeBid;
prInfo.isTradeAsk = isTradeAsk;
prInfo.isDeposit = isDeposit;
prInfo.isWithdraw = isWithdraw;
}
function SetProductMinQty(uint32 prCode, uint104 _minQty) public
{
ProductInfo storage prInfo = prCode_productInfo[prCode];
require(msg.sender == operator || owner_id[msg.sender] == prInfo.ownerId );
prInfo.minQty = _minQty;
}
function SetProductFee(uint256 productFee) onlyOperator public
{
newProductFee = productFee;
}
function GetProductList() view public returns (address[] products, uint32[] productIds)
{
products = new address[](lastProductId);
productIds = new uint32[](lastProductId);
for (uint32 i = 1; i <= lastProductId; i++)
{
products[i - 1] = prCode_product[i];
productIds[i - 1] = i;
}
}
function GetProductInfo(address product) view public returns (uint32 prCode, uint256 divider, bool isTradeBid, bool isTradeAsk, bool isDeposit, bool isWithdraw, uint32 ownerId, uint104 minQty)
{
prCode = product_prCode[product];
require(prCode != 0);
divider = prCode_productInfo[prCode].divider;
isTradeBid = prCode_productInfo[prCode].isTradeBid;
isTradeAsk = prCode_productInfo[prCode].isTradeAsk;
isDeposit = prCode_productInfo[prCode].isDeposit;
isWithdraw = prCode_productInfo[prCode].isWithdraw;
ownerId = prCode_productInfo[prCode].ownerId;
minQty = prCode_productInfo[prCode].minQty;
}
uint32 public lastAcccountId;
mapping (uint32 => uint8) id_announceLV;
mapping (uint32 => address) id_account;
mapping (uint32 => bool) accountId_freeze;
mapping (address => uint32) account_id;
function FindOrAddAccount() private returns (uint32)
{
if (account_id[msg.sender] == 0)
{
account_id[msg.sender] = ++lastAcccountId;
id_account[lastAcccountId] = msg.sender;
}
return account_id[msg.sender];
}
function GetAccountList() view onlyOperator public returns (address[] owners)//, uint32[] Ids)// Delete Later`
{
owners = new address[](lastAcccountId);
//Ids = new uint32[](lastAcccountId);
for (uint32 i = 1; i <= lastAcccountId; i++)
{
owners[i - 1] = id_account[i];
//Ids[i - 1] = i;
}
}
function GetMyAccountId() view public returns (uint32)
{
return account_id[msg.sender];
}
function GetMyAnnounceLV() view public returns (uint32)
{
return id_announceLV[account_id[msg.sender]];
}
function ChangeAnnounceLV(uint8 announceLV) public
{
uint32 accountId = account_id[msg.sender];
if (accountId != 0)
{
id_announceLV[accountId] = announceLV;
}
}
function SetFreezeByAddress(bool isFreeze, address account) onlyOperator public
{
uint32 accountId = account_id[account];
if (accountId != 0)
{
SetFreeze(isFreeze, accountId);
}
}
function SetFreeze(bool isFreeze, uint32 accountId) onlyOperator public
{
accountId_freeze[accountId] = isFreeze;
}
struct Balance
{
uint128 reserved;
uint128 available;
}
struct ListItem
{
uint32 prev;
uint32 next;
}
struct OrderLink
{
//uint32 orderN;
uint32 firstId;
uint32 lastId;
uint80 nextPrice;
uint80 prevPrice;
mapping (uint32 => ListItem) id_orderList;
}
struct Order
{
uint32 ownerId;
uint32 accountId;
uint32 prTrade;
uint32 prBase;
uint104 qty;
uint80 price;
bool isSell;
}
uint32 public lastOrderId;
mapping (uint32 => Order) id_Order;
struct OrderBook
{
uint8 tickSize;
uint80 bestBidPrice;
uint80 bestAskPrice;
mapping (uint80 => OrderLink) bidPrice_Order;
mapping (uint80 => OrderLink) askPrice_Order;
}
mapping (uint32 => mapping (uint32 => OrderBook)) basePID_tradePID_orderBook;
function SetOrderBookTickSize(uint32 prTrade, uint32 prBase, uint8 _tickSize) onlyOperator public
{
basePID_tradePID_orderBook[prBase][prTrade].tickSize = _tickSize;
}
mapping (uint32 => mapping (uint32 => Balance)) prCode_AccountId_Balance;
uint8 public takerFeeRateMain;
function setTakerFeeRateMain(uint8 _takerFeeRateMain) onlyOperator public
{
require (_takerFeeRateMain <= 100);
takerFeeRateMain = _takerFeeRateMain;//bp
}
struct OpenOrder
{
uint32 startId;
mapping(uint32 => ListItem) id_orderList;
}
mapping(uint32 => OpenOrder) accountId_OpenOrder;
function AddOpenOrder(uint32 accountId, uint32 orderId) private
{
OpenOrder memory openOrder = accountId_OpenOrder[accountId];
if (openOrder.startId != 0)
{
accountId_OpenOrder[accountId].id_orderList[openOrder.startId].prev = orderId;
accountId_OpenOrder[accountId].id_orderList[orderId].next = openOrder.startId;
}
accountId_OpenOrder[accountId].startId = orderId;
}
function RemoveOpenOrder(uint32 accountId, uint32 orderId) private
{
OpenOrder memory openOrder = accountId_OpenOrder[accountId];
uint32 nextId = accountId_OpenOrder[accountId].id_orderList[orderId].next;
uint32 prevId = accountId_OpenOrder[accountId].id_orderList[orderId].prev;
if (nextId != 0)
{
accountId_OpenOrder[accountId].id_orderList[nextId].prev = prevId;
}
if (prevId != 0)
{
accountId_OpenOrder[accountId].id_orderList[prevId].next = nextId;
}
if (openOrder.startId == orderId)
{
accountId_OpenOrder[accountId].startId = nextId;
}
}
struct DWrecord
{
uint32 prCode;
bool isDeposit;
uint128 qty;
uint64 timestamp;
}
struct DWrecords
{
uint32 N;
mapping (uint32 => DWrecord) N_DWrecord;
}
mapping (uint32 => DWrecords) AccountId_DWrecords;
function RecordDW(uint32 accountId, uint32 prCode, bool isDeposit, uint128 qty) private
{
DWrecord memory dW;
dW.isDeposit = isDeposit;
dW.prCode = prCode;
dW.qty = qty;
dW.timestamp = uint64(now);
AccountId_DWrecords[accountId].N_DWrecord[++AccountId_DWrecords[accountId].N] = dW;
if (isDeposit == true)
emit NewDeposit(prCode, accountId, qty, dW.timestamp, lastTransferId++);
else
emit NewWithdraw(prCode, accountId, qty, dW.timestamp, lastTransferId++);
}
function GetDWrecords(uint32 accountId) view public returns (uint32[] prCode, bool[] isDeposit, uint128[] qty, uint64[] timestamp)
{
require (id_announceLV[accountId] > 1 || accountId == account_id[msg.sender]);
DWrecords storage dWrecords = AccountId_DWrecords[accountId];
uint32 n = dWrecords.N;
prCode = new uint32[](n);
isDeposit = new bool[](n);
qty = new uint128[](n);
timestamp = new uint64[](n);
for (uint32 i = 0; i < n; i++ )
{
prCode[i] = dWrecords.N_DWrecord[i + 1].prCode;
isDeposit[i] = dWrecords.N_DWrecord[i + 1].isDeposit;
qty[i] = dWrecords.N_DWrecord[i + 1].qty;
timestamp[i] = dWrecords.N_DWrecord[i + 1].timestamp;
}
}
function depositETH() payable public
{
//require (msg.value != 0);//?
uint32 accountId = FindOrAddAccount();
prCode_AccountId_Balance[1][accountId].available = Add(prCode_AccountId_Balance[1][accountId].available, uint128(msg.value));
RecordDW(accountId, 1, true, uint104(msg.value));
}
function withdrawETH(uint104 amount) public
{
//require (amount != 0);//?
uint32 accountId = account_id[msg.sender];
require (accountId != 0);
prCode_AccountId_Balance[1][accountId].available = Sub(prCode_AccountId_Balance[1][accountId].available, amount);
require(msg.sender.send(amount));
RecordDW(accountId, 1, false, amount);
}
function depositWithdrawToken(uint128 amount, bool isDeposit, address prAddress) public
{
uint32 prCode = product_prCode[prAddress];
require(amount < maxBalance && prCode != 0);
uint32 accountId = FindOrAddAccount();
//require(accountId != 0);
if (isDeposit == true)
{
require(prCode_productInfo[prCode].isDeposit = true);
require(Token(prAddress).transferFrom(msg.sender, this, amount));
prCode_AccountId_Balance[prCode][accountId].available = Add(prCode_AccountId_Balance[prCode][accountId].available, amount);
require (prCode_AccountId_Balance[prCode][accountId].available < maxBalance);
}
else
{
require(prCode_productInfo[prCode].isWithdraw = true);
prCode_AccountId_Balance[prCode][accountId].available = Sub(prCode_AccountId_Balance[prCode][accountId].available, amount);
require(Token(prAddress).transfer(msg.sender, amount));
}
RecordDW(accountId, prCode, isDeposit, amount);
}
function emergencyWithdrawal(uint32 prCode, uint256 amount) onlyOwner public
{
if (prCode == 1)
msg.sender.transfer(amount);
else
Token(prCode_product[prCode]).transfer(msg.sender, amount);
}
function GetNextTick(bool isAsk, uint80 price, uint8 n) public pure returns (uint80)
{
if (price > 0)
{
uint80 tick = GetTick(price, n);
if (isAsk == true)
return (((price - 1) / tick) + 1) * tick;
else
return (price / tick) * tick;
}
else
{
return price;
}
}
function GetTick(uint80 price, uint8 n) public pure returns (uint80)
{
uint80 x = 1;
for (uint8 i=1; i <= n / 2; i++)
{
x *= 10;
}
if (price < 10 * x)
return 1;
else
{
uint80 tick = 10000;
uint80 priceTenPercent = price / 10 / x;
while (priceTenPercent > tick)
{
tick *= 10;
}
while (priceTenPercent < tick)
{
tick /= 10;
}
if (n % 2 == 1)
{
if (price >= 50 * tick * x)
{
tick *= 5;
}
}
else
{
if (price < 50 * tick * x)
{
tick *= 5;
}
else
{
tick *= 10;
}
}
return tick;
}
}
function LimitOrder(uint32 ownerId, uint32 prTrade, uint32 prBase, bool isSell, uint80 price, uint104 qty) public onlyContractUse returns (uint32)
{
require(price != 0 && price <= maxPrice && qty <= maxQty);
uint32 accountId = account_id[msg.sender];
require(accountId != 0 && ((isSell == false && prCode_productInfo[prTrade].isTradeBid == true && prCode_productInfo[prBase].isTradeAsk == true)
|| (isSell == true && prCode_productInfo[prTrade].isTradeAsk == true && prCode_productInfo[prBase].isTradeBid == true)));
uint80 lastBestPrice;
OrderBook storage orderBook = basePID_tradePID_orderBook[prBase][prTrade];
require (prCode_productInfo[prTrade].minQty <= qty);
if (isSell == true)
{
price = GetNextTick(true, price, orderBook.tickSize);
lastBestPrice = orderBook.bestAskPrice;
}
else
{
price = GetNextTick(false, price, orderBook.tickSize);
lastBestPrice = orderBook.bestBidPrice;
}
Order memory order;
order.ownerId = ownerId;
order.isSell = isSell;
order.prTrade = prTrade;
order.prBase = prBase;
order.accountId = accountId;
order.price = price;
order.qty = qty;
require (IsPossibleLimit(order));
emit NewOrder(order.prTrade, order.prBase, order.accountId, ++lastOrderId, order.isSell, order.price, order.qty, lastNewOrderId++);
BalanceUpdateByLimitAfterTrade(order, qty, matchOrder(orderBook, order, lastOrderId));
if (order.qty != 0)
{
uint80 priceNext;
uint80 price0;
if (isSell == true)
{
price0 = orderBook.bestAskPrice;
if (price0 == 0)
{
orderBook.askPrice_Order[price].prevPrice = 0;
orderBook.askPrice_Order[price].nextPrice = 0;
orderBook.bestAskPrice = price;
}
else if(price < price0)
{
orderBook.askPrice_Order[price0].prevPrice = price;
orderBook.askPrice_Order[price].prevPrice = 0;
orderBook.askPrice_Order[price].nextPrice = price0;
orderBook.bestAskPrice = price;
}
else if (orderBook.askPrice_Order[price].firstId == 0)// .orderN == 0)
{
priceNext = price0;
while (priceNext != 0 && priceNext < price)
{
price0 = priceNext;
priceNext = orderBook.askPrice_Order[price0].nextPrice;
}
orderBook.askPrice_Order[price0].nextPrice = price;
orderBook.askPrice_Order[price].prevPrice = price0;
orderBook.askPrice_Order[price].nextPrice = priceNext;
if (priceNext != 0)
{
orderBook.askPrice_Order[priceNext].prevPrice = price;
}
}
OrderLink storage orderLink = orderBook.askPrice_Order[price];
}
else
{
price0 = orderBook.bestBidPrice;
if (price0 == 0)
{
orderBook.bidPrice_Order[price].prevPrice = 0;
orderBook.bidPrice_Order[price].nextPrice = 0;
orderBook.bestBidPrice = price;
}
else if (price > price0)
{
orderBook.bidPrice_Order[price0].prevPrice = price;
orderBook.bidPrice_Order[price].prevPrice = 0;
orderBook.bidPrice_Order[price].nextPrice = price0;
orderBook.bestBidPrice = price;
}
else if (orderBook.bidPrice_Order[price].firstId == 0)// .orderN == 0)
{
priceNext = price0;
while (priceNext != 0 && priceNext > price)
{
price0 = priceNext;
priceNext = orderBook.bidPrice_Order[price0].nextPrice;
}
orderBook.bidPrice_Order[price0].nextPrice = price;
orderBook.bidPrice_Order[price].prevPrice = price0;
orderBook.bidPrice_Order[price].nextPrice = priceNext;
if (priceNext != 0)
{
orderBook.bidPrice_Order[priceNext].prevPrice = price;
}
}
orderLink = orderBook.bidPrice_Order[price];
}
if (lastOrderId != 0)
{
orderLink.id_orderList[lastOrderId].prev = orderLink.lastId;// .firstID;
if (orderLink.firstId != 0)
{
orderLink.id_orderList[orderLink.lastId].next = lastOrderId;
}
else
{
orderLink.id_orderList[lastOrderId].next = 0;
orderLink.firstId = lastOrderId;
}
orderLink.lastId = lastOrderId;
}
AddOpenOrder(accountId, lastOrderId);
id_Order[lastOrderId] = order;
}
if (isSell == true && lastBestPrice != orderBook.bestAskPrice)
{
emit NewBestBidAsk(prTrade, prBase, isSell, orderBook.bestAskPrice);
}
if (isSell == false && lastBestPrice != orderBook.bestBidPrice)
{
emit NewBestBidAsk(prTrade, prBase, isSell, orderBook.bestBidPrice);
}
return lastOrderId;
}
function BalanceUpdateByLimitAfterTrade(Order order, uint104 qty, uint104 tradedQty) private
{
uint32 ownerId = order.ownerId;
uint32 accountId = order.accountId;
uint32 prTrade = order.prTrade;
uint32 prBase = order.prBase;
uint80 price = order.price;
uint104 orderQty = order.qty;
uint32 prTemp;
if (order.isSell)
{
Balance storage balance = prCode_AccountId_Balance[prTrade][accountId];
balance.available = Sub(balance.available, qty);
if (orderQty != 0)
balance.reserved = Add(balance.reserved, orderQty);
prTemp = prBase;
}
else
{
balance = prCode_AccountId_Balance[prBase][accountId];
if (orderQty != 0)
{
uint256 temp = prCode_productInfo[prBase].divider * orderQty * price / basePrice / prCode_productInfo[prTrade].divider;
require (temp < maxQty);
balance.available = Sub(balance.available, tradedQty + uint104(temp));
balance.reserved = Add(balance.reserved, uint104(temp));
}
else
{
balance.available = Sub(balance.available, tradedQty);
}
tradedQty = qty - orderQty;
prTemp = prTrade;
}
if (tradedQty != 0)
{
uint104 takeFeeMain = tradedQty * takerFeeRateMain / 10000;
uint104 takeFeeLocal = tradedQty * ownerId_takerFeeRateLocal[ownerId] / 10000;
prCode_AccountId_Balance[prTemp][accountId].available += tradedQty - takeFeeMain - takeFeeLocal;
prCode_AccountId_Balance[prTemp][ownerId_accountId[1]].available += takeFeeMain;
prCode_AccountId_Balance[prTemp][ownerId_accountId[ownerId]].available += takeFeeLocal;
}
}
function IsPossibleLimit(Order memory order) private view returns (bool)
{
if (order.isSell)
{
if (prCode_AccountId_Balance[order.prTrade][order.accountId].available >= order.qty)
return true;
else
return false;
}
else
{
if (prCode_AccountId_Balance[order.prBase][order.accountId].available >= prCode_productInfo[order.prBase].divider * order.qty * order.price / basePrice / prCode_productInfo[order.prTrade].divider)
return true;
else
return false;
}
}
function matchOrder(OrderBook storage ob, Order memory order, uint32 id) private returns (uint104)
{
uint32 prTrade = order.prTrade;
uint32 prBase = order.prBase;
uint80 tradePrice;
if (order.isSell == true)
tradePrice = ob.bestBidPrice;
else
tradePrice = ob.bestAskPrice;
bool isBestPriceUpdate = false;
//OrderLink storage orderLink;// = price_OrderLink[tradePrice];
uint104 qtyBase = 0;
//Order storage matchingOrder;
uint104 tradeAmount;
while (tradePrice != 0 && order.qty > 0 && ((order.isSell && order.price <= tradePrice) || (!order.isSell && order.price >= tradePrice)))
{
if (order.isSell == true)
OrderLink storage orderLink = ob.bidPrice_Order[tradePrice];
else
orderLink = ob.askPrice_Order[tradePrice];
uint32 orderId = orderLink.firstId;
while (orderLink.firstId != 0 && orderId != 0 && order.qty != 0)
{
Order storage matchingOrder = id_Order[orderId];
if (matchingOrder.qty >= order.qty)
{
tradeAmount = order.qty;
matchingOrder.qty -= order.qty;
order.qty = 0;
}
else
{
tradeAmount = matchingOrder.qty;
order.qty -= matchingOrder.qty;
matchingOrder.qty = 0;
}
qtyBase += BalanceUpdateByTradeCp(order, matchingOrder, tradeAmount);
uint32 orderAccountID = order.accountId;
if (order.isSell == true)
emit NewTrade(prTrade, prBase, orderId, id, matchingOrder.accountId, orderAccountID, true, tradePrice, tradeAmount, lastTradeId++, uint64(now));
else
emit NewTrade(prTrade, prBase, id, orderId, orderAccountID, matchingOrder.accountId, false, tradePrice, tradeAmount, lastTradeId++, uint64(now));
if (matchingOrder.qty != 0)
{
//id_Order[tradePrice] = matchingOrder;
break;
}
else
{
if (RemoveOrder(prTrade, prBase, matchingOrder.isSell, tradePrice, orderId) == true)
{
RemoveOpenOrder(matchingOrder.accountId, orderId);
}
orderId = orderLink.firstId;
}
}
//emit NewHogaChange(prTrade, prBase, !order.isSell, tradePrice);
if (orderLink.firstId == 0)// .orderN == 0)
{
tradePrice = orderLink.nextPrice;
isBestPriceUpdate = true;
}
}
if (isBestPriceUpdate == true)
{
if (order.isSell)
{
ob.bestBidPrice = tradePrice;
}
else
{
ob.bestAskPrice = tradePrice;
}
emit NewBestBidAsk(prTrade, prBase, !order.isSell, tradePrice);
}
return qtyBase;
}
function BalanceUpdateByTradeCp(Order order, Order matchingOrder, uint104 tradeAmount) private returns (uint104)
{
uint32 accountId = matchingOrder.accountId;
uint32 prTrade = order.prTrade;
uint32 prBase = order.prBase;
require (tradeAmount < maxQty);
uint256 qtyBase = prCode_productInfo[prBase].divider * tradeAmount * matchingOrder.price / basePrice / prCode_productInfo[prTrade].divider;
require (qtyBase < maxQty);
Balance storage balanceTrade = prCode_AccountId_Balance[prTrade][accountId];
Balance storage balanceBase = prCode_AccountId_Balance[prBase][accountId];
if (order.isSell == true)
{
balanceTrade.available = SafeMath.Add(balanceTrade.available, tradeAmount);
balanceBase.reserved = SafeMath.Sub(balanceBase.reserved, uint104(qtyBase));
}
else
{
balanceTrade.reserved = SafeMath.Sub(balanceTrade.reserved, tradeAmount);
balanceBase.available = SafeMath.Add(balanceBase.available, uint104(qtyBase));
}
return uint104(qtyBase);
}
function RemoveOrder(uint32 prTrade, uint32 prBase, bool isSell, uint80 price, uint32 id) private returns (bool)
{
OrderBook storage ob = basePID_tradePID_orderBook[prBase][prTrade];
if (isSell == false)
{
OrderLink storage orderLink = ob.bidPrice_Order[price];
}
else
{
orderLink = ob.askPrice_Order[price];
}
if (id != 0)
{
ListItem memory removeItem = orderLink.id_orderList[id];
if (removeItem.next != 0)
{
orderLink.id_orderList[removeItem.next].prev = removeItem.prev;
}
if (removeItem.prev != 0)
{
orderLink.id_orderList[removeItem.prev].next = removeItem.next;
}
if (id == orderLink.lastId)
{
orderLink.lastId = removeItem.prev;
}
if (id == orderLink.firstId)
{
orderLink.firstId = removeItem.next;
}
delete orderLink.id_orderList[id];
if (orderLink.firstId == 0)
{
if (orderLink.nextPrice != 0)
{
if (isSell == true)
OrderLink storage replaceLink = ob.askPrice_Order[orderLink.nextPrice];
else
replaceLink = ob.bidPrice_Order[orderLink.nextPrice];
replaceLink.prevPrice = orderLink.prevPrice;
}
if (orderLink.prevPrice != 0)
{
if (isSell == true)
replaceLink = ob.askPrice_Order[orderLink.prevPrice];
else
replaceLink = ob.bidPrice_Order[orderLink.prevPrice];
replaceLink.nextPrice = orderLink.nextPrice;
}
if (price == ob.bestAskPrice)
{
ob.bestAskPrice = orderLink.nextPrice;
}
if (price == ob.bestBidPrice)
{
ob.bestBidPrice = orderLink.nextPrice;
}
}
return true;
}
else
{
return false;
}
}
function cancelOrders(uint32[] id) public returns(bool[])
{
//uint256 orderN = id.length;
bool[] memory cancelResults = new bool[](id.length);
for (uint32 i = 0; i < id.length; i++)
{
cancelOrder(id[i]);
}
return cancelResults;
}
function cancelOrder(uint32 id) public returns (bool)
{
Order memory order = id_Order[id];
uint32 accountId = account_id[msg.sender];
require(order.accountId == accountId);
uint32 prTrade = order.prTrade;
uint32 prBase = order.prBase;
bool isSell = order.isSell;
uint80 price = order.price;
uint104 qty = order.qty;
if (RemoveOrder(prTrade, prBase, isSell, price, id) == false)
return false;
else
{