From 69a5fd5f4d9a9644c522fa661b588509aa7fe7f1 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Mon, 19 Dec 2022 21:14:26 +0100 Subject: [PATCH] Add inscriptions page (#1009) --- src/subcommand/server.rs | 25 ++++++++- src/subcommand/server/templates.rs | 5 +- .../server/templates/inscriptions.rs | 51 +++++++++++++++++++ templates/inscriptions.html | 11 ++++ tests/server.rs | 31 +++++++++++ 5 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 src/subcommand/server/templates/inscriptions.rs create mode 100644 templates/inscriptions.html diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 9b1d66e668..452a9b0541 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -4,8 +4,8 @@ use { self::{ deserialize_from_str::DeserializeFromStr, templates::{ - BlockHtml, ClockSvg, HomeHtml, InputHtml, InscriptionHtml, OutputHtml, PageContent, PageHtml, - RangeHtml, RareTxt, SatHtml, TransactionHtml, + BlockHtml, ClockSvg, HomeHtml, InputHtml, InscriptionHtml, InscriptionsHtml, OutputHtml, + PageContent, PageHtml, RangeHtml, RareTxt, SatHtml, TransactionHtml, }, }, axum::{ @@ -159,6 +159,7 @@ impl Server { .route("/favicon.ico", get(Self::favicon)) .route("/input/:block/:transaction/:input", get(Self::input)) .route("/inscription/:inscription_id", get(Self::inscription)) + .route("/inscriptions", get(Self::inscriptions)) .route("/install.sh", get(Self::install_script)) .route("/ordinal/:sat", get(Self::ordinal)) .route("/output/:output", get(Self::output)) @@ -710,6 +711,26 @@ impl Server { ), ) } + + async fn inscriptions( + Extension(chain): Extension, + Extension(index): Extension>, + ) -> ServerResult { + Ok( + InscriptionsHtml { + inscriptions: index + .get_inscriptions(Some(100)) + .map_err(|err| ServerError::Internal(anyhow!("error getting inscriptions: {err}")))? + .values() + .map(|inscription_id| Ok(*inscription_id)) + .collect::>>()?, + } + .page( + chain, + index.has_satoshi_index().map_err(ServerError::Internal)?, + ), + ) + } } #[cfg(test)] diff --git a/src/subcommand/server/templates.rs b/src/subcommand/server/templates.rs index 91993ceb0e..49c37f64d6 100644 --- a/src/subcommand/server/templates.rs +++ b/src/subcommand/server/templates.rs @@ -2,8 +2,8 @@ use {super::*, boilerplate::Boilerplate}; pub(crate) use { block::BlockHtml, clock::ClockSvg, content::ContentHtml, home::HomeHtml, input::InputHtml, - inscription::InscriptionHtml, output::OutputHtml, range::RangeHtml, rare::RareTxt, sat::SatHtml, - transaction::TransactionHtml, + inscription::InscriptionHtml, inscriptions::InscriptionsHtml, output::OutputHtml, + range::RangeHtml, rare::RareTxt, sat::SatHtml, transaction::TransactionHtml, }; mod block; @@ -12,6 +12,7 @@ mod content; mod home; mod input; mod inscription; +mod inscriptions; mod output; mod range; mod rare; diff --git a/src/subcommand/server/templates/inscriptions.rs b/src/subcommand/server/templates/inscriptions.rs new file mode 100644 index 0000000000..be146a5c23 --- /dev/null +++ b/src/subcommand/server/templates/inscriptions.rs @@ -0,0 +1,51 @@ +use super::*; + +#[derive(Boilerplate)] +pub(crate) struct InscriptionsHtml { + pub(crate) inscriptions: Vec, +} + +impl PageContent for InscriptionsHtml { + fn title(&self) -> String { + "Inscriptions".into() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn inscriptions() { + pretty_assert_eq!( + InscriptionsHtml { + inscriptions: vec![ + InscriptionId::from_str("ec90757eb3b164aa43fc548faa2fa0c52025494f2c15d5ddf11260b4034ac6dc").unwrap(), + InscriptionId::from_str("ec90757eb3b164aa43fc548faa2fa0c52025494f2c15d5ddf11260b4034ac6dc").unwrap(), + InscriptionId::from_str("ec90757eb3b164aa43fc548faa2fa0c52025494f2c15d5ddf11260b4034ac6dc").unwrap(), + ] + }.to_string(), + " +

Inscriptions

+ + + ".unindent() + ); + } +} diff --git a/templates/inscriptions.html b/templates/inscriptions.html new file mode 100644 index 0000000000..f8fd03dcbe --- /dev/null +++ b/templates/inscriptions.html @@ -0,0 +1,11 @@ +

Inscriptions

+ + diff --git a/tests/server.rs b/tests/server.rs index 9e7a4b9add..2d8cc2e7b3 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -187,3 +187,34 @@ fn inscription_content() { ); assert_eq!(response.bytes().unwrap(), "HELLOWORLD"); } + +#[test] +fn inscriptions_page() { + let rpc_server = test_bitcoincore_rpc::spawn_with(Network::Regtest, "ord"); + let txid = rpc_server.mine_blocks(1)[0].txdata[0].txid(); + + let stdout = CommandBuilder::new(format!( + "--chain regtest wallet inscribe --satpoint {txid}:0:0 --file hello.txt" + )) + .write("hello.txt", "HELLOWORLD") + .rpc_server(&rpc_server) + .stdout_regex("commit\t[[:xdigit:]]{64}\nreveal\t[[:xdigit:]]{64}\n") + .run(); + + let reveal_tx = reveal_txid_from_inscribe_stdout(&stdout); + + rpc_server.mine_blocks(1); + + TestServer::spawn_with_args(&rpc_server, &[]).assert_response_regex( + "/inscriptions", + &format!( + ".*

Inscriptions

.* +