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

refactor(adex-cli): orderbook response processing moved #1879

Merged
merged 1 commit into from
Jun 26, 2023
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
14 changes: 7 additions & 7 deletions mm2src/adex_cli/src/adex_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::fmt::{Display, Formatter};
use std::fs;
use std::path::{Path, PathBuf};

use crate::adex_proc::SmarFractPrecision;
use crate::adex_proc::SmartFractPrecision;
use crate::helpers::rewrite_json_file;
use crate::logging::{error_anyhow, warn_bail};

Expand All @@ -20,8 +20,8 @@ const PRICE_PRECISION_MIN: usize = 8;
const PRICE_PRECISION_MAX: usize = 8;
const VOLUME_PRECISION_MIN: usize = 2;
const VOLUME_PRECISION_MAX: usize = 5;
const VOLUME_PRECISION: SmarFractPrecision = (VOLUME_PRECISION_MIN, VOLUME_PRECISION_MAX);
const PRICE_PRECISION: SmarFractPrecision = (PRICE_PRECISION_MIN, PRICE_PRECISION_MAX);
const VOLUME_PRECISION: SmartFractPrecision = (VOLUME_PRECISION_MIN, VOLUME_PRECISION_MAX);
const PRICE_PRECISION: SmartFractPrecision = (PRICE_PRECISION_MIN, PRICE_PRECISION_MAX);

pub(super) fn get_config() {
let Ok(adex_cfg) = AdexConfigImpl::from_config_path() else { return; };
Expand Down Expand Up @@ -52,8 +52,8 @@ pub(super) fn set_config(set_password: bool, rpc_api_uri: Option<String>) -> Res
pub(super) trait AdexConfig {
fn rpc_password(&self) -> Result<String>;
fn rpc_uri(&self) -> Result<String>;
fn orderbook_price_precision(&self) -> &SmarFractPrecision;
fn orderbook_volume_precision(&self) -> &SmarFractPrecision;
fn orderbook_price_precision(&self) -> &SmartFractPrecision;
fn orderbook_volume_precision(&self) -> &SmartFractPrecision;
}

#[derive(Deserialize, Serialize, Debug, Default)]
Expand All @@ -77,8 +77,8 @@ impl AdexConfig for AdexConfigImpl {
.map(String::clone)
.ok_or_else(|| error_anyhow!("No rpc_uri in config"))
}
fn orderbook_price_precision(&self) -> &SmarFractPrecision { &PRICE_PRECISION }
fn orderbook_volume_precision(&self) -> &SmarFractPrecision { &VOLUME_PRECISION }
fn orderbook_price_precision(&self) -> &SmartFractPrecision { &PRICE_PRECISION }
fn orderbook_volume_precision(&self) -> &SmartFractPrecision { &VOLUME_PRECISION }
}

impl Display for AdexConfigImpl {
Expand Down
4 changes: 1 addition & 3 deletions mm2src/adex_cli/src/adex_proc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
mod adex_proc_impl;
mod command;
mod response_handler;
mod smart_fraction_fmt;

pub(super) use adex_proc_impl::AdexProc;
pub(super) use response_handler::{ResponseHandler, ResponseHandlerImpl};
pub(super) use smart_fraction_fmt::SmarFractPrecision;
pub(super) use response_handler::{ResponseHandler, ResponseHandlerImpl, SmartFractPrecision};

#[derive(Clone)]
pub(super) struct OrderbookConfig {
Expand Down
190 changes: 6 additions & 184 deletions mm2src/adex_cli/src/adex_proc/response_handler.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#[path = "response_handler/orderbook.rs"] mod orderbook;
#[path = "response_handler/smart_fraction_fmt.rs"]
mod smart_fraction_fmt;

pub(crate) use smart_fraction_fmt::SmartFractPrecision;

use anyhow::{anyhow, Result};
use itertools::Itertools;
use log::{error, info};
Expand Down Expand Up @@ -199,187 +205,3 @@ impl<'a> SimpleCliTable<'a> {
}
}
}

mod orderbook {
use mm2_number::bigdecimal::ToPrimitive;
use mm2_rpc::data::legacy::AggregatedOrderbookEntry;
use std::cmp::Ordering;
use std::fmt::{Display, Formatter};

use super::super::{smart_fraction_fmt::{SmarFractPrecision, SmartFractionFmt},
OrderbookConfig};

pub(super) fn cmp_bids(left: &&AggregatedOrderbookEntry, right: &&AggregatedOrderbookEntry) -> Ordering {
let cmp = left.entry.price.cmp(&right.entry.price).reverse();
if cmp.is_eq() {
return left
.entry
.base_max_volume
.base_max_volume
.cmp(&right.entry.base_max_volume.base_max_volume)
.reverse();
}
cmp
}

pub(super) fn cmp_asks(left: &&AggregatedOrderbookEntry, right: &&AggregatedOrderbookEntry) -> Ordering {
let cmp = left.entry.price.cmp(&right.entry.price).reverse();
if cmp.is_eq() {
return left
.entry
.base_max_volume
.base_max_volume
.cmp(&right.entry.base_max_volume.base_max_volume);
}
cmp
}

enum AskBidRowVal {
Value(String),
Delim,
}

pub(super) struct AskBidRow<'a> {
volume: AskBidRowVal,
price: AskBidRowVal,
uuid: AskBidRowVal,
min_volume: AskBidRowVal,
max_volume: AskBidRowVal,
age: AskBidRowVal,
public: AskBidRowVal,
address: AskBidRowVal,
is_mine: AskBidRowVal,
conf_settings: AskBidRowVal,
config: &'a OrderbookConfig,
}

