Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bitmex-client): introduce getter for instrument from bitmex #2649

Merged
merged 1 commit into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
}
Loading