Skip to content

Commit

Permalink
chore: improve error handling in lnd
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Feb 18, 2024
1 parent 87fa89d commit 64f8249
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 32 deletions.
26 changes: 7 additions & 19 deletions moksha-mint/src/btconchain/lnd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ impl LndBtcOnchain {

pub async fn client_lock(
&self,
) -> anyhow::Result<MappedMutexGuard<'_, fedimint_tonic_lnd::LightningClient>> {
) -> Result<MappedMutexGuard<'_, fedimint_tonic_lnd::LightningClient>, MokshaMintError> {
let guard = self.0.lock().await;
Ok(MutexGuard::map(guard, |client| client.lightning()))
}

pub async fn wallet_lock(
&self,
) -> anyhow::Result<MappedMutexGuard<'_, fedimint_tonic_lnd::WalletKitClient>> {
) -> Result<MappedMutexGuard<'_, fedimint_tonic_lnd::WalletKitClient>, MokshaMintError> {
let guard = self.0.lock().await;
Ok(MutexGuard::map(guard, |client| client.wallet()))
}
Expand All @@ -50,12 +50,7 @@ impl BtcOnchain for LndBtcOnchain {
..Default::default()
};

let response = self
.wallet_lock()
.await
.expect("failed to lock wallet")
.list_unspent(request)
.await?;
let response = self.wallet_lock().await?.list_unspent(request).await?;

Ok(response
.get_ref()
Expand All @@ -76,12 +71,7 @@ impl BtcOnchain for LndBtcOnchain {
..Default::default()
};

let response = self
.wallet_lock()
.await
.expect("failed to lock wallet")
.list_unspent(request)
.await?;
let response = self.wallet_lock().await?.list_unspent(request).await?;

Ok(response.get_ref().utxos.iter().any(|utxo| {
utxo.address == address
Expand All @@ -91,7 +81,7 @@ impl BtcOnchain for LndBtcOnchain {
}

async fn new_address(&self) -> Result<String, MokshaMintError> {
let mut client = self.client_lock().await.expect("failed to lock client");
let mut client = self.client_lock().await?;
let response = client
.new_address(NewAddressRequest {
r#type: AddressType::WitnessPubkeyHash as i32,
Expand All @@ -111,8 +101,7 @@ impl BtcOnchain for LndBtcOnchain {
) -> Result<SendCoinsResult, MokshaMintError> {
let response = self
.client_lock()
.await
.expect("failed to lock client")
.await?
.send_coins(SendCoinsRequest {
addr: address.to_owned(),
amount: amount as i64,
Expand All @@ -134,8 +123,7 @@ impl BtcOnchain for LndBtcOnchain {
) -> Result<EstimateFeeResult, MokshaMintError> {
let response = self
.client_lock()
.await
.expect("failed to lock client")
.await?
.estimate_fee(EstimateFeeRequest {
addr_to_amount: std::iter::once(&(address.to_owned(), amount as i64))
.cloned()
Expand Down
20 changes: 7 additions & 13 deletions moksha-mint/src/lightning/lnd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl LndLightning {

pub async fn client_lock(
&self,
) -> anyhow::Result<MappedMutexGuard<'_, fedimint_tonic_lnd::LightningClient>> {
) -> Result<MappedMutexGuard<'_, fedimint_tonic_lnd::LightningClient>, MokshaMintError> {
let guard = self.0.lock().await;
Ok(MutexGuard::map(guard, |client| client.lightning()))
}
Expand All @@ -87,11 +87,9 @@ impl Lightning for LndLightning {

let invoice = self
.client_lock()
.await
.expect("failed to lock client")
.await?
.lookup_invoice(fedimint_tonic_lnd::tonic::Request::new(invoice_request))
.await
.expect("failed to lookup invoice")
.await?
.into_inner();

Ok(invoice.state == fedimint_tonic_lnd::lnrpc::invoice::InvoiceState::Settled as i32)
Expand All @@ -105,11 +103,9 @@ impl Lightning for LndLightning {

let invoice = self
.client_lock()
.await
.expect("failed to lock client")
.await?
.add_invoice(fedimint_tonic_lnd::tonic::Request::new(invoice_request))
.await
.expect("failed to create invoice")
.await?
.into_inner();

Ok(CreateInvoiceResult {
Expand All @@ -128,11 +124,9 @@ impl Lightning for LndLightning {
};
let payment_response = self
.client_lock()
.await
.expect("failed to lock client") //FIXME map error
.await?
.send_payment_sync(fedimint_tonic_lnd::tonic::Request::new(pay_req))
.await
.expect("failed to pay invoice")
.await?
.into_inner();

let total_fees = payment_response
Expand Down

0 comments on commit 64f8249

Please sign in to comment.