Moved creation of the blockchain infos to a static context
This commit is contained in:
7
Cargo.lock
generated
7
Cargo.lock
generated
@@ -28,6 +28,7 @@ name = "blockchain_time"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -100,6 +101,12 @@ dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.155"
|
||||
|
||||
@@ -7,3 +7,4 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
chrono = "0.4.38"
|
||||
lazy_static = "1.5.0"
|
||||
|
||||
62
src/lib.rs
62
src/lib.rs
@@ -1,9 +1,9 @@
|
||||
use std::convert::Into;
|
||||
use chrono::prelude::*;
|
||||
use lazy_static::lazy_static;
|
||||
use std::convert::Into;
|
||||
|
||||
// TODO review whether a genesis block has a block height of 1 or 0
|
||||
|
||||
|
||||
pub enum BlockchainSupported {
|
||||
BTC,
|
||||
BSV,
|
||||
@@ -13,41 +13,51 @@ pub enum BlockchainSupported {
|
||||
|
||||
type BlockHeight = i64;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct BlockchainInfo {
|
||||
block_period: u64, // in seconds maybe?
|
||||
|
||||
block_period: u64, // In seconds
|
||||
latest_block_height: BlockHeight,
|
||||
latest_block_time: DateTime<Utc>,
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref BSV_BI: BlockchainInfo = BlockchainInfo {
|
||||
block_period: 10 * 60, // 10 minutes,
|
||||
latest_block_height: 852792,
|
||||
latest_block_time: Utc.with_ymd_and_hms(2024, 7, 11, 19, 53, 7).unwrap(),
|
||||
};
|
||||
static ref BTC_BI: BlockchainInfo = BlockchainInfo {
|
||||
block_period: 10 * 60, // 10 minutes,
|
||||
latest_block_height: 851724,
|
||||
latest_block_time: Utc.with_ymd_and_hms(2024, 7, 11, 19, 38, 6).unwrap(),
|
||||
};
|
||||
static ref XMR_BI: BlockchainInfo = BlockchainInfo {
|
||||
block_period: 2 * 60, // 2 minutes,
|
||||
latest_block_height: 3190705,
|
||||
latest_block_time: Utc.with_ymd_and_hms(2024, 7, 11, 19, 54, 11).unwrap(),
|
||||
};
|
||||
static ref ETH_BI: BlockchainInfo = BlockchainInfo {
|
||||
block_period: 12,
|
||||
latest_block_height: 20285486,
|
||||
latest_block_time: Utc.with_ymd_and_hms(2024, 7, 11, 19, 53, 59).unwrap(),
|
||||
};
|
||||
}
|
||||
|
||||
impl From<BlockchainSupported> for BlockchainInfo {
|
||||
fn from (val: BlockchainSupported) -> Self {
|
||||
fn from(val: BlockchainSupported) -> Self {
|
||||
match val {
|
||||
BlockchainSupported::BTC => BlockchainInfo {
|
||||
block_period: 10 * 60, // 10 minutes,
|
||||
latest_block_height: 851724,
|
||||
latest_block_time: Utc.with_ymd_and_hms(2024, 7, 11, 19, 38, 6).unwrap(),
|
||||
},
|
||||
BlockchainSupported::BSV => BlockchainInfo {
|
||||
block_period: 10 * 60, // 10 minutes,
|
||||
latest_block_height: 852792,
|
||||
latest_block_time: Utc.with_ymd_and_hms(2024, 7, 11, 19, 53, 7).unwrap(),
|
||||
},
|
||||
BlockchainSupported::XMR => BlockchainInfo {
|
||||
block_period: 2 * 60, // 2 minutes,
|
||||
latest_block_height: 3190705,
|
||||
latest_block_time: Utc.with_ymd_and_hms(2024, 7, 11, 19, 54, 11).unwrap(),
|
||||
},
|
||||
BlockchainSupported::ETH => BlockchainInfo {
|
||||
block_period: 12, // TODO investigate what the story is here... I don't think it's as simple as 12
|
||||
latest_block_height: 20285486,
|
||||
latest_block_time: Utc.with_ymd_and_hms(2024, 7, 11, 19, 53, 59).unwrap(),
|
||||
},
|
||||
BlockchainSupported::BTC => *BTC_BI,
|
||||
BlockchainSupported::BSV => *BSV_BI,
|
||||
BlockchainSupported::XMR => *XMR_BI,
|
||||
BlockchainSupported::ETH => *ETH_BI,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn time_to_blockheight_specific_chain(time: DateTime<Utc>, blockchain: BlockchainSupported) -> BlockHeight {
|
||||
pub fn time_to_blockheight_specific_chain(
|
||||
time: DateTime<Utc>,
|
||||
blockchain: BlockchainSupported,
|
||||
) -> BlockHeight {
|
||||
let bi: BlockchainInfo = blockchain.into();
|
||||
time_to_blockheight(time, bi)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user