use std::fmt;
use super::arborter_pb::{Order, SendOrderResponse, TransactionHash};
impl fmt::Display for Order {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Order {{\n side: {},\n quantity: {},\n price: {},\n market_id: {},\n base_account_address: {},\n quote_account_address: {},\n execution_type: {},\n matching_order_ids: {:?}\n}}",
self.side,
self.quantity,
self.price
.clone()
.map_or("None".to_string(), |p| p.to_string()),
self.market_id,
self.base_account_address,
self.quote_account_address,
self.execution_type,
self.matching_order_ids
)
}
}
impl fmt::Display for TransactionHash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"TransactionHash {{ hash_type: {}, hash_value: {} }}",
self.hash_type, self.hash_value
)
}
}
impl TransactionHash {
pub fn format_for_cli(&self) -> String {
format!("[{}] {}", self.hash_type.to_uppercase(), self.hash_value)
}
pub fn get_explorer_hint(&self) -> Option<String> {
Some(
"Paste this hash into your chain's block explorer (e.g., Etherscan, Basescan)"
.to_string(),
)
}
}
impl SendOrderResponse {
pub fn get_formatted_transaction_hashes(&self) -> Vec<String> {
self.transaction_hashes
.iter()
.map(|th| th.format_for_cli())
.collect()
}
}
impl fmt::Display for SendOrderResponse {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"SendOrderResponse {{\n order_id: {},\n order_in_book: {},\n order: {},\n trades: [{}],\n transaction_hashes: [{}]\n}}",
self.order_id,
self.order_in_book,
self.order
.as_ref()
.map_or("None".to_string(), |o| format!("{}", o)),
self.trades
.iter()
.map(|t| format!("{:?}", t))
.collect::<Vec<_>>()
.join(", "),
self.transaction_hashes
.iter()
.map(|th| format!("{}: {}", th.hash_type, th.hash_value))
.collect::<Vec<_>>()
.join(", ")
)
}
}