| Crates.io | pyth-benchmark-rs |
| lib.rs | pyth-benchmark-rs |
| version | 0.1.0 |
| created_at | 2025-06-25 21:22:57.782857+00 |
| updated_at | 2025-06-25 21:22:57.782857+00 |
| description | A Rust client for streaming and processing historical OHLC price data from the Pyth Network oracle |
| homepage | |
| repository | https://github.com/devgreek/pyth-benchmark-rs |
| max_upload_size | |
| id | 1726450 |
| size | 81,039 |
A Rust client for streaming and processing price data from the Pyth Network oracle. This crate provides a robust interface for accessing real-time price data and converting it into OHLC (Open, High, Low, Close) bar data.
Add this to your Cargo.toml:
[dependencies]
pyth-benchmark-rs = "0.1.0"
tokio = { version = "1", features = ["full"] }
use pyth_benchmark_rs::{DataFeed, SymbolInfo, PeriodParams, start_streaming};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use tokio::sync::mpsc;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Initialize subscriptions registry
let subscriptions = Arc::new(Mutex::new(HashMap::new()));
// Create DataFeed instance
let datafeed = DataFeed::new(subscriptions.clone());
// Initialize the data feed
datafeed.on_ready(|config| {
println!("DataFeed ready: {:?}", config);
}).await;
// Start streaming in background
let subs_clone = subscriptions.clone();
tokio::spawn(async move {
let _ = start_streaming(subs_clone).await;
});
// Get historical data
let symbol = SymbolInfo::new("Crypto.BTC/USD");
let period = PeriodParams::last_days(30, true);
datafeed.get_bars(
&symbol,
"1D",
&period,
|bars, no_data| {
if !no_data {
println!("Received {} historical bars", bars.len());
for bar in bars.iter().take(3) {
println!("Bar: O:{:.2} H:{:.2} L:{:.2} C:{:.2}",
bar.open, bar.high, bar.low, bar.close);
}
}
},
|err| eprintln!("Error: {}", err)
).await;
Ok(())
}
Subscribe to real-time price updates:
use tokio::sync::mpsc;
// Create a channel for receiving updates
let (tx, mut rx) = mpsc::unbounded_channel();
// Subscribe to BTC/USD daily bars
let symbol = SymbolInfo::new("Crypto.BTC/USD");
datafeed.subscribe_bars(
symbol,
"1D".to_string(),
tx,
"my_subscription".to_string(),
None
).await;
// Listen for updates
tokio::spawn(async move {
while let Some(bar) = rx.recv().await {
println!("New bar: O:{:.2} H:{:.2} L:{:.2} C:{:.2} T:{}",
bar.open, bar.high, bar.low, bar.close, bar.time);
}
});
DataFeed: Main client for interacting with Pyth NetworkBar: OHLC price data structureSymbolInfo: Symbol information (ticker)PeriodParams: Time period parameters for historical dataon_ready(): Initialize the data feedget_bars(): Fetch historical OHLC datasubscribe_bars(): Subscribe to real-time updatessearch_symbols(): Search for available symbolsresolve_symbol(): Get detailed symbol informationstart_streaming(): Start the main streaming connectionsubscribe_on_stream(): Add a symbol subscriptionunsubscribe_from_stream(): Remove a subscriptionThis repository is specifically designed to read OHLC (Open, High, Low, Close) data from the Pyth Network oracle. Pyth Network is a decentralized oracle that provides real-time market data for various financial instruments, particularly in the cryptocurrency space.
The crate uses these default endpoints:
https://benchmarks.pyth.network/v1/shims/tradingview/streaminghttps://benchmarks.pyth.network/v1/shims/tradingviewBuild the library:
cargo build
Run the example:
cargo run --bin example
Run tests:
cargo test
The src/main.rs file contains a comprehensive example showing:
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under either of
at your option.