use super::Gateio;
use crate::{Identifier, instrument::MarketInstrumentData, subscription::Subscription};
use barter_instrument::{
Keyed,
instrument::{
kind::option::OptionKind,
market_data::{MarketDataInstrument, kind::MarketDataInstrumentKind::*},
},
};
use chrono::{
DateTime, Utc,
format::{DelayedFormat, StrftimeItems},
};
use serde::{Deserialize, Serialize};
use smol_str::{SmolStr, StrExt, format_smolstr};
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
pub struct GateioMarket(pub SmolStr);
impl<Server, Kind> Identifier<GateioMarket>
for Subscription<Gateio<Server>, MarketDataInstrument, Kind>
{
fn id(&self) -> GateioMarket {
gateio_market(&self.instrument)
}
}
impl<Server, InstrumentKey, Kind> Identifier<GateioMarket>
for Subscription<Gateio<Server>, Keyed<InstrumentKey, MarketDataInstrument>, Kind>
{
fn id(&self) -> GateioMarket {
gateio_market(&self.instrument.value)
}
}
impl<Server, InstrumentKey, Kind> Identifier<GateioMarket>
for Subscription<Gateio<Server>, MarketInstrumentData<InstrumentKey>, Kind>
{
fn id(&self) -> GateioMarket {
GateioMarket(self.instrument.name_exchange.name().clone())
}
}
impl AsRef<str> for GateioMarket {
fn as_ref(&self) -> &str {
&self.0
}
}
fn gateio_market(instrument: &MarketDataInstrument) -> GateioMarket {
let MarketDataInstrument { base, quote, kind } = instrument;
GateioMarket(
match kind {
Spot | Perpetual => format_smolstr!("{base}_{quote}"),
Future(contract) => {
format_smolstr!(
"{base}_{quote}_QUARTERLY_{}",
format_expiry(contract.expiry)
)
}
Option(contract) => format_smolstr!(
"{base}_{quote}-{}-{}-{}",
format_expiry(contract.expiry),
contract.strike,
match contract.kind {
OptionKind::Call => "C",
OptionKind::Put => "P",
},
),
}
.to_uppercase_smolstr(),
)
}
fn format_expiry<'a>(expiry: DateTime<Utc>) -> DelayedFormat<StrftimeItems<'a>> {
expiry.date_naive().format("%Y%m%d")
}