crypto_pair/
lib.rs

1#![allow(clippy::unnecessary_wraps)]
2
3use crypto_market_type::MarketType;
4mod exchanges;
5
6/// Normalize a trading currency.
7///
8/// # Arguments
9///
10/// * `currency` - The exchange-specific currency
11/// * `exchange` - The normalized symbol
12pub fn normalize_currency(currency: &str, exchange: &str) -> String {
13    match exchange {
14        "bitfinex" => exchanges::bitfinex::normalize_currency(currency),
15        "bitmex" => exchanges::bitmex::normalize_currency(currency),
16        "kraken" => exchanges::kraken::normalize_currency(currency),
17        "kucoin" => exchanges::kucoin::normalize_currency(currency),
18        _ => currency.to_uppercase(),
19    }
20}
21
22/// Normalize a cryptocurrency trading symbol.
23///
24/// # Arguments
25///
26/// * `symbol` - The original pair of an exchange
27/// * `exchange` - The exchange name
28///
29/// # Examples
30///
31/// ```
32/// use crypto_pair::normalize_pair;
33///
34/// assert_eq!(Some("BTC/USD".to_string()), normalize_pair("XBTUSD", "bitmex"));
35/// assert_eq!(Some("BTC/USD".to_string()), normalize_pair("XBTH21", "bitmex"));
36/// assert_eq!(Some("BTC/USDT".to_string()), normalize_pair("BTCUSDT", "binance"));
37/// assert_eq!(Some("BTC/USDT".to_string()), normalize_pair("btcusdt", "huobi"));
38/// assert_eq!(Some("BTC/USDT".to_string()), normalize_pair("BTCUST", "bitfinex"));
39/// ```
40pub fn normalize_pair(symbol: &str, exchange: &str) -> Option<String> {
41    match exchange {
42        "binance" => exchanges::binance::normalize_pair(symbol),
43        "bitfinex" => exchanges::bitfinex::normalize_pair(symbol),
44        "bitget" => exchanges::bitget::normalize_pair(symbol),
45        "bithumb" => Some(symbol.replace('-', "/")),
46        "bitmex" => exchanges::bitmex::normalize_pair(symbol),
47        "bitstamp" => exchanges::bitstamp::normalize_pair(symbol),
48        "bitz" => Some(symbol.replace('_', "/").to_uppercase()),
49        "bybit" => exchanges::bybit::normalize_pair(symbol),
50        "coinbase_pro" => Some(symbol.replace('-', "/")),
51        "deribit" => exchanges::deribit::normalize_pair(symbol),
52        "dydx" => exchanges::dydx::normalize_pair(symbol),
53        "ftx" => exchanges::ftx::normalize_pair(symbol),
54        "gate" => {
55            let (base, quote) = {
56                let v: Vec<&str> = symbol.split('_').collect();
57                (v[0].to_string(), v[1].to_string())
58            };
59
60            Some(format!("{base}/{quote}"))
61        }
62        "huobi" => exchanges::huobi::normalize_pair(symbol),
63        "kraken" => exchanges::kraken::normalize_pair(symbol),
64        "kucoin" => exchanges::kucoin::normalize_pair(symbol),
65        "mxc" | "mexc" => Some(symbol.replace('_', "/")),
66        "okex" | "okx" => {
67            let v: Vec<&str> = symbol.split('-').collect();
68            Some(format!("{}/{}", v[0], v[1]))
69        }
70        "Poloniex" => Some(symbol.replace('_', "/")),
71        "Upbit" => Some(symbol.replace('-', "/")),
72        "zb" => exchanges::zb::normalize_pair(symbol),
73        "zbg" => exchanges::zbg::normalize_pair(symbol),
74        _ => panic!("Unknown exchange {exchange}"),
75    }
76}
77
78/// Infer out market type from the symbol.
79///
80/// The `is_spot` parameter is not needed in most cases, but at some exchanges
81///  (including binance, gate and mexc) a symbol might exist in both spot and
82/// contract markets, for example:
83/// * At binance `BTCUSDT` exists in both spot and linear_swap markets
84/// * At gate `BTC_USDT` exists in both spot and linear_swap markets,
85/// `BTC_USD` exists in both spot and inverse_swap markets
86pub fn get_market_type(symbol: &str, exchange: &str, is_spot: Option<bool>) -> MarketType {
87    match exchange {
88        "binance" => exchanges::binance::get_market_type(symbol, is_spot),
89        "bitfinex" => exchanges::bitfinex::get_market_type(symbol),
90        "bitget" => exchanges::bitget::get_market_type(symbol),
91        "bithumb" => MarketType::Spot,
92        "bitmex" => exchanges::bitmex::get_market_type(symbol),
93        "bitstamp" => MarketType::Spot,
94        "bybit" => exchanges::bybit::get_market_type(symbol),
95        "coinbase_pro" => MarketType::Spot,
96        "deribit" => exchanges::deribit::get_market_type(symbol),
97        "dydx" => MarketType::LinearSwap,
98        "ftx" => exchanges::ftx::get_market_type(symbol),
99        "gate" => exchanges::gate::get_market_type(symbol, is_spot),
100        "huobi" => exchanges::huobi::get_market_type(symbol),
101        "kraken" => exchanges::kraken::get_market_type(symbol),
102        "kucoin" => exchanges::kucoin::get_market_type(symbol),
103        "mxc" | "mexc" => exchanges::mexc::get_market_type(symbol, is_spot),
104        "okex" | "okx" => exchanges::okx::get_market_type(symbol),
105        "zb" => exchanges::zb::get_market_type(symbol),
106        "zbg" => exchanges::zbg::get_market_type(symbol),
107        _ => MarketType::Unknown,
108    }
109}