Moved creation of the blockchain infos to a static context

This commit is contained in:
2024-07-13 19:26:44 +01:00
parent a382471b33
commit 9620fdb116
3 changed files with 44 additions and 26 deletions

View File

@@ -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)
}