forked from KomodoPlatform/komodo-defi-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lp_ordermatch.rs
1579 lines (1450 loc) · 58.9 KB
/
lp_ordermatch.rs
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
/******************************************************************************
* Copyright © 2014-2019 The SuperNET Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* SuperNET software, including this file may be copied, modified, propagated *
* or distributed except according to the terms contained in the LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
//
// ordermatch.rs
// marketmaker
//
#![cfg_attr(not(feature = "native"), allow(dead_code))]
use bigdecimal::BigDecimal;
use bitcrypto::sha256;
use coins::{lp_coinfind, MmCoinEnum, TradeInfo};
use coins::utxo::{compressed_pub_key_from_priv_raw, ChecksumType};
use common::{bits256, HyRes, json_dir_entries, new_uuid, rpc_response, rpc_err_response};
use common::mm_ctx::{from_ctx, MmArc, MmWeak};
use common::mm_number::{from_dec_to_ratio, from_ratio_to_dec, MmNumber};
use futures01::future::{Either, Future};
use gstuff::{now_ms, slurp};
use keys::{Public, Signature};
#[cfg(test)]
use mocktopus::macros::*;
use num_rational::BigRational;
use num_traits::cast::ToPrimitive;
use num_traits::identities::Zero;
use primitives::hash::H256;
use rpc::v1::types::{H256 as H256Json};
use serde_json::{self as json, Value as Json};
use std::collections::HashSet;
use std::collections::hash_map::{Entry, HashMap};
use std::fs::{self, DirEntry};
use std::path::{PathBuf};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use std::thread;
use uuid::Uuid;
use crate::mm2::lp_swap::{dex_fee_amount, get_locked_amount, MakerSwap, run_maker_swap, run_taker_swap, TakerSwap};
#[cfg(test)]
#[path = "ordermatch_tests.rs"]
mod ordermatch_tests;
#[derive(Clone, Debug, Deserialize, Serialize)]
enum TakerAction {
Buy,
Sell,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
struct TakerRequest {
base: String,
rel: String,
base_amount: BigDecimal,
base_amount_rat: Option<BigRational>,
rel_amount: BigDecimal,
rel_amount_rat: Option<BigRational>,
action: TakerAction,
uuid: Uuid,
method: String,
sender_pubkey: H256Json,
dest_pub_key: H256Json,
}
impl TakerRequest {
fn get_base_amount(&self) -> MmNumber {
match &self.base_amount_rat {
Some(r) => r.clone().into(),
None => self.base_amount.clone().into()
}
}
fn get_rel_amount(&self) -> MmNumber {
match &self.rel_amount_rat {
Some(r) => r.clone().into(),
None => self.rel_amount.clone().into()
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
struct TakerOrder {
created_at: u64,
request: TakerRequest,
matches: HashMap<Uuid, TakerMatch>
}
/// Result of match_reserved function
#[derive(Debug, PartialEq)]
enum MatchReservedResult {
/// Order and reserved message matched,
Matched,
/// Order and reserved didn't match
NotMatched,
}
impl TakerOrder {
fn is_cancellable(&self) -> bool {
self.matches.is_empty()
}
fn match_reserved(&self, reserved: &MakerReserved) -> MatchReservedResult {
let my_base_amount: MmNumber = self.request.get_base_amount();
let my_rel_amount: MmNumber = self.request.get_rel_amount();
let other_base_amount: MmNumber = reserved.get_base_amount();
let other_rel_amount: MmNumber = reserved.get_rel_amount();
match self.request.action {
TakerAction::Buy => if self.request.base == reserved.base && self.request.rel == reserved.rel
&& my_base_amount == other_base_amount && other_rel_amount <= my_rel_amount {
MatchReservedResult::Matched
} else {
MatchReservedResult::NotMatched
},
TakerAction::Sell => if self.request.base == reserved.rel && self.request.rel == reserved.base
&& my_base_amount == other_rel_amount && my_rel_amount <= other_base_amount {
MatchReservedResult::Matched
} else {
MatchReservedResult::NotMatched
}
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
/// Market maker order
/// The "action" is missing here because it's easier to always consider maker order as "sell"
/// So upon ordermatch with request we have only 2 combinations "sell":"sell" and "sell":"buy"
/// Adding "action" to maker order will just double possible combinations making order match more complex.
pub struct MakerOrder {
pub max_base_vol: BigDecimal,
#[serde(default = "zero_rat")]
pub max_base_vol_rat: BigRational,
pub min_base_vol: BigDecimal,
#[serde(default = "zero_rat")]
pub min_base_vol_rat: BigRational,
pub price: BigDecimal,
#[serde(default = "zero_rat")]
pub price_rat: BigRational,
pub created_at: u64,
pub base: String,
pub rel: String,
matches: HashMap<Uuid, MakerMatch>,
started_swaps: Vec<Uuid>,
uuid: Uuid,
}
fn zero_rat() -> BigRational { BigRational::zero() }
impl MakerOrder {
fn available_amount(&self) -> MmNumber {
let reserved: MmNumber = self.matches.iter().fold(
MmNumber::from(BigRational::from_integer(0.into())),
|reserved, (_, order_match)| reserved + order_match.reserved.get_base_amount()
);
MmNumber::from(self.max_base_vol_rat.clone()) - reserved
}
fn is_cancellable(&self) -> bool {
!self.has_ongoing_matches()
}
fn has_ongoing_matches(&self) -> bool {
for (_, order_match) in self.matches.iter() {
// if there's at least 1 ongoing match the order is not cancellable
if order_match.connected.is_none() && order_match.connect.is_none() {
return true;
}
}
return false;
}
}
impl Into<MakerOrder> for TakerOrder {
fn into(self) -> MakerOrder {
let order = match self.request.action {
TakerAction::Sell => MakerOrder {
price: &self.request.rel_amount / &self.request.base_amount,
price_rat: (self.request.get_rel_amount() / self.request.get_base_amount()).into(),
max_base_vol_rat: self.request.get_base_amount().into(),
max_base_vol: self.request.base_amount,
min_base_vol_rat: BigRational::from_integer(0.into()),
min_base_vol: 0.into(),
created_at: now_ms(),
base: self.request.base,
rel: self.request.rel,
matches: HashMap::new(),
started_swaps: Vec::new(),
uuid: self.request.uuid,
},
// The "buy" taker order is recreated with reversed pair as Maker order is always considered as "sell"
TakerAction::Buy => MakerOrder {
price: &self.request.base_amount / &self.request.rel_amount,
price_rat: (self.request.get_base_amount() / self.request.get_rel_amount()).into(),
max_base_vol_rat: self.request.get_rel_amount().into(),
max_base_vol: self.request.rel_amount,
min_base_vol: 0.into(),
min_base_vol_rat: BigRational::from_integer(0.into()),
created_at: now_ms(),
base: self.request.rel,
rel: self.request.base,
matches: HashMap::new(),
started_swaps: Vec::new(),
uuid: self.request.uuid,
},
};
order
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
struct TakerConnect {
taker_order_uuid: Uuid,
maker_order_uuid: Uuid,
method: String,
sender_pubkey: H256Json,
dest_pub_key: H256Json,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
struct MakerReserved {
base: String,
rel: String,
base_amount: BigDecimal,
base_amount_rat: Option<BigRational>,
rel_amount: BigDecimal,
rel_amount_rat: Option<BigRational>,
taker_order_uuid: Uuid,
maker_order_uuid: Uuid,
method: String,
sender_pubkey: H256Json,
dest_pub_key: H256Json,
}
impl MakerReserved {
fn get_base_amount(&self) -> MmNumber {
match &self.base_amount_rat {
Some(r) => r.clone().into(),
None => self.base_amount.clone().into()
}
}
fn get_rel_amount(&self) -> MmNumber {
match &self.rel_amount_rat {
Some(r) => r.clone().into(),
None => self.rel_amount.clone().into()
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
struct MakerConnected {
taker_order_uuid: Uuid,
maker_order_uuid: Uuid,
method: String,
sender_pubkey: H256Json,
dest_pub_key: H256Json,
}
struct OrdermatchContext {
pub my_maker_orders: Mutex<HashMap<Uuid, MakerOrder>>,
pub my_taker_orders: Mutex<HashMap<Uuid, TakerOrder>>,
pub my_cancelled_orders: Mutex<HashMap<Uuid, MakerOrder>>,
pub orderbook: Mutex<HashMap<(String, String), HashMap<Uuid, PricePingRequest>>>,
}
impl OrdermatchContext {
/// Obtains a reference to this crate context, creating it if necessary.
fn from_ctx (ctx: &MmArc) -> Result<Arc<OrdermatchContext>, String> {
Ok (try_s! (from_ctx (&ctx.ordermatch_ctx, move || {
Ok (OrdermatchContext {
my_taker_orders: Mutex::new (HashMap::default()),
my_maker_orders: Mutex::new (HashMap::default()),
my_cancelled_orders: Mutex::new (HashMap::default()),
orderbook: Mutex::new (HashMap::default()),
})
})))
}
/// Obtains a reference to this crate context, creating it if necessary.
#[allow(dead_code)]
fn from_ctx_weak (ctx_weak: &MmWeak) -> Result<Arc<OrdermatchContext>, String> {
let ctx = try_s! (MmArc::from_weak (ctx_weak) .ok_or ("Context expired"));
Self::from_ctx (&ctx)
}
}
fn lp_connect_start_bob(ctx: &MmArc, maker_match: &MakerMatch) -> i32 {
let mut retval = -1;
let loop_thread = thread::Builder::new().name("maker_loop".into()).spawn({
let taker_coin = match lp_coinfind (&ctx, &maker_match.reserved.rel) {
Ok(Some(c)) => c,
Ok(None) => {
log!("Coin " (maker_match.reserved.rel) " is not found/enabled");
return -1;
}
Err(e) => {
log!("!lp_coinfind(" (maker_match.reserved.rel) "): " (e));
return -1;
}
};
let maker_coin = match lp_coinfind (&ctx, &maker_match.reserved.base) {
Ok(Some(c)) => c,
Ok(None) => {
log!("Coin " (maker_match.reserved.base) " is not found/enabled");
return -1;
}
Err(e) => {
log!("!lp_coinfind(" (maker_match.reserved.base) "): " (e));
return -1;
}
};
let ctx = ctx.clone();
let mut alice = bits256::default();
alice.bytes = maker_match.request.sender_pubkey.0;
let maker_amount = maker_match.reserved.get_base_amount().into();
let taker_amount = maker_match.reserved.get_rel_amount().into();
let privkey = &ctx.secp256k1_key_pair().private().secret;
let my_persistent_pub = unwrap!(compressed_pub_key_from_priv_raw(&privkey[..], ChecksumType::DSHA256));
let uuid = maker_match.request.uuid.to_string();
move || {
log!("Entering the maker_swap_loop " (maker_coin.ticker()) "/" (taker_coin.ticker()));
let maker_swap = MakerSwap::new(
ctx,
alice.into(),
maker_coin,
taker_coin,
maker_amount,
taker_amount,
my_persistent_pub,
uuid,
);
run_maker_swap(maker_swap, None);
}
});
match loop_thread {
Ok(_h) => {
retval = 0;
},
Err(e) => {
log!({ "Got error launching bob swap loop: {}", e });
}
}
retval
}
fn lp_connected_alice(ctx: &MmArc, taker_match: &TakerMatch) { // alice
let alice_loop_thread = thread::Builder::new().name("taker_loop".into()).spawn({
let ctx = ctx.clone();
let mut maker = bits256::default();
maker.bytes = taker_match.reserved.sender_pubkey.0;
let taker_coin = match lp_coinfind (&ctx, &taker_match.reserved.rel) {
Ok(Some(c)) => c,
Ok(None) => {
log!("Coin " (taker_match.reserved.rel) " is not found/enabled");
return;
}
Err(e) => {
log!("!lp_coinfind(" (taker_match.reserved.rel) "): " (e));
return;
}
};
let maker_coin = match lp_coinfind (&ctx, &taker_match.reserved.base) {
Ok(Some(c)) => c,
Ok(None) => {
log!("Coin " (taker_match.reserved.base) " is not found/enabled");
return;
}
Err(e) => {
log!("!lp_coinfind(" (taker_match.reserved.base) "): " (e));
return;
}
};
let privkey = &ctx.secp256k1_key_pair().private().secret;
let my_persistent_pub = unwrap!(compressed_pub_key_from_priv_raw(&privkey[..], ChecksumType::DSHA256));
let maker_amount = taker_match.reserved.get_base_amount().into();
let taker_amount = taker_match.reserved.get_rel_amount().into();
let uuid = taker_match.reserved.taker_order_uuid.to_string();
move || {
log!("Entering the taker_swap_loop " (maker_coin.ticker()) "/" (taker_coin.ticker()));
let taker_swap = TakerSwap::new(
ctx,
maker.into(),
maker_coin,
taker_coin,
maker_amount,
taker_amount,
my_persistent_pub,
uuid,
);
run_taker_swap(taker_swap, None);
}
});
match alice_loop_thread {
Ok(_) => (),
Err(e) => {
log!({ "Got error trying to start taker loop {}", e });
}
}
}
pub fn lp_ordermatch_loop(ctx: MmArc) {
const ORDERMATCH_TIMEOUT: u64 = 30000;
let mut last_price_broadcast = 0;
loop {
if ctx.is_stopping() { break }
let ordermatch_ctx = unwrap!(OrdermatchContext::from_ctx(&ctx));
let mut my_taker_orders = unwrap!(ordermatch_ctx.my_taker_orders.lock());
let mut my_maker_orders = unwrap!(ordermatch_ctx.my_maker_orders.lock());
let mut my_cancelled_orders = unwrap!(ordermatch_ctx.my_cancelled_orders.lock());
// move the timed out and unmatched taker orders to maker
*my_taker_orders = my_taker_orders.drain().filter_map(|(uuid, order)| if order.created_at + ORDERMATCH_TIMEOUT < now_ms() {
delete_my_taker_order(&ctx, &order);
if order.matches.is_empty() {
let maker_order = order.into();
save_my_maker_order(&ctx, &maker_order);
my_maker_orders.insert(uuid, maker_order);
}
None
} else {
Some((uuid, order))
}).collect();
// remove timed out unfinished matches to unlock the reserved amount
my_maker_orders.iter_mut().for_each(|(_, order)| {
order.matches = order.matches.drain().filter(
|(_, order_match)| order_match.last_updated + ORDERMATCH_TIMEOUT > now_ms() || order_match.connected.is_some()
).collect();
save_my_maker_order(&ctx, order);
});
*my_maker_orders = my_maker_orders.drain().filter_map(|(uuid, order)| {
let min_amount: BigDecimal = "0.00777".parse().unwrap();
let min_amount: MmNumber = min_amount.into();
if order.available_amount() <= min_amount && !order.has_ongoing_matches() {
delete_my_maker_order(&ctx, &order);
my_cancelled_orders.insert(uuid, order);
None
} else {
Some((uuid, order))
}
}).collect();
drop(my_taker_orders);
drop(my_maker_orders);
drop(my_cancelled_orders);
if now_ms() > last_price_broadcast + 10000 {
if let Err(e) = broadcast_my_maker_orders(&ctx) {
ctx.log.log("", &[&"broadcast_my_maker_orders"], &format!("error {}", e));
}
last_price_broadcast = now_ms();
}
let mut orderbook = unwrap!(ordermatch_ctx.orderbook.lock());
*orderbook = orderbook.drain().filter_map(|((base, rel), mut pair_orderbook)| {
pair_orderbook = pair_orderbook.drain().filter_map(|(pubkey, order)| if now_ms() / 1000 > order.timestamp + 30 {
None
} else {
Some((pubkey, order))
}).collect();
if pair_orderbook.is_empty() {
None
} else {
Some(((base, rel), pair_orderbook))
}
}).collect();
drop(orderbook);
thread::sleep(Duration::from_secs(1));
}
}
pub fn lp_trade_command(
ctx: MmArc,
json: Json,
) -> i32 {
let method = json["method"].as_str();
let ordermatch_ctx = unwrap!(OrdermatchContext::from_ctx(&ctx));
let our_public_id = unwrap!(ctx.public_id());
if method == Some("reserved") {
let reserved_msg: MakerReserved = match json::from_value(json.clone()) {
Ok(r) => r,
Err(_) => return 1,
};
if H256Json::from(our_public_id.bytes) != reserved_msg.dest_pub_key {
// ignore the messages that do not target our node
return 1;
}
let mut my_taker_orders = unwrap!(ordermatch_ctx.my_taker_orders.lock());
let my_order = match my_taker_orders.entry(reserved_msg.taker_order_uuid) {
Entry::Vacant(_) => {
log!("Our node doesn't have the order with uuid " (reserved_msg.taker_order_uuid));
return 1;
},
Entry::Occupied(entry) => entry.into_mut()
};
if my_order.request.dest_pub_key != H256Json::default() && my_order.request.dest_pub_key != reserved_msg.sender_pubkey {
log!("got reserved response from different node " (hex::encode(&reserved_msg.sender_pubkey.0)));
return 1;
}
// send "connect" message if reserved message targets our pubkey AND
// reserved amounts match our order AND order is NOT reserved by someone else (empty matches)
if my_order.match_reserved(&reserved_msg) == MatchReservedResult::Matched && my_order.matches.is_empty() {
let connect = TakerConnect {
sender_pubkey: H256Json::from(our_public_id.bytes),
dest_pub_key: reserved_msg.sender_pubkey.clone(),
method: "connect".into(),
taker_order_uuid: reserved_msg.taker_order_uuid,
maker_order_uuid: reserved_msg.maker_order_uuid,
};
ctx.broadcast_p2p_msg(&unwrap!(json::to_string(&connect)));
let taker_match = TakerMatch {
reserved: reserved_msg,
connect,
connected: None,
last_updated: now_ms(),
};
my_order.matches.insert(taker_match.reserved.maker_order_uuid, taker_match);
save_my_taker_order(&ctx, &my_order);
}
return 1;
}
if method == Some("connected") {
let connected: MakerConnected = match json::from_value(json.clone()) {
Ok(c) => c,
Err(_) => return 1,
};
if H256Json::from(our_public_id.bytes) == connected.dest_pub_key && H256Json::from(our_public_id.bytes) != connected.sender_pubkey {
let mut my_taker_orders = unwrap!(ordermatch_ctx.my_taker_orders.lock());
let my_order_entry = match my_taker_orders.entry(connected.taker_order_uuid) {
Entry::Occupied(e) => e,
Entry::Vacant(_) => {
log!("Our node doesn't have the order with uuid "(connected.taker_order_uuid));
return 1;
},
};
let order_match = match my_order_entry.get().matches.get(&connected.maker_order_uuid) {
Some(o) => o,
None => {
log!("Our node doesn't have the match with uuid "(connected.maker_order_uuid));
return 1;
}
};
// alice
lp_connected_alice(
&ctx,
order_match,
);
// remove the matched order immediately
delete_my_taker_order(&ctx, &my_order_entry.get());
my_order_entry.remove();
// AG: Bob's p2p ID (`LP_mypub25519`) is in `json["srchash"]`.
log!("CONNECTED.(" (json) ")");
}
return 1;
}
// bob
if method == Some("request") {
let taker_request: TakerRequest = match json::from_value(json.clone()) {
Ok(r) => r,
Err(_) => return 1,
};
if our_public_id.bytes == taker_request.dest_pub_key.0 {
log!("Skip the request originating from our pubkey");
return 1;
}
let ordermatch_ctx = unwrap!(OrdermatchContext::from_ctx(&ctx));
let mut my_orders = unwrap!(ordermatch_ctx.my_maker_orders.lock());
for (uuid, order) in my_orders.iter_mut() {
if let OrderMatchResult::Matched((base_amount, rel_amount)) = match_order_and_request(order, &taker_request) {
let reserved = MakerReserved {
dest_pub_key: taker_request.sender_pubkey.clone(),
sender_pubkey: our_public_id.bytes.into(),
base: order.base.clone(),
base_amount: base_amount.clone().into(),
base_amount_rat: Some(base_amount.into()),
rel_amount: rel_amount.clone().into(),
rel_amount_rat: Some(rel_amount.into()),
rel: order.rel.clone(),
method: "reserved".into(),
taker_order_uuid: taker_request.uuid,
maker_order_uuid: *uuid,
};
ctx.broadcast_p2p_msg(&unwrap!(json::to_string(&reserved)));
let maker_match = MakerMatch {
request: taker_request,
reserved,
connect: None,
connected: None,
last_updated: now_ms(),
};
order.matches.insert(maker_match.request.uuid, maker_match);
save_my_maker_order(&ctx, &order);
return 1;
}
}
}
if method == Some("connect") {
// bob
let connect_msg: TakerConnect = match json::from_value(json.clone()) {
Ok(m) => m,
Err(_) => return 1,
};
if our_public_id.bytes == connect_msg.dest_pub_key.0 && our_public_id.bytes != connect_msg.sender_pubkey.0 {
let mut maker_orders = unwrap!(ordermatch_ctx.my_maker_orders.lock());
let my_order = match maker_orders.get_mut(&connect_msg.maker_order_uuid) {
Some(o) => o,
None => {
log!("Our node doesn't have the order with uuid " (connect_msg.maker_order_uuid));
return 1;
},
};
let order_match = match my_order.matches.get_mut(&connect_msg.taker_order_uuid) {
Some(o) => o,
None => {
log!("Our node doesn't have the match with uuid " (connect_msg.taker_order_uuid));
return 1;
},
};
let connected = MakerConnected {
sender_pubkey: our_public_id.bytes.into(),
dest_pub_key: connect_msg.sender_pubkey.clone(),
taker_order_uuid: connect_msg.taker_order_uuid,
maker_order_uuid: connect_msg.maker_order_uuid,
method: "connected".into(),
};
ctx.broadcast_p2p_msg(&unwrap!(json::to_string(&connected)));
order_match.connect = Some(connect_msg);
order_match.connected = Some(connected);
my_order.started_swaps.push(order_match.request.uuid);
lp_connect_start_bob(&ctx, order_match);
save_my_maker_order(&ctx, &my_order);
}
return 1;
}
-1
}
fn check_locked_coins(ctx: &MmArc, amount: &MmNumber, balance: &BigDecimal, ticker: &str) -> impl Future<Item=(), Error=String> {
let locked = get_locked_amount(ctx, ticker);
let available = balance - &locked;
if amount > &available {
futures01::future::err(ERRL!("The {} amount {} is larger than available {:.8}, balance: {}, locked by swaps: {:.8}", ticker, amount, available, balance, locked))
} else {
futures01::future::ok(())
}
}
#[derive(Deserialize, Debug)]
pub struct AutoBuyInput {
base: String,
rel: String,
price: MmNumber,
volume: MmNumber,
timeout: Option<u32>,
/// Not used. Deprecated.
duration: Option<u32>,
// TODO: remove this field on API refactoring, method should be separated from params
method: String,
gui: Option<String>,
#[serde(rename="destpubkey")]
#[serde(default)]
dest_pub_key: H256Json
}
pub fn buy(ctx: MmArc, json: Json) -> HyRes {
let input: AutoBuyInput = try_h!(json::from_value(json.clone()));
if input.base == input.rel {
return rpc_err_response(500, "Base and rel must be different coins");
}
let rel_coin = try_h!(lp_coinfind(&ctx, &input.rel));
let rel_coin = match rel_coin {
Some(c) => c,
None => return rpc_err_response(500, "Rel coin is not found or inactive")
};
let base_coin = try_h!(lp_coinfind(&ctx, &input.base));
let base_coin: MmCoinEnum = match base_coin {
Some(c) => c,
None => return rpc_err_response(500, "Base coin is not found or inactive")
};
let my_amount = &input.volume * &input.price;
Box::new(rel_coin.my_balance().and_then(move |my_balance| {
check_locked_coins(&ctx, &my_amount, &my_balance, rel_coin.ticker()).and_then(move |_| {
let dex_fee = dex_fee_amount(base_coin.ticker(), rel_coin.ticker(), &my_amount.clone().into());
let trade_info = TradeInfo::Taker(dex_fee);
rel_coin.check_i_have_enough_to_trade(&my_amount.clone().into(), &my_balance.clone().into(), trade_info).and_then(move |_|
base_coin.can_i_spend_other_payment().and_then(move |_|
rpc_response(200, try_h!(lp_auto_buy(&ctx, input)))
)
)
})
}))
}
pub fn sell(ctx: MmArc, json: Json) -> HyRes {
let input: AutoBuyInput = try_h!(json::from_value(json.clone()));
if input.base == input.rel {
return rpc_err_response(500, "Base and rel must be different coins");
}
let base_coin = try_h!(lp_coinfind(&ctx, &input.base));
let base_coin = match base_coin {
Some(c) => c,
None => return rpc_err_response(500, "Base coin is not found or inactive")
};
let rel_coin = try_h!(lp_coinfind(&ctx, &input.rel));
let rel_coin = match rel_coin {
Some(c) => c,
None => return rpc_err_response(500, "Rel coin is not found or inactive")
};
Box::new(base_coin.my_balance().and_then(move |my_balance| {
check_locked_coins(&ctx, &input.volume, &my_balance, base_coin.ticker()).and_then(move |_| {
let dex_fee = dex_fee_amount(base_coin.ticker(), rel_coin.ticker(), &input.volume.clone().into());
let trade_info = TradeInfo::Taker(dex_fee);
base_coin.check_i_have_enough_to_trade(&input.volume.clone().into(), &my_balance.clone().into(), trade_info).and_then(move |_|
rel_coin.can_i_spend_other_payment().and_then(move |_|
rpc_response(200, try_h!(lp_auto_buy(&ctx, input)))
)
)
})
}))
}
/// Created when maker order is matched with taker request
#[derive(Clone, Debug, Deserialize, Serialize)]
struct MakerMatch {
request: TakerRequest,
reserved: MakerReserved,
connect: Option<TakerConnect>,
connected: Option<MakerConnected>,
last_updated: u64,
}
/// Created upon taker request broadcast
#[derive(Clone, Debug, Deserialize, Serialize)]
struct TakerMatch {
reserved: MakerReserved,
connect: TakerConnect,
connected: Option<MakerConnected>,
last_updated: u64,
}
pub fn lp_auto_buy(ctx: &MmArc, input: AutoBuyInput) -> Result<String, String> {
if input.price < MmNumber::from(BigRational::new(1.into(), 100000000.into())) {
return ERR!("Price is too low, minimum is 0.00000001");
}
let action = match Some(input.method.as_ref()) {
Some("buy") => {
TakerAction::Buy
},
Some("sell") => {
TakerAction::Sell
},
_ => return ERR!("Auto buy must be called only from buy/sell RPC methods")
};
let ordermatch_ctx = try_s!(OrdermatchContext::from_ctx(&ctx));
let mut my_taker_orders = try_s!(ordermatch_ctx.my_taker_orders.lock());
let uuid = new_uuid();
let our_public_id = try_s!(ctx.public_id());
let rel_volume = &input.volume * &input.price;
let request = TakerRequest {
base: input.base,
rel: input.rel,
rel_amount: rel_volume.clone().into(),
rel_amount_rat: Some(rel_volume.into()),
base_amount: input.volume.clone().into(),
base_amount_rat: Some(BigRational::from(input.volume)),
method: "request".into(),
uuid,
dest_pub_key: input.dest_pub_key,
sender_pubkey: H256Json::from(our_public_id.bytes),
action,
};
ctx.broadcast_p2p_msg(&unwrap!(json::to_string(&request)));
let result = json!({
"result": request
}).to_string();
let order = TakerOrder {
created_at: now_ms(),
matches: HashMap::new(),
request,
};
save_my_taker_order(ctx, &order);
my_taker_orders.insert(uuid, order);
drop(my_taker_orders);
Ok(result)
}
fn price_ping_sig_hash(timestamp: u32, pubsecp: &[u8], pubkey: &[u8], base: &[u8], rel: &[u8], price64: u64) -> H256 {
let mut input = vec![];
input.extend_from_slice(×tamp.to_le_bytes());
input.extend_from_slice(pubsecp);
input.extend_from_slice(pubkey);
input.extend_from_slice(base);
input.extend_from_slice(rel);
input.extend_from_slice(&price64.to_le_bytes());
sha256(&input)
}
#[derive(Debug, Deserialize, Serialize)]
struct PricePingRequest {
method: String,
pubkey: String,
base: String,
rel: String,
price: BigDecimal,
price_rat: Option<BigRational>,
price64: String,
timestamp: u64,
pubsecp: String,
sig: String,
// TODO rename, it's called "balance", but it's actual meaning is max available volume to trade
#[serde(rename="bal")]
balance: BigDecimal,
balance_rat: Option<BigRational>,
uuid: Option<Uuid>,
}
impl PricePingRequest {
fn new(ctx: &MmArc, order: &MakerOrder) -> Result<PricePingRequest, String> {
let base_coin = match try_s!(lp_coinfind(ctx, &order.base)) {
Some(coin) => coin,
None => return ERR!("Base coin {} is not found", order.base),
};
let _rel_coin = match try_s!(lp_coinfind(ctx, &order.rel)) {
Some(coin) => coin,
None => return ERR!("Rel coin {} is not found", order.rel),
};
let public_id = try_s!(ctx.public_id());
let price64 = (&order.price * BigDecimal::from(100000000)).to_u64().unwrap();
let timestamp = now_ms() / 1000;
let sig_hash = price_ping_sig_hash(
timestamp as u32,
&**ctx.secp256k1_key_pair().public(),
&public_id.bytes,
order.base.as_bytes(),
order.rel.as_bytes(),
price64,
);
let sig = try_s!(ctx.secp256k1_key_pair().private().sign(&sig_hash));
let available_amount: BigRational = order.available_amount().into();
let min_amount = BigRational::new(777.into(), 100000.into());
let max_volume = if available_amount > min_amount {
let my_balance = from_dec_to_ratio(try_s!(base_coin.my_balance().wait()));
if available_amount <= my_balance && available_amount > BigRational::from_integer(0.into()) {
available_amount
} else {
my_balance
}
} else {
BigRational::from_integer(0.into())
};
Ok(PricePingRequest {
method: "postprice".into(),
pubkey: hex::encode(&public_id.bytes),
base: order.base.clone(),
rel: order.rel.clone(),
price64: price64.to_string(),
price: order.price.clone(),
price_rat: Some(order.price_rat.clone()),
timestamp,
pubsecp: hex::encode(&**ctx.secp256k1_key_pair().public()),
sig: hex::encode(&*sig),
balance: from_ratio_to_dec(&max_volume),
balance_rat: Some(max_volume),
uuid: Some(order.uuid),
})
}
}
pub fn lp_post_price_recv(ctx: &MmArc, req: Json) -> HyRes {
let req: PricePingRequest = try_h!(json::from_value(req));
let signature: Signature = try_h!(req.sig.parse());
let pub_secp = try_h!(Public::from_slice(&try_h!(hex::decode(&req.pubsecp))));
let pubkey = try_h!(hex::decode(&req.pubkey));
let sig_hash = price_ping_sig_hash(
req.timestamp as u32,
&*pub_secp,
&pubkey,
req.base.as_bytes(),
req.rel.as_bytes(),
try_h!(req.price64.parse()),
);
let sig_check = try_h!(pub_secp.verify(&sig_hash, &signature));
if sig_check {
// identify the order by first 16 bytes of node pubkey to keep backwards-compatibility
// TODO remove this when all nodes are updated
let mut bytes = [0; 16];
bytes.copy_from_slice(&pubkey[..16]);
let uuid = req.uuid.unwrap_or(Uuid::from_bytes(bytes));
let ordermatch_ctx: Arc<OrdermatchContext> = try_h!(OrdermatchContext::from_ctx(ctx));
let mut orderbook = try_h!(ordermatch_ctx.orderbook.lock());
match orderbook.entry((req.base.clone(), req.rel.clone())) {
Entry::Vacant(pair_orders) => if req.balance > 0.into() && req.price > 0.into() {
let mut orders = HashMap::new();
orders.insert(uuid, req);
pair_orders.insert(orders);
},
Entry::Occupied(mut pair_orders) => {
match pair_orders.get_mut().entry(uuid) {
Entry::Vacant(order) => if req.balance > 0.into() && req.price > 0.into() {
order.insert(req);
},
Entry::Occupied(mut order) => if req.balance > 0.into() {
order.insert(req);
} else {
order.remove();
},
}
}
}
rpc_response(200, r#"{"result":"success"}"#)
} else {
rpc_err_response(400, "price ping invalid signature")
}
}
fn lp_send_price_ping(req: &PricePingRequest, ctx: &MmArc) -> Result<(), String> {
let req_string = try_s!(json::to_string(req));
// TODO this is required to process the set price message on our own node, it's the easiest way now
// there might be a better way of doing this so we should consider refactoring
lp_post_price_recv(ctx, try_s!(json::to_value(req)));
ctx.broadcast_p2p_msg(&req_string);
Ok(())
}
fn one() -> u8 { 1 }
fn get_true() -> bool { true }
#[derive(Deserialize)]
struct SetPriceReq {
base: String,
rel: String,
price: MmNumber,
#[serde(default)]
max: bool,
#[allow(dead_code)]
#[serde(default = "one")]
broadcast: u8,
#[serde(default)]
volume: MmNumber,
#[serde(default = "get_true")]
cancel_previous: bool,
}
pub fn set_price(ctx: MmArc, req: Json) -> HyRes {
let req: SetPriceReq = try_h!(json::from_value(req));
if req.price < MmNumber::from(BigRational::new(1.into(), 100000000.into())) {
return rpc_err_response(500, "Price is too low, minimum is 0.00000001");
}
if req.base == req.rel {
return rpc_err_response(500, "Base and rel must be different coins");
}
let base_coin: MmCoinEnum = match try_h!(lp_coinfind(&ctx, &req.base)) {
Some(coin) => coin,
None => return rpc_err_response(500, &format!("Base coin {} is not found", req.base)),
};
let rel_coin: MmCoinEnum = match try_h!(lp_coinfind(&ctx, &req.rel)) {
Some(coin) => coin,
None => return rpc_err_response(500, &format!("Rel coin {} is not found", req.rel)),
};
let balance_f = base_coin.my_balance();
let volume_f = if req.max {
// use entire balance deducting the locked amount and skipping "check_i_have_enough"
Either::A(balance_f.map(move |my_balance| (MmNumber::from(my_balance - get_locked_amount(&ctx, base_coin.ticker())), req, ctx)))