use serde::{Deserialize, Serialize};
use super::string_or_float;
use crate::types::{OrderSide, OrderStatus, OrderType, TimeInForce};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MarginTransferType {
#[serde(rename = "1")]
MainToMargin,
#[serde(rename = "2")]
MarginToMain,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum IsolatedMarginTransferType {
Spot,
IsolatedMargin,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum SideEffectType {
#[default]
NoSideEffect,
MarginBuy,
AutoRepay,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransactionId {
pub tran_id: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginAccountDetails {
pub borrow_enabled: bool,
#[serde(with = "string_or_float")]
pub margin_level: f64,
#[serde(with = "string_or_float")]
pub total_asset_of_btc: f64,
#[serde(with = "string_or_float")]
pub total_liability_of_btc: f64,
#[serde(with = "string_or_float")]
pub total_net_asset_of_btc: f64,
pub trade_enabled: bool,
pub transfer_enabled: bool,
pub user_assets: Vec<MarginAsset>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginAsset {
pub asset: String,
#[serde(with = "string_or_float")]
pub borrowed: f64,
#[serde(with = "string_or_float")]
pub free: f64,
#[serde(with = "string_or_float")]
pub interest: f64,
#[serde(with = "string_or_float")]
pub locked: f64,
#[serde(with = "string_or_float")]
pub net_asset: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IsolatedMarginAccountDetails {
#[serde(default)]
pub assets: Vec<IsolatedMarginAccountAsset>,
#[serde(default, with = "string_or_float_option")]
pub total_asset_of_btc: Option<f64>,
#[serde(default, with = "string_or_float_option")]
pub total_liability_of_btc: Option<f64>,
#[serde(default, with = "string_or_float_option")]
pub total_net_asset_of_btc: Option<f64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IsolatedMarginAccountAsset {
pub base_asset: IsolatedAssetDetails,
pub quote_asset: IsolatedAssetDetails,
pub symbol: String,
pub isolated_created: bool,
pub enabled: bool,
#[serde(with = "string_or_float")]
pub margin_level: f64,
#[serde(with = "string_or_float")]
pub margin_ratio: f64,
#[serde(with = "string_or_float")]
pub index_price: f64,
#[serde(with = "string_or_float")]
pub liquidate_price: f64,
#[serde(with = "string_or_float")]
pub liquidate_rate: f64,
pub trade_enabled: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IsolatedAssetDetails {
pub asset: String,
pub borrow_enabled: bool,
#[serde(with = "string_or_float")]
pub borrowed: f64,
#[serde(with = "string_or_float")]
pub free: f64,
#[serde(with = "string_or_float")]
pub interest: f64,
#[serde(with = "string_or_float")]
pub locked: f64,
#[serde(with = "string_or_float")]
pub net_asset: f64,
#[serde(with = "string_or_float")]
pub net_asset_of_btc: f64,
pub repay_enabled: bool,
#[serde(with = "string_or_float")]
pub total_asset: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MaxBorrowableAmount {
#[serde(with = "string_or_float")]
pub amount: f64,
#[serde(default, with = "string_or_float_option")]
pub borrow_limit: Option<f64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MaxTransferableAmount {
#[serde(with = "string_or_float")]
pub amount: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginOrderResult {
pub symbol: String,
pub order_id: u64,
pub client_order_id: String,
pub transact_time: u64,
#[serde(with = "string_or_float")]
pub price: f64,
#[serde(with = "string_or_float")]
pub orig_qty: f64,
#[serde(with = "string_or_float")]
pub executed_qty: f64,
#[serde(with = "string_or_float")]
pub cummulative_quote_qty: f64,
pub status: OrderStatus,
pub time_in_force: TimeInForce,
#[serde(rename = "type")]
pub order_type: OrderType,
pub side: OrderSide,
#[serde(default)]
pub is_isolated: Option<bool>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginOrderState {
pub client_order_id: String,
#[serde(with = "string_or_float")]
pub cummulative_quote_qty: f64,
#[serde(with = "string_or_float")]
pub executed_qty: f64,
#[serde(default, with = "string_or_float_option")]
pub iceberg_qty: Option<f64>,
pub is_working: bool,
pub order_id: u64,
#[serde(with = "string_or_float")]
pub orig_qty: f64,
#[serde(with = "string_or_float")]
pub price: f64,
pub side: OrderSide,
pub status: OrderStatus,
#[serde(default, with = "string_or_float_option")]
pub stop_price: Option<f64>,
pub symbol: String,
#[serde(default)]
pub is_isolated: Option<bool>,
pub time: u64,
pub time_in_force: TimeInForce,
#[serde(rename = "type")]
pub order_type: OrderType,
pub update_time: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginOrderCancellation {
pub symbol: String,
#[serde(default)]
pub order_id: Option<u64>,
pub orig_client_order_id: String,
pub client_order_id: String,
#[serde(with = "string_or_float")]
pub price: f64,
#[serde(with = "string_or_float")]
pub orig_qty: f64,
#[serde(with = "string_or_float")]
pub executed_qty: f64,
#[serde(with = "string_or_float")]
pub cummulative_quote_qty: f64,
pub status: OrderStatus,
pub time_in_force: TimeInForce,
#[serde(rename = "type")]
pub order_type: OrderType,
pub side: OrderSide,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginTrade {
#[serde(with = "string_or_float")]
pub commission: f64,
pub commission_asset: String,
pub id: u64,
pub is_buyer: bool,
#[serde(default)]
pub is_isolated: Option<bool>,
pub is_maker: bool,
pub order_id: u64,
#[serde(with = "string_or_float")]
pub price: f64,
#[serde(with = "string_or_float")]
pub qty: f64,
pub symbol: String,
pub time: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InterestHistoryRecord {
pub asset: String,
#[serde(with = "string_or_float")]
pub interest: f64,
pub interest_accured_time: u64,
#[serde(with = "string_or_float")]
pub interest_rate: f64,
#[serde(with = "string_or_float")]
pub principal: f64,
#[serde(rename = "type")]
pub interest_type: String,
#[serde(default)]
pub isolated_symbol: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InterestRateRecord {
pub asset: String,
#[serde(with = "string_or_float")]
pub daily_interest_rate: f64,
pub timestamp: u64,
#[serde(default)]
pub vip_level: Option<u32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LoanRecord {
pub asset: String,
#[serde(with = "string_or_float")]
pub principal: f64,
pub timestamp: u64,
pub status: String,
#[serde(default)]
pub isolated_symbol: Option<String>,
pub tx_id: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RepayRecord {
pub asset: String,
#[serde(with = "string_or_float")]
pub amount: f64,
#[serde(with = "string_or_float")]
pub interest: f64,
#[serde(with = "string_or_float")]
pub principal: f64,
pub timestamp: u64,
pub status: String,
#[serde(default)]
pub isolated_symbol: Option<String>,
pub tx_id: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecordsQueryResult<T> {
pub total: u64,
pub rows: Vec<T>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginPairDetails {
pub id: u64,
pub symbol: String,
pub base: String,
pub quote: String,
pub is_margin_trade: bool,
pub is_buy_allowed: bool,
pub is_sell_allowed: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginAssetInfo {
pub asset_name: String,
pub asset_full_name: String,
pub is_borrowable: bool,
pub is_mortgageable: bool,
#[serde(with = "string_or_float")]
pub user_min_borrow: f64,
#[serde(with = "string_or_float")]
pub user_min_repay: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginPriceIndex {
pub calc_time: u64,
#[serde(with = "string_or_float")]
pub price: f64,
pub symbol: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IsolatedAccountLimit {
pub enabled_account: u32,
pub max_account: u32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BnbBurnStatus {
pub spot_bnb_burn: bool,
pub interest_bnb_burn: bool,
}
mod string_or_float_option {
use serde::{self, Deserialize, Deserializer, Serializer};
pub fn serialize<S>(value: &Option<f64>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match value {
Some(v) => serializer.serialize_str(&v.to_string()),
None => serializer.serialize_none(),
}
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<f64>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum StringOrFloat {
String(String),
Float(f64),
}
let opt: Option<StringOrFloat> = Option::deserialize(deserializer)?;
match opt {
Some(StringOrFloat::Float(f)) => Ok(Some(f)),
Some(StringOrFloat::String(s)) => {
s.parse::<f64>().map(Some).map_err(serde::de::Error::custom)
}
None => Ok(None),
}
}
}