Skip to content

Commit

Permalink
Add wallet batch outputs and inscriptions endpoints (ordinals#3456)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Apr 9, 2024
1 parent 82fba21 commit 7578c99
Show file tree
Hide file tree
Showing 6 changed files with 405 additions and 388 deletions.
8 changes: 4 additions & 4 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,25 +136,25 @@ impl Output {
chain: Chain,
inscriptions: Vec<InscriptionId>,
outpoint: OutPoint,
output: TxOut,
tx_out: TxOut,
indexed: bool,
runes: Vec<(SpacedRune, Pile)>,
sat_ranges: Option<Vec<(u64, u64)>>,
spent: bool,
) -> Self {
Self {
address: chain
.address_from_script(&output.script_pubkey)
.address_from_script(&tx_out.script_pubkey)
.ok()
.map(|address| uncheck(&address)),
indexed,
inscriptions,
runes,
sat_ranges,
script_pubkey: output.script_pubkey.to_asm_string(),
script_pubkey: tx_out.script_pubkey.to_asm_string(),
spent,
transaction: outpoint.txid.to_string(),
value: output.value,
value: tx_out.value,
}
}
}
Expand Down
134 changes: 102 additions & 32 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,6 @@ pub(crate) struct TransactionInfo {
pub(crate) starting_timestamp: u128,
}

pub(crate) struct InscriptionInfo {
pub(crate) children: Vec<InscriptionId>,
pub(crate) entry: InscriptionEntry,
pub(crate) parents: Vec<InscriptionId>,
pub(crate) output: Option<TxOut>,
pub(crate) satpoint: SatPoint,
pub(crate) inscription: Inscription,
pub(crate) previous: Option<InscriptionId>,
pub(crate) next: Option<InscriptionId>,
pub(crate) rune: Option<SpacedRune>,
pub(crate) charms: u16,
}

pub(crate) trait BitcoinCoreRpcResultExt<T> {
fn into_option(self) -> Result<Option<T>>;
}
Expand Down Expand Up @@ -1801,15 +1788,11 @@ impl Index {
)
}

pub fn inscription_info_benchmark(index: &Index, inscription_number: i32) {
Self::inscription_info(index, query::Inscription::Number(inscription_number)).unwrap();
}

pub(crate) fn inscription_info(
index: &Index,
&self,
query: query::Inscription,
) -> Result<Option<InscriptionInfo>> {
let rtx = index.database.begin_read()?;
) -> Result<Option<(api::Inscription, Option<TxOut>, Inscription)>> {
let rtx = self.database.begin_read()?;

let sequence_number = match query {
query::Inscription::Id(id) => rtx
Expand Down Expand Up @@ -1842,7 +1825,7 @@ impl Index {
.value(),
);

let Some(transaction) = index.get_transaction(entry.id.txid)? else {
let Some(transaction) = self.get_transaction(entry.id.txid)? else {
return Ok(None);
};

Expand All @@ -1866,7 +1849,7 @@ impl Index {
{
None
} else {
let Some(transaction) = index.get_transaction(satpoint.outpoint.txid)? else {
let Some(transaction) = self.get_transaction(satpoint.outpoint.txid)? else {
return Ok(None);
};

Expand Down Expand Up @@ -1943,18 +1926,50 @@ impl Index {
Charm::Lost.set(&mut charms);
}

Ok(Some(InscriptionInfo {
children,
entry,
parents,
let effective_mime_type = if let Some(delegate_id) = inscription.delegate() {
let delegate_result = self.get_inscription_by_id(delegate_id);
if let Ok(Some(delegate)) = delegate_result {
delegate.content_type().map(str::to_string)
} else {
inscription.content_type().map(str::to_string)
}
} else {
inscription.content_type().map(str::to_string)
};

Ok(Some((
api::Inscription {
address: output
.as_ref()
.and_then(|o| {
self
.settings
.chain()
.address_from_script(&o.script_pubkey)
.ok()
})
.map(|address| address.to_string()),
charms: Charm::charms(charms),
children,
content_length: inscription.content_length(),
content_type: inscription.content_type().map(|s| s.to_string()),
effective_content_type: effective_mime_type,
fee: entry.fee,
height: entry.height,
id: entry.id,
next,
number: entry.inscription_number,
parents,
previous,
rune,
sat: entry.sat,
satpoint,
timestamp: timestamp(entry.timestamp.into()).timestamp(),
value: output.as_ref().map(|o| o.value),
},
output,
satpoint,
inscription,
previous,
next,
rune,
charms,
}))
)))
}

pub(crate) fn get_inscription_entry(
Expand Down Expand Up @@ -2104,6 +2119,61 @@ impl Index {
.collect(),
)
}

pub(crate) fn get_output_info(&self, outpoint: OutPoint) -> Result<Option<(api::Output, TxOut)>> {
let sat_ranges = self.list(outpoint)?;

let indexed;

let txout = if outpoint == OutPoint::null() || outpoint == unbound_outpoint() {
let mut value = 0;

if let Some(ranges) = &sat_ranges {
for (start, end) in ranges {
value += end - start;
}
}

indexed = true;

TxOut {
value,
script_pubkey: ScriptBuf::new(),
}
} else {
indexed = self.contains_output(&outpoint)?;

let Some(tx) = self.get_transaction(outpoint.txid)? else {
return Ok(None);
};

let Some(output) = tx.output.into_iter().nth(outpoint.vout as usize) else {
return Ok(None);
};

output
};

let inscriptions = self.get_inscriptions_on_output(outpoint)?;

let runes = self.get_rune_balances_for_outpoint(outpoint)?;

let spent = self.is_output_spent(outpoint)?;

Ok(Some((
api::Output::new(
self.settings.chain(),
inscriptions,
outpoint,
txout.clone(),
indexed,
runes,
sat_ranges,
spent,
),
txout,
)))
}
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit 7578c99

Please sign in to comment.