use super::models::{
AccountBalance, AccountPositions, AccountPositionsHistory, AccountSetLeverage, RestApi,
};
use crate::client::OkxClient;
use anyhow::Result;
use std::collections::BTreeMap;
impl OkxClient {
pub async fn account_balance(&self, ccy: Option<String>) -> Result<RestApi<AccountBalance>> {
let mut params: BTreeMap<String, String> = BTreeMap::new();
if let Some(ccy) = ccy {
params.insert("ccy".into(), ccy.into());
}
Ok(self
.get::<RestApi<AccountBalance>>("/api/v5/account/balance", ¶ms)
.await?)
}
pub async fn account_positions<T>(
&self,
inst_type: Option<T>,
inst_id: Option<T>,
pos_id: Option<T>,
) -> Result<RestApi<AccountPositions>>
where
T: Into<String>,
{
let mut params: BTreeMap<String, String> = BTreeMap::new();
if let Some(inst_type) = inst_type {
params.insert("instType".into(), inst_type.into());
}
if let Some(inst_id) = inst_id {
params.insert("instId".into(), inst_id.into());
}
if let Some(pos_id) = pos_id {
params.insert("posId".into(), pos_id.into());
}
Ok(self
.get::<RestApi<AccountPositions>>("/api/v5/account/positions", ¶ms)
.await?)
}
pub async fn account_set_leverage<T>(
&self,
inst_id: Option<T>,
ccy: Option<T>,
lever: T,
mgn_mode: T,
pos_side: Option<T>,
) -> Result<RestApi<AccountSetLeverage>>
where
T: Into<String>,
{
let mut params: BTreeMap<String, String> = BTreeMap::new();
if let Some(inst_id) = inst_id {
params.insert("instId".into(), inst_id.into());
}
if let Some(ccy) = ccy {
params.insert("ccy".into(), ccy.into());
}
if let Some(pos_side) = pos_side {
params.insert("posSide".into(), pos_side.into());
}
params.insert("lever".into(), lever.into());
params.insert("mgnMode".into(), mgn_mode.into());
Ok(self
.post::<RestApi<AccountSetLeverage>>("/api/v5/account/set-leverage", ¶ms)
.await?)
}
pub async fn account_positions_history<T>(
&self,
inst_type: Option<T>,
inst_id: Option<T>,
mgn_mode: Option<T>,
ptype: Option<T>,
pos_id: Option<T>,
after: Option<T>,
before: Option<T>,
limit: Option<T>,
) -> Result<RestApi<AccountPositionsHistory>>
where
T: Into<String>,
{
let mut params: BTreeMap<String, String> = BTreeMap::new();
if let Some(inst_type) = inst_type {
params.insert("instType".into(), inst_type.into());
}
if let Some(inst_id) = inst_id {
params.insert("instId".into(), inst_id.into());
}
if let Some(mgn_mode) = mgn_mode {
params.insert("mgnMode".into(), mgn_mode.into());
}
if let Some(ptype) = ptype {
params.insert("type".into(), ptype.into());
}
if let Some(pos_id) = pos_id {
params.insert("posId".into(), pos_id.into());
}
if let Some(after) = after {
params.insert("after".into(), after.into());
}
if let Some(before) = before {
params.insert("before".into(), before.into());
}
if let Some(limit) = limit {
params.insert("limit".into(), limit.into());
}
Ok(self
.get::<RestApi<AccountPositionsHistory>>("/api/v5/account/positions-history", ¶ms)
.await?)
}
}