Skip to content

Commit

Permalink
Add bulk /txs/outspends endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mononaut committed Aug 15, 2023
1 parent 572a1ec commit 3858f5a
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,38 @@ fn handle_request(
.map_err(|err| HttpError::from(err.description().to_string()))?;
http_message(StatusCode::OK, txid.to_hex(), 0)
}
(&Method::GET, Some(&"txs"), Some(&"outspends"), None, None, None) => {
let txid_strings: Vec<&str> = query_params
.get("txids")
.ok_or(HttpError::from("No txids specified".to_string()))?
.as_str()
.split(',')
.collect();

if txid_strings.len() > 50 {
return http_message(StatusCode::BAD_REQUEST, "Too many txids requested", 0);
}

let spends: Vec<Vec<SpendingValue>> = txid_strings
.into_iter()
.map(|txid_str| {
Txid::from_hex(txid_str)
.ok()
.and_then(|txid| query.lookup_txn(&txid))
.map_or_else(Vec::new, |tx| {
query
.lookup_tx_spends(tx)
.into_iter()
.map(|spend| {
spend.map_or_else(SpendingValue::default, SpendingValue::from)
})
.collect()
})
})
.collect();

json_response(spends, TTL_SHORT)
}

(&Method::GET, Some(&"mempool"), None, None, None, None) => {
json_response(query.mempool().backlog_stats(), TTL_SHORT)
Expand Down

0 comments on commit 3858f5a

Please sign in to comment.