Move PaymentError to error.rs

This commit is contained in:
Erdem Yerebasmaz
2024-04-29 17:09:16 +03:00
parent cb7ccf35ac
commit 63be6097e7
5 changed files with 102 additions and 98 deletions

View File

@@ -2,8 +2,9 @@ use std::sync::Arc;
use anyhow::{Error, Result};
use ls_sdk::{
error::PaymentError,
model::{
Network, PaymentError, PrepareReceiveRequest, PrepareReceiveResponse, PrepareSendResponse,
Network, PrepareReceiveRequest, PrepareReceiveResponse, PrepareSendResponse,
ReceivePaymentResponse, SendPaymentResponse, WalletInfo,
},
wallet::Wallet,

View File

@@ -3,8 +3,9 @@ pub(crate) use boltz_client::util::secrets::LBtcReverseRecovery;
use std::sync::{Arc, OnceLock};
use crate::{
error::PaymentError,
model::{
Network, PaymentError, PrepareReceiveRequest, PrepareReceiveResponse, PrepareSendResponse,
Network, PrepareReceiveRequest, PrepareReceiveResponse, PrepareSendResponse,
ReceivePaymentResponse, SendPaymentResponse, WalletInfo,
},
wallet::Wallet,

View File

@@ -21,3 +21,91 @@ impl From<anyhow::Error> for LsSdkError {
LsSdkError::Generic { err: e.to_string() }
}
}
#[derive(thiserror::Error, Debug)]
pub enum PaymentError {
#[error("Invoice amount is out of range")]
AmountOutOfRange,
#[error("The specified funds have already been claimed")]
AlreadyClaimed,
#[error("Generic error: {err}")]
Generic { err: String },
#[error("The specified invoice is not valid")]
InvalidInvoice,
#[error("The generated preimage is not valid")]
InvalidPreimage,
#[error("Lwk error: {err}")]
LwkError { err: String },
#[error("Boltz did not return any pairs from the request")]
PairsNotFound,
#[error("Could not store the swap details locally")]
PersistError,
#[error("Could not sign/send the transaction: {err}")]
SendError { err: String },
#[error("Could not sign the transaction: {err}")]
SignerError { err: String },
}
impl From<boltz_client::error::Error> for PaymentError {
fn from(err: boltz_client::error::Error) -> Self {
match err {
boltz_client::error::Error::Protocol(msg) => {
if msg == "Could not find utxos for script" {
return PaymentError::AlreadyClaimed;
}
PaymentError::Generic { err: msg }
}
_ => PaymentError::Generic {
err: format!("{err:?}"),
},
}
}
}
#[allow(clippy::match_single_binding)]
impl From<lwk_wollet::Error> for PaymentError {
fn from(err: lwk_wollet::Error) -> Self {
match err {
_ => PaymentError::LwkError {
err: format!("{err:?}"),
},
}
}
}
#[allow(clippy::match_single_binding)]
impl From<lwk_signer::SignerError> for PaymentError {
fn from(err: lwk_signer::SignerError) -> Self {
match err {
_ => PaymentError::SignerError {
err: format!("{err:?}"),
},
}
}
}
impl From<anyhow::Error> for PaymentError {
fn from(err: anyhow::Error) -> Self {
Self::Generic {
err: err.to_string(),
}
}
}
impl From<LsSdkError> for PaymentError {
fn from(err: LsSdkError) -> Self {
Self::Generic {
err: err.to_string(),
}
}
}

View File

@@ -6,8 +6,6 @@ use serde::Serialize;
use crate::get_invoice_amount;
use super::error::LsSdkError;
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Network {
Liquid,
@@ -95,94 +93,6 @@ pub struct SendPaymentResponse {
pub txid: String,
}
#[derive(thiserror::Error, Debug)]
pub enum PaymentError {
#[error("Invoice amount is out of range")]
AmountOutOfRange,
#[error("The specified funds have already been claimed")]
AlreadyClaimed,
#[error("Generic error: {err}")]
Generic { err: String },
#[error("The specified invoice is not valid")]
InvalidInvoice,
#[error("The generated preimage is not valid")]
InvalidPreimage,
#[error("Lwk error: {err}")]
LwkError { err: String },
#[error("Boltz did not return any pairs from the request")]
PairsNotFound,
#[error("Could not store the swap details locally")]
PersistError,
#[error("Could not sign/send the transaction: {err}")]
SendError { err: String },
#[error("Could not sign the transaction: {err}")]
SignerError { err: String },
}
impl From<boltz_client::error::Error> for PaymentError {
fn from(err: boltz_client::error::Error) -> Self {
match err {
boltz_client::error::Error::Protocol(msg) => {
if msg == "Could not find utxos for script" {
return PaymentError::AlreadyClaimed;
}
PaymentError::Generic { err: msg }
}
_ => PaymentError::Generic {
err: format!("{err:?}"),
},
}
}
}
#[allow(clippy::match_single_binding)]
impl From<lwk_wollet::Error> for PaymentError {
fn from(err: lwk_wollet::Error) -> Self {
match err {
_ => PaymentError::LwkError {
err: format!("{err:?}"),
},
}
}
}
#[allow(clippy::match_single_binding)]
impl From<lwk_signer::SignerError> for PaymentError {
fn from(err: lwk_signer::SignerError) -> Self {
match err {
_ => PaymentError::SignerError {
err: format!("{err:?}"),
},
}
}
}
impl From<anyhow::Error> for PaymentError {
fn from(err: anyhow::Error) -> Self {
Self::Generic {
err: err.to_string(),
}
}
}
impl From<LsSdkError> for PaymentError {
fn from(err: LsSdkError) -> Self {
Self::Generic {
err: err.to_string(),
}
}
}
#[derive(Debug, Serialize)]
pub struct WalletInfo {
pub balance_sat: u64,

View File

@@ -29,12 +29,16 @@ use lwk_wollet::{
ElementsNetwork, FsPersister, Wollet as LwkWollet, WolletDescriptor,
};
use crate::{ensure_sdk, get_invoice_amount, persist::Persister};
use crate::model::{
Network, OngoingSwap, Payment, PaymentData, PaymentError, PaymentType, PrepareReceiveRequest,
PrepareReceiveResponse, PrepareSendResponse, ReceivePaymentResponse, SendPaymentResponse,
WalletInfo, WalletOptions,
use crate::{
ensure_sdk,
error::PaymentError,
get_invoice_amount,
model::{
Network, OngoingSwap, Payment, PaymentData, PaymentType, PrepareReceiveRequest,
PrepareReceiveResponse, PrepareSendResponse, ReceivePaymentResponse, SendPaymentResponse,
WalletInfo, WalletOptions,
},
persist::Persister,
};
/// Claim tx feerate for Receive, in sats per vbyte.