Skip to content

Commit 14251f7

Browse files
authored
Merge pull request #45 from Foundation-Devices/exchange-rate-history
exchange rate history
2 parents 868c1ea + 198011a commit 14251f7

File tree

4 files changed

+51
-23
lines changed

4 files changed

+51
-23
lines changed

api-demo/src/demo/envoy.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ impl Envoy {
7272
loop {
7373
chapter_title("💸 Envoy tells Passport the USD exchange rate.");
7474
let msg = EnvoyMessage::new(
75-
QuantumLinkMessage::ExchangeRate(ExchangeRate::new("USD", 65432.21)),
75+
QuantumLinkMessage::ExchangeRate(ExchangeRate {
76+
currency_code: String::from("USD"),
77+
rate: 65432.21,
78+
timestamp: 0,
79+
}),
7680
12345,
7781
);
7882

api/src/api/fx.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@ pub struct ExchangeRate {
66
pub currency_code: String,
77
#[n(1)]
88
pub rate: f32,
9+
#[n(2)]
10+
pub timestamp: u64,
911
}
1012

11-
impl ExchangeRate {
12-
pub fn new(currency_code: &str, rate: f32) -> Self {
13-
Self {
14-
currency_code: currency_code.to_string(),
15-
rate,
16-
}
17-
}
18-
19-
pub fn currency_code(&self) -> &str {
20-
&self.currency_code
21-
}
13+
#[quantum_link]
14+
pub struct ExchangeRateHistory {
15+
#[n(0)]
16+
pub history: Vec<PricePoint>,
17+
#[n(1)]
18+
pub currency_code: String,
19+
}
2220

23-
pub fn rate(&self) -> f32 {
24-
self.rate
25-
}
21+
#[quantum_link]
22+
pub struct PricePoint {
23+
#[n(0)]
24+
pub rate: f32,
25+
#[n(1)]
26+
pub timestamp: u64,
2627
}

api/src/api/message.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
FirmwareFetchEvent, FirmwareFetchRequest, FirmwareUpdateCheckRequest,
1515
FirmwareUpdateCheckResponse, FirmwareUpdateResult,
1616
},
17-
fx::ExchangeRate,
17+
fx::{ExchangeRate, ExchangeRateHistory},
1818
pairing::{PairingRequest, PairingResponse},
1919
scv::SecurityCheck,
2020
status::{DeviceStatus, EnvoyStatus},
@@ -74,6 +74,9 @@ impl PassportMessage {
7474
pub enum QuantumLinkMessage {
7575
#[n(0)]
7676
ExchangeRate(#[n(0)] ExchangeRate),
77+
#[n(26)]
78+
ExchangeRateHistory(#[n(0)] ExchangeRateHistory),
79+
7780
#[n(1)]
7881
FirmwareUpdateCheckRequest(#[n(0)] FirmwareUpdateCheckRequest),
7982
#[n(2)]

api/src/api/quantum_link.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,11 @@ mod tests {
256256

257257
#[test]
258258
fn test_encode_decode_quantumlink_message() {
259-
let fx_rate = ExchangeRate::new("USD", 0.85);
259+
let fx_rate = ExchangeRate {
260+
currency_code: String::from("USD"),
261+
rate: 0.85,
262+
timestamp: 0,
263+
};
260264
let original_message = QuantumLinkMessage::ExchangeRate(fx_rate.clone());
261265

262266
// Encode the message
@@ -271,15 +275,19 @@ mod tests {
271275
};
272276

273277
// Assert that the original and decoded messages are the same
274-
assert_eq!(fx_rate.rate(), fx_rate_decoded.rate());
278+
assert_eq!(fx_rate.rate, fx_rate_decoded.rate);
275279
}
276280

277281
#[test]
278282
fn test_seal_unseal_quantumlink_message() {
279283
let envoy = QuantumLinkIdentity::generate();
280284
let passport = QuantumLinkIdentity::generate();
281285

282-
let fx_rate = ExchangeRate::new("USD", 0.85);
286+
let fx_rate = ExchangeRate {
287+
currency_code: String::from("USD"),
288+
rate: 0.85,
289+
timestamp: 0,
290+
};
283291
let original_message = EnvoyMessage {
284292
message: QuantumLinkMessage::ExchangeRate(fx_rate.clone()),
285293
timestamp: 123456,
@@ -302,7 +310,7 @@ mod tests {
302310
};
303311

304312
// Assert that the original and decoded messages are the same
305-
assert_eq!(fx_rate.rate(), fx_rate_decoded.rate());
313+
assert_eq!(fx_rate.rate, fx_rate_decoded.rate);
306314
}
307315

308316
#[test]
@@ -325,7 +333,11 @@ mod tests {
325333
let passport = QuantumLinkIdentity::generate();
326334
let mut arid_cache = ARIDCache::new();
327335

328-
let fx_rate = ExchangeRate::new("USD", 0.85);
336+
let fx_rate = ExchangeRate {
337+
currency_code: String::from("USD"),
338+
rate: 0.85,
339+
timestamp: 0,
340+
};
329341
let original_message = EnvoyMessage {
330342
message: QuantumLinkMessage::ExchangeRate(fx_rate.clone()),
331343
timestamp: 123456,
@@ -366,7 +378,11 @@ mod tests {
366378
let passport = QuantumLinkIdentity::generate();
367379

368380
// Create and seal multiple messages
369-
let fx_rate = ExchangeRate::new("USD", 0.85);
381+
let fx_rate = ExchangeRate {
382+
currency_code: String::from("USD"),
383+
rate: 0.85,
384+
timestamp: 0,
385+
};
370386
let message1 = EnvoyMessage {
371387
message: QuantumLinkMessage::ExchangeRate(fx_rate.clone()),
372388
timestamp: 123456,
@@ -439,7 +455,11 @@ fn test_replay_check() {
439455
let envoy = QuantumLinkIdentity::generate();
440456
let passport = QuantumLinkIdentity::generate();
441457

442-
let fx_rate = ExchangeRate::new("USD", 0.85);
458+
let fx_rate = ExchangeRate {
459+
currency_code: String::from("USD"),
460+
rate: 0.85,
461+
timestamp: 0,
462+
};
443463
let message = EnvoyMessage {
444464
message: QuantumLinkMessage::ExchangeRate(fx_rate),
445465
timestamp: 123456,

0 commit comments

Comments
 (0)