Added BTC blockinfo

This commit is contained in:
2024-07-13 20:42:36 +01:00
parent bdc027aa16
commit f791c0743a

View File

@@ -103,7 +103,33 @@ pub fn update_bsv_blockheight() {
println!("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::<HashMap<String, serde_json::Value>>();
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::<DateTime<Utc>>().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)] #[cfg(test)]
@@ -111,6 +137,17 @@ mod tests {
use super::*; use super::*;
use chrono::Duration; 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] #[test]
#[ignore] #[ignore]
fn get_latest_bsv_blockinfo() { fn get_latest_bsv_blockinfo() {