From a427c270565f892d980f1861165f710885787e52 Mon Sep 17 00:00:00 2001 From: patrini32 <171664803+patrini32@users.noreply.github.com> Date: Wed, 5 Jun 2024 13:13:21 +0300 Subject: [PATCH] use original approach and add timelock check --- swap/src/cli/cancel_and_refund.rs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/swap/src/cli/cancel_and_refund.rs b/swap/src/cli/cancel_and_refund.rs index c61bea45..f370b9c0 100644 --- a/swap/src/cli/cancel_and_refund.rs +++ b/swap/src/cli/cancel_and_refund.rs @@ -117,21 +117,25 @@ pub async fn refund( match state6.publish_refund_btc(bitcoin_wallet.as_ref()).await { Ok(_) => (), - Err(refund_error) => { - if refund_error - .to_string() - .contains("Failed to broadcast Bitcoin refund transaction") - { - let state = BobState::BtcPunished { - tx_lock_id: state6.tx_lock_id(), - }; - db.insert_latest_state(swap_id, state.clone().into()) - .await?; + Err(error) => { + if let Ok(error_code) = parse_rpc_error_code(&error) { + if error_code == i64::from(RpcErrorCode::RpcVerifyError) { + if let ExpiredTimelocks::None { .. } = + state6.expired_timelock(bitcoin_wallet.as_ref()).await? + { + bail!(error); + } else { + let state = BobState::BtcPunished { + tx_lock_id: state6.tx_lock_id(), + }; + db.insert_latest_state(swap_id, state.clone().into()) + .await?; - tracing::info!("Cannot refund because BTC is punished by Alice."); - return Ok(state); + tracing::info!("Cannot refund because BTC is punished by Alice."); + return Ok(state); + } + } } - bail!(refund_error); } } let state = BobState::BtcRefunded(state6);