binance / binance-spot-connector-rust

MIT License
109 stars 39 forks source link

How do I set credentials using Websocket API? #15

Open avnerbarr opened 5 months ago

avnerbarr commented 5 months ago

I'm trying to use the Websocket version of the api but I don't see any place to put the credentials

There is a lot of unclarity in the docs and I don't see anything in the examples explaining or showing how this is done for the Websocket side.

here is a trivial snippet which leads to a crash when unwrapping the connection. The error isn't clear as to what the issue is

use binance_spot_connector_rust::{
    http::Credentials,
    market::klines::KlineInterval, market_stream::kline::KlineStream,
    tokio_tungstenite::BinanceWebSocketClient,
};
use env_logger::Builder;
use futures_util::StreamExt;
#[tokio::main]
async fn main() {

    let res =  BinanceWebSocketClient::connect_async("wss://testnet.binance.vision/ws-api/v3.").await;
    if res.is_err() {
        println!("{:?}", res.as_ref().err());
    }

    let (mut conn, y ) = res.unwrap();
    /// use binance_spot_connector_rust::market_stream::ticker::TickerStream;
///
 let individual_symbol_stream = binance_spot_connector_rust::market_stream::ticker::TickerStream::from_symbol("BTCUSDT");
    conn.subscribe(vec![
        &individual_symbol_stream.into()
    ]
    ).await;
    println!("Hello, world!");
}
Some(Http(Response { status: 404, version: HTTP/1.1, headers: {"content-length": "48", "connection": "close", "date": "Mon, 29 Jan 2024 13:10:39 GMT", "server": "X", "x-mbx-uuid": "656dbd59-91ed-4b67-9d61-88b87ec8a82a", "x-cache": "Error from cloudfront", "via": "1.1 7d30cf029bde247eef16106ceffb39ca.cloudfront.net (CloudFront)", "x-amz-cf-pop": "TLV50-C1", "x-amz-cf-id": "83iERQTXPkgGVJNB7GqEk2X3gjH9HIN2v9hwqF22f_YuaIYmNpxBqA=="}, body: None }))
thread 'main' panicked at src/main.rs:15:30:
called `Result::unwrap()` on an `Err` value: Http(Response { status: 404, version: HTTP/1.1, headers: {"content-length": "48", "connection": "close", "date": "Mon, 29 Jan 2024 13:10:39 GMT", "server": "X", "x-mbx-uuid": "656dbd59-91ed-4b67-9d61-88b87ec8a82a", "x-cache": "Error from cloudfront", "via": "1.1 7d30cf029bde247eef16106ceffb39ca.cloudfront.net (CloudFront)", "x-amz-cf-pop": "TLV50-C1", "x-amz-cf-id": "83iERQTXPkgGVJNB7GqEk2X3gjH9HIN2v9hwqF22f_YuaIYmNpxBqA=="}, body: None })
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Pasted_Image_29_01_2024__15_29
0xdevgranero commented 4 months ago

have you resolved that yet?Thks

JaydenElliott commented 1 month ago

Hi, I don't think the problem is authentication here. You received a 404, implying the endpoint wasn't found. Your websocket url has a fullstop . at the end .

For future reference, all ws endpoints requiring any sort of authentication will take a listen key as an argument. E.g.

        // get listen key
        let client = BinanceHttpClient::default().credentials(self.exchange_credentials.clone());
        let listen_key_response = client
            .send(margin_stream::new_listen_key())
            .await?
            .into_body_str()
            .await?;
        let listen_key = serde_json::from_str::<ListenKeyResponse>(&listen_key_response)?;

        // setup websocket
        let (mut user_data_stream_connection, _) = BinanceWebSocketClient::connect_async_default()
            .await
            .expect("Failed to connect");
        user_data_stream_connection
            .subscribe(vec![&UserDataStream::new(&listen_key.listen_key).into()]) // <---  adding listen key here
            .await;