Skip to content

Commit

Permalink
Merge pull request #2649 from get10101/feat/instrument-endpoint
Browse files Browse the repository at this point in the history
feat(bitmex-client): introduce getter for instrument from bitmex
  • Loading branch information
bonomat authored Aug 10, 2024
2 parents 07394d7 + 43890cf commit 26dd351
Show file tree
Hide file tree
Showing 2 changed files with 63 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
40 changes: 40 additions & 0 deletions crates/bitmex-client/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,43 @@ 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,
/// 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,
}

0 comments on commit 26dd351

Please sign in to comment.