diff --git a/crates/bitmex-client/src/client.rs b/crates/bitmex-client/src/client.rs index fbb10e11c..55e2badd2 100644 --- a/crates/bitmex-client/src/client.rs +++ b/crates/bitmex-client/src/client.rs @@ -1,5 +1,8 @@ use crate::models::ContractSymbol; +use crate::models::ContractSymbol::XbtUsd; +use crate::models::GetInstrumentRequest; use crate::models::GetPositionRequest; +use crate::models::Instrument; use crate::models::Network; use crate::models::OrdType; use crate::models::Order; @@ -8,6 +11,7 @@ use crate::models::PostOrderRequest; use crate::models::Request; use crate::models::Side; use anyhow::bail; +use anyhow::Context; use anyhow::Result; use hex::encode as hexify; use reqwest; @@ -78,6 +82,25 @@ impl Client { Ok(positions) } + /// Returns the latest instrument + pub async fn latest_instrument(&self) -> Result { + let instruments = self + .send_request(GetInstrumentRequest { + symbol: Some(XbtUsd), + count: Some(1), + end_time: None, + start_time: None, + reverse: Some(true), + }) + .await? + .first() + .cloned(); + + let instrument = instruments.context("No instrument found")?; + + Ok(instrument) + } + async fn send_request(&self, req: R) -> Result where R: Request, diff --git a/crates/bitmex-client/src/models.rs b/crates/bitmex-client/src/models.rs index be5e45abb..cf3a12a80 100644 --- a/crates/bitmex-client/src/models.rs +++ b/crates/bitmex-client/src/models.rs @@ -219,3 +219,43 @@ pub struct Position { #[serde(with = "time::serde::rfc3339::option")] pub timestamp: Option, } + +#[derive(Clone, Debug, Serialize)] +pub struct GetInstrumentRequest { + pub symbol: Option, + /// Number of results to fetch. + pub count: Option, + /// If true, will sort results newest first. + pub reverse: Option, + /// Starting date filter for results. + #[serde(with = "time::serde::rfc3339::option", rename = "startTime")] + pub start_time: Option, + /// Ending date filter for results. + #[serde(with = "time::serde::rfc3339::option", rename = "endTime")] + pub end_time: Option, +} + +impl Request for GetInstrumentRequest { + const METHOD: Method = Method::GET; + const SIGNED: bool = false; + const ENDPOINT: &'static str = "/instrument"; + const HAS_PAYLOAD: bool = true; + type Response = Vec; +} + +/// Note: only relevant fields have been added +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct Instrument { + pub symbol: ContractSymbol, + #[serde(rename = "fundingTimestamp", with = "time::serde::rfc3339")] + pub funding_timestamp: OffsetDateTime, + #[serde(rename = "fundingInterval", with = "time::serde::rfc3339")] + pub funding_interval: OffsetDateTime, + #[serde(rename = "fundingRate")] + pub funding_rate: f64, + /// Predicted funding rate for the the next interval after funding_timestamp + #[serde(rename = "indicativeFundingRate")] + pub indicative_funding_rate: f64, + #[serde(with = "time::serde::rfc3339")] + pub timestamp: OffsetDateTime, +}