use crate::core::types::AccountType;
#[derive(Debug, Clone)]
pub struct OkxUrls {
pub rest: &'static str,
pub ws_public: &'static str,
pub ws_private: &'static str,
pub ws_business: &'static str,
}
impl OkxUrls {
pub const MAINNET: Self = Self {
rest: "https://www.okx.com",
ws_public: "wss://ws.okx.com:8443/ws/v5/public",
ws_private: "wss://ws.okx.com:8443/ws/v5/private",
ws_business: "wss://ws.okx.com:8443/ws/v5/business",
};
pub const TESTNET: Self = Self {
rest: "https://www.okx.com", ws_public: "wss://wspap.okx.com:8443/ws/v5/public",
ws_private: "wss://wspap.okx.com:8443/ws/v5/private",
ws_business: "wss://wspap.okx.com:8443/ws/v5/business",
};
pub fn rest_url(&self) -> &str {
self.rest
}
pub fn ws_url(&self, private: bool) -> &str {
if private {
self.ws_private
} else {
self.ws_public
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum OkxEndpoint {
ServerTime,
Ticker,
AllTickers,
Orderbook,
OrderbookFull,
Klines,
HistoryKlines,
Trades,
HistoryTrades,
Instruments,
PlaceOrder,
PlaceBatchOrders,
CancelOrder,
CancelBatchOrders,
CancelAllAfter,
AmendOrder,
AmendBatchOrders,
GetOrder,
OpenOrders,
OrderHistory,
OrderHistoryArchive,
AlgoOrder,
AlgoOrderCancel,
AlgoOpenOrders,
Balance,
AssetBalances,
AccountConfig,
Positions,
PositionHistory,
MaxOrderSize,
SetLeverage,
GetLeverage,
SetPositionMode,
OpenInterest,
LongShortRatio,
FillsHistory,
FillsArchive,
FundingRate,
FundingRateHistory,
AssetTransfer,
TransferState,
AssetBills,
AccountBills,
AccountBillsArchive,
DepositAddress,
Withdrawal,
DepositHistory,
WithdrawalHistory,
SubAccountCreate,
SubAccountList,
SubAccountTransfer,
SubAccountBalances,
}
impl OkxEndpoint {
pub fn path(&self) -> &'static str {
match self {
Self::ServerTime => "/api/v5/public/time",
Self::Ticker => "/api/v5/market/ticker",
Self::AllTickers => "/api/v5/market/tickers",
Self::Orderbook => "/api/v5/market/books",
Self::OrderbookFull => "/api/v5/market/books-full",
Self::Klines => "/api/v5/market/candles",
Self::HistoryKlines => "/api/v5/market/history-candles",
Self::Trades => "/api/v5/market/trades",
Self::HistoryTrades => "/api/v5/market/history-trades",
Self::Instruments => "/api/v5/public/instruments",
Self::PlaceOrder => "/api/v5/trade/order",
Self::PlaceBatchOrders => "/api/v5/trade/batch-orders",
Self::CancelOrder => "/api/v5/trade/cancel-order",
Self::CancelBatchOrders => "/api/v5/trade/cancel-batch-orders",
Self::CancelAllAfter => "/api/v5/trade/cancel-all-after",
Self::AmendOrder => "/api/v5/trade/amend-order",
Self::AmendBatchOrders => "/api/v5/trade/amend-batch-orders",
Self::GetOrder => "/api/v5/trade/order",
Self::OpenOrders => "/api/v5/trade/orders-pending",
Self::OrderHistory => "/api/v5/trade/orders-history",
Self::OrderHistoryArchive => "/api/v5/trade/orders-history-archive",
Self::Balance => "/api/v5/account/balance",
Self::AssetBalances => "/api/v5/asset/balances",
Self::AccountConfig => "/api/v5/account/config",
Self::Positions => "/api/v5/account/positions",
Self::PositionHistory => "/api/v5/account/positions-history",
Self::MaxOrderSize => "/api/v5/account/max-size",
Self::SetLeverage => "/api/v5/account/set-leverage",
Self::GetLeverage => "/api/v5/account/leverage-info",
Self::SetPositionMode => "/api/v5/account/set-position-mode",
Self::OpenInterest => "/api/v5/public/open-interest",
Self::LongShortRatio => "/api/v5/rubik/stat/contracts/long-short-account-ratio",
Self::FillsHistory => "/api/v5/trade/fills",
Self::FillsArchive => "/api/v5/trade/fills-history",
Self::FundingRate => "/api/v5/public/funding-rate",
Self::FundingRateHistory => "/api/v5/public/funding-rate-history",
Self::AlgoOrder => "/api/v5/trade/order-algo",
Self::AlgoOrderCancel => "/api/v5/trade/cancel-algos",
Self::AlgoOpenOrders => "/api/v5/trade/orders-algo-pending",
Self::AssetTransfer => "/api/v5/asset/transfer",
Self::TransferState => "/api/v5/asset/transfer-state",
Self::AssetBills => "/api/v5/asset/bills",
Self::AccountBills => "/api/v5/account/bills",
Self::AccountBillsArchive => "/api/v5/account/bills-archive",
Self::DepositAddress => "/api/v5/asset/deposit-address",
Self::Withdrawal => "/api/v5/asset/withdrawal",
Self::DepositHistory => "/api/v5/asset/deposit-history",
Self::WithdrawalHistory => "/api/v5/asset/withdrawal-history",
Self::SubAccountCreate => "/api/v5/users/subaccount/create",
Self::SubAccountList => "/api/v5/users/subaccount/list",
Self::SubAccountTransfer => "/api/v5/asset/subaccount/transfer",
Self::SubAccountBalances => "/api/v5/account/subaccount/balances",
}
}
pub fn requires_auth(&self) -> bool {
match self {
Self::ServerTime
| Self::Ticker
| Self::AllTickers
| Self::Orderbook
| Self::OrderbookFull
| Self::Klines
| Self::HistoryKlines
| Self::Trades
| Self::HistoryTrades
| Self::Instruments
| Self::FundingRate
| Self::FundingRateHistory
| Self::OpenInterest
| Self::LongShortRatio => false,
_ => true,
}
}
pub fn method(&self) -> &'static str {
match self {
Self::PlaceOrder
| Self::PlaceBatchOrders
| Self::CancelOrder
| Self::CancelBatchOrders
| Self::CancelAllAfter
| Self::AmendOrder
| Self::AmendBatchOrders
| Self::SetLeverage
| Self::SetPositionMode
| Self::AlgoOrder
| Self::AlgoOrderCancel
| Self::AssetTransfer
| Self::Withdrawal
| Self::SubAccountCreate
| Self::SubAccountTransfer => "POST",
_ => "GET",
}
}
}
pub fn format_symbol(base: &str, quote: &str, account_type: AccountType) -> String {
match account_type {
AccountType::Spot | AccountType::Margin => {
format!("{}-{}", base.to_uppercase(), quote.to_uppercase())
}
AccountType::FuturesCross | AccountType::FuturesIsolated => {
format!("{}-{}-SWAP", base.to_uppercase(), quote.to_uppercase())
}
_ => {
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",
"12h" => "12H",
"1d" => "1D",
"1w" => "1W",
"1M" => "1M",
"3M" => "3M",
"6M" => "6M",
"1y" => "1Y",
_ => "1H", }
}
pub fn get_inst_type(account_type: AccountType) -> &'static str {
match account_type {
AccountType::Spot => "SPOT",
AccountType::Margin => "MARGIN",
AccountType::FuturesCross | AccountType::FuturesIsolated => "SWAP",
_ => "SPOT",
}
}
pub fn get_trade_mode(account_type: AccountType) -> &'static str {
match account_type {
AccountType::Spot => "cash",
AccountType::Margin => "cross",
AccountType::FuturesCross => "cross",
AccountType::FuturesIsolated => "isolated",
_ => "cash",
}
}
pub fn get_account_id(account_type: AccountType) -> &'static str {
match account_type {
AccountType::Spot => "6",
AccountType::Margin => "5",
AccountType::FuturesCross | AccountType::FuturesIsolated => "18",
_ => "6",
}
}