Skip to main content

fedimint_wallet_client/
error.rs

1//! Error types of the wallet client.
2//!
3//! Every failure this module reports to its callers is named here, so there is
4//! one place for an integrator to look.
5
6use bitcoin::Network;
7use fedimint_api_client::api::FederationError;
8use fedimint_bitcoind::BitcoinRpcError;
9use fedimint_client_module::error::{
10    OperationAlreadyExistsError, OperationLookupError, TransactionSubmitError,
11};
12use fedimint_core::core::OperationId;
13use fedimint_core::db::{AutocommitError, DatabaseError};
14use thiserror::Error;
15
16use crate::client_db::TweakIdx;
17
18/// A failure to look up or drive one of this client's deposit addresses.
19///
20/// The peg-in side of the wallet is address-oriented: a caller names a deposit
21/// by its address, by the operation that allocated it, or by the tweak index
22/// behind both, and then waits for the federation to claim what was sent
23/// there. Every way that can go wrong is named here.
24#[derive(Debug, Error)]
25#[non_exhaustive]
26pub enum PegInError {
27    /// The address is not one this client derived.
28    #[error("The address is not one of this client's deposit addresses")]
29    AddressNotDerived,
30
31    /// No deposit address was allocated under this operation.
32    #[error("No deposit address belongs to operation {}", .operation_id.fmt_short())]
33    NoAddressForOperation {
34        /// The operation that was looked up.
35        operation_id: OperationId,
36    },
37
38    /// The client has no record of this deposit address index.
39    #[error("No deposit address is recorded for {tweak_idx}")]
40    TweakIdxNotFound {
41        /// The index that was looked up.
42        tweak_idx: TweakIdx,
43    },
44
45    /// A database operation failed.
46    #[error("Database error")]
47    Database(#[from] DatabaseError),
48
49    /// The federation rejected the transaction that would have claimed the
50    /// deposit.
51    ///
52    /// The payload is the message the submission recorded rather than an error
53    /// value, so it is part of this error's own message.
54    #[error("The transaction claiming the deposit was rejected: {reason}")]
55    TransactionRejected {
56        /// What the submission reported.
57        reason: String,
58    },
59
60    /// The peg-in monitor stopped, so no further deposit will ever be claimed.
61    #[error("The peg-in monitor is no longer running")]
62    MonitorStopped,
63}
64
65impl From<AutocommitError<PegInError>> for PegInError {
66    fn from(e: AutocommitError<Self>) -> Self {
67        match e {
68            AutocommitError::ClosureError { error, .. } => error,
69            AutocommitError::CommitFailed { last_error, .. } => Self::Database(last_error),
70        }
71    }
72}
73
74/// A failure to hand out a deposit address.
75///
76/// Covers both the plain allocation and the pooled one, which can also lose a
77/// race against a deposit landing on the address it was about to reuse.
78#[derive(Debug, Error)]
79#[non_exhaustive]
80pub enum DepositAddressError {
81    /// The client has never been online to confirm that the federation's
82    /// wallet module handles every deposit safely.
83    #[error("The federation was not verified to support safe deposits")]
84    SafeDepositUnverified,
85
86    /// An operation for this deposit address already exists.
87    #[error("The deposit address's operation already exists")]
88    OperationAlreadyExists(#[from] OperationAlreadyExistsError),
89
90    /// The bitcoin backend would not start watching the address, so a deposit
91    /// to it would never be noticed.
92    #[error("The bitcoin backend could not watch the deposit address")]
93    BitcoinRpc(#[from] BitcoinRpcError),
94
95    /// The deposit address could not be written to the database.
96    #[error("Database error")]
97    Database(#[from] DatabaseError),
98
99    /// A pooled address vanished from the database between being offered for
100    /// reuse and being reused.
101    #[error("The pooled deposit address {tweak_idx} disappeared while it was being reused")]
102    PooledAddressDisappeared {
103        /// The address index that was being reused.
104        tweak_idx: TweakIdx,
105    },
106
107    /// A deposit landed on a pooled address between it being offered for reuse
108    /// and being reused, so it is no longer free.
109    #[error("The pooled deposit address {tweak_idx} was used while it was being reused")]
110    PooledAddressUsed {
111        /// The address index that was being reused.
112        tweak_idx: TweakIdx,
113    },
114}
115
116impl From<AutocommitError<DepositAddressError>> for DepositAddressError {
117    fn from(e: AutocommitError<Self>) -> Self {
118        match e {
119            AutocommitError::ClosureError { error, .. } => error,
120            AutocommitError::CommitFailed { last_error, .. } => Self::Database(last_error),
121        }
122    }
123}
124
125/// A failure to follow a deposit operation.
126#[derive(Debug, Error)]
127#[non_exhaustive]
128pub enum SubscribeDepositError {
129    /// The operation could not be looked up, or belongs to another module.
130    #[error("The deposit operation could not be looked up")]
131    Operation(#[from] OperationLookupError),
132
133    /// The operation exists and belongs to the wallet, but it is a withdrawal
134    /// (or an RBF bump of one) rather than a deposit.
135    #[error("The operation is not a deposit")]
136    NotADeposit,
137
138    /// The deposit address recorded with the operation is not valid on the
139    /// network this client is configured for.
140    #[error("The deposit address is not valid on {expected}")]
141    WrongNetwork {
142        /// The network this client expects.
143        expected: Network,
144    },
145
146    /// The deposit predates the 0.4 release, is still pending, and has no
147    /// state machine left to report progress from.
148    #[error("An old pending deposit cannot be subscribed to")]
149    OldPendingDeposit,
150
151    /// The deposit predates the 0.4 release and the outcome recorded for it is
152    /// not one of the final ones.
153    #[error("The recorded outcome of an old deposit is not final")]
154    NonFinalOutcome,
155}
156
157/// A failure to report the progress of a deposit operation's unclaimed
158/// deposits.
159#[derive(Debug, Error)]
160#[non_exhaustive]
161pub enum DepositProgressError {
162    /// The operation has no deposit address.
163    #[error("The deposit address of the operation could not be found")]
164    Address(#[from] PegInError),
165
166    /// The federation could not report its consensus block count.
167    #[error("The consensus block count could not be fetched")]
168    Federation(#[source] Box<FederationError>),
169
170    /// The bitcoin backend could not report the address's history or a
171    /// deposit's block height.
172    #[error("The bitcoin backend could not report the deposits")]
173    BitcoinRpc(#[from] BitcoinRpcError),
174}
175
176impl From<FederationError> for DepositProgressError {
177    fn from(source: FederationError) -> Self {
178        Self::Federation(Box::new(source))
179    }
180}
181
182/// A failure to quote the on-chain fees of a peg-out.
183#[derive(Debug, Error)]
184#[non_exhaustive]
185pub enum WithdrawFeesError {
186    /// The federation could not be asked, or its guardians disagreed.
187    #[error("The peg-out fees could not be fetched")]
188    Federation(#[source] Box<FederationError>),
189
190    /// The federation was reached and agreed, but it has no quote to give for
191    /// this amount.
192    #[error("The federation did not quote peg-out fees")]
193    NoQuote,
194}
195
196impl From<FederationError> for WithdrawFeesError {
197    fn from(source: FederationError) -> Self {
198        Self::Federation(Box::new(source))
199    }
200}
201
202/// A failure to work out the largest amount a "withdraw everything" sweep can
203/// send.
204#[derive(Debug, Error)]
205#[non_exhaustive]
206pub enum MaxWithdrawableAmountError {
207    /// The on-chain fees the answer is computed against could not be quoted.
208    #[error("The peg-out fees could not be quoted")]
209    Fees(#[from] WithdrawFeesError),
210
211    /// The balance cannot cover the destination's dust limit plus the fees, so
212    /// there is no amount to withdraw.
213    #[error("The balance {balance} is too low to cover the dust limit {dust_limit} and fees")]
214    BalanceTooLow {
215        /// The balance the sweep was computed against.
216        balance: fedimint_core::Amount,
217        /// The destination's dust limit, the smallest output it can receive.
218        dust_limit: bitcoin::Amount,
219    },
220
221    /// The fee probe failed for a reason unrelated to the balance.
222    #[error("The fee quote for the withdrawal failed")]
223    Quote(#[source] TransactionSubmitError),
224}
225
226/// A failure to start an on-chain withdrawal from a peg-out request.
227#[derive(Debug, Error)]
228#[non_exhaustive]
229pub enum PegOutError {
230    /// The destination address is not valid on the network this client is
231    /// configured for.
232    #[error("The destination address is not valid on {expected}")]
233    WrongNetwork {
234        /// The network this client expects.
235        expected: Network,
236    },
237
238    /// The on-chain fees of the withdrawal could not be quoted.
239    #[error("The peg-out fees could not be quoted")]
240    Fees(#[from] WithdrawFeesError),
241
242    /// The withdrawal transaction could not be built or submitted.
243    #[error("The withdrawal transaction could not be submitted")]
244    Transaction(#[from] TransactionSubmitError),
245}
246
247/// A failure to follow a withdrawal operation.
248#[derive(Debug, Error)]
249#[non_exhaustive]
250pub enum SubscribeWithdrawError {
251    /// The operation could not be looked up, or belongs to another module.
252    #[error("The withdrawal operation could not be looked up")]
253    Operation(#[from] OperationLookupError),
254
255    /// The operation exists and belongs to the wallet, but it is a deposit
256    /// rather than a withdrawal.
257    #[error("The operation is not a withdrawal")]
258    NotAWithdrawal,
259}
260
261/// A failure to vote for activating the next wallet module consensus version.
262#[derive(Debug, Error)]
263#[non_exhaustive]
264pub enum ConsensusVersionVotingError {
265    /// Voting is a guardian action and this client holds no admin
266    /// credentials.
267    #[error("Admin auth is not set")]
268    AdminAuthMissing,
269
270    /// The vote could not be submitted to the federation.
271    #[error("The vote could not be submitted to the federation")]
272    Federation(#[source] Box<FederationError>),
273}
274
275impl From<FederationError> for ConsensusVersionVotingError {
276    fn from(source: FederationError) -> Self {
277        Self::Federation(Box::new(source))
278    }
279}