Skip to content

Commit

Permalink
Add inscriptions page (ordinals#1009)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Dec 19, 2022
1 parent d654283 commit 69a5fd5
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 4 deletions.
25 changes: 23 additions & 2 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -710,6 +711,26 @@ impl Server {
),
)
}

async fn inscriptions(
Extension(chain): Extension<Chain>,
Extension(index): Extension<Arc<Index>>,
) -> ServerResult<PageHtml> {
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::<ServerResult<Vec<InscriptionId>>>()?,
}
.page(
chain,
index.has_satoshi_index().map_err(ServerError::Internal)?,
),
)
}
}

#[cfg(test)]
Expand Down
5 changes: 3 additions & 2 deletions src/subcommand/server/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,6 +12,7 @@ mod content;
mod home;
mod input;
mod inscription;
mod inscriptions;
mod output;
mod range;
mod rare;
Expand Down
51 changes: 51 additions & 0 deletions src/subcommand/server/templates/inscriptions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use super::*;

#[derive(Boilerplate)]
pub(crate) struct InscriptionsHtml {
pub(crate) inscriptions: Vec<InscriptionId>,
}

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(),
"
<h1>Inscriptions</h1>
<ul class=monospace>
<li>
<a href=/inscription/ec90757eb3b164aa43fc548faa2fa0c52025494f2c15d5ddf11260b4034ac6dc class=monospace>
ec90757eb3b164aa43fc548faa2fa0c52025494f2c15d5ddf11260b4034ac6dc
</a>
</li>
<li>
<a href=/inscription/ec90757eb3b164aa43fc548faa2fa0c52025494f2c15d5ddf11260b4034ac6dc class=monospace>
ec90757eb3b164aa43fc548faa2fa0c52025494f2c15d5ddf11260b4034ac6dc
</a>
</li>
<li>
<a href=/inscription/ec90757eb3b164aa43fc548faa2fa0c52025494f2c15d5ddf11260b4034ac6dc class=monospace>
ec90757eb3b164aa43fc548faa2fa0c52025494f2c15d5ddf11260b4034ac6dc
</a>
</li>
</ul>
".unindent()
);
}
}
11 changes: 11 additions & 0 deletions templates/inscriptions.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1>Inscriptions</h1>

<ul class=monospace>
%% for inscription_id in &self.inscriptions {
<li>
<a href=/inscription/{{inscription_id}} class=monospace>
{{ inscription_id }}
</a>
</li>
%% }
</ul>
31 changes: 31 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
".*<h1>Inscriptions</h1>.*
<ul class=monospace>
<li>
<a href=/inscription/{reveal_tx} class=monospace>
{reveal_tx}
</a>
</li>.*",
),
);
}

0 comments on commit 69a5fd5

Please sign in to comment.