Skip to content

Commit

Permalink
Show latest inscriptions on home page (ordinals#996)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Dec 16, 2022
1 parent d236dd8 commit 5ddf84a
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 34 deletions.
6 changes: 5 additions & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,14 +574,18 @@ impl Index {
}
}

pub(crate) fn get_inscriptions(&self) -> Result<BTreeMap<SatPoint, InscriptionId>> {
pub(crate) fn get_inscriptions(
&self,
n: Option<usize>,
) -> Result<BTreeMap<SatPoint, InscriptionId>> {
Ok(
self
.database
.begin_read()?
.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(),
)
}
Expand Down
15 changes: 15 additions & 0 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<ServerResult<Vec<(Inscription, InscriptionId)>>>()?,
)
.page(
chain,
Expand Down
8 changes: 6 additions & 2 deletions src/subcommand/server/templates/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ pub(crate) struct ContentHtml<'a>(pub(crate) Option<Content<'a>>);
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, "<p>")?;
text.escape(f, false)?;
write!(f, "</p>")
}
Some(Content::Png(png)) => write!(
f,
"<img src='data:image/png;base64,{}'>",
base64::encode(png)
),
None => write!(f, "UNKNOWN"),
None => write!(f, "<p>UNKNOWN</p>"),
}
}
}
47 changes: 31 additions & 16 deletions src/subcommand/server/templates/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ pub(crate) struct HomeHtml {
last: u64,
blocks: Vec<BlockHash>,
starting_sat: Option<Sat>,
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)
Expand All @@ -19,6 +23,7 @@ impl HomeHtml {
.cloned()
.unwrap_or(0),
blocks: blocks.into_iter().map(|(_, hash)| hash).collect(),
inscriptions,
}
}
}
Expand All @@ -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(),
"<h1>Bitcoin-native NFTs</h1>.*<h2>Status</h2>
"<h1>Bitcoin-native NFTs</h1>.*<h2>Latest Inscriptions</h2>
<div class=inscriptions>
<a href=/inscription/1111111111111111111111111111111111111111111111111111111111111111><p>HELLOWORLD</p></a>
</div>
<h2>Status</h2>
<dl>
<dt>cycle</dt><dd>1</dd>
<dt>epoch</dt><dd>6</dd>
Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/server/templates/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mod tests {
<dt>location</dt>
<dd>1111111111111111111111111111111111111111111111111111111111111111:1:0</dd>
</dl>
HELLOWORLD
<p>HELLOWORLD</p>
"
.unindent()
);
Expand Down Expand Up @@ -88,7 +88,7 @@ mod tests {
<dt>location</dt>
<dd>1111111111111111111111111111111111111111111111111111111111111111:1:0</dd>
</dl>
UNKNOWN
<p>UNKNOWN</p>
"
.unindent()
);
Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/server/templates/sat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ mod tests {
<dt>rarity</dt><dd><span class=mythic>mythic</span></dd>
<dt>time</dt><dd>1970-01-01 00:00:00</dd>
<dt>inscription</dt>
<dd>HELLOWORLD</dd>
<dd><p>HELLOWORLD</p></dd>
</dl>
prev
<a href=/sat/1>next</a>
Expand Down Expand Up @@ -139,7 +139,7 @@ mod tests {
<dt>rarity</dt><dd><span class=mythic>mythic</span></dd>
<dt>time</dt><dd>1970-01-01 00:00:00</dd>
<dt>inscription</dt>
<dd>&lt;script&gt;alert(&apos;HELLOWORLD&apos;);&lt;/script&gt;</dd>
<dd><p>&lt;script&gt;alert(&apos;HELLOWORLD&apos;);&lt;/script&gt;</p></dd>
</dl>
prev
<a href=/sat/1>next</a>
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;

Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/wallet/inscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/wallet/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;

Expand Down
8 changes: 6 additions & 2 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions static/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,12 @@ a.legendary {
a.mythic {
color: var(--mythic);
}

.inscriptions {
display: flex;
flex-wrap: wrap;
}

.inscriptions > * {
width: 25%;
}
8 changes: 8 additions & 0 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ <h1>Bitcoin-native NFTs</h1>

<p><a href=https://docs.ordinals.com>Get Started</a></p>

%% if !&self.inscriptions.is_empty() {
<h2>Latest Inscriptions</h2>
<div class=inscriptions>
%% for (inscription, id) in &self.inscriptions {
<a href=/inscription/{{id}}>{{inscription.content_html()}}</a>
%% }
</div>
%% }
%% if let Some(starting_sat) = self.starting_sat {
<h2>Status</h2>
<dl>
Expand Down
8 changes: 4 additions & 4 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn inscription_page() {
<dt>location</dt>
<dd>{reveal_tx}:0:0</dd>
</dl>
HELLOWORLD.*",
<p>HELLOWORLD</p>.*",
),
);
}
Expand Down Expand Up @@ -91,7 +91,7 @@ fn inscription_appears_on_reveal_transaction_page() {
".*<h1>Transaction .*</h1>.*
<h2>Inscription</h2>
<a href=/inscription/{reveal_tx}>
HELLOWORLD
<p>HELLOWORLD</p>
</a>.*",
),
);
Expand Down Expand Up @@ -127,7 +127,7 @@ fn inscription_page_after_send() {
<dt>location</dt>
<dd>{reveal_txid}:0:0</dd>
</dl>
HELLOWORLD.*",
<p>HELLOWORLD</p>.*",
),
);

Expand All @@ -154,7 +154,7 @@ HELLOWORLD.*",
<dt>location</dt>
<dd>{}:0:0</dd>
</dl>
HELLOWORLD.*",
<p>HELLOWORLD</p>.*",
txid.trim(),
),
)
Expand Down
4 changes: 2 additions & 2 deletions tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ fn inscribe() {

TestServer::spawn_with_args(&rpc_server, &["--index-sats"]).assert_response_regex(
"/sat/5000000000",
".*<dt>inscription</dt>\n <dd>HELLOWORLD</dd>.*",
".*<dt>inscription</dt>\n <dd><p>HELLOWORLD</p></dd>.*",
);

TestServer::spawn_with_args(&rpc_server, &[]).assert_response_regex(
Expand Down Expand Up @@ -610,7 +610,7 @@ fn inscribe_with_optional_satpoint_arg() {

TestServer::spawn_with_args(&rpc_server, &["--index-sats"]).assert_response_regex(
"/sat/5000000000",
".*<dt>inscription</dt>\n <dd>HELLOWORLD</dd>.*",
".*<dt>inscription</dt>\n <dd><p>HELLOWORLD</p></dd>.*",
);

TestServer::spawn_with_args(&rpc_server, &[]).assert_response_regex(
Expand Down

0 comments on commit 5ddf84a

Please sign in to comment.