use crate::core::types::AccountType;
#[derive(Debug, Clone)]
pub struct BinanceUrls {
pub spot_rest: &'static str,
pub futures_rest: &'static str,
pub coin_futures_rest: &'static str,
pub spot_ws: &'static str,
pub futures_ws: &'static str,
}
impl BinanceUrls {
pub const MAINNET: Self = Self {
spot_rest: "https://api.binance.com",
futures_rest: "https://fapi.binance.com",
coin_futures_rest: "https://dapi.binance.com",
spot_ws: "wss://stream.binance.com:9443",
futures_ws: "wss://fstream.binance.com/market",
};
pub const TESTNET: Self = Self {
spot_rest: "https://testapi.binance.vision",
futures_rest: "https://testnet.binancefuture.com",
coin_futures_rest: "https://testnet.binancefuture.com",
spot_ws: "wss://testnet.binance.vision",
futures_ws: "wss://stream.binancefuture.com",
};
pub fn rest_url(&self, account_type: AccountType) -> &str {
match account_type {
AccountType::Spot | AccountType::Margin => self.spot_rest,
AccountType::FuturesCross | AccountType::FuturesIsolated => self.futures_rest,
_ => self.spot_rest,
}
}
pub fn ws_url(&self, account_type: AccountType) -> &str {
match account_type {
AccountType::Spot | AccountType::Margin => self.spot_ws,
AccountType::FuturesCross | AccountType::FuturesIsolated => self.futures_ws,
_ => self.spot_ws,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BinanceEndpoint {
Ping,
ServerTime,
SpotPrice,
SpotOrderbook,
SpotKlines,
SpotTicker,
SpotExchangeInfo,
SpotCreateOrder,
SpotCancelOrder,
SpotCancelAllOrders,
SpotGetOrder,
SpotOpenOrders,
SpotAllOrders,
SpotOcoOrder,
SpotOtocoOrder,
SpotTradeFee,
SpotAlgoTwap,
SpotAccount,
FuturesPrice,
FuturesOrderbook,
FuturesKlines,
FuturesTicker,
FuturesExchangeInfo,
FundingRate,
FuturesCreateOrder,
FuturesCancelOrder,
FuturesCancelAllOrders,
FuturesGetOrder,
FuturesOpenOrders,
FuturesAllOrders,
FuturesAmendOrder,
FuturesBatchOrders,
FuturesBatchAmend,
FuturesAlgoOrder,
FuturesAlgoTwap,
FuturesAccount,
FuturesPositions,
FuturesSetLeverage,
FuturesSetMarginType,
FuturesPositionMargin,
FuturesCommissionRate,
SpotListenKey,
FuturesListenKey,
SpotRecentTrades,
SpotHistoricalTrades,
SpotAvgPrice,
SpotBookTicker,
FuturesRecentTrades,
FuturesOpenInterest,
FuturesOpenInterestHist,
FuturesPremiumIndex,
FuturesLongShortRatio,
FuturesTopLongShortPositionRatio,
FuturesGlobalLongShortAccountRatio,
FuturesTakerLongShortRatio,
FuturesForceOrders,
SpotMyTrades,
FuturesMyTrades,
FuturesIncomeHistory,
ListenKeyKeepAlive,
ListenKeyClose,
AssetTransfer,
AssetTransferHistory,
DepositAddress,
Withdraw,
DepositHistory,
WithdrawHistory,
SubAccountCreate,
SubAccountList,
SubAccountTransfer,
SubAccountAssets,
CmOpenInterest,
CmForceOrders,
CmFundingRate,
CmDeliveryPrice,
CmOpenInterestHist,
BasisHistory,
InsuranceFundIncome,
}
impl BinanceEndpoint {
pub fn path(&self) -> &'static str {
match self {
Self::Ping => "/api/v3/ping",
Self::ServerTime => "/api/v3/time",
Self::SpotPrice => "/api/v3/ticker/price",
Self::SpotOrderbook => "/api/v3/depth",
Self::SpotKlines => "/api/v3/klines",
Self::SpotTicker => "/api/v3/ticker/24hr",
Self::SpotExchangeInfo => "/api/v3/exchangeInfo",
Self::SpotCreateOrder => "/api/v3/order",
Self::SpotCancelOrder => "/api/v3/order",
Self::SpotCancelAllOrders => "/api/v3/openOrders",
Self::SpotGetOrder => "/api/v3/order",
Self::SpotOpenOrders => "/api/v3/openOrders",
Self::SpotAllOrders => "/api/v3/allOrders",
Self::SpotOcoOrder => "/api/v3/orderList/oco",
Self::SpotOtocoOrder => "/api/v3/orderList/otoco",
Self::SpotTradeFee => "/sapi/v1/asset/tradeFee",
Self::SpotAlgoTwap => "/sapi/v1/algo/spot/newOrderTwap",
Self::SpotAccount => "/api/v3/account",
Self::FuturesPrice => "/fapi/v1/ticker/price",
Self::FuturesOrderbook => "/fapi/v1/depth",
Self::FuturesKlines => "/fapi/v1/klines",
Self::FuturesTicker => "/fapi/v1/ticker/24hr",
Self::FuturesExchangeInfo => "/fapi/v1/exchangeInfo",
Self::FundingRate => "/fapi/v1/fundingRate",
Self::FuturesCreateOrder => "/fapi/v1/order",
Self::FuturesCancelOrder => "/fapi/v1/order",
Self::FuturesCancelAllOrders => "/fapi/v1/allOpenOrders",
Self::FuturesGetOrder => "/fapi/v1/order",
Self::FuturesOpenOrders => "/fapi/v1/openOrders",
Self::FuturesAllOrders => "/fapi/v1/allOrders",
Self::FuturesAmendOrder => "/fapi/v1/order",
Self::FuturesBatchOrders => "/fapi/v1/batchOrders",
Self::FuturesBatchAmend => "/fapi/v1/batchOrders",
Self::FuturesAlgoOrder => "/fapi/v1/order/algo",
Self::FuturesAlgoTwap => "/sapi/v1/algo/futures/newOrderTwap",
Self::FuturesAccount => "/fapi/v3/account",
Self::FuturesPositions => "/fapi/v2/positionRisk",
Self::FuturesSetLeverage => "/fapi/v1/leverage",
Self::FuturesSetMarginType => "/fapi/v1/marginType",
Self::FuturesPositionMargin => "/fapi/v1/positionMargin",
Self::FuturesCommissionRate => "/fapi/v1/commissionRate",
Self::SpotListenKey => "/api/v3/userDataStream",
Self::FuturesListenKey => "/fapi/v1/listenKey",
Self::SpotRecentTrades => "/api/v3/trades",
Self::SpotHistoricalTrades => "/api/v3/historicalTrades",
Self::SpotAvgPrice => "/api/v3/avgPrice",
Self::SpotBookTicker => "/api/v3/ticker/bookTicker",
Self::FuturesRecentTrades => "/fapi/v1/trades",
Self::FuturesOpenInterest => "/fapi/v1/openInterest",
Self::FuturesOpenInterestHist => "/futures/data/openInterestHist",
Self::FuturesPremiumIndex => "/fapi/v1/premiumIndex",
Self::FuturesLongShortRatio => "/futures/data/topLongShortAccountRatio",
Self::FuturesTopLongShortPositionRatio => "/futures/data/topLongShortPositionRatio",
Self::FuturesGlobalLongShortAccountRatio => "/futures/data/globalLongShortAccountRatio",
Self::FuturesTakerLongShortRatio => "/futures/data/takerlongshortRatio",
Self::FuturesForceOrders => "/fapi/v1/forceOrders",
Self::SpotMyTrades => "/api/v3/myTrades",
Self::FuturesMyTrades => "/fapi/v1/userTrades",
Self::FuturesIncomeHistory => "/fapi/v1/income",
Self::ListenKeyKeepAlive => "/api/v3/userDataStream",
Self::ListenKeyClose => "/api/v3/userDataStream",
Self::AssetTransfer => "/sapi/v1/asset/transfer",
Self::AssetTransferHistory => "/sapi/v1/asset/transfer",
Self::DepositAddress => "/sapi/v1/capital/deposit/address",
Self::Withdraw => "/sapi/v1/capital/withdraw/apply",
Self::DepositHistory => "/sapi/v1/capital/deposit/hisrec",
Self::WithdrawHistory => "/sapi/v1/capital/withdraw/history",
Self::SubAccountCreate => "/sapi/v1/sub-account/virtualSubAccount",
Self::SubAccountList => "/sapi/v1/sub-account/list",
Self::SubAccountTransfer => "/sapi/v1/sub-account/universalTransfer",
Self::SubAccountAssets => "/sapi/v3/sub-account/assets",
Self::CmOpenInterest => "/dapi/v1/openInterest",
Self::CmForceOrders => "/dapi/v1/forceOrders",
Self::CmFundingRate => "/dapi/v1/fundingRate",
Self::CmDeliveryPrice => "/dapi/v1/deliveryPrice",
Self::CmOpenInterestHist => "/futures/data/openInterestHist",
Self::BasisHistory => "/futures/data/basis",
Self::InsuranceFundIncome => "/fapi/v1/income",
}
}
pub fn requires_auth(&self) -> bool {
match self {
Self::Ping
| Self::ServerTime
| Self::SpotPrice
| Self::SpotOrderbook
| Self::SpotKlines
| Self::SpotTicker
| Self::SpotExchangeInfo
| Self::FuturesPrice
| Self::FuturesOrderbook
| Self::FuturesKlines
| Self::FuturesTicker
| Self::FuturesExchangeInfo
| Self::FundingRate
| Self::SpotRecentTrades
| Self::SpotAvgPrice
| Self::SpotBookTicker
| Self::FuturesRecentTrades
| Self::FuturesOpenInterest
| Self::FuturesOpenInterestHist
| Self::FuturesPremiumIndex
| Self::FuturesLongShortRatio
| Self::FuturesTopLongShortPositionRatio
| Self::FuturesGlobalLongShortAccountRatio
| Self::FuturesTakerLongShortRatio
| Self::FuturesForceOrders
| Self::CmOpenInterest
| Self::CmForceOrders
| Self::CmFundingRate
| Self::CmOpenInterestHist
| Self::BasisHistory => false,
_ => true,
}
}
pub fn method(&self) -> &'static str {
match self {
Self::SpotCreateOrder
| Self::FuturesCreateOrder
| Self::FuturesSetLeverage
| Self::FuturesSetMarginType
| Self::FuturesPositionMargin
| Self::SpotOcoOrder
| Self::SpotOtocoOrder
| Self::SpotAlgoTwap
| Self::FuturesAlgoOrder
| Self::FuturesAlgoTwap
| Self::FuturesBatchOrders
| Self::SpotListenKey
| Self::FuturesListenKey
| Self::AssetTransfer
| Self::Withdraw
| Self::SubAccountCreate
| Self::SubAccountTransfer => "POST",
Self::SpotCancelOrder
| Self::SpotCancelAllOrders
| Self::FuturesCancelOrder
| Self::FuturesCancelAllOrders
| Self::ListenKeyClose => "DELETE",
Self::FuturesAmendOrder | Self::ListenKeyKeepAlive => "PUT",
Self::FuturesBatchAmend => "PATCH",
_ => "GET",
}
}
}
pub fn format_symbol(base: &str, quote: &str, _account_type: AccountType) -> String {
format!("{}{}", base.to_uppercase(), quote.to_uppercase())
}
pub fn map_kline_interval(interval: &str) -> &'static str {
match interval {
"1m" => "1m",
"3m" => "3m",
"5m" => "5m",
"15m" => "15m",
"30m" => "30m",
"1h" => "1h",
"2h" => "2h",
"4h" => "4h",
"6h" => "6h",
"8h" => "8h",
"12h" => "12h",
"1d" => "1d",
"3d" => "3d",
"1w" => "1w",
"1M" => "1M",
_ => "1h", }
}