Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lazer/sdk/rust): add rust client for Lazer #2310

Merged
merged 20 commits into from
Jan 31, 2025
Merged
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
bc150e4
feat: add rust consumer sdk for lazer
devin-ai-integration[bot] Jan 30, 2025
f0ce60d
feat: rename crate to pyth-lazer-sdk and client to LazerClient
devin-ai-integration[bot] Jan 30, 2025
f97880e
fix: update base64 encoding to use Engine::encode
devin-ai-integration[bot] Jan 30, 2025
aab0230
fix: add base64 Engine trait import
devin-ai-integration[bot] Jan 30, 2025
31a0959
feat: add example for pyth-lazer-sdk
devin-ai-integration[bot] Jan 30, 2025
0692f33
fix: fix example
tejasbadadare Jan 30, 2025
c31d118
refactor: separate client new() and start() functions, improve unsubs…
devin-ai-integration[bot] Jan 30, 2025
d3e8844
refactor: use Bearer token authentication in header and improve unsub…
devin-ai-integration[bot] Jan 30, 2025
9474cfc
refactor: make access token required parameter
devin-ai-integration[bot] Jan 30, 2025
9964432
fix: remove invalid client return from start function
devin-ai-integration[bot] Jan 30, 2025
2cd41a3
style: improve code formatting and fix example documentation
devin-ai-integration[bot] Jan 30, 2025
c15f283
fix: formatting, example
tejasbadadare Jan 30, 2025
4160bc8
Merge branch 'devin/1738263054-add-lazer-rust-consumer-sdk' of github…
tejasbadadare Jan 30, 2025
c9f700b
refactor: rename, remove doctest
tejasbadadare Jan 30, 2025
f71d95a
fix: example
tejasbadadare Jan 30, 2025
c3d11c3
feat: add close()
tejasbadadare Jan 30, 2025
5a3b3c9
refactor: move binary msg parsing to protocol crate, improve examples…
tejasbadadare Jan 31, 2025
d8fba66
feat: bump protocol ver
tejasbadadare Jan 31, 2025
a8472d0
fix: don't ser/de the binary message, leave that to the client. only …
tejasbadadare Jan 31, 2025
19ffbdd
fix: explicitly choose JsonBinaryEncoding::Base64 instead of default()
tejasbadadare Jan 31, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions lazer/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lazer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
resolver = "2"
members = [
"sdk/rust/protocol",
"sdk/rust/client",
"contracts/solana/programs/pyth-lazer-solana-contract",
]

18 changes: 18 additions & 0 deletions lazer/sdk/rust/client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "pyth-lazer-client"
version = "0.1.0"
edition = "2021"
description = "A Rust client for Pyth Lazer"
license = "Apache-2.0"

[dependencies]
pyth-lazer-protocol = { path = "../protocol" }
tokio = { version = "1", features = ["full"] }
tokio-tungstenite = { version = "0.20", features = ["native-tls"] }
futures-util = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
base64 = "0.21"
anyhow = "1.0"
tracing = "0.1"
url = "2.4"
60 changes: 60 additions & 0 deletions lazer/sdk/rust/client/examples/subscribe_price_feeds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use futures_util::StreamExt;
use pyth_lazer_client::LazerClient;
use pyth_lazer_protocol::router::{
Chain, Channel, DeliveryFormat, FixedRate, JsonBinaryEncoding, PriceFeedId, PriceFeedProperty,
SubscriptionParams, SubscriptionParamsRepr,
};
use pyth_lazer_protocol::subscription::{Request, SubscribeRequest, SubscriptionId};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Create and start the client
let mut client = LazerClient::new(
"wss://pyth-lazer.dourolabs.app/v1/stream",
"YOUR_ACCESS_TOKEN",
)?;
let mut stream = client.start().await?;

// Create subscription request
let subscription_request = SubscribeRequest {
subscription_id: SubscriptionId(1),
params: SubscriptionParams::new(SubscriptionParamsRepr {
price_feed_ids: vec![PriceFeedId(1), PriceFeedId(2), PriceFeedId(3)],
properties: vec![PriceFeedProperty::Price],
chains: vec![Chain::Solana],
delivery_format: DeliveryFormat::Binary,
json_binary_encoding: JsonBinaryEncoding::default(),
parsed: false,
channel: Channel::FixedRate(FixedRate::from_ms(200).expect("unsupported update rate")),
})
.expect("invalid subscription params"),
};

client
.subscribe(Request::Subscribe(subscription_request))
.await?;

println!("Subscribed to BTC/USD price feed. Waiting for updates...");

// Process the first 100 updates
let mut count = 0;
while let Some(msg) = stream.next().await {
println!("Received update: {:?}", msg?);
count += 1;
if count >= 100 {
break;
}
}

// Unsubscribe from all feeds before exiting
for feed_id in 1..=3 {
client.unsubscribe(SubscriptionId(feed_id)).await?;
println!("Unsubscribed from feed {}", feed_id);
// Add a small delay between unsubscribe requests
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
}

// Wait a moment to ensure unsubscribe messages are sent
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
Ok(())
}
Loading