Skip to content

Commit

Permalink
fix: set initial pulled state to Recoverable
Browse files Browse the repository at this point in the history
  • Loading branch information
hydra-yse committed Dec 5, 2024
1 parent c0d3732 commit 79b3d8d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 deletions.
5 changes: 5 additions & 0 deletions lib/core/src/chain_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,11 @@ impl ChainSwapHandler {
to_state: PaymentState,
) -> Result<(), PaymentError> {
match (from_state, to_state) {
(Recoverable, Pending | Refundable | RefundPending | Failed | Complete) => Ok(()),
(_, Recoverable) => Err(PaymentError::Generic {
err: format!("Cannot transition from {from_state:?} to Recoverable state"),
}),

(_, Created) => Err(PaymentError::Generic {
err: "Cannot transition to Created state".to_string(),
}),
Expand Down
6 changes: 6 additions & 0 deletions lib/core/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,12 @@ pub enum PaymentState {
///
/// When the refund tx is broadcast, `refund_tx_id` is set in the swap.
RefundPending = 6,

/// ## Recoverable Swaps
///
/// The status for swaps that have been synced in, and whose information is recoverable from
/// chain
Recoverable = 7,
}
impl ToSql for PaymentState {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
Expand Down
6 changes: 3 additions & 3 deletions lib/core/src/persist/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ impl Persister {
:description,
:state
)",
[params, &[(":state", &PaymentState::Created)]]
[params, &[(":state", &PaymentState::Recoverable)]]
.concat()
.as_slice(),
)?;
Expand Down Expand Up @@ -572,7 +572,7 @@ impl Persister {
:description,
:state
)",
[params, &[(":state", &PaymentState::Created)]]
[params, &[(":state", &PaymentState::Recoverable)]]
.concat()
.as_slice(),
)?;
Expand Down Expand Up @@ -684,7 +684,7 @@ impl Persister {
:server_lockup_tx_id,
:state
)",
[params, &[(":state", &PaymentState::Created)]]
[params, &[(":state", &PaymentState::Recoverable)]]
.concat()
.as_slice(),
)?;
Expand Down
9 changes: 6 additions & 3 deletions lib/core/src/receive_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ use lwk_wollet::hashes::hex::DisplayHex;
use tokio::sync::{broadcast, Mutex};

use crate::chain::liquid::LiquidChainService;
use crate::model::PaymentState::{
Complete, Created, Failed, Pending, RefundPending, Refundable, TimedOut,
};
use crate::model::PaymentState::*;
use crate::model::{Config, PaymentTxData, PaymentType, ReceiveSwap};
use crate::prelude::{Swap, Transaction};
use crate::{ensure_sdk, utils};
Expand Down Expand Up @@ -372,6 +370,11 @@ impl ReceiveSwapHandler {
to_state: PaymentState,
) -> Result<(), PaymentError> {
match (from_state, to_state) {
(Recoverable, Pending | Refundable | RefundPending | Failed | Complete) => Ok(()),
(_, Recoverable) => Err(PaymentError::Generic {
err: format!("Cannot transition from {from_state:?} to Recoverable state"),
}),

(_, Created) => Err(PaymentError::Generic {
err: "Cannot transition to Created state".to_string(),
}),
Expand Down
6 changes: 3 additions & 3 deletions lib/core/src/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,12 +546,12 @@ impl LiquidSdk {
None => pending_send_sat += p.amount_sat,
},
Created => pending_send_sat += p.amount_sat,
Refundable | RefundPending | TimedOut => {}
Refundable | RefundPending | TimedOut | Recoverable => {}
},
PaymentType::Receive => match p.status {
Complete => confirmed_received_sat += p.amount_sat,
Pending => pending_receive_sat += p.amount_sat,
Created | Refundable | RefundPending | Failed | TimedOut => {}
Created | Refundable | RefundPending | Failed | TimedOut | Recoverable => {}
},
}
}
Expand Down Expand Up @@ -1035,7 +1035,7 @@ impl LiquidSdk {
}
Pending => return Err(PaymentError::PaymentInProgress),
Complete => return Err(PaymentError::AlreadyPaid),
RefundPending | Refundable | Failed => {
RefundPending | Refundable | Failed | Recoverable => {
return Err(PaymentError::invalid_invoice(
"Payment has already failed. Please try with another invoice",
))
Expand Down
5 changes: 5 additions & 0 deletions lib/core/src/send_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,11 @@ impl SendSwapHandler {
to_state: PaymentState,
) -> Result<(), PaymentError> {
match (from_state, to_state) {
(Recoverable, Pending | Refundable | RefundPending | Failed | Complete) => Ok(()),
(_, Recoverable) => Err(PaymentError::Generic {
err: format!("Cannot transition from {from_state:?} to Recoverable state"),
}),

(TimedOut, Created) => Ok(()),
(_, Created) => Err(PaymentError::Generic {
err: "Cannot transition from {from_state:?} to Created state".to_string(),
Expand Down

0 comments on commit 79b3d8d

Please sign in to comment.