pyth_benchmark_rs/
lib.rs

1//! # Pyth Benchmark Rust Client
2//!
3//! A Rust client for streaming and processing price data from the Pyth Network oracle.
4//! This crate provides a robust interface for accessing real-time price data and
5//! converting it into OHLC (Open, High, Low, Close) bar data.
6//!
7//! ## Features
8//!
9//! - Real-time price streaming from Pyth Network
10//! - OHLC bar generation from price feeds
11//! - Daily bar aggregation
12//! - Subscription-based architecture
13//! - Automatic reconnection with exponential backoff
14//! - Thread-safe subscription management
15//!
16//! ## Quick Start
17//!
18//! ```rust,no_run
19//! use pyth_benchmark_rs::{DataFeed, SymbolInfo, PeriodParams, start_streaming};
20//! use std::sync::{Arc, Mutex};
21//! use std::collections::HashMap;
22//! use tokio::sync::mpsc;
23//!
24//! #[tokio::main]
25//! async fn main() -> anyhow::Result<()> {
26//!     // Initialize subscriptions
27//!     let subscriptions = Arc::new(Mutex::new(HashMap::new()));
28//!     
29//!     // Create DataFeed instance
30//!     let datafeed = DataFeed::new(subscriptions.clone());
31//!     
32//!     // Start streaming in background
33//!     let subs_clone = subscriptions.clone();
34//!     tokio::spawn(async move {
35//!         let _ = start_streaming(subs_clone).await;
36//!     });
37//!     
38//!     // Use the datafeed...
39//!     Ok(())
40//! }
41//! ```
42
43pub mod client;
44pub mod streaming;
45pub mod types;
46
47pub use client::DataFeed;
48pub use streaming::{start_streaming, subscribe_on_stream, unsubscribe_from_stream};
49pub use types::{Bar, Handler, Subscription, Subscriptions, SymbolInfo, PeriodParams};
50
51/// The default Pyth Network streaming URL
52pub const STREAMING_URL: &str = "https://benchmarks.pyth.network/v1/shims/tradingview/streaming";
53
54/// The default Pyth Network API endpoint
55pub const API_ENDPOINT: &str = "https://benchmarks.pyth.network/v1/shims/tradingview";
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60    use std::collections::HashMap;
61    use std::sync::{Arc, Mutex};
62
63    #[test]
64    fn test_bar_creation() {
65        let bar = Bar {
66            time: 1_717_000_000,
67            open: 100.0,
68            high: 105.0,
69            low: 95.0,
70            close: 102.0,
71        };
72        
73        assert_eq!(bar.time, 1_717_000_000);
74        assert_eq!(bar.open, 100.0);
75        assert_eq!(bar.high, 105.0);
76        assert_eq!(bar.low, 95.0);
77        assert_eq!(bar.close, 102.0);
78    }
79
80    #[test]
81    fn test_symbol_info_creation() {
82        let symbol = SymbolInfo {
83            ticker: "BTC/USD".to_string(),
84        };
85        
86        assert_eq!(symbol.ticker, "BTC/USD");
87    }
88
89    #[test]
90    fn test_datafeed_creation() {
91        let subscriptions = Arc::new(Mutex::new(HashMap::new()));
92        let datafeed = DataFeed::new(subscriptions);
93        
94        // Basic test to ensure DataFeed can be created
95        assert!(format!("{:?}", datafeed.client).len() > 0);
96    }
97}