diff --git a/daemon/src/db.rs b/daemon/src/db.rs index ffa6659..454415e 100644 --- a/daemon/src/db.rs +++ b/daemon/src/db.rs @@ -493,7 +493,6 @@ pub async fn load_cfds_by_oracle_event_id( #[cfg(test)] mod tests { use std::fs::File; - use std::time::SystemTime; use pretty_assertions::assert_eq; use rust_decimal_macros::dec; @@ -503,7 +502,7 @@ mod tests { use time::OffsetDateTime; use crate::db::insert_order; - use crate::model::cfd::{Cfd, CfdState, CfdStateCommon, Order}; + use crate::model::cfd::{Cfd, CfdState, Order}; use crate::model::Usd; use super::*; @@ -591,9 +590,7 @@ mod tests { let mut cfd_1 = Cfd::dummy().insert(&mut conn).await; - cfd_1.state = CfdState::Accepted { - common: CfdStateCommon::default(), - }; + cfd_1.state = CfdState::accepted(); append_cfd_state(&cfd_1, &mut conn).await.unwrap(); let cfds_from_db = load_all_cfds(&mut conn).await.unwrap(); @@ -604,9 +601,7 @@ mod tests { let cfds_from_db = load_all_cfds(&mut conn).await.unwrap(); assert_eq!(vec![cfd_1.clone(), cfd_2.clone()], cfds_from_db); - cfd_2.state = CfdState::Rejected { - common: CfdStateCommon::default(), - }; + cfd_2.state = CfdState::rejected(); append_cfd_state(&cfd_2, &mut conn).await.unwrap(); let cfds_from_db = load_all_cfds(&mut conn).await.unwrap(); @@ -643,11 +638,7 @@ mod tests { Cfd::new( Order::dummy(), Usd(dec!(1000)), - CfdState::OutgoingOrderRequest { - common: CfdStateCommon { - transition_timestamp: SystemTime::now(), - }, - }, + CfdState::outgoing_order_request(), ) } diff --git a/daemon/src/housekeeping.rs b/daemon/src/housekeeping.rs index cca0d32..32056bc 100644 --- a/daemon/src/housekeeping.rs +++ b/daemon/src/housekeeping.rs @@ -1,5 +1,5 @@ use crate::db::{append_cfd_state, load_all_cfds}; -use crate::model::cfd::{Cfd, CfdState, CfdStateCommon}; +use crate::model::cfd::{Cfd, CfdState}; use crate::wallet::Wallet; use anyhow::Result; use sqlx::pool::PoolConnection; @@ -11,10 +11,10 @@ pub async fn transition_non_continue_cfds_to_setup_failed( let mut cfds = load_all_cfds(conn).await?; for cfd in cfds.iter_mut().filter(|cfd| Cfd::is_cleanup(cfd)) { - cfd.state = CfdState::SetupFailed { - common: CfdStateCommon::default(), - info: format!("Was in state {} which cannot be continued.", cfd.state), - }; + cfd.state = CfdState::setup_failed(format!( + "Was in state {} which cannot be continued.", + cfd.state + )); append_cfd_state(cfd, conn).await?; } diff --git a/daemon/src/maker_cfd.rs b/daemon/src/maker_cfd.rs index 83b2300..b61f22f 100644 --- a/daemon/src/maker_cfd.rs +++ b/daemon/src/maker_cfd.rs @@ -563,9 +563,7 @@ impl Actor { .with_context(|| format!("Announcement {} not found", cfd.order.oracle_event_id))?; // 3. Insert that we are in contract setup and refresh our own feed - cfd.state = CfdState::ContractSetup { - common: CfdStateCommon::default(), - }; + cfd.state = CfdState::contract_setup(); append_cfd_state(&cfd, &mut conn, &self.cfd_feed_actor_inbox).await?; @@ -649,10 +647,7 @@ impl Actor { mut conn: PoolConnection, ) -> Result<()> { // Update order in db - cfd.state = CfdState::Rejected { - common: CfdStateCommon::default(), - }; - + cfd.state = CfdState::rejected(); append_cfd_state(&cfd, &mut conn, &self.cfd_feed_actor_inbox).await?; self.takers diff --git a/daemon/src/model/cfd.rs b/daemon/src/model/cfd.rs index 47e9a1d..f2f64db 100644 --- a/daemon/src/model/cfd.rs +++ b/daemon/src/model/cfd.rs @@ -293,6 +293,60 @@ pub enum CfdState { }, } +impl CfdState { + pub fn outgoing_order_request() -> Self { + Self::OutgoingOrderRequest { + common: CfdStateCommon::default(), + } + } + + pub fn accepted() -> Self { + Self::Accepted { + common: CfdStateCommon::default(), + } + } + + pub fn rejected() -> Self { + Self::Rejected { + common: CfdStateCommon::default(), + } + } + + pub fn contract_setup() -> Self { + Self::ContractSetup { + common: CfdStateCommon::default(), + } + } + + pub fn closed(payout: Payout) -> Self { + Self::Closed { + common: CfdStateCommon::default(), + payout, + } + } + + pub fn must_refund(dlc: Dlc) -> Self { + Self::MustRefund { + common: CfdStateCommon::default(), + dlc, + } + } + + pub fn refunded(dlc: Dlc) -> Self { + Self::Refunded { + common: CfdStateCommon::default(), + dlc, + } + } + + pub fn setup_failed(info: String) -> Self { + Self::SetupFailed { + common: CfdStateCommon::default(), + info, + } + } +} + #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub enum Payout { CollaborativeClose(CollaborativeSettlement), @@ -718,12 +772,7 @@ impl Cfd { monitor::Event::CloseFinality(_) => { let collaborative_close = self.collaborative_close().context("No collaborative close after reaching collaborative close finality")?; - CfdState::Closed { - common: CfdStateCommon { - transition_timestamp: SystemTime::now(), - }, - payout: Payout::CollaborativeClose(collaborative_close) - } + CfdState::closed(Payout::CollaborativeClose(collaborative_close)) }, monitor::Event::CetTimelockExpired(_) => match self.state.clone() { @@ -788,36 +837,21 @@ impl Cfd { ) }; - MustRefund { - common: CfdStateCommon { - transition_timestamp: SystemTime::now(), - }, - dlc, - } + CfdState::must_refund(dlc) } monitor::Event::RefundFinality(_) => { let dlc = self .dlc() .context("No dlc available when reaching refund finality")?; - Refunded { - common: CfdStateCommon { - transition_timestamp: SystemTime::now(), - }, - dlc, - } + CfdState::refunded(dlc) } monitor::Event::CetFinality(_) => { let attestation = self .attestation() .context("No attestation available when reaching CET finality")?; - CfdState::Closed { - common: CfdStateCommon { - transition_timestamp: SystemTime::now(), - }, - payout: Payout::Cet(attestation) - } + CfdState::closed(Payout::Cet(attestation)) } monitor::Event::RevokedTransactionFound(_) => { todo!("Punish bad counterparty") diff --git a/daemon/src/taker_cfd.rs b/daemon/src/taker_cfd.rs index efd4814..1710b18 100644 --- a/daemon/src/taker_cfd.rs +++ b/daemon/src/taker_cfd.rs @@ -149,11 +149,7 @@ impl Actor { let cfd = Cfd::new( current_order.clone(), quantity, - CfdState::OutgoingOrderRequest { - common: CfdStateCommon { - transition_timestamp: SystemTime::now(), - }, - }, + CfdState::outgoing_order_request(), ); insert_cfd(&cfd, &mut conn, &self.cfd_feed_actor_inbox).await?; @@ -269,9 +265,7 @@ impl Actor { let mut conn = self.db.acquire().await?; let mut cfd = load_cfd_by_order_id(order_id, &mut conn).await?; - cfd.state = CfdState::ContractSetup { - common: CfdStateCommon::default(), - }; + cfd.state = CfdState::contract_setup(); append_cfd_state(&cfd, &mut conn, &self.cfd_feed_actor_inbox).await?; @@ -320,9 +314,7 @@ impl Actor { let mut conn = self.db.acquire().await?; let mut cfd = load_cfd_by_order_id(order_id, &mut conn).await?; - cfd.state = CfdState::Rejected { - common: CfdStateCommon::default(), - }; + cfd.state = CfdState::rejected(); append_cfd_state(&cfd, &mut conn, &self.cfd_feed_actor_inbox).await?;