Skip to content

Commit

Permalink
Show sat owner address when present (ordinals#4016)
Browse files Browse the repository at this point in the history
  • Loading branch information
lifofifoX authored Nov 21, 2024
1 parent ffe27e2 commit 6a9b3bd
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ impl Output {

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Sat {
pub address: Option<String>,
pub block: u32,
pub charms: Vec<Charm>,
pub cycle: u32,
Expand Down
23 changes: 23 additions & 0 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,30 @@ impl Server {

let charms = sat.charms();

let address = satpoint.and_then(|satpoint| {
let outpoint = satpoint.outpoint;

if outpoint == unbound_outpoint() {
None
} else {
index
.get_transaction(outpoint.txid)
.ok()
.flatten()
.and_then(|tx| tx.output.into_iter().nth(outpoint.vout.try_into().unwrap()))
.and_then(|output| {
server_config
.chain
.address_from_script(&output.script_pubkey)
.ok()
.map(|address| address.to_string())
})
}
});

Ok(if accept_json {
Json(api::Sat {
address,
number: sat.0,
decimal: sat.decimal().to_string(),
degree: sat.degree().to_string(),
Expand All @@ -591,6 +613,7 @@ impl Server {
.into_response()
} else {
SatHtml {
address,
sat,
satpoint,
blocktime,
Expand Down
22 changes: 22 additions & 0 deletions src/templates/sat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::*;

#[derive(Boilerplate)]
pub(crate) struct SatHtml {
pub(crate) address: Option<String>,
pub(crate) blocktime: Blocktime,
pub(crate) inscriptions: Vec<InscriptionId>,
pub(crate) sat: Sat,
Expand All @@ -22,6 +23,7 @@ mod tests {
fn first() {
assert_regex_match!(
SatHtml {
address: None,
sat: Sat(0),
satpoint: None,
blocktime: Blocktime::confirmed(0),
Expand Down Expand Up @@ -61,6 +63,7 @@ mod tests {
fn last() {
assert_regex_match!(
SatHtml {
address: None,
sat: Sat(2099999997689999),
satpoint: None,
blocktime: Blocktime::confirmed(0),
Expand Down Expand Up @@ -98,6 +101,7 @@ mod tests {
fn sat_with_next_and_prev() {
assert_regex_match!(
SatHtml {
address: None,
sat: Sat(1),
satpoint: None,
blocktime: Blocktime::confirmed(0),
Expand All @@ -111,6 +115,7 @@ mod tests {
fn sat_with_inscription() {
assert_regex_match!(
SatHtml {
address: None,
sat: Sat(0),
satpoint: None,
blocktime: Blocktime::confirmed(0),
Expand All @@ -132,6 +137,7 @@ mod tests {
fn sat_with_reinscription() {
assert_regex_match!(
SatHtml {
address: None,
sat: Sat(0),
satpoint: None,
blocktime: Blocktime::confirmed(0),
Expand All @@ -154,6 +160,7 @@ mod tests {
fn last_sat_next_link_is_disabled() {
assert_regex_match!(
SatHtml {
address: None,
sat: Sat::LAST,
satpoint: None,
blocktime: Blocktime::confirmed(0),
Expand All @@ -167,6 +174,7 @@ mod tests {
fn sat_with_satpoint() {
assert_regex_match!(
SatHtml {
address: None,
sat: Sat(0),
satpoint: Some(satpoint(1, 0)),
blocktime: Blocktime::confirmed(0),
Expand All @@ -175,4 +183,18 @@ mod tests {
"<h1>Sat 0</h1>.*<dt>location</dt><dd><a class=collapse href=/satpoint/1{64}:1:0>1{64}:1:0</a></dd>.*",
);
}

#[test]
fn sat_with_address() {
assert_regex_match!(
SatHtml {
address: Some(address(0).to_string()),
sat: Sat(0),
satpoint: Some(satpoint(1, 0)),
blocktime: Blocktime::confirmed(0),
inscriptions: Vec::new(),
},
"<h1>Sat 0</h1>.*<dt>address</dt><dd class=monospace><a href=/address/bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4>bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4</a></dd>.*",
);
}
}
3 changes: 3 additions & 0 deletions templates/sat.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ <h1>Sat {{ self.sat.n() }}</h1>
%% if let Some(satpoint) = self.satpoint {
<dt>location</dt><dd><a class=collapse href=/satpoint/{{ satpoint }}>{{ satpoint }}</a></dd>
%% }
%% if let Some(address) = &self.address {
<dt>address</dt><dd class=monospace><a href=/address/{{address}}>{{ address }}</a></dd>
%% }
</dl>
<div class=center>
%% if self.sat.n() > 0 {
Expand Down
13 changes: 11 additions & 2 deletions tests/json_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn get_sat_without_sat_index() {
pretty_assert_eq!(
sat_json,
api::Sat {
address: None,
number: 2099999997689999,
decimal: "6929999.0".into(),
degree: "5°209999′1007″0‴".into(),
Expand Down Expand Up @@ -55,11 +56,15 @@ fn get_sat_with_inscription_and_sat_index() {

assert_eq!(response.status(), StatusCode::OK);

let sat_json: api::Sat = serde_json::from_str(&response.text().unwrap()).unwrap();
let mut sat_json: api::Sat = serde_json::from_str(&response.text().unwrap()).unwrap();

assert_regex_match!(sat_json.address.unwrap(), r"bc1p.*");
sat_json.address = None;

pretty_assert_eq!(
sat_json,
api::Sat {
address: None,
number: 50 * COIN_VALUE,
decimal: "1.0".into(),
degree: "0°1′1″0‴".into(),
Expand Down Expand Up @@ -111,11 +116,15 @@ fn get_sat_with_inscription_on_common_sat_and_more_inscriptions() {

assert_eq!(response.status(), StatusCode::OK);

let sat_json: api::Sat = serde_json::from_str(&response.text().unwrap()).unwrap();
let mut sat_json: api::Sat = serde_json::from_str(&response.text().unwrap()).unwrap();

assert_regex_match!(sat_json.address.unwrap(), r"bc1p.*");
sat_json.address = None;

pretty_assert_eq!(
sat_json,
api::Sat {
address: None,
number: 3 * 50 * COIN_VALUE + 1,
decimal: "3.1".into(),
degree: "0°3′3″1‴".into(),
Expand Down

0 comments on commit 6a9b3bd

Please sign in to comment.