okx-client 0.1.2

Async Rust client for the OKX exchange API v5
Documentation
use crate::types::ws::channels::WsSubscriptionArg;
use crate::types::ws::events::WsConnectionType;

/// Route a subscription to the correct connection type (public, private, or business).
pub fn route_subscription(arg: &WsSubscriptionArg) -> WsConnectionType {
    if arg.is_business() {
        return WsConnectionType::Business;
    }
    if arg.is_private() {
        return WsConnectionType::Private;
    }
    WsConnectionType::Public
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_route_public() {
        let arg = WsSubscriptionArg::with_inst_id("tickers", "BTC-USDT");
        assert_eq!(route_subscription(&arg), WsConnectionType::Public);

        let arg = WsSubscriptionArg::with_inst_type("event-contract-markets", "EVENTS");
        assert_eq!(route_subscription(&arg), WsConnectionType::Public);
    }

    #[test]
    fn test_route_private() {
        let arg = WsSubscriptionArg::channel_only("orders");
        assert_eq!(route_subscription(&arg), WsConnectionType::Private);

        let arg = WsSubscriptionArg::channel_only("account");
        assert_eq!(route_subscription(&arg), WsConnectionType::Private);

        let arg = WsSubscriptionArg::channel_only("adl-warning");
        assert_eq!(route_subscription(&arg), WsConnectionType::Private);
    }

    #[test]
    fn test_route_business() {
        let channels = [
            "candle1m",
            "mark-price-candle1H",
            "index-candle1D",
            "orders-algo",
            "algo-advance",
            "deposit-info",
            "withdrawal-info",
            "grid-orders-spot",
            "grid-orders-contract",
            "grid-orders-moon",
            "grid-positions",
            "grid-sub-orders",
            "algo-recurring-buy",
        ];

        for channel in channels {
            let arg = WsSubscriptionArg::channel_only(channel);
            assert_eq!(route_subscription(&arg), WsConnectionType::Business);
        }
    }
}