Skip to content

Commit

Permalink
Handle errors when retrieving sat address (ordinals#4094)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Nov 21, 2024
1 parent 4695b2b commit 79f7c8e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
55 changes: 28 additions & 27 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,54 +570,55 @@ impl Server {

let charms = sat.charms();

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

if outpoint == unbound_outpoint() {
let address = if let Some(satpoint) = satpoint {
if satpoint.outpoint == unbound_outpoint() {
None
} else {
index
.get_transaction(outpoint.txid)
let tx = index
.get_transaction(satpoint.outpoint.txid)?
.context("could not get transaction for sat")?;

let tx_out = tx
.output
.get::<usize>(satpoint.outpoint.vout.try_into().unwrap())
.context("could not get vout for sat")?;

server_config
.chain
.address_from_script(&tx_out.script_pubkey)
.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())
})
}
});
} else {
None
};

Ok(if accept_json {
Json(api::Sat {
address,
number: sat.0,
decimal: sat.decimal().to_string(),
degree: sat.degree().to_string(),
name: sat.name(),
address: address.map(|address| address.to_string()),
block: sat.height().0,
charms: Charm::charms(charms),
cycle: sat.cycle(),
decimal: sat.decimal().to_string(),
degree: sat.degree().to_string(),
epoch: sat.epoch().0,
period: sat.period(),
inscriptions,
name: sat.name(),
number: sat.0,
offset: sat.third(),
rarity: sat.rarity(),
percentile: sat.percentile(),
period: sat.period(),
rarity: sat.rarity(),
satpoint,
timestamp: blocktime.timestamp().timestamp(),
inscriptions,
charms: Charm::charms(charms),
})
.into_response()
} else {
SatHtml {
address,
sat,
satpoint,
blocktime,
inscriptions,
sat,
satpoint,
}
.page(server_config)
.into_response()
Expand Down
4 changes: 2 additions & 2 deletions src/templates/sat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;

#[derive(Boilerplate)]
pub(crate) struct SatHtml {
pub(crate) address: Option<String>,
pub(crate) address: Option<Address>,
pub(crate) blocktime: Blocktime,
pub(crate) inscriptions: Vec<InscriptionId>,
pub(crate) sat: Sat,
Expand Down Expand Up @@ -188,7 +188,7 @@ mod tests {
fn sat_with_address() {
assert_regex_match!(
SatHtml {
address: Some(address(0).to_string()),
address: Some(address(0)),
sat: Sat(0),
satpoint: Some(satpoint(1, 0)),
blocktime: Blocktime::confirmed(0),
Expand Down

0 comments on commit 79f7c8e

Please sign in to comment.