Skip to content

Commit d317ff6

Browse files
feat: add Rust consumer SDK for Pyth Lazer with redundant connections
Co-Authored-By: Tejas Badadare <[email protected]>
1 parent de934f5 commit d317ff6

File tree

6 files changed

+694
-0
lines changed

6 files changed

+694
-0
lines changed

lazer/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
resolver = "2"
33
members = [
44
"sdk/rust/protocol",
5+
"sdk/rust/consumer",
56
"contracts/solana/programs/pyth-lazer-solana-contract",
67
]
78

lazer/sdk/rust/consumer/Cargo.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "pyth-lazer-consumer"
3+
version = "0.1.0"
4+
edition = "2021"
5+
description = "Rust consumer SDK for Pyth Lazer"
6+
license = "Apache-2.0"
7+
8+
[dependencies]
9+
tokio = { version = "1", features = ["rt-multi-thread", "macros", "time", "sync"] }
10+
tokio-tungstenite = { version = "0.20", features = ["native-tls"] }
11+
futures-util = "0.3"
12+
serde = { version = "1.0", features = ["derive"] }
13+
serde_json = "1.0"
14+
anyhow = "1.0"
15+
tracing = "0.1"
16+
http = "0.2"
17+
rand = { version = "0.8", features = ["std"] }
18+
ttl_cache = "0.5"
19+
pyth-lazer-protocol = { path = "../protocol" }
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use {
2+
anyhow::Result,
3+
pyth_lazer_consumer::{Chain, DeliveryFormat, PriceFeedId, PriceFeedProperty, PythLazerConsumer, Response},
4+
tokio,
5+
};
6+
7+
#[tokio::main]
8+
async fn main() -> Result<()> {
9+
let mut consumer = PythLazerConsumer::new(
10+
vec!["wss://endpoint.pyth.network".to_string()],
11+
"your_token_here".to_string(),
12+
)
13+
.await?;
14+
15+
// Connect to the WebSocket server
16+
consumer.connect().await?;
17+
18+
// Subscribe to some price feeds
19+
consumer
20+
.subscribe(
21+
1, // subscription_id
22+
vec![PriceFeedId(1)],
23+
Some(vec![PriceFeedProperty::Price, PriceFeedProperty::Exponent]),
24+
Some(vec![Chain::Evm]),
25+
Some(DeliveryFormat::Json),
26+
)
27+
.await?;
28+
29+
// Receive updates
30+
let mut rx = consumer.subscribe_to_updates();
31+
while let Ok(update) = rx.recv().await {
32+
match update {
33+
Response::StreamUpdated(update) => {
34+
println!("Received update for subscription {}", update.subscription_id.0);
35+
if let Some(parsed) = update.payload.parsed {
36+
for feed in parsed.price_feeds {
37+
println!(" Feed ID: {:?}", feed.price_feed_id);
38+
println!(" Price: {:?}", feed.price);
39+
println!(" Exponent: {:?}", feed.exponent);
40+
}
41+
}
42+
}
43+
_ => {}
44+
}
45+
}
46+
47+
Ok(())
48+
}

0 commit comments

Comments
 (0)