6 Commits

Author SHA1 Message Date
3091cd80fd Found unclosed div 2024-07-15 21:00:50 +01:00
846a9b94c7 Added blocktime generation
Also refactored the "base" template stuff - really should've done that in
a separate commit
2024-07-15 20:47:32 +01:00
eb08b348b1 Added updating blockchains button 2024-07-14 19:21:41 +01:00
cc7678ec18 Added current block info and skeleton of website 2024-07-14 19:08:38 +01:00
0fbd04e791 Initial Rocket commit 2024-07-14 18:34:14 +01:00
12f77ce9af Added an "update all" function
Probably not really worth a whole commit
2024-07-14 17:25:11 +01:00
8 changed files with 2101 additions and 15 deletions

1962
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -8,5 +8,16 @@ edition = "2021"
[dependencies]
chrono = "0.4.38"
lazy_static = "1.5.0"
rocket = "0.5.1"
serde_json = "1.0.120"
ureq = { version = "2.10.0", features = ["json"] }
serde = { version = "1.0", features = ["derive"] }
[dependencies.rocket_dyn_templates]
version = "0.2.0"
features = ["tera"]
[dependencies.serde_with]
version = "1.9.1"
features = [ "chrono" ]

View File

@@ -1,7 +1,9 @@
use chrono::prelude::*;
use lazy_static::lazy_static;
use rocket::serde::Serialize;
use std::convert::Into;
use std::sync::RwLock;
use serde_with::{serde_as, DisplayFromStr};
pub enum BlockchainSupported {
BTC,
@@ -12,10 +14,12 @@ pub enum BlockchainSupported {
type BlockHeight = i64;
#[derive(Clone, Copy, PartialEq, Debug)]
#[serde_as]
#[derive(Clone, Copy, PartialEq, Debug, Serialize)]
pub struct BlockchainInfo {
block_period: u64, // In seconds
latest_block_height: BlockHeight,
#[serde_as(as = "DisplayFromStr")]
latest_block_time: DateTime<Utc>,
}
@@ -157,6 +161,13 @@ pub fn update_xmr_blockheight() {
}
}
pub fn update_all_blockheights() {
update_bsv_blockheight();
update_btc_blockheight();
update_eth_blockheight();
update_xmr_blockheight();
}
#[cfg(test)]
mod tests {
use super::*;

56
src/main.rs Normal file
View File

@@ -0,0 +1,56 @@
#[macro_use] extern crate rocket;
use rocket_dyn_templates::{context, Template};
use blockchain_time::{BlockchainInfo, BlockchainSupported, update_all_blockheights, time_to_blockheight_specific_chain};
use chrono::prelude::*;
#[get("/")]
fn index() -> Template {
let blockchain_infos: Vec<(String, BlockchainInfo)> = vec!{
("BSV".to_string(), BlockchainSupported::BSV.into()),
("BTC".to_string(), BlockchainSupported::BTC.into()),
("XMR".to_string(), BlockchainSupported::XMR.into()),
("ETH".to_string(), BlockchainSupported::ETH.into()),
};
let context = context! {
blockchain_infos
};
Template::render("index", context)
}
#[get("/update_blockchains")]
fn update_blockchains() -> Template {
update_all_blockheights();
Template::render("update_complete", context!{})
}
#[get("/to_blockchain_time?<input_datetime>")]
fn to_blockchain_time(input_datetime: &str) -> Template {
let dt = NaiveDateTime::parse_from_str(input_datetime, "%FT%R");
let dt = match dt {
Ok(dt) => {
dt.and_utc()
},
Err(e) => {
println!("error: {}", e);
panic!();
}
};
let blocktimes = vec!{
("BSV".to_string(), time_to_blockheight_specific_chain(dt, BlockchainSupported::BSV)),
("BTC".to_string(), time_to_blockheight_specific_chain(dt, BlockchainSupported::BTC)),
("XMR".to_string(), time_to_blockheight_specific_chain(dt, BlockchainSupported::XMR)),
("ETH".to_string(), time_to_blockheight_specific_chain(dt, BlockchainSupported::ETH)),
};
let context = context!{
blocktimes
};
println!("{:?}", context);
Template::render("blockchain_time_results", context)
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index, update_blockchains, to_blockchain_time])
.attach(Template::fairing())
}

22
templates/base.html.tera Normal file
View File

@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Big Tim</title>
<style>
.blockchain_infos {
display: flex;
flex-wrap: wrap;
}
.blockchain_info {
margin: 20px;
}
.result {
font-family: monospace, monospace;
}
</style>
</head>
<body>
{% block content %}{% endblock content %}
</body>
</html>

View File

@@ -0,0 +1,13 @@
{% extends "base" %}
{% block content %}
<h1>Big Tim Results</h1>
<div class="blockchain_infos">
{% for bts in blocktimes %}
<div class="blockchain_info">
<h2>{{bts[0]}}</h2>
<p>Height: <span class="result">{{bts[1]}}</span></p>
</div>
{% endfor %}
</div>
<p>I hope that was useful for you! <a href="/">Go home</a> to try again.
{% endblock content %}

35
templates/index.html.tera Normal file
View File

@@ -0,0 +1,35 @@
{% extends "base" %}
{% block content %}
<h1>Welcome to Blockchain Time</h1><p>A.K.A. Big Tim</p>
<div>
<h2>Convert to Blockchain Time</h2>
<form id="form" method="GET" action="to_blockchain_time">
<label for="input_datetime">Input DateTime (in UTC only currently)</label>
<input type="datetime-local" id="input_datetime" name="input_datetime">
<input type="submit" value="Submit">
</form>
</div>
<div>
<h2>Convert from Blockchain Time</h2>
<p><b>Booo</b> why are you going this way??? Don't you know your blocktimes??</p>
<p><b><i><a href="#">UNIMPLEMENTED</a></i></b></p>
</div>
<div class="cur_blocks">
<h2>Latest Block Heights and Times Cached</h2>
<div class="blockchain_infos">
{% for bi in blockchain_infos %}
<div class="blockchain_info">
<h3>{{ bi[0] }}</h3>
<p>Blockheight: <span class="result">{{ bi[1].latest_block_height }}</span></p>
<p>Time Seen: <span class="result">{{ bi[1].latest_block_time }}</span></p>
</div>
{% endfor %}
</div>
</div>
<a href="update_blockchains">Update Blockchain Information</a>
<p>Pressing the above button might take some time... or it might crash the appliaction...
or it'll do what you want... you won't know till you press it.</p>
<p>Please don't press it multiple times</p>
<p>In fact, maybe don't press it at all</p>
{% endblock content %}

View File

@@ -0,0 +1,4 @@
{% extends "base" %}
{% block content %}
If you're seeing this, it probably worked. Go back <a href="/">here</a> to check.
{% endblock content %}