Crates.io | brk_fetcher |
lib.rs | brk_fetcher |
version | 0.0.95 |
created_at | 2025-02-23 23:31:20.578563+00 |
updated_at | 2025-08-28 10:45:17.8872+00 |
description | A Bitcoin price fetcher |
homepage | https://bitcoinresearchkit.org |
repository | https://github.com/bitcoinresearchkit/brk |
max_upload_size | |
id | 1566726 |
size | 68,779 |
Bitcoin price data fetcher with multi-source fallback and retry logic
brk_fetcher
provides reliable Bitcoin price data retrieval from multiple sources including Binance, Kraken, and BRK instances. It offers both date-based and block height-based price queries with automatic fallback and retry mechanisms for robust data collection.
use brk_fetcher::Fetcher;
use brk_structs::{Date, Height, Timestamp};
// Initialize fetcher with exchange APIs enabled
let mut fetcher = Fetcher::import(true, None)?;
// Initialize without exchange APIs (BRK-only mode)
let mut fetcher = Fetcher::import(false, None)?;
// Initialize with HAR file for historical Binance data
let har_path = Path::new("./binance.har");
let mut fetcher = Fetcher::import(true, Some(har_path))?;
use brk_structs::Date;
// Fetch OHLC data for a specific date
let date = Date::new(2024, 12, 25);
let ohlc = fetcher.get_date(date)?;
println!("Bitcoin price on {}: ${:.2}", date, ohlc.close.dollars());
println!("Daily high: ${:.2}", ohlc.high.dollars());
println!("Daily low: ${:.2}", ohlc.low.dollars());
use brk_structs::{Height, Timestamp};
// Fetch price at specific block height
let height = Height::new(900_000);
let timestamp = Timestamp::from_block_height(height);
let previous_timestamp = Some(Timestamp::from_block_height(Height::new(899_999)));
let ohlc = fetcher.get_height(height, timestamp, previous_timestamp)?;
println!("Bitcoin price at block {}: ${:.2}", height, ohlc.close.dollars());
use brk_structs::OHLCCents;
// OHLC data is returned in cents for precision
let ohlc: OHLCCents = fetcher.get_date(date)?;
// Convert to dollars for display
println!("Open: ${:.2}", ohlc.open.dollars());
println!("High: ${:.2}", ohlc.high.dollars());
println!("Low: ${:.2}", ohlc.low.dollars());
println!("Close: ${:.2}", ohlc.close.dollars());
// Access raw cent values
println!("Close in cents: {}", ohlc.close.0);
use brk_fetcher::{Binance, Kraken, BRK};
// Use specific exchanges directly
let binance = Binance::init(None);
let kraken = Kraken::default();
let brk = BRK::default();
// Fetch from specific source
let binance_data = binance.get_from_1d(&date)?;
let kraken_data = kraken.get_from_1mn(timestamp, previous_timestamp)?;
let brk_data = brk.get_from_height(height)?;
// The fetcher automatically retries on failures
match fetcher.get_date(date) {
Ok(ohlc) => println!("Successfully fetched: ${:.2}", ohlc.close.dollars()),
Err(e) => {
// After all retries and sources exhausted
eprintln!("Failed to fetch price data: {}", e);
}
}
// Clear cache to force fresh data
fetcher.clear();
For historical data beyond API limits:
The fetcher tries sources in this order:
If all sources fail, it retries up to 12 hours with 60-second intervals.
brk_structs
- Bitcoin-aware type system (Date, Height, OHLC types)brk_error
- Unified error handlingminreq
- HTTP client for API requestsserde_json
- JSON parsing for API responseslog
- Logging for retry and error reportingThis README was generated by Claude Code