1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use anyhow::Error;
use lwk_wollet::secp256k1;
use sdk_common::prelude::LnUrlAuthError;

pub type SdkResult<T, E = SdkError> = Result<T, E>;

#[macro_export]
macro_rules! ensure_sdk {
    ($cond:expr, $err:expr) => {
        if !$cond {
            return Err($err);
        }
    };
}

// TODO Unify error enum
#[derive(Debug, thiserror::Error)]
pub enum SdkError {
    #[error("Liquid SDK instance is already running")]
    AlreadyStarted,

    #[error("Error: {err}")]
    Generic { err: String },

    #[error("Liquid SDK instance is not running")]
    NotStarted,

    #[error("Service connectivity: {err}")]
    ServiceConnectivity { err: String },
}

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

impl From<boltz_client::error::Error> for SdkError {
    fn from(err: boltz_client::error::Error) -> Self {
        match err {
            boltz_client::error::Error::HTTP(e) => SdkError::Generic {
                err: format!("Could not contact servers: {e:?}"),
            },
            _ => SdkError::Generic {
                err: format!("{err:?}"),
            },
        }
    }
}

impl From<secp256k1::Error> for SdkError {
    fn from(err: secp256k1::Error) -> Self {
        SdkError::Generic {
            err: format!("{err:?}"),
        }
    }
}

#[derive(thiserror::Error, Debug)]
pub enum PaymentError {
    #[error("The specified funds have already been claimed")]
    AlreadyClaimed,

    #[error("The specified funds have already been sent")]
    AlreadyPaid,

    #[error("The payment is already in progress")]
    PaymentInProgress,

    #[error("Amount is out of range")]
    AmountOutOfRange,

    #[error("Amount is missing: {err}")]
    AmountMissing { err: String },

    #[error("Invalid network: {err}")]
    InvalidNetwork { err: String },

    #[error("Generic error: {err}")]
    Generic { err: String },

    #[error("The provided fees have expired")]
    InvalidOrExpiredFees,

    #[error("Cannot pay: not enough funds")]
    InsufficientFunds,

    #[error("Invalid description: {err}")]
    InvalidDescription { err: String },

    #[error("The specified invoice is not valid: {err}")]
    InvalidInvoice { err: String },

    #[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("The payment timed out")]
    PaymentTimeout,

    #[error("Could not store the swap details locally")]
    PersistError,

    #[error("Could not process the Receive Payment: {err}")]
    ReceiveError { err: String },

    #[error("The payment has been refunded. Reason for failure: {err}")]
    Refunded { err: String, refund_tx_id: String },

    #[error("The payment is a self-transfer, which is not supported")]
    SelfTransferNotSupported,

    #[error("Could not process the Send Payment: {err}")]
    SendError { err: String },

    #[error("Could not sign the transaction: {err}")]
    SignerError { err: String },
}
impl PaymentError {
    pub(crate) fn receive_error(err: &str) -> Self {
        Self::ReceiveError {
            err: err.to_string(),
        }
    }
}

impl From<boltz_client::error::Error> for PaymentError {
    fn from(err: boltz_client::error::Error) -> Self {
        match err {
            boltz_client::error::Error::HTTP(e) => PaymentError::Generic {
                err: format!("Could not contact servers: {e:?}"),
            },
            _ => PaymentError::Generic {
                err: format!("{err:?}"),
            },
        }
    }
}

impl From<boltz_client::bitcoin::hex::HexToArrayError> for PaymentError {
    fn from(err: boltz_client::bitcoin::hex::HexToArrayError) -> Self {
        PaymentError::Generic {
            err: format!("{err:?}"),
        }
    }
}

impl From<lwk_wollet::Error> for PaymentError {
    fn from(err: lwk_wollet::Error) -> Self {
        match err {
            lwk_wollet::Error::InsufficientFunds => PaymentError::InsufficientFunds,
            _ => PaymentError::LwkError {
                err: format!("{err:?}"),
            },
        }
    }
}

impl From<lwk_signer::SignerError> for PaymentError {
    fn from(err: lwk_signer::SignerError) -> Self {
        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<SdkError> for PaymentError {
    fn from(err: SdkError) -> Self {
        Self::Generic {
            err: err.to_string(),
        }
    }
}

impl From<crate::bitcoin::util::bip32::Error> for PaymentError {
    fn from(err: crate::bitcoin::util::bip32::Error) -> Self {
        Self::SignerError {
            err: err.to_string(),
        }
    }
}

impl From<PaymentError> for LnUrlAuthError {
    fn from(value: PaymentError) -> Self {
        Self::Generic {
            err: format!("Failed to perform LNURL-auth: {value:?}"),
        }
    }
}