Added XMR
This commit is contained in:
35
src/lib.rs
35
src/lib.rs
@@ -3,8 +3,6 @@ use lazy_static::lazy_static;
|
|||||||
use std::convert::Into;
|
use std::convert::Into;
|
||||||
use std::sync::RwLock;
|
use std::sync::RwLock;
|
||||||
|
|
||||||
// TODO review whether a genesis block has a block height of 1 or 0
|
|
||||||
|
|
||||||
pub enum BlockchainSupported {
|
pub enum BlockchainSupported {
|
||||||
BTC,
|
BTC,
|
||||||
BSV,
|
BSV,
|
||||||
@@ -114,6 +112,28 @@ pub fn update_btc_blockheight() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// FIXME - use a different API. I think localmonero is going down soon
|
||||||
|
pub fn update_xmr_blockheight() {
|
||||||
|
let blockcypher_url = "https://localmonero.co/blocks/api/get_stats";
|
||||||
|
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 unixtime = json.get("last_timestamp").unwrap().as_i64().unwrap();
|
||||||
|
let datetime = Utc.timestamp_opt(unixtime, 0).unwrap();
|
||||||
|
{
|
||||||
|
let mut bi = XMR_BI.write().unwrap();
|
||||||
|
bi.latest_block_height = height;
|
||||||
|
bi.latest_block_time = datetime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
println!("Err: {}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -141,6 +161,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_xmr_blockinfo() {
|
||||||
|
let bi_a = *XMR_BI.read().unwrap();
|
||||||
|
update_xmr_blockheight();
|
||||||
|
let bi_b = *XMR_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();
|
||||||
|
|||||||
Reference in New Issue
Block a user