impl<'a> AskBidRow<'a> {
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
volume: &str,
price: &str,
uuid: &str,
min_volume: &str,
max_volume: &str,
age: &str,
public: &str,
address: &str,
conf_settings: &str,
config: &'a OrderbookConfig,
) -> Self {
Self {
is_mine: AskBidRowVal::Value(String::new()),
volume: AskBidRowVal::Value(volume.to_string()),
price: AskBidRowVal::Value(price.to_string()),
uuid: AskBidRowVal::Value(uuid.to_string()),
min_volume: AskBidRowVal::Value(min_volume.to_string()),
max_volume: AskBidRowVal::Value(max_volume.to_string()),
age: AskBidRowVal::Value(age.to_string()),
public: AskBidRowVal::Value(public.to_string()),
address: AskBidRowVal::Value(address.to_string()),
conf_settings: AskBidRowVal::Value(conf_settings.to_string()),
config,
}
}

pub(super) fn new_delimiter(config: &'a OrderbookConfig) -> Self {
Self {
is_mine: AskBidRowVal::Delim,
volume: AskBidRowVal::Delim,
price: AskBidRowVal::Delim,
uuid: AskBidRowVal::Delim,
min_volume: AskBidRowVal::Delim,
max_volume: AskBidRowVal::Delim,
age: AskBidRowVal::Delim,
public: AskBidRowVal::Delim,
address: AskBidRowVal::Delim,
conf_settings: AskBidRowVal::Delim,
config,
}
}

pub(super) fn from_orderbook_entry(
entry: &AggregatedOrderbookEntry,
vol_prec: &SmarFractPrecision,
price_prec: &SmarFractPrecision,
config: &'a OrderbookConfig,
) -> Self {
AskBidRow {
is_mine: AskBidRowVal::Value((if entry.entry.is_mine { "*" } else { "" }).to_string()),
volume: AskBidRowVal::Value(
SmartFractionFmt::new(
vol_prec.0,
vol_prec.1,
entry.entry.base_max_volume.base_max_volume.to_f64().unwrap(),
)
.expect("volume smart fraction should be constructed properly")
.to_string(),
),
price: AskBidRowVal::Value(
SmartFractionFmt::new(price_prec.0, price_prec.1, entry.entry.price.to_f64().unwrap())
.expect("price smart fraction should be constructed properly")
.to_string(),
),
uuid: AskBidRowVal::Value(entry.entry.uuid.to_string()),
min_volume: AskBidRowVal::Value(
SmartFractionFmt::new(vol_prec.0, vol_prec.1, entry.entry.min_volume.to_f64().unwrap())
.expect("min_volume smart fraction should be constructed properly")
.to_string(),
),
max_volume: AskBidRowVal::Value(
SmartFractionFmt::new(vol_prec.0, vol_prec.1, entry.entry.max_volume.to_f64().unwrap())
.expect("max_volume smart fraction should be constructed properly")
.to_string(),
),
age: AskBidRowVal::Value(entry.entry.age.to_string()),
public: AskBidRowVal::Value(entry.entry.pubkey.clone()),
address: AskBidRowVal::Value(entry.entry.address.clone()),
conf_settings: AskBidRowVal::Value(entry.entry.conf_settings.as_ref().map_or(
"none".to_string(),
|settings| {
format!(
"{},{}:{},{}",
settings.base_confs, settings.base_nota, settings.rel_confs, settings.rel_nota
)
},
)),
config,
}
}
}

impl Display for AskBidRow<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
macro_rules! write_ask_bid_row {
($value: expr, $width: expr, $alignment: literal) => {
if let AskBidRowVal::Value(value) = &$value {
write!(
f,
concat!("{:", $alignment, "width$} "),
value,
width = $width
)?;
} else {
write!(f, "{:-<width$} ", "", width = $width)?;
};
};
($config: expr, $value: expr, $width: expr, $alignment: literal) => {
if $config {
write_ask_bid_row!($value, $width, $alignment);
}
};
}
write_ask_bid_row!(self.is_mine, 1, "<");
write_ask_bid_row!(self.volume, 15, ">");
write_ask_bid_row!(self.price, 13, "<");
write_ask_bid_row!(self.config.uuids, self.uuid, 36, "<");
write_ask_bid_row!(self.config.min_volume, self.min_volume, 10, "<");
write_ask_bid_row!(self.config.max_volume, self.max_volume, 10, "<");
write_ask_bid_row!(self.config.age, self.age, 10, "<");
write_ask_bid_row!(self.config.publics, self.public, 66, "<");
write_ask_bid_row!(self.config.address, self.address, 34, "<");
write_ask_bid_row!(self.config.conf_settings, self.conf_settings, 24, "<");
Ok(())
}
}
}
Loading