Skip to content

Commit 31a0959

Browse files
feat: add example for pyth-lazer-sdk
Co-Authored-By: Tejas Badadare <[email protected]>
1 parent aab0230 commit 31a0959

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use pyth_lazer_sdk::LazerClient;
2+
use pyth_lazer_protocol::subscription::{Request, SubscribeRequest, SubscriptionId, SubscriptionParams};
3+
use futures_util::StreamExt;
4+
5+
#[tokio::main]
6+
async fn main() -> anyhow::Result<()> {
7+
// Connect to the Pyth Lazer WebSocket endpoint
8+
let (mut client, mut stream) = LazerClient::start("wss://hermes.pyth.network").await?;
9+
10+
// Subscribe to BTC/USD price feed
11+
let btc_feed_id = "ff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace";
12+
13+
client.subscribe(Request::Subscribe(SubscribeRequest {
14+
subscription_id: SubscriptionId(1),
15+
params: SubscriptionParams {
16+
ids: vec![btc_feed_id.to_string()],
17+
binary_message_format: true,
18+
},
19+
})).await?;
20+
21+
println!("Subscribed to BTC/USD price feed. Waiting for updates...");
22+
23+
// Process the first 5 updates
24+
let mut count = 0;
25+
while let Some(msg) = stream.next().await {
26+
println!("Received update: {:?}", msg?);
27+
count += 1;
28+
if count >= 5 {
29+
break;
30+
}
31+
}
32+
33+
// Unsubscribe before exiting
34+
client.unsubscribe(SubscriptionId(1)).await?;
35+
println!("Unsubscribed from feed");
36+
37+
Ok(())
38+
}

0 commit comments

Comments
 (0)