use crate::database::{Database, Swap}; use crate::protocol::alice::AliceState; use anyhow::{bail, Result}; use std::sync::Arc; use uuid::Uuid; pub async fn safely_abort(swap_id: Uuid, db: Arc) -> Result { let state = db.get_state(swap_id)?.try_into_alice()?.into(); match state { AliceState::Started { .. } | AliceState::BtcLocked { .. } => { let state = AliceState::SafelyAborted; let db_state = (&state).into(); db.insert_latest_state(swap_id, Swap::Alice(db_state)) .await?; Ok(state) } AliceState::XmrLockTransactionSent { .. } | AliceState::XmrLocked { .. } | AliceState::XmrLockTransferProofSent { .. } | AliceState::EncSigLearned { .. } | AliceState::CancelTimelockExpired { .. } | AliceState::BtcCancelled { .. } | AliceState::BtcRefunded { .. } | AliceState::BtcPunishable { .. } | AliceState::BtcRedeemed | AliceState::XmrRefunded | AliceState::BtcPunished | AliceState::SafelyAborted => bail!( "Cannot safely abort swap {} because it is in state {} which cannot be safely aborted", swap_id, state ), } }