Skip to content

Commit 0fe9fc2

Browse files
committed
Create proper trusted error type
1 parent 2d57fef commit 0fe9fc2

5 files changed

Lines changed: 178 additions & 85 deletions

File tree

graduated-rebalancer/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl Default for RebalanceTunables {
6161
/// Trait representing a trusted wallet backend
6262
pub trait TrustedWallet: Send + Sync {
6363
/// Error type for trusted wallet operations
64-
type Error: std::error::Error + Send + Sync + 'static;
64+
type Error: Debug + Send + Sync + 'static;
6565

6666
/// Get the current balance of the trusted wallet
6767
fn get_balance(&self) -> impl Future<Output = Result<Amount, Self::Error>> + Send;
@@ -83,7 +83,7 @@ pub trait TrustedWallet: Send + Sync {
8383
/// Trait representing a lightning wallet backend
8484
pub trait LightningWallet: Send + Sync {
8585
/// Error type for lightning wallet operations
86-
type Error: std::error::Error + Send + Sync + 'static;
86+
type Error: Debug + Send + Sync + 'static;
8787

8888
/// Get the current balance of the lightning wallet
8989
fn get_balance(&self) -> LightningBalance;

orange-sdk/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub mod trusted_wallet;
5353

5454
use lightning_wallet::LightningWallet;
5555
use logging::Logger;
56-
use trusted_wallet::Error as TrustedError;
56+
use trusted_wallet::TrustedError;
5757
use trusted_wallet::TrustedWalletInterface;
5858

5959
pub use bitcoin_payment_instructions;
@@ -380,7 +380,7 @@ pub enum InitFailure {
380380
/// Failure to start the LDK node.
381381
LdkNodeStartFailure(NodeError),
382382
/// Failure in the trusted wallet implementation.
383-
TrustedFailure(Box<TrustedError>),
383+
TrustedFailure(TrustedError),
384384
}
385385

386386
impl From<io::Error> for InitFailure {
@@ -403,7 +403,7 @@ impl From<NodeError> for InitFailure {
403403

404404
impl From<TrustedError> for InitFailure {
405405
fn from(e: TrustedError) -> InitFailure {
406-
InitFailure::TrustedFailure(Box::new(e))
406+
InitFailure::TrustedFailure(e)
407407
}
408408
}
409409

orange-sdk/src/trusted_wallet/dummy.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::bitcoin::hashes::Hash;
44
use crate::event::EventQueue;
55
use crate::store::TxStatus;
6-
use crate::trusted_wallet::{Error, Payment, TrustedWalletInterface};
6+
use crate::trusted_wallet::{Payment, TrustedError, TrustedWalletInterface};
77
use crate::{InitFailure, WalletConfig};
88
use bitcoin_payment_instructions::PaymentMethod;
99
use bitcoin_payment_instructions::amount::Amount;
@@ -187,25 +187,27 @@ impl TrustedWalletInterface for DummyTrustedWallet {
187187
}
188188
}
189189

190-
fn get_balance(&self) -> impl Future<Output = Result<Amount, Error>> + Send {
190+
fn get_balance(&self) -> impl Future<Output = Result<Amount, TrustedError>> + Send {
191191
async move {
192192
let msats = self.current_bal_msats.load(Ordering::SeqCst);
193193
Ok(Amount::from_milli_sats(msats).expect("valid msats"))
194194
}
195195
}
196196

197-
fn get_reusable_receive_uri(&self) -> impl Future<Output = Result<String, Error>> + Send {
197+
fn get_reusable_receive_uri(
198+
&self,
199+
) -> impl Future<Output = Result<String, TrustedError>> + Send {
198200
async move {
199201
match self.ldk_node.bolt12_payment().receive_variable_amount("dummy offer", None) {
200202
Ok(offer) => Ok(offer.to_string()),
201-
Err(e) => Err(Error::Generic(e.to_string())),
203+
Err(e) => Err(TrustedError::WalletOperationFailed(e.to_string())),
202204
}
203205
}
204206
}
205207

206208
fn get_bolt11_invoice(
207209
&self, amount: Option<Amount>,
208-
) -> impl Future<Output = Result<Bolt11Invoice, Error>> + Send {
210+
) -> impl Future<Output = Result<Bolt11Invoice, TrustedError>> + Send {
209211
async move {
210212
let desc = Bolt11InvoiceDescription::Direct(Description::empty());
211213
match amount {
@@ -226,19 +228,19 @@ impl TrustedWalletInterface for DummyTrustedWallet {
226228
}
227229
}
228230

229-
fn list_payments(&self) -> impl Future<Output = Result<Vec<Payment>, Error>> + Send {
231+
fn list_payments(&self) -> impl Future<Output = Result<Vec<Payment>, TrustedError>> + Send {
230232
async move { Ok(self.payments.read().unwrap().clone()) }
231233
}
232234

233235
fn estimate_fee(
234236
&self, _method: &PaymentMethod, _amount: Amount,
235-
) -> impl Future<Output = Result<Amount, Error>> + Send {
237+
) -> impl Future<Output = Result<Amount, TrustedError>> + Send {
236238
async move { Ok(Amount::ZERO) }
237239
}
238240

239241
fn pay(
240242
&self, method: &PaymentMethod, amount: Amount,
241-
) -> impl Future<Output = Result<[u8; 32], Error>> + Send {
243+
) -> impl Future<Output = Result<[u8; 32], TrustedError>> + Send {
242244
async move {
243245
let id = match method {
244246
PaymentMethod::LightningBolt11(inv) => {

orange-sdk/src/trusted_wallet/mod.rs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ use ldk_node::lightning_invoice::Bolt11Invoice;
1111
use bitcoin_payment_instructions::PaymentMethod;
1212
use bitcoin_payment_instructions::amount::Amount;
1313

14-
use spark_wallet::SparkWalletError;
15-
1614
use std::future::Future;
1715
use std::sync::Arc;
1816
use std::time::Duration;
@@ -21,9 +19,6 @@ use std::time::Duration;
2119
pub mod dummy;
2220
pub mod spark;
2321

24-
// todo generic error type
25-
pub(crate) type Error = SparkWalletError;
26-
2722
/// Represents a payment with its associated details.
2823
///
2924
/// This struct contains information about a payment, including its unique ID,
@@ -57,29 +52,30 @@ pub trait TrustedWalletInterface: Sized + Send + Sync + private::Sealed {
5752
) -> impl Future<Output = Result<Self, InitFailure>> + Send;
5853

5954
/// Returns the current balance of the wallet.
60-
fn get_balance(&self) -> impl Future<Output = Result<Amount, Error>> + Send;
55+
fn get_balance(&self) -> impl Future<Output = Result<Amount, TrustedError>> + Send;
6156

6257
/// Generates a new reusable address for receiving payments.
6358
/// Generally, this should be a BOLT 12 offer.
64-
fn get_reusable_receive_uri(&self) -> impl Future<Output = Result<String, Error>> + Send;
59+
fn get_reusable_receive_uri(&self)
60+
-> impl Future<Output = Result<String, TrustedError>> + Send;
6561

6662
/// Generates a Bolt11 invoice for the specified amount.
6763
fn get_bolt11_invoice(
6864
&self, amount: Option<Amount>,
69-
) -> impl Future<Output = Result<Bolt11Invoice, Error>> + Send;
65+
) -> impl Future<Output = Result<Bolt11Invoice, TrustedError>> + Send;
7066

7167
/// Lists all payments made through the wallet.
72-
fn list_payments(&self) -> impl Future<Output = Result<Vec<Payment>, Error>> + Send;
68+
fn list_payments(&self) -> impl Future<Output = Result<Vec<Payment>, TrustedError>> + Send;
7369

7470
/// Estimates the fee for a payment to the given payment method with the specified amount.
7571
fn estimate_fee(
7672
&self, method: &PaymentMethod, amount: Amount,
77-
) -> impl Future<Output = Result<Amount, Error>> + Send;
73+
) -> impl Future<Output = Result<Amount, TrustedError>> + Send;
7874

7975
/// Pays to the given payment method with the specified amount.
8076
fn pay(
8177
&self, method: &PaymentMethod, amount: Amount,
82-
) -> impl Future<Output = Result<[u8; 32], Error>> + Send;
78+
) -> impl Future<Output = Result<[u8; 32], TrustedError>> + Send;
8379

8480
/// Stops the wallet, cleaning up any resources.
8581
/// This is typically used to gracefully shut down the wallet.
@@ -89,7 +85,7 @@ pub trait TrustedWalletInterface: Sized + Send + Sync + private::Sealed {
8985
pub(crate) struct WalletTrusted<T: TrustedWalletInterface>(pub(crate) Arc<T>);
9086

9187
impl<T: TrustedWalletInterface> graduated_rebalancer::TrustedWallet for WalletTrusted<T> {
92-
type Error = crate::trusted_wallet::Error;
88+
type Error = TrustedError;
9389

9490
fn get_balance(&self) -> impl Future<Output = Result<Amount, Self::Error>> + Send {
9591
async move { self.0.get_balance().await }
@@ -127,3 +123,28 @@ mod private {
127123
#[cfg(feature = "_test-utils")]
128124
impl Sealed for super::dummy::DummyTrustedWallet {}
129125
}
126+
127+
/// An error type for the Spark wallet implementation.
128+
#[derive(Debug)]
129+
pub enum TrustedError {
130+
/// Not enough funds to complete the operation.
131+
InsufficientFunds,
132+
/// The wallet operation failed with a specific message.
133+
WalletOperationFailed(String),
134+
/// The provided network is invalid.
135+
InvalidNetwork,
136+
/// The spark wallet does not yet support the operation.
137+
UnsupportedOperation(String),
138+
/// Failed to convert an amount.
139+
AmountError,
140+
/// An I/O error occurred.
141+
IOError(ldk_node::lightning::io::Error),
142+
/// An unspecified error occurred.
143+
Other(String),
144+
}
145+
146+
impl From<ldk_node::lightning::io::Error> for TrustedError {
147+
fn from(e: ldk_node::lightning::io::Error) -> Self {
148+
TrustedError::IOError(e)
149+
}
150+
}

0 commit comments

Comments
 (0)