Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use correct direction of the pair for best_orders sell action. #863

Merged
merged 1 commit into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions mm2src/lp_ordermatch/best_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ pub async fn process_best_orders_p2p_request(
None => return Ok(None),
};
let mut result = HashMap::new();
let pairs = tickers.iter().map(|ticker| (coin.clone(), ticker.clone()));
let pairs = tickers.iter().map(|ticker| match action {
BestOrdersAction::Buy => (coin.clone(), ticker.clone()),
sergeyboyko0791 marked this conversation as resolved.
Show resolved Hide resolved
BestOrdersAction::Sell => (ticker.clone(), coin.clone()),
});
for pair in pairs {
let orders = match orderbook.ordered.get(&pair) {
Some(orders) => orders,
Expand All @@ -65,6 +68,7 @@ pub async fn process_best_orders_p2p_request(
BestOrdersAction::Sell => &o.min_volume * &o.price,
};
if min_volume > required_volume {
log::debug!("Order {} min_vol {:?} > {:?}", o.uuid, min_volume, required_volume);
continue;
}

Expand All @@ -90,7 +94,10 @@ pub async fn process_best_orders_p2p_request(
},
};
}
result.insert(pair.1, best_orders);
match action {
BestOrdersAction::Buy => result.insert(pair.1, best_orders),
BestOrdersAction::Sell => result.insert(pair.0, best_orders),
};
}
let response = BestOrdersRes { orders: result };
let encoded = rmp_serde::to_vec(&response).expect("rmp_serde::to_vec should not fail here");
Expand Down
19 changes: 19 additions & 0 deletions mm2src/mm2_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5327,6 +5327,8 @@ fn test_best_orders() {
("MORTY", "RICK", "0.8", "0.9", None),
("MORTY", "RICK", "0.9", "0.9", None),
("ETH", "RICK", "0.8", "0.9", None),
("MORTY", "ETH", "0.8", "0.8", None),
("MORTY", "ETH", "0.7", "0.8", Some("0.8")),
];
for (base, rel, price, volume, min_volume) in bob_orders.iter() {
let rc = block_on(mm_bob.rpc(json! ({
Expand Down Expand Up @@ -5427,6 +5429,23 @@ fn test_best_orders() {
let best_eth_orders = response.result.get("ETH").unwrap();
assert_eq!(expected_price, best_eth_orders[0].price);

let rc = block_on(mm_alice.rpc(json! ({
"userpass": mm_alice.userpass,
"method": "best_orders",
"coin": "ETH",
"action": "sell",
"volume": "0.1",
})))
.unwrap();
assert!(rc.0.is_success(), "!best_orders: {}", rc.1);
let response: BestOrdersResponse = json::from_str(&rc.1).unwrap();

let expected_price: BigDecimal = "1.25".parse().unwrap();

let best_morty_orders = response.result.get("MORTY").unwrap();
assert_eq!(expected_price, best_morty_orders[0].price);
assert_eq!(1, best_morty_orders.len());

block_on(mm_bob.stop()).unwrap();
block_on(mm_alice.stop()).unwrap();
}
Expand Down