Skip to main content

basic_stream/
basic_stream.rs

1//! Minimal example: subscribe to the USDC mint and print updates.
2//!
3//! Run with (PowerShell):
4//!     $env:HELIUS_API_KEY="your_key"; cargo run --example basic_stream
5//! Or (cmd.exe / bash):
6//!     HELIUS_API_KEY=your_key cargo run --example basic_stream
7
8use helius_stream::{HeliusStream, StreamConfig};
9
10const USDC_MINT: &str = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";
11
12fn main() -> Result<(), Box<dyn std::error::Error>> {
13    // No logger init — keeps example dependency-free.
14    // The crate uses `log` facade; to see internal logs, add env_logger
15    // (or any log backend) to your binary's Cargo.toml and init here.
16
17    let api_key = std::env::var("HELIUS_API_KEY")
18        .map_err(|_| "set HELIUS_API_KEY in environment")?;
19
20    let config = StreamConfig::mainnet(api_key);
21    let mut stream = HeliusStream::connect(config)?;
22
23    println!("[INIT] subscribing to USDC mint");
24    stream.subscribe_account_b58(USDC_MINT)?;
25
26    let mut received = 0;
27    while let Some(update) = stream.next_update() {
28        received += 1;
29        println!(
30            "[UPDATE] slot={} lamports={} data_bytes={} safe={} gap_rate={:.3}",
31            update.slot,
32            update.lamports,
33            update.data.len(),
34            stream.is_safe_for_simulation(),
35            stream.health().gap_rate(),
36        );
37        if received >= 10 {
38            println!("[DONE] received 10 updates, exiting");
39            break;
40        }
41    }
42    Ok(())
43}