use crate::core::types::AccountType;
#[derive(Debug, Clone)]
pub struct KuCoinUrls {
pub spot_rest: &'static str,
pub futures_rest: &'static str,
pub spot_ws: &'static str,
pub futures_ws: &'static str,
}
impl KuCoinUrls {
pub const MAINNET: Self = Self {
spot_rest: "https://api.kucoin.com",
futures_rest: "https://api-futures.kucoin.com",
spot_ws: "wss://ws-api-spot.kucoin.com",
futures_ws: "wss://ws-api-futures.kucoin.com",
};
pub const TESTNET: Self = Self {
spot_rest: "https://openapi-sandbox.kucoin.com",
futures_rest: "https://api-sandbox-futures.kucoin.com",
spot_ws: "wss://ws-api-sandbox.kucoin.com",
futures_ws: "wss://ws-api-sandbox-futures.kucoin.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 KuCoinEndpoint {
Timestamp,
SpotPrice,
SpotOrderbook,
SpotKlines,
SpotTicker,
SpotAllTickers,
SpotSymbols,
SpotCreateOrder,
SpotCancelOrder,
SpotGetOrder,
SpotOpenOrders,
SpotAllOrders,
SpotCancelAllOrders,
SpotOcoOrder,
SpotBatchOrders,
FuturesBatchOrders,
SpotAccounts,
SpotAccountDetail,
FuturesPrice,
FuturesOrderbook,
FuturesKlines,
FuturesTicker,
FuturesAllTickers,
FuturesContracts,
FundingRate,
FuturesCreateOrder,
FuturesCancelOrder,
FuturesGetOrder,
FuturesOpenOrders,
FuturesAllOrders,
FuturesCancelAllOrders,
FuturesAmendOrder,
FuturesAccount,
FuturesPositions,
FuturesPosition,
FuturesSetLeverage,
InnerTransfer,
TransferHistory,
DepositAddress,
Withdraw,
DepositHistory,
WithdrawalHistory,
SubAccountCreate,
SubAccountList,
SubAccountTransfer,
SubAccountBalance,
SpotRecentTrades,
FullOrderbook,
SpotFills,
FuturesTradeHistory,
FuturesFundingRates,
FuturesMarkPrice,
FuturesIndexPrice,
FuturesPremiumIndex,
FuturesFills,
WsPublicToken,
WsPrivateToken,
TransferQuotas,
WithdrawalCancel,
WithdrawalQuotas,
FuturesFundingHistory,
SpotLedger,
}
impl KuCoinEndpoint {
pub fn path(&self) -> &'static str {
match self {
Self::Timestamp => "/api/v1/timestamp",
Self::SpotPrice => "/api/v1/market/orderbook/level1",
Self::SpotOrderbook => "/api/v1/market/orderbook/level2_100",
Self::SpotKlines => "/api/v1/market/candles",
Self::SpotTicker => "/api/v1/market/stats",
Self::SpotAllTickers => "/api/v1/market/allTickers",
Self::SpotSymbols => "/api/v2/symbols",
Self::SpotCreateOrder => "/api/v1/orders",
Self::SpotCancelOrder => "/api/v1/orders/{orderId}",
Self::SpotGetOrder => "/api/v1/orders/{orderId}",
Self::SpotOpenOrders => "/api/v1/orders",
Self::SpotAllOrders => "/api/v1/orders",
Self::SpotCancelAllOrders => "/api/v1/orders",
Self::SpotOcoOrder => "/api/v3/oco/order",
Self::SpotBatchOrders => "/api/v1/hf/orders/multi",
Self::FuturesBatchOrders => "/api/v1/orders/multi",
Self::SpotAccounts => "/api/v1/accounts",
Self::SpotAccountDetail => "/api/v1/accounts/{accountId}",
Self::FuturesPrice => "/api/v1/ticker",
Self::FuturesOrderbook => "/api/v1/level2/depth100",
Self::FuturesKlines => "/api/v1/kline/query",
Self::FuturesTicker => "/api/v1/ticker",
Self::FuturesAllTickers => "/api/v1/allTickers",
Self::FuturesContracts => "/api/v1/contracts/active",
Self::FundingRate => "/api/v1/funding-rate/{symbol}/current",
Self::FuturesCreateOrder => "/api/v1/orders",
Self::FuturesCancelOrder => "/api/v1/orders/{orderId}",
Self::FuturesGetOrder => "/api/v1/orders/{orderId}",
Self::FuturesOpenOrders => "/api/v1/orders",
Self::FuturesAllOrders => "/api/v1/orders",
Self::FuturesCancelAllOrders => "/api/v1/orders",
Self::FuturesAmendOrder => "/api/v1/orders/{orderId}",
Self::FuturesAccount => "/api/v1/account-overview",
Self::FuturesPositions => "/api/v1/positions",
Self::FuturesPosition => "/api/v1/position",
Self::FuturesSetLeverage => "/api/v1/position/risk-limit-level/change",
Self::InnerTransfer => "/api/v3/accounts/inner-transfer",
Self::TransferHistory => "/api/v1/accounts/inner-transfer",
Self::DepositAddress => "/api/v3/deposit-addresses",
Self::Withdraw => "/api/v1/withdrawals",
Self::DepositHistory => "/api/v1/deposits",
Self::WithdrawalHistory => "/api/v1/withdrawals",
Self::SubAccountCreate => "/api/v2/sub/user/created",
Self::SubAccountList => "/api/v2/sub/user",
Self::SubAccountTransfer => "/api/v2/accounts/sub-transfer",
Self::SubAccountBalance => "/api/v1/sub-accounts/{subUserId}",
Self::SpotRecentTrades => "/api/v1/market/histories",
Self::FullOrderbook => "/api/v1/market/orderbook/level2",
Self::SpotFills => "/api/v1/fills",
Self::FuturesTradeHistory => "/api/v1/trade/history",
Self::FuturesFundingRates => "/api/v1/contract/funding-rates",
Self::FuturesMarkPrice => "/api/v1/mark-price/{symbol}/current",
Self::FuturesIndexPrice => "/api/v1/index-price/{symbol}/current",
Self::FuturesPremiumIndex => "/api/v1/premium-index/{symbol}/current",
Self::FuturesFills => "/api/v1/fills",
Self::WsPublicToken => "/api/v1/bullet-public",
Self::WsPrivateToken => "/api/v1/bullet-private",
Self::TransferQuotas => "/api/v1/accounts/transferable",
Self::WithdrawalCancel => "/api/v1/withdrawals/{withdrawalId}",
Self::WithdrawalQuotas => "/api/v1/withdrawals/quotas",
Self::FuturesFundingHistory => "/api/v1/funding-history",
Self::SpotLedger => "/api/v1/accounts/ledgers",
}
}
pub fn requires_auth(&self) -> bool {
match self {
Self::Timestamp
| Self::SpotPrice
| Self::SpotOrderbook
| Self::SpotKlines
| Self::SpotTicker
| Self::SpotAllTickers
| Self::SpotSymbols
| Self::SpotRecentTrades
| Self::FullOrderbook
| Self::FuturesPrice
| Self::FuturesOrderbook
| Self::FuturesKlines
| Self::FuturesTicker
| Self::FuturesAllTickers
| Self::FuturesContracts
| Self::FundingRate
| Self::FuturesTradeHistory
| Self::FuturesFundingRates
| Self::FuturesMarkPrice
| Self::FuturesIndexPrice
| Self::FuturesPremiumIndex
| Self::WsPublicToken => false,
_ => true,
}
}
pub fn method(&self) -> &'static str {
match self {
Self::SpotCreateOrder
| Self::FuturesCreateOrder
| Self::FuturesAmendOrder
| Self::SpotOcoOrder
| Self::SpotBatchOrders
| Self::FuturesBatchOrders
| Self::WsPublicToken
| Self::WsPrivateToken
| Self::InnerTransfer
| Self::Withdraw
| Self::SubAccountCreate
| Self::SubAccountTransfer => "POST",
Self::SpotCancelOrder
| Self::SpotCancelAllOrders
| Self::FuturesCancelOrder
| Self::FuturesCancelAllOrders
| Self::WithdrawalCancel => "DELETE",
_ => "GET",
}
}
}
pub fn format_symbol(base: &str, quote: &str, account_type: AccountType) -> String {
match account_type {
AccountType::Spot | AccountType::Margin => {
format!("{}-{}", base, quote)
}
AccountType::FuturesCross | AccountType::FuturesIsolated => {
let base = if base.to_uppercase() == "BTC" { "XBT" } else { base };
match quote.to_uppercase().as_str() {
"USDT" => format!("{}USDTM", base), "USD" => format!("{}USDM", base), _ => format!("{}{}M", base, quote.to_uppercase()), }
}
_ => {
format!("{}-{}", base, quote)
}
}
}
pub fn map_kline_interval(interval: &str) -> &'static str {
match interval {
"1m" => "1min",
"3m" => "3min",
"5m" => "5min",
"15m" => "15min",
"30m" => "30min",
"1h" => "1hour",
"2h" => "2hour",
"4h" => "4hour",
"6h" => "6hour",
"8h" => "8hour",
"12h" => "12hour",
"1d" => "1day",
"1w" => "1week",
"1M" => "1month",
_ => "1hour",
}
}
pub fn map_futures_granularity(interval: &str) -> u32 {
match interval {
"1m" => 1,
"5m" => 5,
"15m" => 15,
"30m" => 30,
"1h" => 60,
"2h" => 120,
"4h" => 240,
"8h" => 480,
"12h" => 720,
"1d" => 1440,
"1w" => 10080,
_ => 60, }
}