use crate::core::types::AccountType;
#[derive(Debug, Clone)]
pub struct GateioUrls {
pub spot_rest: &'static str,
pub futures_rest: &'static str,
pub spot_ws: &'static str,
pub futures_ws: &'static str,
}
impl GateioUrls {
pub const MAINNET: Self = Self {
spot_rest: "https://api.gateio.ws/api/v4",
futures_rest: "https://fx-api.gateio.ws/api/v4",
spot_ws: "wss://api.gateio.ws/ws/v4/",
futures_ws: "wss://fx-ws.gateio.ws/v4/ws/usdt",
};
pub const TESTNET: Self = Self {
spot_rest: "https://api-testnet.gateapi.io/api/v4",
futures_rest: "https://fx-api-testnet.gateio.ws/api/v4",
spot_ws: "wss://api-testnet.gateapi.io/ws/v4",
futures_ws: "wss://fx-api-testnet.gateio.ws/ws/v4",
};
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,
}
}
pub fn settle(&self, account_type: AccountType) -> &'static str {
match account_type {
AccountType::FuturesCross | AccountType::FuturesIsolated => "usdt",
_ => "usdt",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GateioEndpoint {
ServerTime,
SpotTickers,
SpotOrderbook,
SpotKlines,
SpotSymbols,
SpotCreateOrder,
SpotCancelOrder,
SpotGetOrder,
SpotOpenOrders,
SpotCancelAllOrders,
SpotAccounts,
FuturesTickers,
FuturesOrderbook,
FuturesKlines,
FuturesContracts,
FundingRate,
FuturesCreateOrder,
FuturesCancelOrder,
FuturesGetOrder,
FuturesOpenOrders,
FuturesCancelAllOrders,
SpotPriceOrders,
SpotAmendOrder,
SpotBatchOrders,
FuturesBatchOrders,
FuturesAmendOrder,
FuturesBatchAmend,
FuturesAccounts,
FuturesPositions,
FuturesPosition,
FuturesSetLeverage,
WalletTransfer,
WalletTransferHistory,
DepositAddress,
Withdraw,
DepositHistory,
WithdrawalHistory,
SubAccountCreate,
SubAccountList,
SubAccountTransfer,
SubAccountBalance,
SpotTrades,
SpotMyTrades,
FuturesTrades,
FuturesMyTrades,
FuturesOpenInterest,
FuturesFundingRateHistory,
FuturesFundingPayments,
WalletLedger,
FuturesAccountBook,
}
impl GateioEndpoint {
pub fn path(&self, settle: Option<&str>) -> String {
match self {
Self::ServerTime => "/spot/time".to_string(),
Self::SpotTickers => "/spot/tickers".to_string(),
Self::SpotOrderbook => "/spot/order_book".to_string(),
Self::SpotKlines => "/spot/candlesticks".to_string(),
Self::SpotSymbols => "/spot/currency_pairs".to_string(),
Self::SpotCreateOrder => "/spot/orders".to_string(),
Self::SpotCancelOrder => "/spot/orders/{order_id}".to_string(),
Self::SpotGetOrder => "/spot/orders/{order_id}".to_string(),
Self::SpotOpenOrders => "/spot/orders".to_string(),
Self::SpotCancelAllOrders => "/spot/orders".to_string(),
Self::SpotAccounts => "/spot/accounts".to_string(),
Self::FuturesTickers => format!("/futures/{}/tickers", settle.unwrap_or("usdt")),
Self::FuturesOrderbook => format!("/futures/{}/order_book", settle.unwrap_or("usdt")),
Self::FuturesKlines => format!("/futures/{}/candlesticks", settle.unwrap_or("usdt")),
Self::FuturesContracts => format!("/futures/{}/contracts", settle.unwrap_or("usdt")),
Self::FundingRate => format!("/futures/{}/funding_rate", settle.unwrap_or("usdt")),
Self::FuturesCreateOrder => format!("/futures/{}/orders", settle.unwrap_or("usdt")),
Self::FuturesCancelOrder => format!("/futures/{}/orders/{{order_id}}", settle.unwrap_or("usdt")),
Self::FuturesGetOrder => format!("/futures/{}/orders/{{order_id}}", settle.unwrap_or("usdt")),
Self::FuturesOpenOrders => format!("/futures/{}/orders", settle.unwrap_or("usdt")),
Self::FuturesCancelAllOrders => format!("/futures/{}/orders", settle.unwrap_or("usdt")),
Self::SpotPriceOrders => "/spot/price_orders".to_string(),
Self::SpotAmendOrder => "/spot/orders/{order_id}".to_string(),
Self::SpotBatchOrders => "/spot/batch_orders".to_string(),
Self::FuturesBatchOrders => format!("/futures/{}/batch_orders", settle.unwrap_or("usdt")),
Self::FuturesAmendOrder => format!("/futures/{}/orders/{{order_id}}", settle.unwrap_or("usdt")),
Self::FuturesBatchAmend => format!("/futures/{}/batch_amend_orders", settle.unwrap_or("usdt")),
Self::FuturesAccounts => format!("/futures/{}/accounts", settle.unwrap_or("usdt")),
Self::FuturesPositions => format!("/futures/{}/positions", settle.unwrap_or("usdt")),
Self::FuturesPosition => format!("/futures/{}/positions/{{contract}}", settle.unwrap_or("usdt")),
Self::FuturesSetLeverage => format!("/futures/{}/positions/{{contract}}/leverage", settle.unwrap_or("usdt")),
Self::WalletTransfer => "/wallet/transfers".to_string(),
Self::WalletTransferHistory => "/wallet/transfers".to_string(),
Self::DepositAddress => "/wallet/deposit_address".to_string(),
Self::Withdraw => "/withdrawals".to_string(),
Self::DepositHistory => "/wallet/deposits".to_string(),
Self::WithdrawalHistory => "/wallet/withdrawals".to_string(),
Self::SubAccountCreate => "/sub_accounts".to_string(),
Self::SubAccountList => "/sub_accounts".to_string(),
Self::SubAccountTransfer => "/sub_accounts/transfers".to_string(),
Self::SubAccountBalance => "/sub_accounts/{user_id}/balances".to_string(),
Self::SpotTrades => "/spot/trades".to_string(),
Self::SpotMyTrades => "/spot/my_trades".to_string(),
Self::FuturesTrades => format!("/futures/{}/trades", settle.unwrap_or("usdt")),
Self::FuturesMyTrades => format!("/futures/{}/my_trades", settle.unwrap_or("usdt")),
Self::FuturesOpenInterest => format!("/futures/{}/contract_stats", settle.unwrap_or("usdt")),
Self::FuturesFundingRateHistory => format!("/futures/{}/funding_rate", settle.unwrap_or("usdt")),
Self::FuturesFundingPayments => format!("/futures/{}/funding_payments", settle.unwrap_or("usdt")),
Self::WalletLedger => "/wallet/ledger".to_string(),
Self::FuturesAccountBook => format!("/futures/{}/account_book", settle.unwrap_or("usdt")),
}
}
pub fn requires_auth(&self) -> bool {
match self {
Self::ServerTime
| Self::SpotTickers
| Self::SpotOrderbook
| Self::SpotKlines
| Self::SpotSymbols
| Self::FuturesTickers
| Self::FuturesOrderbook
| Self::FuturesKlines
| Self::FuturesContracts
| Self::FundingRate
| Self::SpotTrades
| Self::FuturesTrades
| Self::FuturesOpenInterest
| Self::FuturesFundingRateHistory => false,
_ => true,
}
}
pub fn method(&self) -> &'static str {
match self {
Self::SpotCreateOrder
| Self::FuturesCreateOrder
| Self::SpotPriceOrders
| Self::SpotBatchOrders
| Self::FuturesBatchOrders
| Self::FuturesBatchAmend
| Self::FuturesSetLeverage
| Self::WalletTransfer
| Self::Withdraw
| Self::SubAccountCreate
| Self::SubAccountTransfer => "POST",
Self::SpotCancelOrder
| Self::SpotCancelAllOrders
| Self::FuturesCancelOrder
| Self::FuturesCancelAllOrders => "DELETE",
Self::FuturesAmendOrder
| Self::SpotAmendOrder => "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 {
"10s" => "10s",
"1m" => "1m",
"3m" => "1m", "5m" => "5m",
"15m" => "15m",
"30m" => "30m",
"1h" => "1h",
"2h" => "1h", "4h" => "4h",
"6h" => "4h", "8h" => "8h",
"12h" => "8h", "1d" => "1d",
"1w" => "7d",
"1M" => "30d",
_ => "1h",
}
}