17 releases
new 0.2.7 | Apr 18, 2025 |
---|---|
0.2.6 | Apr 17, 2025 |
0.1.8 | Apr 10, 2025 |
#127 in WebSocket
1,371 downloads per month
35KB
544 lines
pragma-rs π¦
pragma-rs is a Rust SDK for the Pragma API, making it easy to fetch offchain and onchain data or get real-time updates via WebSocket.
Warning
This crate is still in progress and not ready for production
π¦ Installation
Add pragma-rs
to your Rust project by tossing this into your Cargo.toml
:
[dependencies]
pragma-rs = "0.2.6"
Available features:
sync
: sync version of http calls,bigdecimal
: returns prices asBigDecimal
.
π Quick Start
1. Set Up Your Config
Kick things off by setting up your API key and environment:
use pragma_rs::{Config, Environment};
let api_key = "MY_API_KEY".to_string();
let config = Config::new(api_key, Environment::Development);
π Environments:
Development
: Perfect for testing and tinkering.Production
: not available yet
2. Create a Client
Spin up a PragmaClient with your config:
let client = PragmaClient::new(config).unwrap();
3. Fetch data using http
use pragma_rs::{AggregationMode, GetEntryParams, Interval, PragmaClient};
#[tokio::main]
async fn main() {
let api_key = "MY_API_KEY".to_string();
let config = Config::new(api_key, Environment::Development);
let client = PragmaClient::new(config).unwrap();
let r = client.get_entry("BTC", "USD", None).await.unwrap();
println!("BTC/USD data:\n{r:?}");
// Or with options
let r = client
.get_entry(
"BTC",
"USD",
Some(GetEntryParams {
timestamp: None,
interval: Some(Interval::OneMinute),
routing: Some(false),
aggregation: Some(AggregationMode::Median),
entry_type: None,
with_components: Some(true),
}),
)
.await
.unwrap();
println!("BTC/USD detailed data:\n{r:?}");
}
4. Or using the real-time websocket
use std::time::Duration;
use pragma_rs::{Config, Environment, LightspeedMessage, PragmaClient};
#[tokio::main]
async fn main() {
let api_key = "MY_API_KEY".to_string();
let config = Config::new(api_key, Environment::Development);
let client = PragmaClient::new(config).unwrap();
let mut ws_client = client.lightspeed_ws_client();
ws_client.connect().await.unwrap();
ws_client
.send(LightspeedMessage::Subscribe {
msg_type: "subscribe".into(),
pairs: vec!["BTC/USD".to_string(), "ETH/USD".to_string()],
})
.await
.unwrap();
tokio::spawn(async move {
while let Some(msg) = ws_client.next_message().await {
match msg {
LightspeedMessage::PriceUpdate {
oracle_prices,
timestamp,
} => {
println!("[{timestamp}] {oracle_prices:?}");
}
LightspeedMessage::Subscribe { msg_type, pairs } => {
println!("{msg_type} to {pairs:?}");
}
_ => {}
}
}
});
// Hang out for 20 seconds to see the updates
tokio::time::sleep(Duration::from_secs(20)).await;
}
π οΈ Contribute
Weβre still building this crate, and weβd love your help! Hereβs how you can chip in:
- π Found a bug? Open an issue.
- π‘ Got an idea? Suggest a feature.
- π§ Want to code? Send us a pull request!
π License
Licensed under the MIT License - see here for details.
Dependencies
~25β42MB
~695K SLoC