use crate::util::{build_request, build_signed_request};
use crate::futures::model::{
AggTrades, BookTickers, KlineSummaries, KlineSummary, LiquidationOrders, MarkPrices,
OpenInterest, OpenInterestHist, OrderBook, PriceStats, SymbolPrice, Tickers, Trades,
};
use crate::client::Client;
use crate::errors::Result; use std::collections::BTreeMap;
use serde_json::Value;
use crate::api::API;
use crate::api::Futures;
#[derive(Clone)]
pub struct FuturesMarket {
pub client: Client,
pub recv_window: u64,
}
impl FuturesMarket {
pub async fn get_depth<S>(&self, symbol: S) -> Result<OrderBook>
where
S: Into<String>,
{
let mut parameters: BTreeMap<String, String> = BTreeMap::new();
parameters.insert("symbol".into(), symbol.into());
let request = build_request(parameters);
self.client
.get(API::Futures(Futures::Depth), Some(request))
.await }
pub async fn get_custom_depth<S>(&self, symbol: S, depth: u64) -> Result<OrderBook>
where
S: Into<String>,
{
let mut parameters: BTreeMap<String, String> = BTreeMap::new();
parameters.insert("symbol".into(), symbol.into());
parameters.insert("limit".into(), depth.to_string());
let request = build_request(parameters);
self.client
.get(API::Futures(Futures::Depth), Some(request))
.await }
pub async fn get_trades<S>(&self, symbol: S) -> Result<Trades>
where
S: Into<String>,
{
let mut parameters: BTreeMap<String, String> = BTreeMap::new();
parameters.insert("symbol".into(), symbol.into());
let request = build_request(parameters);
self.client
.get(API::Futures(Futures::Trades), Some(request))
.await }
pub async fn get_historical_trades<S1, S2, S3>(
&self,
symbol: S1,
from_id: S2,
limit: S3,
) -> Result<Trades>
where
S1: Into<String>,
S2: Into<Option<u64>>,
S3: Into<Option<u16>>,
{
let mut parameters: BTreeMap<String, String> = BTreeMap::new();
parameters.insert("symbol".into(), symbol.into());
if let Some(lt) = limit.into() {
parameters.insert("limit".into(), format!("{}", lt));
}
if let Some(fi) = from_id.into() {
parameters.insert("fromId".into(), format!("{}", fi));
}
let request = build_signed_request(parameters, self.recv_window)?;
self.client
.get_signed(API::Futures(Futures::HistoricalTrades), Some(request))
.await }
pub async fn get_agg_trades<S1, S2, S3, S4, S5>(
&self,
symbol: S1,
from_id: S2,
start_time: S3,
end_time: S4,
limit: S5,
) -> Result<AggTrades>
where
S1: Into<String>,
S2: Into<Option<u64>>,
S3: Into<Option<u64>>,
S4: Into<Option<u64>>,
S5: Into<Option<u16>>,
{
let mut parameters: BTreeMap<String, String> = BTreeMap::new();
parameters.insert("symbol".into(), symbol.into());
if let Some(lt) = limit.into() {
parameters.insert("limit".into(), format!("{}", lt));
}
if let Some(st) = start_time.into() {
parameters.insert("startTime".into(), format!("{}", st));
}
if let Some(et) = end_time.into() {
parameters.insert("endTime".into(), format!("{}", et));
}
if let Some(fi) = from_id.into() {
parameters.insert("fromId".into(), format!("{}", fi));
}
let request = build_request(parameters);
self.client
.get(API::Futures(Futures::AggTrades), Some(request))
.await }
pub async fn get_klines<S1, S2, S3, S4, S5>(
&self,
symbol: S1,
interval: S2,
limit: S3,
start_time: S4,
end_time: S5,
) -> Result<KlineSummaries>
where
S1: Into<String>,
S2: Into<String>,
S3: Into<Option<u16>>,
S4: Into<Option<u64>>,
S5: Into<Option<u64>>,
{
let mut parameters: BTreeMap<String, String> = BTreeMap::new();
parameters.insert("symbol".into(), symbol.into());
parameters.insert("interval".into(), interval.into());
if let Some(lt) = limit.into() {
parameters.insert("limit".into(), format!("{}", lt));
}
if let Some(st) = start_time.into() {
parameters.insert("startTime".into(), format!("{}", st));
}
if let Some(et) = end_time.into() {
parameters.insert("endTime".into(), format!("{}", et));
}
let request = build_request(parameters);
let data: Vec<Vec<Value>> = self
.client
.get(API::Futures(Futures::Klines), Some(request))
.await?;
let klines = KlineSummaries::AllKlineSummaries(
data.iter()
.map(|row| row.try_into())
.collect::<Result<Vec<KlineSummary>>>()?,
);
Ok(klines)
}
pub async fn get_24h_price_stats<S>(&self, symbol: S) -> Result<PriceStats>
where
S: Into<String>,
{
let mut parameters: BTreeMap<String, String> = BTreeMap::new();
parameters.insert("symbol".into(), symbol.into());
let request = build_request(parameters);
self.client
.get(API::Futures(Futures::Ticker24hr), Some(request))
.await }
pub async fn get_all_24h_price_stats(&self) -> Result<Vec<PriceStats>> {
self.client
.get(API::Futures(Futures::Ticker24hr), None)
.await }
pub async fn get_price<S>(&self, symbol: S) -> Result<SymbolPrice>
where
S: Into<String>,
{
let mut parameters: BTreeMap<String, String> = BTreeMap::new();
parameters.insert("symbol".into(), symbol.into());
let request = build_request(parameters);
self.client
.get(API::Futures(Futures::TickerPrice), Some(request))
.await }
pub async fn get_all_prices(&self) -> Result<crate::model::Prices> {
self.client
.get(API::Futures(Futures::TickerPrice), None)
.await }
pub async fn get_all_book_tickers(&self) -> Result<BookTickers> {
self.client
.get(API::Futures(Futures::BookTicker), None)
.await }
pub async fn get_book_ticker<S>(&self, symbol: S) -> Result<Tickers>
where
S: Into<String>,
{
let mut parameters: BTreeMap<String, String> = BTreeMap::new();
parameters.insert("symbol".into(), symbol.into());
let request = build_request(parameters);
self.client
.get(API::Futures(Futures::BookTicker), Some(request))
.await }
pub async fn get_mark_prices(&self) -> Result<MarkPrices> {
self.client
.get(API::Futures(Futures::PremiumIndex), None)
.await }
pub async fn get_all_liquidation_orders(&self) -> Result<LiquidationOrders> {
self.client
.get(API::Futures(Futures::AllForceOrders), None)
.await }
pub async fn open_interest<S>(&self, symbol: S) -> Result<OpenInterest>
where
S: Into<String>,
{
let mut parameters: BTreeMap<String, String> = BTreeMap::new();
parameters.insert("symbol".into(), symbol.into());
let request = build_request(parameters);
self.client
.get(API::Futures(Futures::OpenInterest), Some(request))
.await }
pub async fn open_interest_statistics<S1, S2, S3, S4, S5>(
&self,
symbol: S1,
period: S2,
limit: S3,
start_time: S4,
end_time: S5,
) -> Result<Vec<OpenInterestHist>>
where
S1: Into<String>,
S2: Into<String>,
S3: Into<Option<u16>>,
S4: Into<Option<u64>>,
S5: Into<Option<u64>>,
{
let mut parameters: BTreeMap<String, String> = BTreeMap::new();
parameters.insert("symbol".into(), symbol.into());
parameters.insert("period".into(), period.into());
if let Some(lt) = limit.into() {
parameters.insert("limit".into(), format!("{}", lt));
}
if let Some(st) = start_time.into() {
parameters.insert("startTime".into(), format!("{}", st));
}
if let Some(et) = end_time.into() {
parameters.insert("endTime".into(), format!("{}", et));
}
let request = build_request(parameters);
self.client
.get(API::Futures(Futures::OpenInterestHist), Some(request))
.await }
}