Skip to content

Commit

Permalink
Return address we sent to for onchain
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Mar 21, 2024
1 parent e0c8443 commit 6fa7633
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ async fn onchain_handler(
Extension(state): Extension<AppState>,
Json(payload): Json<OnchainRequest>,
) -> Result<Json<OnchainResponse>, AppError> {
let txid = pay_onchain(state, payload.clone()).await?;
let res = pay_onchain(state, payload).await?;

Ok(Json(OnchainResponse { txid }))
Ok(Json(res))
}

#[axum::debug_handler]
Expand Down
32 changes: 16 additions & 16 deletions src/onchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ pub struct OnchainRequest {
#[derive(Clone, Serialize)]
pub struct OnchainResponse {
pub txid: String,
pub address: String,
}

pub async fn pay_onchain(state: AppState, payload: OnchainRequest) -> anyhow::Result<String> {
pub async fn pay_onchain(
state: AppState,
payload: OnchainRequest,
) -> anyhow::Result<OnchainResponse> {
if payload.sats > MAX_SEND_AMOUNT {
anyhow::bail!("max amount is 10,000,000");
}

let txid = {
let res = {
let network = state.network;

// need to convert from different rust-bitcoin versions
Expand All @@ -34,7 +38,7 @@ pub async fn pay_onchain(state: AppState, payload: OnchainRequest) -> anyhow::Re
let address =
Address::from_str(&address_str.to_string()).map_err(|e| anyhow::anyhow!(e))?;

let address = if address.is_valid_for_network(network) {
let address = if let Ok(address) = address.require_network(network) {
address
} else {
anyhow::bail!(
Expand All @@ -47,19 +51,15 @@ pub async fn pay_onchain(state: AppState, payload: OnchainRequest) -> anyhow::Re

let amount = Amount::from_sat(payload.sats);

task::block_in_place(|| {
bitcoin_client.send_to_address(
&address.assume_checked(), // we just checked it above,
amount,
None,
None,
None,
None,
None,
None,
)
})?
let txid = task::block_in_place(|| {
bitcoin_client.send_to_address(&address, amount, None, None, None, None, None, None)
})?;

OnchainResponse {
txid: txid.to_string(),
address: address.to_string(),
}
};

Ok(txid.to_string())
Ok(res)
}

0 comments on commit 6fa7633

Please sign in to comment.