#crypto #exchange #crypto-api #api #websocket #api-bindings

crypto-com-exchange

crypto com交易所的WebSocket库 https://exchange-docs.crypto.com/spot/index.html#websocket-root-endpoints

10个稳定版本

1.2.0 2024年7月5日
1.0.8 2023年9月7日
1.0.5 2023年7月31日
1.0.2 2023年6月20日
1.0.1 2022年5月9日

#1636 in 魔法豆

Download history 1/week @ 2024-06-27 124/week @ 2024-07-04 3/week @ 2024-07-11 1/week @ 2024-07-25

每月128次下载
apecast中使用

Apache-2.0

53KB
1K SLoC

rs-crypto-com-exchange

这是一个crypto com交易所WebSocket API的官方库 https://exchange-docs.crypto.com/spot/index.html#websocket-root-endpoints 库文档可在 https://docs.rs/crypto-com-exchange/latest/crypto_com_exchange/ 查找

基本上有两种客户端:MarketClientUserClientMarketClient用于获取全局市场信息,例如用于监控。 UserClient需要认证,用于创建订单、检查余额以及所有与用户相关的事务。

这是MarketClient的一个示例

use tokio::time::{sleep, Duration};
use crypto_com_exchange::{MarketClient, MarketSubscribeResponse, MarketSubscribeResult};


#[tokio::main]
async fn main() {

    let (client, event_receiver) = MarketClient::new().await.unwrap();
    //Documentations says that it is recommented to wait one second before start sending messages
    sleep(Duration::from_secs(1)).await;
    
    // One of every type of message. You can put as many you want. Check the documentation for more details
    let channels = vec![
        "trade.ETH_CRO".to_string(),
        "candlestick.1h.ETH_CRO".to_string(),
        "ticker.ETH_CRO".to_string(),
        "book.ETH_CRO.150".to_string(),
        ];
    println!("Subscribing to {:?}", channels);
    
    client.subscribe(channels).await.unwrap();
    println!("Ready");
    loop {
        match event_receiver.recv_async().await {
            Ok(message) => {
                match message {
                    MarketSubscribeResponse::Confirmation{id, code} => {
                        if code == 0 {
                            println!("Sub {} OK", id);
                        } else {
                            println!("Sub {} fail with code {}", id, code);
                        }

                    },
                    MarketSubscribeResponse::Result{result} => {
                        match result {
                            MarketSubscribeResult::TradeResult(result) => {
                                println!("Trade: {:?}", result);
                                
                            },
                            MarketSubscribeResult::CandlestickResult(result) => {
                                println!("Candlestick: {:?}", result);
                                
                            },
                            MarketSubscribeResult::TickerResult(result) => {
                                println!("Ticker: {:?}", result);
                                
                            },
                            MarketSubscribeResult::BookResult(result) => {
                                println!("Book: {:?}", result);
                                
                            },
                            
                        }
                        

                    }
                }
            },
            Err(err) => {
                println!("Error when receiving a message: {}", err)
            }
        }
    }

}

这是UserClient的一个示例。它目前正在开发中,但至少可以完成认证和获取余额

use tokio::time::{sleep, Duration};
use crypto_com_exchange::{UserClient, UserSubscribeResponse, UserSubscribeResult};


#[tokio::main]
async fn main() {

    let (client, event_receiver) = UserClient::new("api_key".to_string(), "api_secret".to_string()).await.unwrap();
    //Documentations says that it is recommented to wait one second before start sending messages
    sleep(Duration::from_secs(1)).await;
    
    // One of every type of message. You can put as many you want. Check the documentation for more details
    let channels = vec![
        "trade.ETH_CRO".to_string(),
        "candlestick.1h.ETH_CRO".to_string(),
        "ticker.ETH_CRO".to_string(),
        "book.ETH_CRO.150".to_string(),
        ];
    println!("Subscribing to {:?}", channels);
    
    client.subscribe(channels).await.unwrap();
    println!("Ready");
    loop {
        match event_receiver.recv_async().await {
            Ok(message) => {
                match message {
                    UserSubscribeResponse::Auth{id, code} => {
                        if code == 0 {
                            println!("Auth {} OK", id);
                        } else {
                            println!("Auth {} fail with code {}", id, code);
                        }
                    }
                    UserSubscribeResponse::Confirmation{id, code} => {
                        if code == 0 {
                            println!("Sub {} OK", id);
                        } else {
                            println!("Sub {} fail with code {}", id, code);
                        }

                    },
                    UserSubscribeResponse::Result{result} => {
                        match result {
                            UserSubscribeResult::BalanceResult(result) => {
                                println!("Trade: {:?}", result);
                                
                            },

                        }
                        

                    }
                }
            },
            Err(err) => {
                println!("Error when receiving a message: {}", err)
            }
        }
    }

}

依赖项

~6–16MB
~202K SLoC