Changed reqwest for ureq
Seemed much smaller and more what I needed
This commit is contained in:
66
src/lib.rs
66
src/lib.rs
@@ -1,10 +1,9 @@
|
||||
use chrono::prelude::*;
|
||||
use lazy_static::lazy_static;
|
||||
use std::convert::Into;
|
||||
use std::sync::RwLock;
|
||||
use reqwest;
|
||||
use serde_json;
|
||||
use std::collections::HashMap;
|
||||
use std::convert::Into;
|
||||
use std::sync::RwLock;
|
||||
|
||||
// TODO review whether a genesis block has a block height of 1 or 0
|
||||
|
||||
@@ -12,10 +11,9 @@ pub enum BlockchainSupported {
|
||||
BTC,
|
||||
BSV,
|
||||
XMR,
|
||||
ETH
|
||||
ETH,
|
||||
}
|
||||
|
||||
|
||||
type BlockHeight = i64;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Debug)]
|
||||
@@ -75,28 +73,21 @@ pub fn time_to_blockheight(time: DateTime<Utc>, bi: BlockchainInfo) -> BlockHeig
|
||||
|
||||
pub fn update_bsv_blockheight() {
|
||||
let whatsonchain_url = "https://api.whatsonchain.com/v1/bsv/main/block/headers";
|
||||
match reqwest::blocking::get(whatsonchain_url) {
|
||||
match ureq::get(whatsonchain_url).call() {
|
||||
Ok(resp) => {
|
||||
let json = resp.json::<Vec<HashMap<String, serde_json::Value>>>();
|
||||
match json {
|
||||
Ok(json) => {
|
||||
let height = json[0].get("height").unwrap().as_i64().unwrap();
|
||||
let json: serde_json::Value = resp.into_json().unwrap();
|
||||
let height = json[0].get("height").unwrap().as_i64().unwrap();
|
||||
|
||||
// I am presuming that "time" is when WhatsOnChain saw the block rather than the
|
||||
// timestamp in the block. I could be very wrong about that though... As I understand
|
||||
// the timestamp in the block has no garuntee of anything other than maybe it's more
|
||||
// than the last one.
|
||||
let unixtime = json[0].get("time").unwrap().as_i64().unwrap();
|
||||
let datetime = Utc.timestamp_opt(unixtime, 0).unwrap();
|
||||
{
|
||||
let mut bi = BSV_BI.write().unwrap();
|
||||
bi.latest_block_height = height;
|
||||
bi.latest_block_time = datetime;
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Err: {}", err);
|
||||
}
|
||||
// I am presuming that "time" is when WhatsOnChain saw the block rather than the
|
||||
// timestamp in the block. I could be very wrong about that though... As I understand
|
||||
// the timestamp in the block has no garuntee of anything other than maybe it's more
|
||||
// than the last one.
|
||||
let unixtime = json[0].get("time").unwrap().as_i64().unwrap();
|
||||
let datetime = Utc.timestamp_opt(unixtime, 0).unwrap();
|
||||
{
|
||||
let mut bi = BSV_BI.write().unwrap();
|
||||
bi.latest_block_height = height;
|
||||
bi.latest_block_time = datetime;
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
@@ -107,23 +98,16 @@ pub fn update_bsv_blockheight() {
|
||||
|
||||
pub fn update_btc_blockheight() {
|
||||
let blockcypher_url = "https://api.blockcypher.com/v1/btc/main";
|
||||
match reqwest::blocking::get(blockcypher_url) {
|
||||
match ureq::get(blockcypher_url).call() {
|
||||
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);
|
||||
}
|
||||
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 = BTC_BI.write().unwrap();
|
||||
bi.latest_block_height = height;
|
||||
bi.latest_block_time = datetime;
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
|
||||
Reference in New Issue
Block a user