Skip to content

Commit 37c1126

Browse files
authored
DS-899 Implement V11 support for Rust. (#49)
2 parents 0a6df7b + 23f2a37 commit 37c1126

File tree

9 files changed

+306
-23
lines changed

9 files changed

+306
-23
lines changed

rust/Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ Add the following to your `Cargo.toml`:
2020

2121
```toml
2222
[dependencies]
23-
chainlink-data-streams-report = "1.1.0"
24-
chainlink-data-streams-sdk = { version = "1.1.0", features = ["full"] }
23+
chainlink-data-streams-report = "1.2.0"
24+
chainlink-data-streams-sdk = { version = "1.2.0", features = ["full"] }
2525
```
2626

2727
#### Features

rust/crates/report/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "chainlink-data-streams-report"
3-
version = "1.1.0"
3+
version = "1.2.0"
44
edition = "2021"
55
description = "Chainlink Data Streams Report"
66
license = "MIT"

rust/crates/report/src/report.rs

Lines changed: 92 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
pub mod base;
22
pub mod compress;
33
pub mod v1;
4+
pub mod v10;
5+
pub mod v11;
6+
pub mod v13;
47
pub mod v2;
58
pub mod v3;
69
pub mod v4;
@@ -9,8 +12,6 @@ pub mod v6;
912
pub mod v7;
1013
pub mod v8;
1114
pub mod v9;
12-
pub mod v10;
13-
pub mod v13;
1415

1516
use base::{ReportBase, ReportError};
1617

@@ -124,8 +125,12 @@ pub fn decode_full_report(payload: &[u8]) -> Result<(Vec<[u8; 32]>, Vec<u8>), Re
124125
#[cfg(test)]
125126
mod tests {
126127
use super::*;
127-
use crate::report::{v1::ReportDataV1, v2::ReportDataV2, v3::ReportDataV3, v4::ReportDataV4, v5::ReportDataV5, v6::ReportDataV6, v7::ReportDataV7, v8::ReportDataV8, v9::ReportDataV9, v10::ReportDataV10, v13::ReportDataV13};
128-
use num_bigint::{BigInt};
128+
use crate::report::{
129+
v1::ReportDataV1, v10::ReportDataV10, v11::ReportDataV11, v13::ReportDataV13,
130+
v2::ReportDataV2, v3::ReportDataV3, v4::ReportDataV4, v5::ReportDataV5, v6::ReportDataV6,
131+
v7::ReportDataV7, v8::ReportDataV8, v9::ReportDataV9,
132+
};
133+
use num_bigint::BigInt;
129134

130135
const V1_FEED_ID: ID = ID([
131136
0, 1, 107, 74, 167, 229, 124, 167, 182, 138, 225, 191, 69, 101, 63, 86, 182, 86, 253, 58,
@@ -167,20 +172,29 @@ mod tests {
167172
00, 10, 107, 74, 167, 229, 124, 167, 182, 138, 225, 191, 69, 101, 63, 86, 182, 86, 253, 58,
168173
163, 53, 239, 127, 174, 105, 107, 102, 63, 27, 132, 114,
169174
]);
175+
const V11_FEED_ID: ID = ID([
176+
00, 11, 251, 109, 19, 88, 151, 228, 170, 245, 101, 123, 255, 211, 176, 180, 143, 142, 42,
177+
81, 49, 33, 76, 158, 194, 214, 46, 172, 93, 83, 32, 103,
178+
]);
170179
const V13_FEED_ID: ID = ID([
171180
00, 13, 19, 169, 185, 197, 227, 122, 9, 159, 55, 78, 146, 195, 121, 20, 175, 92, 38, 143,
172181
58, 138, 151, 33, 241, 114, 81, 53, 191, 180, 203, 184,
173182
]);
174183

175184
pub const MOCK_TIMESTAMP: u32 = 1718885772;
185+
pub const MOCK_LAST_SEEN_TIMESTAMP_NS: u64 = 1718885772000000000;
176186
pub const MOCK_FEE: usize = 10;
177187
pub const MOCK_PRICE: isize = 100;
178188
pub const MARKET_STATUS_OPEN: u32 = 2;
179-
pub const MOCK_BEST_ASK: isize = 227;
180-
pub const MOCK_BEST_BID: isize = 229;
189+
pub const MOCK_ASK: isize = 229;
190+
pub const MOCK_BEST_ASK: isize = 229;
191+
pub const MOCK_BID: isize = 227;
192+
pub const MOCK_BEST_BID: isize = 227;
181193
pub const MOCK_ASK_VOLUME: u64 = 1500;
182194
pub const MOCK_BID_VOLUME: u64 = 1200;
183195
pub const MOCK_LAST_TRADED_PRICE: isize = 228;
196+
pub const MOCK_MID: isize = 228;
197+
pub const MOCK_MARKET_STATUS: u32 = 2;
184198

185199
pub fn generate_mock_report_data_v1() -> ReportDataV1 {
186200
let report_data = ReportDataV1 {
@@ -357,6 +371,31 @@ mod tests {
357371
report_data
358372
}
359373

374+
pub fn generate_mock_report_data_v11() -> ReportDataV11 {
375+
let multiplier: BigInt = "1000000000000000000".parse::<BigInt>().unwrap(); // 1.0 with 18 decimals
376+
377+
let report_data = ReportDataV11 {
378+
feed_id: V11_FEED_ID,
379+
valid_from_timestamp: MOCK_TIMESTAMP,
380+
observations_timestamp: MOCK_TIMESTAMP,
381+
native_fee: BigInt::from(MOCK_FEE),
382+
link_fee: BigInt::from(MOCK_FEE),
383+
expires_at: MOCK_TIMESTAMP + 100,
384+
mid: BigInt::from(MOCK_MID).checked_mul(&multiplier).unwrap(),
385+
last_seen_timestamp_ns: MOCK_LAST_SEEN_TIMESTAMP_NS,
386+
bid: BigInt::from(MOCK_BID).checked_mul(&multiplier).unwrap(),
387+
bid_volume: MOCK_BID_VOLUME,
388+
ask: BigInt::from(MOCK_ASK).checked_mul(&multiplier).unwrap(),
389+
ask_volume: MOCK_ASK_VOLUME,
390+
last_traded_price: BigInt::from(MOCK_LAST_TRADED_PRICE)
391+
.checked_mul(&multiplier)
392+
.unwrap(),
393+
market_status: MOCK_MARKET_STATUS,
394+
};
395+
396+
report_data
397+
}
398+
360399
pub fn generate_mock_report_data_v13() -> ReportDataV13 {
361400
let multiplier: BigInt = "1000000000000000000".parse::<BigInt>().unwrap(); // 1.0 with 18 decimals
362401

@@ -367,11 +406,17 @@ mod tests {
367406
native_fee: BigInt::from(MOCK_FEE),
368407
link_fee: BigInt::from(MOCK_FEE),
369408
expires_at: MOCK_TIMESTAMP + 100,
370-
best_ask: BigInt::from(MOCK_BEST_ASK).checked_mul(&multiplier).unwrap(),
371-
best_bid: BigInt::from(MOCK_BEST_BID).checked_mul(&multiplier).unwrap(),
409+
best_ask: BigInt::from(MOCK_BEST_ASK)
410+
.checked_mul(&multiplier)
411+
.unwrap(),
412+
best_bid: BigInt::from(MOCK_BEST_BID)
413+
.checked_mul(&multiplier)
414+
.unwrap(),
372415
ask_volume: MOCK_ASK_VOLUME,
373416
bid_volume: MOCK_BID_VOLUME,
374-
last_traded_price: BigInt::from(MOCK_LAST_TRADED_PRICE).checked_mul(&multiplier).unwrap(),
417+
last_traded_price: BigInt::from(MOCK_LAST_TRADED_PRICE)
418+
.checked_mul(&multiplier)
419+
.unwrap(),
375420
};
376421

377422
report_data
@@ -727,6 +772,42 @@ mod tests {
727772
assert_eq!(decoded_report.feed_id, V10_FEED_ID);
728773
}
729774

775+
#[test]
776+
fn test_decode_report_v11() {
777+
let report_data = generate_mock_report_data_v11();
778+
let encoded_report_data = report_data.abi_encode().unwrap();
779+
780+
let report = generate_mock_report(&encoded_report_data);
781+
782+
let (_report_context, report_blob) = decode_full_report(&report).unwrap();
783+
784+
let expected_report_blob = vec![
785+
"000bfb6d135897e4aaf5657bffd3b0b48f8e2a5131214c9ec2d62eac5d532067", // feed_id
786+
"0000000000000000000000000000000000000000000000000000000066741d8c", // valid_from_timestamp
787+
"0000000000000000000000000000000000000000000000000000000066741d8c", // observations_timestamp
788+
"000000000000000000000000000000000000000000000000000000000000000a", // native_fee
789+
"000000000000000000000000000000000000000000000000000000000000000a", // link_fee
790+
"0000000000000000000000000000000000000000000000000000000066741df0", // expires_at
791+
"00000000000000000000000000000000000000000000000c5c22b80115100000", // mid: 228 * 10^18
792+
"00000000000000000000000000000000000000000000000017dab580a9887800", // last_seen_timestamp_ns
793+
"00000000000000000000000000000000000000000000000c4e42014d6dac0000", // bid: 227 * 10^18
794+
"00000000000000000000000000000000000000000000000000000000000004b0", // bid_volume: 1200
795+
"00000000000000000000000000000000000000000000000c6a036eb4bc740000", // ask: 229 * 10^18
796+
"00000000000000000000000000000000000000000000000000000000000005dc", // ask_volume: 1500
797+
"00000000000000000000000000000000000000000000000c5c22b80115100000", // last_traded_price: 228 * 10^18
798+
"0000000000000000000000000000000000000000000000000000000000000002", // market_status: 2 (open)
799+
];
800+
801+
let expected = bytes(&format!("0x{}", expected_report_blob.join("")));
802+
println!("Actual : {}", hex::encode(&report_blob));
803+
println!("Expected: {}", hex::encode(&expected));
804+
assert_eq!(report_blob, expected);
805+
806+
let decoded_report = ReportDataV10::decode(&report_blob).unwrap();
807+
808+
assert_eq!(decoded_report.feed_id, V11_FEED_ID);
809+
}
810+
730811
#[test]
731812
fn test_decode_report_v13() {
732813
let report_data = generate_mock_report_data_v13();
@@ -743,8 +824,8 @@ mod tests {
743824
"000000000000000000000000000000000000000000000000000000000000000a", // native_fee
744825
"000000000000000000000000000000000000000000000000000000000000000a", // link_fee
745826
"0000000000000000000000000000000000000000000000000000000066741df0", // expires_at
746-
"00000000000000000000000000000000000000000000000c4e42014d6dac0000", // best_ask: 227 * 10^18
747-
"00000000000000000000000000000000000000000000000c6a036eb4bc740000", // best_bid: 229 * 10^18
827+
"00000000000000000000000000000000000000000000000c6a036eb4bc740000", // best_ask: 229 * 10^18
828+
"00000000000000000000000000000000000000000000000c4e42014d6dac0000", // best_bid: 227 * 10^18
748829
"00000000000000000000000000000000000000000000000000000000000005dc", // ask_volume: 1500
749830
"00000000000000000000000000000000000000000000000000000000000004b0", // bid_volume: 1200
750831
"00000000000000000000000000000000000000000000000c5c22b80115100000", // last_traded_price: 228 * 10^18

0 commit comments

Comments
 (0)