Skip to content

Commit

Permalink
feat(bitmex-client): introduce getter for instrument
Browse files Browse the repository at this point in the history
This allows us to fetch funding rates etc from bitmex.
  • Loading branch information
bonomat committed Aug 9, 2024
1 parent d3e144c commit f3854ef
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
23 changes: 23 additions & 0 deletions crates/bitmex-client/src/client.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -78,6 +82,25 @@ impl Client {
Ok(positions)
}

/// Returns the latest instrument
pub async fn latest_instrument(&self) -> Result<Instrument> {
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<R>(&self, req: R) -> Result<R::Response>
where
R: Request,
Expand Down
37 changes: 37 additions & 0 deletions crates/bitmex-client/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,40 @@ pub struct Position {
#[serde(with = "time::serde::rfc3339::option")]
pub timestamp: Option<OffsetDateTime>,
}

#[derive(Clone, Debug, Serialize)]
pub struct GetInstrumentRequest {
pub symbol: Option<ContractSymbol>,
/// Number of results to fetch.
pub count: Option<u64>,
/// If true, will sort results newest first.
pub reverse: Option<bool>,
/// Starting date filter for results.
#[serde(with = "time::serde::rfc3339::option", rename = "startTime")]
pub start_time: Option<OffsetDateTime>,
/// Ending date filter for results.
#[serde(with = "time::serde::rfc3339::option", rename = "endTime")]
pub end_time: Option<OffsetDateTime>,
}

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<Instrument>;
}

/// 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,
#[serde(with = "time::serde::rfc3339")]
pub timestamp: OffsetDateTime,
}

0 comments on commit f3854ef

Please sign in to comment.