brokapi_bybit_protocol/lib.rs
1//! Bybit API V5 wire protocol: REST and WebSocket clients, serde models, request signing.
2//!
3//! This is the protocol layer for the Bybit API V5 (unified trading account, category `linear`: USDT perpetuals and
4//! futures). It talks to the exchange in the exchange's own words and does nothing else: no order state, no mapping
5//! to common types, no retry of orders. The connector crate `brokapi-bybit` sits on top of it and maps these types to
6//! the broker-independent ones. Use this crate directly when you want the raw API.
7//!
8//! # Parts
9//!
10//! * Module [`config`]: [`BybitConfig`](config::BybitConfig), the key pair, the contour (main, demo or test) and the
11//! base URLs.
12//! * Module [`sign`]: the HMAC-SHA256 signature of a request and of the WebSocket login.
13//! * Module [`http`]: [`BybitHttpClient`](http::client::BybitHttpClient) with one method per REST endpoint, and the
14//! request and response models in [`http::models`].
15//! * Module [`ws`]: [`WsSession`](ws::session::WsSession), a background WebSocket session (public data or the private
16//! streams) that reconnects and restores subscriptions, and the frames in [`ws::models`].
17//! * Module [`error`]: [`BybitApiError`](error::BybitApiError).
18//!
19//! # Authorization in one paragraph
20//!
21//! There are no tokens to renew. Every private request carries the API key, a timestamp, the receive window and the
22//! HMAC-SHA256 signature of `timestamp + key + recv_window + query-or-body` in headers. The client keeps the offset
23//! of the exchange clock (read once, from the server time endpoint) so a clock that runs a little off does not fail
24//! the receive window. The private WebSocket logs in with the same key and an `expires` time.
25//!
26//! # Example
27//!
28//! The example runs against the in-process imitation of the exchange (`brokapi-mocks`), so it needs no network and no
29//! account. With a real account you build `cfg` with [`BybitConfig::from_env`](config::BybitConfig::from_env) or
30//! [`BybitConfig::new`](config::BybitConfig::new).
31//!
32//! ```
33//! # #[tokio::main(flavor = "current_thread")]
34//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
35//! # let server = brokapi_mocks::bybit::MockServer::start(Default::default()).await;
36//! # let cfg = brokapi_bybit_protocol::config::BybitConfig::for_mock("mock-key", "mock-secret", &server.http_base);
37//! use brokapi_bybit_protocol::http::client::BybitHttpClient;
38//!
39//! let client = BybitHttpClient::new(cfg)?;
40//! let book = client.get_order_book("linear", "MOCKUSDT", Some(5)).await?;
41//! assert!(!book.bids.is_empty());
42//! # Ok(())
43//! # }
44//! ```
45
46#![warn(missing_docs)]
47
48pub mod config;
49pub mod error;
50pub mod http;
51pub mod sign;
52pub mod ws;