Skip to content

Commit

Permalink
Merge pull request #1060 from breez/redeem-error
Browse files Browse the repository at this point in the history
Return insufficient funds error when needed
  • Loading branch information
roeierez authored Aug 6, 2024
2 parents 6a2aff4 + 1c7f91d commit 45b9903
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 17 deletions.
11 changes: 9 additions & 2 deletions libs/sdk-bindings/src/breez_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ enum SendPaymentError {
"ServiceConnectivity",
};

[Error]
enum RedeemOnchainError {
"Generic",
"ServiceConnectivity",
"InsufficientFunds",
};

enum EnvironmentType {
"Production",
"Staging",
Expand Down Expand Up @@ -874,7 +881,7 @@ interface BlockingBreezServices {
[Throws=SdkError]
void set_payment_metadata(string hash, string metadata);

[Throws=SdkError]
[Throws=RedeemOnchainError]
RedeemOnchainFundsResponse redeem_onchain_funds(RedeemOnchainFundsRequest req);

[Throws=SdkError]
Expand Down Expand Up @@ -973,7 +980,7 @@ interface BlockingBreezServices {
[Throws=ReceiveOnchainError]
BuyBitcoinResponse buy_bitcoin(BuyBitcoinRequest req);

[Throws=SdkError]
[Throws=RedeemOnchainError]
PrepareRedeemOnchainFundsResponse prepare_redeem_onchain_funds(PrepareRedeemOnchainFundsRequest req);
};

Expand Down
4 changes: 2 additions & 2 deletions libs/sdk-bindings/src/uniffi_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl BlockingBreezServices {
pub fn redeem_onchain_funds(
&self,
req: RedeemOnchainFundsRequest,
) -> SdkResult<RedeemOnchainFundsResponse> {
) -> RedeemOnchainResult<RedeemOnchainFundsResponse> {
rt().block_on(self.breez_services.redeem_onchain_funds(req))
}

Expand Down Expand Up @@ -382,7 +382,7 @@ impl BlockingBreezServices {
pub fn prepare_redeem_onchain_funds(
&self,
req: PrepareRedeemOnchainFundsRequest,
) -> SdkResult<PrepareRedeemOnchainFundsResponse> {
) -> RedeemOnchainResult<PrepareRedeemOnchainFundsResponse> {
rt().block_on(self.breez_services.prepare_redeem_onchain_funds(req))
}
}
Expand Down
8 changes: 4 additions & 4 deletions libs/sdk-core/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use tokio::sync::Mutex;
use crate::breez_services::{self, BreezEvent, BreezServices, EventListener};
use crate::chain::RecommendedFees;
use crate::error::{
ConnectError, ReceiveOnchainError, ReceivePaymentError, SdkError, SendOnchainError,
SendPaymentError,
ConnectError, ReceiveOnchainError, ReceivePaymentError, RedeemOnchainError, SdkError,
SendOnchainError, SendPaymentError,
};
use crate::lsp::LspInformation;
use crate::models::{Config, LogEntry, NodeState, Payment, SwapInfo};
Expand Down Expand Up @@ -639,7 +639,7 @@ pub fn buy_bitcoin(req: BuyBitcoinRequest) -> Result<BuyBitcoinResponse> {
/// See [BreezServices::redeem_onchain_funds]
pub fn redeem_onchain_funds(req: RedeemOnchainFundsRequest) -> Result<RedeemOnchainFundsResponse> {
block_on(async { get_breez_services().await?.redeem_onchain_funds(req).await })
.map_err(anyhow::Error::new::<SdkError>)
.map_err(anyhow::Error::new::<RedeemOnchainError>)
}

/// See [BreezServices::prepare_redeem_onchain_funds]
Expand All @@ -652,7 +652,7 @@ pub fn prepare_redeem_onchain_funds(
.prepare_redeem_onchain_funds(req)
.await
})
.map_err(anyhow::Error::new::<SdkError>)
.map_err(anyhow::Error::new::<RedeemOnchainError>)
}

/* Refundables API's */
Expand Down
8 changes: 4 additions & 4 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use crate::chain::{
DEFAULT_MEMPOOL_SPACE_URL,
};
use crate::error::{
ConnectError, ReceiveOnchainError, ReceiveOnchainResult, ReceivePaymentError, SdkError,
SdkResult, SendOnchainError, SendPaymentError,
ConnectError, ReceiveOnchainError, ReceiveOnchainResult, ReceivePaymentError,
RedeemOnchainResult, SdkError, SdkResult, SendOnchainError, SendPaymentError,
};
use crate::greenlight::{GLBackupTransport, Greenlight};
use crate::lnurl::pay::*;
Expand Down Expand Up @@ -626,7 +626,7 @@ impl BreezServices {
pub async fn redeem_onchain_funds(
&self,
req: RedeemOnchainFundsRequest,
) -> SdkResult<RedeemOnchainFundsResponse> {
) -> RedeemOnchainResult<RedeemOnchainFundsResponse> {
self.start_node().await?;
let txid = self
.node_api
Expand All @@ -639,7 +639,7 @@ impl BreezServices {
pub async fn prepare_redeem_onchain_funds(
&self,
req: PrepareRedeemOnchainFundsRequest,
) -> SdkResult<PrepareRedeemOnchainFundsResponse> {
) -> RedeemOnchainResult<PrepareRedeemOnchainFundsResponse> {
self.start_node().await?;
let response = self.node_api.prepare_redeem_onchain_funds(req).await?;
Ok(response)
Expand Down
47 changes: 47 additions & 0 deletions libs/sdk-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,53 @@ impl From<PersistError> for ReceiveOnchainError {
}
}

pub type RedeemOnchainResult<T, E = RedeemOnchainError> = Result<T, E>;

#[derive(Debug, Error)]
pub enum RedeemOnchainError {
/// This error is raised when a general error occurs not specific to other error variants
/// in this enum.
#[error("Generic: {err}")]
Generic { err: String },

/// This error is raised when a connection to an external service fails.
#[error("Service connectivity: {err}")]
ServiceConnectivity { err: String },

/// This error is raised when the node does not have enough funds to redeem the onchain balance.
#[error("{err}")]
InsufficientFunds { err: String },
}

impl From<NodeError> for RedeemOnchainError {
fn from(value: NodeError) -> Self {
match value {
NodeError::InsufficientFunds(err) => Self::InsufficientFunds { err },
NodeError::ServiceConnectivity(err) => Self::ServiceConnectivity { err },
_ => Self::Generic {
err: value.to_string(),
},
}
}
}

impl From<anyhow::Error> for RedeemOnchainError {
fn from(err: anyhow::Error) -> Self {
Self::Generic {
err: err.to_string(),
}
}
}

impl From<SdkError> for RedeemOnchainError {
fn from(value: SdkError) -> Self {
match value {
SdkError::Generic { err } => Self::Generic { err },
SdkError::ServiceConnectivity { err } => Self::ServiceConnectivity { err },
}
}
}

/// Error returned by [crate::breez_services::BreezServices::receive_payment]
#[derive(Debug, Error)]
pub enum ReceivePaymentError {
Expand Down
4 changes: 3 additions & 1 deletion libs/sdk-core/src/greenlight/node_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,9 @@ impl NodeAPI for Greenlight {
+ witness_input_size * txins.len() as u64;
let fee: u64 = tx_weight * req.sat_per_vbyte as u64 / WITNESS_SCALE_FACTOR as u64;
if fee >= amount_sat {
return Err(NodeError::generic("Insufficient funds to pay fees"));
return Err(NodeError::InsufficientFunds(
"Insufficient funds to pay fees".to_string(),
));
}

return Ok(PrepareRedeemOnchainFundsResponse {
Expand Down
11 changes: 7 additions & 4 deletions libs/sdk-core/src/node_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use crate::{
bitcoin::util::bip32::{ChildNumber, ExtendedPrivKey},
lightning_invoice::RawBolt11Invoice,
persist::error::PersistError,
CustomMessage, LspInformation, MaxChannelAmount, NodeCredentials, Payment, PaymentResponse,
PrepareRedeemOnchainFundsRequest, PrepareRedeemOnchainFundsResponse, RouteHint, RouteHintHop,
SyncResponse, TlvEntry, LnUrlAuthError
CustomMessage, LnUrlAuthError, LspInformation, MaxChannelAmount, NodeCredentials, Payment,
PaymentResponse, PrepareRedeemOnchainFundsRequest, PrepareRedeemOnchainFundsResponse,
RouteHint, RouteHintHop, SyncResponse, TlvEntry,
};

pub type NodeResult<T, E = NodeError> = Result<T, E>;
Expand Down Expand Up @@ -59,6 +59,9 @@ pub enum NodeError {

#[error("{0}")]
ServiceConnectivity(String),

#[error("{0}")]
InsufficientFunds(String),
}

impl NodeError {
Expand Down Expand Up @@ -86,7 +89,7 @@ impl From<NodeError> for LnUrlAuthError {
match value {
NodeError::ServiceConnectivity(err) => Self::ServiceConnectivity { err },
_ => Self::Generic {
err: value.to_string()
err: value.to_string(),
},
}
}
Expand Down

0 comments on commit 45b9903

Please sign in to comment.