From 8b03fbd21de50a5c694f0dcc0e5b1f6ccefe299e Mon Sep 17 00:00:00 2001 From: Arthur Roberts Date: Sat, 13 Jul 2024 21:25:30 +0100 Subject: [PATCH] Added XMR --- src/lib.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 56bf0a0..d1f07cb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,8 +3,6 @@ use lazy_static::lazy_static; use std::convert::Into; use std::sync::RwLock; -// TODO review whether a genesis block has a block height of 1 or 0 - pub enum BlockchainSupported { BTC, BSV, @@ -114,6 +112,28 @@ pub fn update_btc_blockheight() { } } + +// FIXME - use a different API. I think localmonero is going down soon +pub fn update_xmr_blockheight() { + let blockcypher_url = "https://localmonero.co/blocks/api/get_stats"; + match ureq::get(blockcypher_url).call() { + Ok(resp) => { + let json: serde_json::Value = resp.into_json().unwrap(); + let height = json.get("height").unwrap().as_i64().unwrap(); + let unixtime = json.get("last_timestamp").unwrap().as_i64().unwrap(); + let datetime = Utc.timestamp_opt(unixtime, 0).unwrap(); + { + let mut bi = XMR_BI.write().unwrap(); + bi.latest_block_height = height; + bi.latest_block_time = datetime; + } + } + Err(err) => { + println!("Err: {}", err); + } + } +} + #[cfg(test)] mod tests { use super::*; @@ -141,6 +161,17 @@ mod tests { assert!(bi_a.latest_block_time < bi_b.latest_block_time); } + #[test] + #[ignore] + fn get_latest_xmr_blockinfo() { + let bi_a = *XMR_BI.read().unwrap(); + update_xmr_blockheight(); + let bi_b = *XMR_BI.read().unwrap(); + assert_ne!(bi_a, bi_b); + assert!(bi_a.latest_block_height < bi_b.latest_block_height); + assert!(bi_a.latest_block_time < bi_b.latest_block_time); + } + #[test] fn one_block_time_away() { let latest_block_time = Utc.with_ymd_and_hms(2024, 10, 10, 10, 10, 0).unwrap();