use crate::core::types::{AccountType, Symbol};
pub struct BybitUrls;
impl BybitUrls {
pub fn base_url(testnet: bool) -> &'static str {
if testnet {
"https://api-testnet.bybit.com"
} else {
"https://api.bybit.com"
}
}
pub fn ws_spot_url(testnet: bool) -> &'static str {
if testnet {
"wss://stream-testnet.bybit.com/v5/public/spot"
} else {
"wss://stream.bybit.com/v5/public/spot"
}
}
pub fn ws_linear_url(testnet: bool) -> &'static str {
if testnet {
"wss://stream-testnet.bybit.com/v5/public/linear"
} else {
"wss://stream.bybit.com/v5/public/linear"
}
}
pub fn ws_private_url(testnet: bool) -> &'static str {
if testnet {
"wss://stream-testnet.bybit.com/v5/private"
} else {
"wss://stream.bybit.com/v5/private"
}
}
pub fn ws_url(account_type: AccountType, testnet: bool) -> &'static str {
match account_type {
AccountType::Spot | AccountType::Margin => Self::ws_spot_url(testnet),
AccountType::FuturesCross | AccountType::FuturesIsolated => Self::ws_linear_url(testnet),
_ => Self::ws_spot_url(testnet),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BybitEndpoint {
Ticker, Orderbook, Klines, Symbols, RecentTrades, ServerTime,
Balance, AccountInfo,
PlaceOrder, CancelOrder, CancelAllOrders, OrderStatus, OpenOrders, OrderHistory,
Positions, SetLeverage, SetMarginMode, AddMargin, TpSlMode, FundingRate,
AmendOrder, BatchPlaceOrders, BatchCancelOrders, BatchAmendOrders,
OpenInterest, LongShortRatio, MarkPriceKline, IndexPriceKline, PremiumIndexKline,
MyTrades, ClosedPnl,
FeeRate,
InterTransfer, TransferHistory,
DepositAddress, Withdraw, DepositHistory, WithdrawHistory,
CreateSubMember, ListSubMembers, UniversalTransfer, SubAccountBalance,
TransactionLog,
}
impl BybitEndpoint {
pub fn path(&self) -> &'static str {
match self {
Self::Ticker => "/v5/market/tickers",
Self::Orderbook => "/v5/market/orderbook",
Self::Klines => "/v5/market/kline",
Self::Symbols => "/v5/market/instruments-info",
Self::RecentTrades => "/v5/market/recent-trade",
Self::ServerTime => "/v5/market/time",
Self::Balance => "/v5/account/wallet-balance",
Self::AccountInfo => "/v5/account/info",
Self::PlaceOrder => "/v5/order/create",
Self::CancelOrder => "/v5/order/cancel",
Self::CancelAllOrders => "/v5/order/cancel-all",
Self::OrderStatus => "/v5/order/realtime",
Self::OpenOrders => "/v5/order/realtime",
Self::OrderHistory => "/v5/order/history",
Self::Positions => "/v5/position/list",
Self::SetLeverage => "/v5/position/set-leverage",
Self::SetMarginMode => "/v5/position/switch-isolated",
Self::AddMargin => "/v5/position/add-margin",
Self::TpSlMode => "/v5/position/trading-stop",
Self::FundingRate => "/v5/market/funding/history",
Self::AmendOrder => "/v5/order/amend",
Self::BatchPlaceOrders => "/v5/order/create-batch",
Self::BatchCancelOrders => "/v5/order/cancel-batch",
Self::BatchAmendOrders => "/v5/order/amend-batch",
Self::OpenInterest => "/v5/market/open-interest",
Self::LongShortRatio => "/v5/market/account-ratio",
Self::MarkPriceKline => "/v5/market/mark-price-kline",
Self::IndexPriceKline => "/v5/market/index-price-kline",
Self::PremiumIndexKline => "/v5/market/premium-index-price-kline",
Self::MyTrades => "/v5/execution/list",
Self::ClosedPnl => "/v5/position/closed-pnl",
Self::FeeRate => "/v5/account/fee-rate",
Self::InterTransfer => "/v5/asset/transfer/inter-transfer",
Self::TransferHistory => "/v5/asset/transfer/query-inter-transfer-list",
Self::DepositAddress => "/v5/asset/deposit/query-address",
Self::Withdraw => "/v5/asset/withdraw/create",
Self::DepositHistory => "/v5/asset/deposit/query-record",
Self::WithdrawHistory => "/v5/asset/withdraw/query-record",
Self::CreateSubMember => "/v5/user/create-sub-member",
Self::ListSubMembers => "/v5/user/query-sub-members",
Self::UniversalTransfer => "/v5/asset/transfer/universal-transfer",
Self::SubAccountBalance => "/v5/asset/transfer/query-account-coins-balance",
Self::TransactionLog => "/v5/account/transaction-log",
}
}
pub fn method(&self) -> &'static str {
match self {
Self::PlaceOrder
| Self::CancelOrder
| Self::CancelAllOrders
| Self::AmendOrder
| Self::BatchPlaceOrders
| Self::BatchCancelOrders
| Self::BatchAmendOrders
| Self::SetLeverage
| Self::SetMarginMode
| Self::AddMargin
| Self::TpSlMode
| Self::InterTransfer
| Self::Withdraw
| Self::CreateSubMember
| Self::UniversalTransfer => "POST",
_ => "GET",
}
}
pub fn is_private(&self) -> bool {
match self {
Self::Ticker
| Self::Orderbook
| Self::Klines
| Self::Symbols
| Self::RecentTrades
| Self::ServerTime
| Self::FundingRate
| Self::OpenInterest
| Self::LongShortRatio
| Self::MarkPriceKline
| Self::IndexPriceKline
| Self::PremiumIndexKline => false,
_ => true,
}
}
}
pub fn format_symbol(symbol: &Symbol, _account_type: AccountType) -> String {
format!("{}{}", symbol.base.to_uppercase(), symbol.quote.to_uppercase())
}
pub fn account_type_to_category(account_type: AccountType) -> &'static str {
match account_type {
AccountType::Spot | AccountType::Margin => "spot",
AccountType::FuturesCross | AccountType::FuturesIsolated => "linear",
_ => "spot",
}
}
pub fn account_type_to_transfer_type(account_type: AccountType) -> &'static str {
match account_type {
AccountType::Spot => "SPOT",
AccountType::Margin => "UNIFIED",
AccountType::FuturesCross => "CONTRACT",
AccountType::FuturesIsolated => "CONTRACT",
_ => "SPOT",
}
}
pub fn map_kline_interval(interval: &str) -> &'static str {
match interval {
"1m" => "1",
"3m" => "3",
"5m" => "5",
"15m" => "15",
"30m" => "30",
"1h" => "60",
"2h" => "120",
"4h" => "240",
"6h" => "360",
"12h" => "720",
"1d" => "D",
"1w" => "W",
"1M" => "M",
_ => "60", }
}