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 1cd7a65 commit b0f1089
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/bindings/src/breez_sdk_liquid.udl
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ enum PaymentState {
"TimedOut",
"Refundable",
"RefundPending",
"Recoverable",
};

dictionary RefundableSwap {
Expand Down
5 changes: 5 additions & 0 deletions lib/core/src/chain_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,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
4 changes: 4 additions & 0 deletions lib/core/src/frb_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2063,6 +2063,7 @@ impl CstDecode<crate::model::PaymentState> for i32 {
4 => crate::model::PaymentState::TimedOut,
5 => crate::model::PaymentState::Refundable,
6 => crate::model::PaymentState::RefundPending,
7 => crate::model::PaymentState::Recoverable,
_ => unreachable!("Invalid variant for PaymentState: {}", self),
}
}
Expand Down Expand Up @@ -3629,6 +3630,7 @@ impl SseDecode for crate::model::PaymentState {
4 => crate::model::PaymentState::TimedOut,
5 => crate::model::PaymentState::Refundable,
6 => crate::model::PaymentState::RefundPending,
7 => crate::model::PaymentState::Recoverable,
_ => unreachable!("Invalid variant for PaymentState: {}", inner),
};
}
Expand Down Expand Up @@ -5620,6 +5622,7 @@ impl flutter_rust_bridge::IntoDart for crate::model::PaymentState {
Self::TimedOut => 4.into_dart(),
Self::Refundable => 5.into_dart(),
Self::RefundPending => 6.into_dart(),
Self::Recoverable => 7.into_dart(),
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -7588,6 +7591,7 @@ impl SseEncode for crate::model::PaymentState {
crate::model::PaymentState::TimedOut => 4,
crate::model::PaymentState::Refundable => 5,
crate::model::PaymentState::RefundPending => 6,
crate::model::PaymentState::Recoverable => 7,
_ => {
unimplemented!("");
}
Expand Down
7 changes: 7 additions & 0 deletions lib/core/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,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 All @@ -1035,6 +1041,7 @@ impl FromSql for PaymentState {
4 => Ok(PaymentState::TimedOut),
5 => Ok(PaymentState::Refundable),
6 => Ok(PaymentState::RefundPending),
7 => Ok(PaymentState::Recoverable),
_ => Err(FromSqlError::OutOfRange(i)),
},
_ => Err(FromSqlError::InvalidType),
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 @@ -562,12 +562,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 @@ -1241,7 +1241,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 @@ -496,6 +496,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
6 changes: 6 additions & 0 deletions packages/dart/lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,12 @@ enum PaymentState {
///
/// When the refund tx is broadcast, `refund_tx_id` is set in the swap.
refundPending,

/// ## Recoverable Swaps
///
/// The status for swaps that have been synced in, and whose information is recoverable from
/// chain
recoverable,
;
}

Expand Down
6 changes: 6 additions & 0 deletions packages/react-native/ios/BreezSDKLiquidMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3733,6 +3733,9 @@ enum BreezSDKLiquidMapper {
case "refundPending":
return PaymentState.refundPending

case "recoverable":
return PaymentState.recoverable

default: throw SdkError.Generic(message: "Invalid variant \(paymentState) for enum PaymentState")
}
}
Expand All @@ -3759,6 +3762,9 @@ enum BreezSDKLiquidMapper {

case .refundPending:
return "refundPending"

case .recoverable:
return "recoverable"
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/react-native/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,8 @@ export enum PaymentState {
FAILED = "failed",
TIMED_OUT = "timedOut",
REFUNDABLE = "refundable",
REFUND_PENDING = "refundPending"
REFUND_PENDING = "refundPending",
RECOVERABLE = "recoverable"
}

export enum PaymentType {
Expand Down

0 comments on commit b0f1089

Please sign in to comment.