From 5ddf84a897166105f52c0c22ca2682ee5dfee1b4 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Fri, 16 Dec 2022 23:42:19 +0100 Subject: [PATCH] Show latest inscriptions on home page (#996) --- src/index.rs | 6 ++- src/subcommand/server.rs | 15 ++++++ src/subcommand/server/templates/content.rs | 8 +++- src/subcommand/server/templates/home.rs | 47 ++++++++++++------- .../server/templates/inscription.rs | 4 +- src/subcommand/server/templates/sat.rs | 4 +- src/subcommand/wallet/inscribe.rs | 2 +- src/subcommand/wallet/inscriptions.rs | 2 +- src/subcommand/wallet/send.rs | 2 +- src/test.rs | 8 +++- static/index.css | 9 ++++ templates/home.html | 8 ++++ tests/server.rs | 8 ++-- tests/wallet.rs | 4 +- 14 files changed, 93 insertions(+), 34 deletions(-) diff --git a/src/index.rs b/src/index.rs index 8ddf6cc6b7..a8e0a5075f 100644 --- a/src/index.rs +++ b/src/index.rs @@ -574,7 +574,10 @@ impl Index { } } - pub(crate) fn get_inscriptions(&self) -> Result> { + pub(crate) fn get_inscriptions( + &self, + n: Option, + ) -> Result> { Ok( self .database @@ -582,6 +585,7 @@ impl Index { .open_table(SATPOINT_TO_INSCRIPTION_ID)? .range([0; 44]..)? .map(|(satpoint, id)| (decode_satpoint(*satpoint), decode_inscription_id(*id))) + .take(n.unwrap_or(usize::MAX)) .collect(), ) } diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index ca2716f85b..9b1d66e668 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -413,6 +413,21 @@ impl Server { index .blocks(100) .map_err(|err| ServerError::Internal(anyhow!("error getting blocks: {err}")))?, + index + .get_inscriptions(Some(8)) + .map_err(|err| ServerError::Internal(anyhow!("error getting inscriptions: {err}")))? + .values() + .map(|inscription_id| { + Ok(( + index + .get_inscription_by_inscription_id(*inscription_id) + .map_err(|err| ServerError::Internal(anyhow!("error getting inscriptions: {err}")))? + .unwrap() + .0, + *inscription_id, + )) + }) + .collect::>>()?, ) .page( chain, diff --git a/src/subcommand/server/templates/content.rs b/src/subcommand/server/templates/content.rs index 10fd0734a4..25ca329556 100644 --- a/src/subcommand/server/templates/content.rs +++ b/src/subcommand/server/templates/content.rs @@ -5,13 +5,17 @@ pub(crate) struct ContentHtml<'a>(pub(crate) Option>); impl<'a> Display for ContentHtml<'a> { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match self.0 { - Some(Content::Text(text)) => text.escape(f, false), + Some(Content::Text(text)) => { + write!(f, "

")?; + text.escape(f, false)?; + write!(f, "

") + } Some(Content::Png(png)) => write!( f, "", base64::encode(png) ), - None => write!(f, "UNKNOWN"), + None => write!(f, "

UNKNOWN

"), } } } diff --git a/src/subcommand/server/templates/home.rs b/src/subcommand/server/templates/home.rs index 7b3c3e7b4d..101831845c 100644 --- a/src/subcommand/server/templates/home.rs +++ b/src/subcommand/server/templates/home.rs @@ -5,10 +5,14 @@ pub(crate) struct HomeHtml { last: u64, blocks: Vec, starting_sat: Option, + inscriptions: Vec<(Inscription, InscriptionId)>, } impl HomeHtml { - pub(crate) fn new(blocks: Vec<(u64, BlockHash)>) -> Self { + pub(crate) fn new( + blocks: Vec<(u64, BlockHash)>, + inscriptions: Vec<(Inscription, InscriptionId)>, + ) -> Self { Self { starting_sat: blocks .get(0) @@ -19,6 +23,7 @@ impl HomeHtml { .cloned() .unwrap_or(0), blocks: blocks.into_iter().map(|(_, hash)| hash).collect(), + inscriptions, } } } @@ -36,22 +41,32 @@ mod tests { #[test] fn home_html() { assert_regex_match!( - &HomeHtml::new(vec![ - ( - 1260001, - "1111111111111111111111111111111111111111111111111111111111111111" - .parse() - .unwrap() - ), - ( - 1260000, - "0000000000000000000000000000000000000000000000000000000000000000" - .parse() - .unwrap() - ) - ],) + &HomeHtml::new( + vec![ + ( + 1260001, + "1111111111111111111111111111111111111111111111111111111111111111" + .parse() + .unwrap() + ), + ( + 1260000, + "0000000000000000000000000000000000000000000000000000000000000000" + .parse() + .unwrap() + ) + ], + vec![( + inscription("text/plain;charset=utf-8", "HELLOWORLD"), + txid(1) + )], + ) .to_string(), - "

Bitcoin-native NFTs

.*

Status

+ "

Bitcoin-native NFTs

.*

Latest Inscriptions

+ +

Status

cycle
1
epoch
6
diff --git a/src/subcommand/server/templates/inscription.rs b/src/subcommand/server/templates/inscription.rs index a0c76cf6aa..9481bab3ba 100644 --- a/src/subcommand/server/templates/inscription.rs +++ b/src/subcommand/server/templates/inscription.rs @@ -39,7 +39,7 @@ mod tests {
location
1111111111111111111111111111111111111111111111111111111111111111:1:0
- HELLOWORLD +

HELLOWORLD

" .unindent() ); @@ -88,7 +88,7 @@ mod tests {
location
1111111111111111111111111111111111111111111111111111111111111111:1:0
- UNKNOWN +

UNKNOWN

" .unindent() ); diff --git a/src/subcommand/server/templates/sat.rs b/src/subcommand/server/templates/sat.rs index a6105a3869..738665769c 100644 --- a/src/subcommand/server/templates/sat.rs +++ b/src/subcommand/server/templates/sat.rs @@ -103,7 +103,7 @@ mod tests {
rarity
mythic
time
1970-01-01 00:00:00
inscription
-
HELLOWORLD
+

HELLOWORLD

prev next @@ -139,7 +139,7 @@ mod tests {
rarity
mythic
time
1970-01-01 00:00:00
inscription
-
<script>alert('HELLOWORLD');</script>
+

<script>alert('HELLOWORLD');</script>

prev next diff --git a/src/subcommand/wallet/inscribe.rs b/src/subcommand/wallet/inscribe.rs index 54026552b4..65bb9c5aca 100644 --- a/src/subcommand/wallet/inscribe.rs +++ b/src/subcommand/wallet/inscribe.rs @@ -35,7 +35,7 @@ impl Inscribe { let utxos = list_utxos(&options)?; - let inscriptions = index.get_inscriptions()?; + let inscriptions = index.get_inscriptions(None)?; let commit_tx_change = get_change_addresses(&options, 2)?; diff --git a/src/subcommand/wallet/inscriptions.rs b/src/subcommand/wallet/inscriptions.rs index ba58b6a8ff..05221ab6eb 100644 --- a/src/subcommand/wallet/inscriptions.rs +++ b/src/subcommand/wallet/inscriptions.rs @@ -8,7 +8,7 @@ impl Inscriptions { let index = Index::open(&options)?; index.update()?; - let inscriptions = index.get_inscriptions()?; + let inscriptions = index.get_inscriptions(None)?; let utxos = list_utxos(&options)?; for (satpoint, inscription_id) in inscriptions { diff --git a/src/subcommand/wallet/send.rs b/src/subcommand/wallet/send.rs index 19c78abba9..e79f3eec47 100644 --- a/src/subcommand/wallet/send.rs +++ b/src/subcommand/wallet/send.rs @@ -41,7 +41,7 @@ impl Send { let utxos = list_utxos(&options)?; - let inscriptions = index.get_inscriptions()?; + let inscriptions = index.get_inscriptions(None)?; let change = get_change_addresses(&options, 2)?; diff --git a/src/test.rs b/src/test.rs index cbefa003c0..fcdee16eff 100644 --- a/src/test.rs +++ b/src/test.rs @@ -17,14 +17,18 @@ macro_rules! assert_regex_match { }; } -pub(crate) fn outpoint(n: u64) -> OutPoint { +pub(crate) fn txid(n: u64) -> Txid { let hex = format!("{n:x}"); if hex.is_empty() || hex.len() > 1 { panic!(); } - format!("{}:{}", hex.repeat(64), n).parse().unwrap() + hex.repeat(64).parse().unwrap() +} + +pub(crate) fn outpoint(n: u64) -> OutPoint { + format!("{}:{}", txid(n), n).parse().unwrap() } pub(crate) fn satpoint(n: u64, offset: u64) -> SatPoint { diff --git a/static/index.css b/static/index.css index 16071dab13..ce89264e71 100644 --- a/static/index.css +++ b/static/index.css @@ -157,3 +157,12 @@ a.legendary { a.mythic { color: var(--mythic); } + +.inscriptions { + display: flex; + flex-wrap: wrap; +} + +.inscriptions > * { + width: 25%; +} diff --git a/templates/home.html b/templates/home.html index 6b8c6ddc98..a3889b8a4d 100644 --- a/templates/home.html +++ b/templates/home.html @@ -6,6 +6,14 @@

Bitcoin-native NFTs

Get Started

+%% if !&self.inscriptions.is_empty() { +

Latest Inscriptions

+
+%% for (inscription, id) in &self.inscriptions { + {{inscription.content_html()}} +%% } +
+%% } %% if let Some(starting_sat) = self.starting_sat {

Status

diff --git a/tests/server.rs b/tests/server.rs index c6961dbee1..9e7a4b9add 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -63,7 +63,7 @@ fn inscription_page() {
location
{reveal_tx}:0:0
-HELLOWORLD.*", +

HELLOWORLD

.*", ), ); } @@ -91,7 +91,7 @@ fn inscription_appears_on_reveal_transaction_page() { ".*

Transaction .*

.*

Inscription

-HELLOWORLD +

HELLOWORLD

.*", ), ); @@ -127,7 +127,7 @@ fn inscription_page_after_send() {
location
{reveal_txid}:0:0
-HELLOWORLD.*", +

HELLOWORLD

.*", ), ); @@ -154,7 +154,7 @@ HELLOWORLD.*",
location
{}:0:0
-HELLOWORLD.*", +

HELLOWORLD

.*", txid.trim(), ), ) diff --git a/tests/wallet.rs b/tests/wallet.rs index a50eef4d77..601fd98d26 100644 --- a/tests/wallet.rs +++ b/tests/wallet.rs @@ -257,7 +257,7 @@ fn inscribe() { TestServer::spawn_with_args(&rpc_server, &["--index-sats"]).assert_response_regex( "/sat/5000000000", - ".*
inscription
\n
HELLOWORLD
.*", + ".*
inscription
\n

HELLOWORLD

.*", ); TestServer::spawn_with_args(&rpc_server, &[]).assert_response_regex( @@ -610,7 +610,7 @@ fn inscribe_with_optional_satpoint_arg() { TestServer::spawn_with_args(&rpc_server, &["--index-sats"]).assert_response_regex( "/sat/5000000000", - ".*
inscription
\n
HELLOWORLD
.*", + ".*
inscription
\n

HELLOWORLD

.*", ); TestServer::spawn_with_args(&rpc_server, &[]).assert_response_regex(