From a5011eb810821b85cb184b31ed428c3ef8f12f47 Mon Sep 17 00:00:00 2001 From: Masaki Muranaka Date: Tue, 28 Mar 2023 11:11:15 +0900 Subject: [PATCH] Add /wallet/fail_transfer API endpoint. This will fix diamondhands-dev/shiro-backend#60 . Signed-off-by: Masaki Muranaka --- src/wallet.rs | 1 + src/wallet/fail_transfers.rs | 54 ++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/wallet/fail_transfers.rs diff --git a/src/wallet.rs b/src/wallet.rs index 7fab00c..317fd1b 100644 --- a/src/wallet.rs +++ b/src/wallet.rs @@ -11,6 +11,7 @@ pub mod blind; pub mod data; pub mod dir; pub mod drain_to; +pub mod fail_transfers; pub mod go_online; pub mod invoice; pub mod issue; diff --git a/src/wallet/fail_transfers.rs b/src/wallet/fail_transfers.rs new file mode 100644 index 0000000..8f90721 --- /dev/null +++ b/src/wallet/fail_transfers.rs @@ -0,0 +1,54 @@ +use crate::ShiroWallet; +use actix_web::{put, web, HttpResponse, Responder}; +use serde::Deserialize; +use serde::Serialize; +use std::sync::Mutex; + +#[derive(Deserialize, Serialize)] +pub struct FailTransfersParams { + blinded_utxo: Option, + txid: Option, + no_asset_only: bool, +} + +#[derive(Deserialize, Serialize)] +pub struct FailTransersResult { + transfer_changed: bool, +} + +#[put("/wallet/fail_transifers")] +pub async fn put( + params: web::Json, + data: web::Data>, +) -> impl Responder { + if data.lock().unwrap().wallet.is_some() { + match actix_web::rt::task::spawn_blocking(move || { + let mut shiro_wallet = data.lock().unwrap(); + let online = shiro_wallet.get_online().unwrap(); + shiro_wallet.wallet.as_mut().unwrap().fail_transfers( + online, + params.blinded_utxo.clone(), + params.txid.clone(), + params.no_asset_only, + ) + }) + .await + .unwrap() + { + Ok(transfer_changed) => { + HttpResponse::Ok().json(FailTransersResult { transfer_changed }) + } + Err(e) => HttpResponse::BadRequest().body(e.to_string()), + } + } else { + HttpResponse::BadRequest().body("wallet should be created first") + } +} + +#[cfg(test)] +mod tests { + #[actix_web::test] + async fn test_put() { + //TODO: implement + } +}