Added ETH

This commit is contained in:
2024-07-13 21:28:45 +01:00
parent 8b03fbd21d
commit 640bace871

View File

@@ -67,6 +67,9 @@ pub fn time_to_blockheight(time: DateTime<Utc>, bi: BlockchainInfo) -> BlockHeig
num_blocks + bi.latest_block_height 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() { pub fn update_bsv_blockheight() {
let whatsonchain_url = "https://api.whatsonchain.com/v1/bsv/main/block/headers"; let whatsonchain_url = "https://api.whatsonchain.com/v1/bsv/main/block/headers";
match ureq::get(whatsonchain_url).call() { 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::<DateTime<Utc>>().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 // FIXME - use a different API. I think localmonero is going down soon
pub fn update_xmr_blockheight() { pub fn update_xmr_blockheight() {
@@ -172,6 +195,17 @@ mod tests {
assert!(bi_a.latest_block_time < bi_b.latest_block_time); 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] #[test]
fn one_block_time_away() { fn one_block_time_away() {
let latest_block_time = Utc.with_ymd_and_hms(2024, 10, 10, 10, 10, 0).unwrap(); let latest_block_time = Utc.with_ymd_and_hms(2024, 10, 10, 10, 10, 0).unwrap();