From f791c0743ac14773d925e1fc6e21654e1545c2bb Mon Sep 17 00:00:00 2001 From: Arthur Roberts Date: Sat, 13 Jul 2024 20:42:36 +0100 Subject: [PATCH] Added BTC blockinfo --- src/lib.rs | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 6509db5..e23447d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -102,8 +102,34 @@ pub fn update_bsv_blockheight() { Err(err) => { println!("Err: {}", err); } - } + } +} +pub fn update_btc_blockheight() { + let blockcypher_url = "https://api.blockcypher.com/v1/btc/main"; + match reqwest::blocking::get(blockcypher_url) { + Ok(resp) => { + let json = resp.json::>(); + match json { + Ok(json) => { + let height = json.get("height").unwrap().as_i64().unwrap(); + let time = json.get("time").unwrap().as_str().unwrap(); + let datetime = time.parse::>().unwrap(); + { + let mut bi = BTC_BI.write().unwrap(); + bi.latest_block_height = height; + bi.latest_block_time = datetime; + } + } + Err(err) => { + println!("Err: {}", err); + } + } + } + Err(err) => { + println!("Err: {}", err); + } + } } #[cfg(test)] @@ -111,6 +137,17 @@ mod tests { use super::*; use chrono::Duration; + #[test] + #[ignore] + fn get_latest_btc_blockinfo() { + let bi_a = *BTC_BI.read().unwrap(); + update_btc_blockheight(); + let bi_b = *BTC_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] #[ignore] fn get_latest_bsv_blockinfo() {