Skip to content

Commit b5788a7

Browse files
authored
Add wallet batch outputs and inscriptions endpoints (#3456)
1 parent c749c47 commit b5788a7

File tree

6 files changed

+405
-388
lines changed

6 files changed

+405
-388
lines changed

src/api.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,25 +136,25 @@ impl Output {
136136
chain: Chain,
137137
inscriptions: Vec<InscriptionId>,
138138
outpoint: OutPoint,
139-
output: TxOut,
139+
tx_out: TxOut,
140140
indexed: bool,
141141
runes: Vec<(SpacedRune, Pile)>,
142142
sat_ranges: Option<Vec<(u64, u64)>>,
143143
spent: bool,
144144
) -> Self {
145145
Self {
146146
address: chain
147-
.address_from_script(&output.script_pubkey)
147+
.address_from_script(&tx_out.script_pubkey)
148148
.ok()
149149
.map(|address| uncheck(&address)),
150150
indexed,
151151
inscriptions,
152152
runes,
153153
sat_ranges,
154-
script_pubkey: output.script_pubkey.to_asm_string(),
154+
script_pubkey: tx_out.script_pubkey.to_asm_string(),
155155
spent,
156156
transaction: outpoint.txid.to_string(),
157-
value: output.value,
157+
value: tx_out.value,
158158
}
159159
}
160160
}

src/index.rs

Lines changed: 102 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,6 @@ pub(crate) struct TransactionInfo {
157157
pub(crate) starting_timestamp: u128,
158158
}
159159

160-
pub(crate) struct InscriptionInfo {
161-
pub(crate) children: Vec<InscriptionId>,
162-
pub(crate) entry: InscriptionEntry,
163-
pub(crate) parents: Vec<InscriptionId>,
164-
pub(crate) output: Option<TxOut>,
165-
pub(crate) satpoint: SatPoint,
166-
pub(crate) inscription: Inscription,
167-
pub(crate) previous: Option<InscriptionId>,
168-
pub(crate) next: Option<InscriptionId>,
169-
pub(crate) rune: Option<SpacedRune>,
170-
pub(crate) charms: u16,
171-
}
172-
173160
pub(crate) trait BitcoinCoreRpcResultExt<T> {
174161
fn into_option(self) -> Result<Option<T>>;
175162
}
@@ -1801,15 +1788,11 @@ impl Index {
18011788
)
18021789
}
18031790

1804-
pub fn inscription_info_benchmark(index: &Index, inscription_number: i32) {
1805-
Self::inscription_info(index, query::Inscription::Number(inscription_number)).unwrap();
1806-
}
1807-
18081791
pub(crate) fn inscription_info(
1809-
index: &Index,
1792+
&self,
18101793
query: query::Inscription,
1811-
) -> Result<Option<InscriptionInfo>> {
1812-
let rtx = index.database.begin_read()?;
1794+
) -> Result<Option<(api::Inscription, Option<TxOut>, Inscription)>> {
1795+
let rtx = self.database.begin_read()?;
18131796

18141797
let sequence_number = match query {
18151798
query::Inscription::Id(id) => rtx
@@ -1842,7 +1825,7 @@ impl Index {
18421825
.value(),
18431826
);
18441827

1845-
let Some(transaction) = index.get_transaction(entry.id.txid)? else {
1828+
let Some(transaction) = self.get_transaction(entry.id.txid)? else {
18461829
return Ok(None);
18471830
};
18481831

@@ -1866,7 +1849,7 @@ impl Index {
18661849
{
18671850
None
18681851
} else {
1869-
let Some(transaction) = index.get_transaction(satpoint.outpoint.txid)? else {
1852+
let Some(transaction) = self.get_transaction(satpoint.outpoint.txid)? else {
18701853
return Ok(None);
18711854
};
18721855

@@ -1943,18 +1926,50 @@ impl Index {
19431926
Charm::Lost.set(&mut charms);
19441927
}
19451928

1946-
Ok(Some(InscriptionInfo {
1947-
children,
1948-
entry,
1949-
parents,
1929+
let effective_mime_type = if let Some(delegate_id) = inscription.delegate() {
1930+
let delegate_result = self.get_inscription_by_id(delegate_id);
1931+
if let Ok(Some(delegate)) = delegate_result {
1932+
delegate.content_type().map(str::to_string)
1933+
} else {
1934+
inscription.content_type().map(str::to_string)
1935+
}
1936+
} else {
1937+
inscription.content_type().map(str::to_string)
1938+
};
1939+
1940+
Ok(Some((
1941+
api::Inscription {
1942+
address: output
1943+
.as_ref()
1944+
.and_then(|o| {
1945+
self
1946+
.settings
1947+
.chain()
1948+
.address_from_script(&o.script_pubkey)
1949+
.ok()
1950+
})
1951+
.map(|address| address.to_string()),
1952+
charms: Charm::charms(charms),
1953+
children,
1954+
content_length: inscription.content_length(),
1955+
content_type: inscription.content_type().map(|s| s.to_string()),
1956+
effective_content_type: effective_mime_type,
1957+
fee: entry.fee,
1958+
height: entry.height,
1959+
id: entry.id,
1960+
next,
1961+
number: entry.inscription_number,
1962+
parents,
1963+
previous,
1964+
rune,
1965+
sat: entry.sat,
1966+
satpoint,
1967+
timestamp: timestamp(entry.timestamp.into()).timestamp(),
1968+
value: output.as_ref().map(|o| o.value),
1969+
},
19501970
output,
1951-
satpoint,
19521971
inscription,
1953-
previous,
1954-
next,
1955-
rune,
1956-
charms,
1957-
}))
1972+
)))
19581973
}
19591974

19601975
pub(crate) fn get_inscription_entry(
@@ -2104,6 +2119,61 @@ impl Index {
21042119
.collect(),
21052120
)
21062121
}
2122+
2123+
pub(crate) fn get_output_info(&self, outpoint: OutPoint) -> Result<Option<(api::Output, TxOut)>> {
2124+
let sat_ranges = self.list(outpoint)?;
2125+
2126+
let indexed;
2127+
2128+
let txout = if outpoint == OutPoint::null() || outpoint == unbound_outpoint() {
2129+
let mut value = 0;
2130+
2131+
if let Some(ranges) = &sat_ranges {
2132+
for (start, end) in ranges {
2133+
value += end - start;
2134+
}
2135+
}
2136+
2137+
indexed = true;
2138+
2139+
TxOut {
2140+
value,
2141+
script_pubkey: ScriptBuf::new(),
2142+
}
2143+
} else {
2144+
indexed = self.contains_output(&outpoint)?;
2145+
2146+
let Some(tx) = self.get_transaction(outpoint.txid)? else {
2147+
return Ok(None);
2148+
};
2149+
2150+
let Some(output) = tx.output.into_iter().nth(outpoint.vout as usize) else {
2151+
return Ok(None);
2152+
};
2153+
2154+
output
2155+
};
2156+
2157+
let inscriptions = self.get_inscriptions_on_output(outpoint)?;
2158+
2159+
let runes = self.get_rune_balances_for_outpoint(outpoint)?;
2160+
2161+
let spent = self.is_output_spent(outpoint)?;
2162+
2163+
Ok(Some((
2164+
api::Output::new(
2165+
self.settings.chain(),
2166+
inscriptions,
2167+
outpoint,
2168+
txout.clone(),
2169+
indexed,
2170+
runes,
2171+
sat_ranges,
2172+
spent,
2173+
),
2174+
txout,
2175+
)))
2176+
}
21072177
}
21082178

21092179
#[cfg(test)]

0 commit comments

Comments
 (0)