use std::{collections::HashMap, fmt};
use chrono::{DateTime, Utc, serde::ts_milliseconds};
use serde::Deserialize;
use crate::shared::models::price::Price;
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Ticker {
index: Price,
last_price: Price,
ask_price: Price,
bid_price: Price,
carry_fee_rate: f64,
#[serde(with = "ts_milliseconds")]
carry_fee_timestamp: DateTime<Utc>,
exchanges_weights: HashMap<String, f64>,
}
impl Ticker {
pub fn index(&self) -> Price {
self.index
}
pub fn last_price(&self) -> Price {
self.last_price
}
pub fn ask_price(&self) -> Price {
self.ask_price
}
pub fn bid_price(&self) -> Price {
self.bid_price
}
pub fn carry_fee_rate(&self) -> f64 {
self.carry_fee_rate
}
pub fn carry_fee_timestamp(&self) -> DateTime<Utc> {
self.carry_fee_timestamp
}
pub fn exchanges_weights(&self) -> &HashMap<String, f64> {
&self.exchanges_weights
}
pub fn as_data_str(&self) -> String {
format!(
"index: {}\nlast_price: {}\nbid_price: {}\nask_price: {}\ncarry_fee_rate: {:.6}\ncarry_fee_timestamp: {}",
self.index,
self.last_price,
self.bid_price,
self.ask_price,
self.carry_fee_rate,
self.carry_fee_timestamp.to_rfc3339()
)
}
}
impl fmt::Display for Ticker {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Ticker:")?;
for line in self.as_data_str().lines() {
write!(f, "\n {line}")?;
}
Ok(())
}
}