From 640bace871595038d4d322ac831b03e10737660f Mon Sep 17 00:00:00 2001 From: Arthur Roberts Date: Sat, 13 Jul 2024 21:28:45 +0100 Subject: [PATCH] Added ETH --- src/lib.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index d1f07cb..2ce549e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -67,6 +67,9 @@ pub fn time_to_blockheight(time: DateTime, bi: BlockchainInfo) -> BlockHeig num_blocks + bi.latest_block_height } +// I know there's code duplication... I _could_ abstract it out. +// I think it's probably very YAGNI + pub fn update_bsv_blockheight() { let whatsonchain_url = "https://api.whatsonchain.com/v1/bsv/main/block/headers"; match ureq::get(whatsonchain_url).call() { @@ -112,6 +115,26 @@ pub fn update_btc_blockheight() { } } +pub fn update_eth_blockheight() { + let blockcypher_url = "https://api.blockcypher.com/v1/eth/main"; + 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 time = json.get("time").unwrap().as_str().unwrap(); + let datetime = time.parse::>().unwrap(); + { + let mut bi = ETH_BI.write().unwrap(); + bi.latest_block_height = height; + bi.latest_block_time = datetime; + } + } + Err(err) => { + println!("Err: {}", err); + } + } +} + // FIXME - use a different API. I think localmonero is going down soon pub fn update_xmr_blockheight() { @@ -172,6 +195,17 @@ mod tests { assert!(bi_a.latest_block_time < bi_b.latest_block_time); } + #[test] + #[ignore] + fn get_latest_eth_blockinfo() { + let bi_a = *ETH_BI.read().unwrap(); + update_eth_blockheight(); + let bi_b = *ETH_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();