Skip to content

Commit

Permalink
Add inscriptions to address page (#3843)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Jul 9, 2024
1 parent cc40344 commit ee2512a
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,23 @@ impl Index {
)
}

pub fn get_inscriptions_for_outputs(
&self,
outpoints: &Vec<OutPoint>,
) -> Result<Vec<InscriptionId>> {
let mut inscriptions = Vec::new();
for outpoint in outpoints {
inscriptions.extend(
self
.get_inscriptions_on_output_with_satpoints(*outpoint)?
.iter()
.map(|(_satpoint, inscription_id)| *inscription_id),
);
}

Ok(inscriptions)
}

pub fn get_transaction(&self, txid: Txid) -> Result<Option<Transaction>> {
if txid == self.genesis_block_coinbase_txid {
return Ok(Some(self.genesis_block_coinbase_transaction.clone()));
Expand Down
3 changes: 3 additions & 0 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,8 @@ impl Server {

let sat_balance = index.get_sat_balances_for_outputs(&outputs)?;

let inscriptions = index.get_inscriptions_for_outputs(&outputs)?;

let runes_balances = index.get_aggregated_rune_balances_for_outputs(&outputs)?;

Ok(if accept_json {
Expand All @@ -872,6 +874,7 @@ impl Server {
AddressHtml {
address,
outputs,
inscriptions,
sat_balance,
runes_balances,
}
Expand Down
9 changes: 9 additions & 0 deletions src/templates/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::*;
pub(crate) struct AddressHtml {
pub(crate) address: Address,
pub(crate) outputs: Vec<OutPoint>,
pub(crate) inscriptions: Vec<InscriptionId>,
pub(crate) sat_balance: u64,
pub(crate) runes_balances: Vec<(SpacedRune, Decimal, Option<char>)>,
}
Expand All @@ -25,6 +26,7 @@ mod tests {
.require_network(Network::Bitcoin)
.unwrap(),
outputs: vec![outpoint(1), outpoint(2)],
inscriptions: vec![inscription_id(1)],
sat_balance: 99,
runes_balances: vec![
(
Expand Down Expand Up @@ -68,6 +70,13 @@ mod tests {
assert_regex_match!(address_html, expected_pattern);
}

#[test]
fn test_inscriptions_rendering() {
let address_html = setup();
let expected_pattern = r#".*<dt>inscriptions</dt>\n\s*<dd class=thumbnails>.*<a href=/inscription/1{64}i1><iframe .* src=/preview/1{64}i1></iframe></a>.*</dd>.*"#;
assert_regex_match!(address_html, expected_pattern);
}

#[test]
fn test_runes_balances_rendering() {
let address_html = setup();
Expand Down
8 changes: 8 additions & 0 deletions templates/address.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ <h1>Address {{ self.address }}</h1>
<dl>
<dt>sat balance</dt>
<dd>{{ self.sat_balance }}</dd>
%% if !self.inscriptions.is_empty() {
<dt>inscriptions</dt>
<dd class=thumbnails>
%% for inscription in &self.inscriptions {
{{Iframe::thumbnail(*inscription)}}
%% }
</dd>
%% }
<dt>runes balances</dt>
%% for (rune, decimal, symbol) in self.runes_balances.iter() {
%% if let Some(symbol) = symbol {
Expand Down
47 changes: 47 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,53 @@ fn address_page_shows_aggregated_runes_balance() {
);
}

#[test]
fn address_page_shows_aggregated_inscriptions() {
let core = mockcore::builder().network(Network::Regtest).build();
let ord =
TestServer::spawn_with_args(&core, &["--index-runes", "--index-addresses", "--regtest"]);

create_wallet(&core, &ord);

let (inscription_id_1, _reveal) = inscribe(&core, &ord);

let address = "bcrt1qs758ursh4q9z627kt3pp5yysm78ddny6txaqgw";

CommandBuilder::new(format!(
"--chain regtest --index-runes wallet send --fee-rate 1 {address} {inscription_id_1}",
))
.core(&core)
.ord(&ord)
.stdout_regex(".*")
.run_and_deserialize_output::<Output>();

core.mine_blocks(1);

let (inscription_id_2, _reveal) = inscribe(&core, &ord);

CommandBuilder::new(format!(
"--chain regtest --index-runes wallet send --fee-rate 1 {address} {inscription_id_2}",
))
.core(&core)
.ord(&ord)
.stdout_regex(".*")
.run_and_deserialize_output::<Output>();

core.mine_blocks(1);

ord.assert_response_regex(
format!("/address/{address}"),
r".*
<dl>.*
<dt>inscriptions</dt>
<dd class=thumbnails>
<a href=/inscription/[[:xdigit:]]{64}i\d><iframe .* src=/preview/[[:xdigit:]]{64}i\d></iframe></a>
<a href=/inscription/[[:xdigit:]]{64}i\d><iframe .* src=/preview/[[:xdigit:]]{64}i\d></iframe></a>
</dd>.*"
,
);
}

#[test]
fn inscription_page() {
let core = mockcore::spawn();
Expand Down

0 comments on commit ee2512a

Please sign in to comment.