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

fix(dex-api): best orders proper is_mine provided #1849

Merged
merged 16 commits into from
Jun 15, 2023
Merged
Changes from 6 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
32 changes: 26 additions & 6 deletions mm2src/mm2_main/src/lp_ordermatch/best_orders.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::{addr_format_from_protocol_info, BaseRelProtocolInfo, OrderConfirmationsSettings,
OrderbookP2PItemWithProof, OrdermatchContext, OrdermatchRequest};
use crate::mm2::lp_network::{request_any_relay, P2PRequest};
use crate::mm2::lp_ordermatch::{orderbook_address, RpcOrderbookEntryV2};
use crate::mm2::lp_ordermatch::{is_my_order, orderbook_address, RpcOrderbookEntryV2};
use coins::{address_by_coin_conf_and_pubkey_str, coin_conf, is_wallet_only_conf, is_wallet_only_ticker};
use common::{log, HttpStatusCode};
use crypto::{CryptoCtx, CryptoCtxError};
use derive_more::Display;
use http::{Response, StatusCode};
use mm2_core::mm_ctx::MmArc;
Expand Down Expand Up @@ -51,6 +52,7 @@ pub struct BestOrdersRequestV2 {
coin: String,
action: BestOrdersAction,
request_by: RequestBestOrdersBy,
exclude_my: Option<bool>,
caglaryucekaya marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn process_best_orders_p2p_request(
Expand Down Expand Up @@ -223,6 +225,8 @@ pub async fn best_orders_rpc(ctx: MmArc, req: Json) -> Result<Response<Vec<u8>>,
let mut response = HashMap::new();
if let Some((p2p_response, peer_id)) = best_orders_res {
log::debug!("Got best orders {:?} from peer {}", p2p_response, peer_id);
let my_pubsecp = mm2_internal_pubkey_hex(&ctx).map_err(|mm2_err| mm2_err.to_string())?;
let orderbook = ordermatch_ctx.orderbook.lock();
shamardy marked this conversation as resolved.
Show resolved Hide resolved
for (coin, orders_w_proofs) in p2p_response.orders {
let coin_conf = coin_conf(&ctx, &coin);
if coin_conf.is_null() {
Expand Down Expand Up @@ -256,9 +260,10 @@ pub async fn best_orders_rpc(ctx: MmArc, req: Json) -> Result<Response<Vec<u8>>,
},
};
let conf_settings = p2p_response.conf_infos.get(&order.uuid);
let is_mine = is_my_order(&orderbook.my_p2p_pubkeys, &my_pubsecp, &order.pubkey);
let entry = match req.action {
BestOrdersAction::Buy => order.as_rpc_best_orders_buy(address, conf_settings, false),
BestOrdersAction::Sell => order.as_rpc_best_orders_sell(address, conf_settings, false),
BestOrdersAction::Buy => order.as_rpc_best_orders_buy(address, conf_settings, is_mine),
BestOrdersAction::Sell => order.as_rpc_best_orders_sell(address, conf_settings, is_mine),
};
if let Some(original_tickers) = ordermatch_ctx.original_tickers.get(&coin) {
for ticker in original_tickers {
Expand Down Expand Up @@ -286,13 +291,14 @@ pub async fn best_orders_rpc(ctx: MmArc, req: Json) -> Result<Response<Vec<u8>>,
pub enum BestOrdersRpcError {
CoinIsWalletOnly(String),
P2PError(String),
CtxError(String),
}

impl HttpStatusCode for BestOrdersRpcError {
fn status_code(&self) -> StatusCode {
match self {
BestOrdersRpcError::CoinIsWalletOnly(_) => StatusCode::BAD_REQUEST,
BestOrdersRpcError::P2PError(_) => StatusCode::INTERNAL_SERVER_ERROR,
_ => StatusCode::INTERNAL_SERVER_ERROR,
shamardy marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down Expand Up @@ -330,6 +336,8 @@ pub async fn best_orders_rpc_v2(
let mut orders = HashMap::new();
if let Some((p2p_response, peer_id)) = best_orders_res {
log::debug!("Got best orders {:?} from peer {}", p2p_response, peer_id);
let my_pubsecp = mm2_internal_pubkey_hex(&ctx)?;
let orderbook = ordermatch_ctx.orderbook.lock();
for (coin, orders_w_proofs) in p2p_response.orders {
let coin_conf = coin_conf(&ctx, &coin);
if coin_conf.is_null() {
Expand All @@ -345,6 +353,10 @@ pub async fn best_orders_rpc_v2(
}
for order_w_proof in orders_w_proofs {
let order = order_w_proof.order;
let is_mine = is_my_order(&orderbook.my_p2p_pubkeys, &my_pubsecp, &order.pubkey);
if let (Some(true), true) = (req.exclude_my, is_mine) {
continue;
}
let empty_proto_info = BaseRelProtocolInfo::default();
let proto_infos = p2p_response
.protocol_infos
Expand All @@ -363,8 +375,8 @@ pub async fn best_orders_rpc_v2(
};
let conf_settings = p2p_response.conf_infos.get(&order.uuid);
let entry = match req.action {
BestOrdersAction::Buy => order.as_rpc_best_orders_buy_v2(address, conf_settings, false),
BestOrdersAction::Sell => order.as_rpc_best_orders_sell_v2(address, conf_settings, false),
BestOrdersAction::Buy => order.as_rpc_best_orders_buy_v2(address, conf_settings, is_mine),
BestOrdersAction::Sell => order.as_rpc_best_orders_sell_v2(address, conf_settings, is_mine),
};
if let Some(original_tickers) = ordermatch_ctx.original_tickers.get(&coin) {
for ticker in original_tickers {
Expand All @@ -387,6 +399,14 @@ pub async fn best_orders_rpc_v2(
})
}

fn mm2_internal_pubkey_hex(ctx: &MmArc) -> Result<Option<String>, MmError<BestOrdersRpcError>> {
match CryptoCtx::from_ctx(ctx).discard_mm_trace() {
Ok(crypto_ctx) => Ok(Some(crypto_ctx.mm2_internal_pubkey_hex())),
Err(CryptoCtxError::NotInitialized) => Ok(None),
Err(other) => MmError::err(BestOrdersRpcError::CtxError(format!("{}", other))),
}
}

#[cfg(all(test, not(target_arch = "wasm32")))]
mod best_orders_test {
use super::*;
Expand Down