Added BTC blockinfo
This commit is contained in:
39
src/lib.rs
39
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::<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)]
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user