Skip to content

Commit 80a555a

Browse files
authored
DS - Update Rust SDK imports to use official crates (#2613)
* update Rust SDK imports to use official crates * update Rust SDK imports to use official crates
1 parent cec0a3a commit 80a555a

File tree

5 files changed

+295
-301
lines changed

5 files changed

+295
-301
lines changed

src/content/data-streams/reference/streams-direct/streams-direct-rust-sdk.mdx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ Add the SDK to your project by including it in your `Cargo.toml`:
5353

5454
```toml
5555
[dependencies]
56-
data-streams-sdk = { git = "https://github.com/smartcontractkit/data-streams-sdk.git", subdir = "rust/crates/sdk" }
57-
data-streams-report = { git = "https://github.com/smartcontractkit/data-streams-sdk.git", subdir = "rust/crates/report" }
56+
chainlink-data-streams-sdk = "1.0.0"
57+
chainlink-data-streams-report = "1.0.0"
5858
```
5959

6060
### Feature Flags
@@ -78,7 +78,7 @@ Below are basic code snippets for decoding these reports with the SDK:
7878
### V3 Reports (Crypto Streams)
7979

8080
```rust
81-
use data_streams_report::report::v3::ReportDataV3;
81+
use chainlink_data_streams_report::report::{ decode_full_report, v3::ReportDataV3 };
8282

8383
// After you get 'report_blob' (for example, from 'decode_full_report' or a contract call):
8484
let report_data = ReportDataV3::decode(&report_blob)?;
@@ -96,7 +96,7 @@ For more details on every field in V3 (Crypto) reports, see the [V3 report schem
9696
### V4 Reports (RWA Streams)
9797

9898
```rust
99-
use data_streams_report::report::v4::ReportDataV4;
99+
use chainlink_data_streams_report::report::v4::ReportDataV4;
100100

101101
let report_data = ReportDataV4::decode(&report_blob)?;
102102

@@ -113,7 +113,7 @@ For more details on every field in V4 (RWA) reports, see the [V4 report schema p
113113
The SDK uses HMAC authentication for all API requests. Configure your credentials:
114114

115115
```rust
116-
use data_streams_sdk::config::Config;
116+
use chainlink_data_streams_sdk::config::Config;
117117
use std::env;
118118

119119
let api_key = env::var("API_KEY").expect("API_KEY must be set");
@@ -140,7 +140,7 @@ Security best practices:
140140
### High Availability Mode
141141

142142
```rust
143-
use data_streams_sdk::config::{Config, WebSocketHighAvailability};
143+
use chainlink_data_streams_sdk::config::{Config, WebSocketHighAvailability};
144144

145145
let ws_urls = "wss://ws1.dataengine.chain.link,wss://ws2.dataengine.chain.link";
146146
let config = Config::new(api_key, api_secret, rest_url, ws_urls)
@@ -166,7 +166,7 @@ The SDK allows you to:
166166
Example:
167167

168168
```rust
169-
use data_streams_sdk::config::{Config, InsecureSkipVerify, WebSocketHighAvailability};
169+
use chainlink_data_streams_sdk::config::{Config, InsecureSkipVerify, WebSocketHighAvailability};
170170

171171
let ws_urls = "wss://ws.testnet-dataengine.chain.link";
172172

@@ -192,8 +192,8 @@ The SDK defines distinct error types:
192192
Example:
193193

194194
```rust
195-
use data_streams_sdk::client::Client;
196-
use data_streams_sdk::client::ClientError;
195+
use chainlink_data_streams_sdk::client::Client;
196+
use chainlink_data_streams_sdk::client::ClientError;
197197

198198
match client.get_latest_report(feed_id).await {
199199
Ok(report_response) => {

src/content/data-streams/tutorials/streams-direct/streams-direct-api-rust.mdx

Lines changed: 62 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ In this guide, you'll learn how to use Chainlink Data Streams with the _[Streams
4646

4747
## Requirements
4848

49-
- **Git**: Make sure you have Git installed. You can check your current version by running <CopyText text="git --version" code/> in your terminal and download the latest version from the official [Git website](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) if necessary.
5049
- **Rust**: Make sure you have Rust installed. You can install Rust by following the instructions on the official [Rust website](https://www.rust-lang.org/tools/install).
5150
- **API Credentials**: Access to the Streams Direct implementation requires API credentials. If you haven't already, [contact us](https://chainlinkcommunity.typeform.com/datastreams?typeform-source=docs.chain.link#ref_id=docs) to request mainnet or testnet access.
5251

@@ -68,12 +67,12 @@ You'll start with the set up of your Rust project. Next, you'll fetch and decode
6867
cargo init
6968
```
7069

71-
1. Add the Data Streams SDK to your `Cargo.toml`:
70+
1. Add the following dependencies to your `Cargo.toml` file:
7271

7372
```toml
7473
[dependencies]
75-
data-streams-sdk = { git = "https://github.com/smartcontractkit/data-streams-sdk.git", subdir = "rust/crates/sdk" }
76-
data-streams-report = { git = "https://github.com/smartcontractkit/data-streams-sdk.git", subdir = "rust/crates/report" }
74+
chainlink-data-streams-sdk = "1.0.0"
75+
chainlink-data-streams-report = "1.0.0"
7776
tokio = { version = "1.4", features = ["full"] }
7877
hex = "0.4"
7978
```
@@ -83,65 +82,64 @@ You'll start with the set up of your Rust project. Next, you'll fetch and decode
8382
1. Replace the contents of `src/main.rs` with the following code:
8483

8584
```rust
86-
use data_streams_report::feed_id::ID;
87-
use data_streams_report::report::{decode_full_report, v3::ReportDataV3};
88-
use data_streams_sdk::client::Client;
89-
use data_streams_sdk::config::Config;
85+
use chainlink_data_streams_report::feed_id::ID;
86+
use chainlink_data_streams_report::report::{ decode_full_report, v3::ReportDataV3 };
87+
use chainlink_data_streams_sdk::client::Client;
88+
use chainlink_data_streams_sdk::config::Config;
9089
use std::env;
9190
use std::error::Error;
9291

9392
#[tokio::main]
9493
async fn main() -> Result<(), Box<dyn Error>> {
95-
// Get feed ID from command line arguments
96-
let args: Vec<String> = env::args().collect();
97-
if args.len() < 2 {
98-
eprintln!("Usage: cargo run [FeedID]");
99-
std::process::exit(1);
100-
}
101-
let feed_id_input = &args[1];
102-
103-
// Get API credentials from environment variables
104-
let api_key = env::var("API_KEY").expect("API_KEY must be set");
105-
let api_secret = env::var("API_SECRET").expect("API_SECRET must be set");
106-
107-
// Initialize the configuration
108-
let config = Config::new(
109-
api_key,
110-
api_secret,
111-
"https://api.testnet-dataengine.chain.link".to_string(),
112-
"wss://api.testnet-dataengine.chain.link/ws".to_string(),
113-
)
114-
.build()?;
115-
116-
// Initialize the client
117-
let client = Client::new(config)?;
118-
119-
// Parse the feed ID
120-
let feed_id = ID::from_hex_str(feed_id_input)?;
121-
122-
// Fetch the latest report
123-
let response = client.get_latest_report(feed_id).await?;
124-
println!("\nRaw report data: {:?}\n", response.report);
125-
126-
// Decode the report
127-
let full_report = hex::decode(&response.report.full_report[2..])?;
128-
let (_report_context, report_blob) = decode_full_report(&full_report)?;
129-
let report_data = ReportDataV3::decode(&report_blob)?;
130-
131-
// Print decoded report details
132-
println!("\nDecoded Report for Stream ID {}:", feed_id_input);
133-
println!("------------------------------------------");
134-
println!("Observations Timestamp: {}", response.report.observations_timestamp);
135-
println!("Benchmark Price : {}", report_data.benchmark_price);
136-
println!("Bid : {}", report_data.bid);
137-
println!("Ask : {}", report_data.ask);
138-
println!("Valid From Timestamp : {}", response.report.valid_from_timestamp);
139-
println!("Expires At : {}", report_data.expires_at);
140-
println!("Link Fee : {}", report_data.link_fee);
141-
println!("Native Fee : {}", report_data.native_fee);
142-
println!("------------------------------------------");
143-
144-
Ok(())
94+
// Get feed ID from command line arguments
95+
let args: Vec<String> = env::args().collect();
96+
if args.len() < 2 {
97+
eprintln!("Usage: cargo run [FeedID]");
98+
std::process::exit(1);
99+
}
100+
let feed_id_input = &args[1];
101+
102+
// Get API credentials from environment variables
103+
let api_key = env::var("API_KEY").expect("API_KEY must be set");
104+
let api_secret = env::var("API_SECRET").expect("API_SECRET must be set");
105+
106+
// Initialize the configuration
107+
let config = Config::new(
108+
api_key,
109+
api_secret,
110+
"https://api.testnet-dataengine.chain.link".to_string(),
111+
"wss://api.testnet-dataengine.chain.link/ws".to_string()
112+
).build()?;
113+
114+
// Initialize the client
115+
let client = Client::new(config)?;
116+
117+
// Parse the feed ID
118+
let feed_id = ID::from_hex_str(feed_id_input)?;
119+
120+
// Fetch the latest report
121+
let response = client.get_latest_report(feed_id).await?;
122+
println!("\nRaw report data: {:?}\n", response.report);
123+
124+
// Decode the report
125+
let full_report = hex::decode(&response.report.full_report[2..])?;
126+
let (_report_context, report_blob) = decode_full_report(&full_report)?;
127+
let report_data = ReportDataV3::decode(&report_blob)?;
128+
129+
// Print decoded report details
130+
println!("\nDecoded Report for Stream ID {}:", feed_id_input);
131+
println!("------------------------------------------");
132+
println!("Observations Timestamp: {}", response.report.observations_timestamp);
133+
println!("Benchmark Price : {}", report_data.benchmark_price);
134+
println!("Bid : {}", report_data.bid);
135+
println!("Ask : {}", report_data.ask);
136+
println!("Valid From Timestamp : {}", response.report.valid_from_timestamp);
137+
println!("Expires At : {}", report_data.expires_at);
138+
println!("Link Fee : {}", report_data.link_fee);
139+
println!("Native Fee : {}", report_data.native_fee);
140+
println!("------------------------------------------");
141+
142+
Ok(())
145143
}
146144
```
147145

@@ -215,7 +213,7 @@ The API client is initialized in two steps:
215213
- Sets up the REST API endpoint for data retrieval
216214
- Configures optional settings like TLS verification
217215

218-
2. [`Client::new`](https://github.com/smartcontractkit/data-streams-sdk/blob/main/rust/crates/sdk/src/client.rs#L131) creates the HTTP client with your configuration. This client:
216+
1. [`Client::new`](https://github.com/smartcontractkit/data-streams-sdk/blob/main/rust/crates/sdk/src/client.rs#L131) creates the HTTP client with your configuration. This client:
219217
- Handles authentication automatically
220218
- Manages HTTP connections
221219
- Implements retry logic for failed requests
@@ -230,7 +228,7 @@ The SDK provides several methods to fetch reports through the REST API:
230228
- Returns a single report with the latest timestamp
231229
- Useful for applications that need the most current data
232230

233-
2. Historical report: [`get_report`](https://github.com/smartcontractkit/data-streams-sdk/blob/main/rust/crates/sdk/src/client.rs#L242) fetches a report at a specific timestamp:
231+
1. Historical report: [`get_report`](https://github.com/smartcontractkit/data-streams-sdk/blob/main/rust/crates/sdk/src/client.rs#L242) fetches a report at a specific timestamp:
234232
- Takes both feed ID and timestamp
235233
- Returns the report closest to the requested timestamp
236234
- Helpful for historical analysis or verification
@@ -252,13 +250,13 @@ Reports are decoded in three stages:
252250
let full_report = hex::decode(&response.report.full_report[2..])?;
253251
```
254252

255-
2. Report separation: [`decode_full_report`](https://github.com/smartcontractkit/data-streams-sdk/blob/main/rust/crates/report/src/report.rs#L77) splits the binary data:
253+
1. Report separation: [`decode_full_report`](https://github.com/smartcontractkit/data-streams-sdk/blob/main/rust/crates/report/src/report.rs#L77) splits the binary data:
256254

257255
- Extracts the report context (metadata)
258256
- Isolates the report blob (actual data)
259257
- Validates the report format
260258

261-
3. Data extraction: [`ReportDataV3::decode`](https://github.com/smartcontractkit/data-streams-sdk/blob/main/rust/crates/report/src/report/v3.rs#L80) parses the report blob into structured data:
259+
1. Data extraction: [`ReportDataV3::decode`](https://github.com/smartcontractkit/data-streams-sdk/blob/main/rust/crates/report/src/report/v3.rs#L80) parses the report blob into structured data:
262260
- Benchmark price (18 decimal places)
263261
- Bid and ask prices for [liquidity-weighted pricing](/data-streams/concepts/liquidity-weighted-prices)
264262
- Fee information for onchain verification
@@ -274,13 +272,13 @@ The example demonstrates Rust's robust error handling:
274272
- Implements the `Error` trait for proper error propagation
275273
- Provides detailed error messages for debugging
276274

277-
2. Error propagation:
275+
1. Error propagation:
278276

279277
- Uses the `?` operator for clean error handling
280278
- Converts errors between types when needed
281279
- Bubbles up errors to the main function
282280

283-
3. Input validation:
281+
1. Input validation:
284282
- Checks command-line arguments
285283
- Validates environment variables
286284
- Verifies feed ID format

0 commit comments

Comments
 (0)