Skip to content

Commit

Permalink
Add /txs endpoint to submit tx packages
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenroose committed Sep 25, 2024
1 parent 3bb331d commit b6e1aa7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,19 @@ impl Daemon {
)
}

pub fn broadcast_pkg_raw(&self, txshex: &[String]) -> Result<Vec<Txid>> {
let res = self.request("submitpackage", json!([txshex]))?;
res.as_object().chain_err(|| "no object")?
.get("tx-results").chain_err(|| "missing key tx-results")?
.as_object().chain_err(|| "tx-results not an object")?
.into_iter().map(|(_, tx_ret)| tx_ret
.as_object().chain_err(|| "tx result not an object")?
.get("txid").chain_err(|| "missing txid field")?
.as_str().chain_err(|| "txid not a string")?
.parse().chain_err(|| "invalid txid")
).collect()
}

// Get estimated feerates for the provided confirmation targets using a batch RPC request
// Missing estimates are logged but do not cause a failure, whatever is available is returned
#[allow(clippy::float_cmp)]
Expand Down
8 changes: 8 additions & 0 deletions src/new_index/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ impl Query {
Ok(txid)
}

pub fn broadcast_pkg_raw(&self, txshex: &[String]) -> Result<Vec<Txid>> {
let txids = self.daemon.broadcast_pkg_raw(txshex)?;
for txid in &txids {
self.mempool.write().unwrap().add_by_txid(&self.daemon, &txid);
}
Ok(txids)
}

pub fn utxo(&self, scripthash: &[u8]) -> Result<Vec<Utxo>> {
let mut utxos = self.chain.utxo(scripthash, self.config.utxos_limit)?;
let mempool = self.mempool();
Expand Down
8 changes: 8 additions & 0 deletions src/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,14 @@ fn handle_request(
.map_err(|err| HttpError::from(err.description().to_string()))?;
http_message(StatusCode::OK, txid.to_string(), 0)
}
(&Method::POST, Some(&"txs"), None, None, None, None) => {
let txshex = serde_json::from_slice::<Vec<String>>(&body[..]).map_err(|_| {
HttpError(StatusCode::BAD_REQUEST, "expected array of hexes".into())
})?;
let txids = query.broadcast_pkg_raw(&txshex)
.map_err(|err| HttpError::from(err.description().to_string()))?;
http_message(StatusCode::OK, serde_json::to_vec(&txids)?, 0)
}

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

0 comments on commit b6e1aa7

Please sign in to comment.