price_adapter_raw/
lib.rs

1//! # price-explorer
2//! price-explorer is a price query adapter for crypto exchanges and price aggregators.
3//!
4//! It provides a unified interface for querying price data from different exchanges and
5//! price aggregators. Currently, it supports the following exchanges and price aggregators:
6//! - Binance
7//! - CoinGecko
8
9//! # Usage
10//!
11//! ## Coingecko
12//! To use coingecko api, you need to create a `CoingeckoPro` instance and set the api key.
13//! ```rust
14//! use price_adapter_raw::CoinGecko;
15//!
16//! #[tokio::main]
17//! async fn main() {
18//!     let coingecko = CoinGecko::new_with_api_key("$API_KEY".into());
19//!     let queries = vec!["ethereum"];
20//!     let prices = coingecko.get_prices(&queries).await;
21//!     println!("prices: {:?}", prices);
22//! }
23//! ````
24//!
25//! ## Binance Websocket
26//! To use binance websocket api, you need to create a `BinanceWebsocket` instance and set the
27//! query symbols.
28//! ```rust
29//! use price_adapter_raw::BinanceWebsocket;
30//! use futures_util::StreamExt;
31//!
32//! #[tokio::main]
33//! async fn main() {
34//!     let mut binance_ws = BinanceWebsocket::new("wss://stream.binance.com:9443", &["ethbtc", "btcusdt"]);
35//!     binance_ws.connect().await.unwrap();
36//!     while let Some(data) = binance_ws.next().await {
37//!         match data {
38//!             Ok(price) => {
39//!                 println!("price: {}", price);
40//!                 # break;
41//!             }
42//!             Err(e) => {
43//!                 eprintln!("Error: {}", e);
44//!                 break;
45//!             }
46//!         }
47//!     }
48//! }
49//! ```
50//!
51//! Or use `BinanceWebsocketService` to query price data.
52//! ```rust
53//! use price_adapter_raw::{BinanceWebsocket, BinanceWebsocketService};
54//! use std::time::Duration;
55//!
56//! #[tokio::main]
57//! async fn main() {
58//!     let mut binance_ws = BinanceWebsocket::new("wss://stream.binance.com:9443", &["ethbtc", "btcusdt"]);
59//!
60//!     let mut service = BinanceWebsocketService::new(binance_ws);
61//!     service.start().await.unwrap();
62//!     tokio::time::sleep(Duration::from_secs(1)).await;
63//!
64//!     let price = service.get_prices(&["btcusdt"]).await;
65//!     println!("price: {:?}", price);
66//! }
67//! ```
68
69mod binance_websocket;
70mod coingecko;
71pub mod error;
72pub mod types;
73
74pub use binance_websocket::{BinanceWebsocket, BinanceWebsocketService};
75pub use coingecko::CoinGecko;