use NwcError instead of nwc::Error
need to clone Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
use std::sync::Arc;
|
use std::{fmt::Display, sync::Arc};
|
||||||
|
|
||||||
use nwc::{
|
use nwc::{
|
||||||
nostr::nips::nip47::{NostrWalletConnectURI, PayInvoiceRequest, PayInvoiceResponse},
|
nostr::nips::nip47::{NostrWalletConnectURI, PayInvoiceRequest, PayInvoiceResponse},
|
||||||
@@ -57,7 +57,7 @@ pub enum WalletError {
|
|||||||
pub struct Wallet {
|
pub struct Wallet {
|
||||||
pub uri: String,
|
pub uri: String,
|
||||||
wallet: Arc<RwLock<NWC>>,
|
wallet: Arc<RwLock<NWC>>,
|
||||||
balance: Option<Promise<Result<u64, nwc::Error>>>,
|
balance: Option<Promise<Result<u64, NwcError>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@@ -95,7 +95,7 @@ impl Wallet {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_balance(&mut self) -> Option<&Result<u64, nwc::Error>> {
|
pub fn get_balance(&mut self) -> Option<&Result<u64, NwcError>> {
|
||||||
if self.balance.is_none() {
|
if self.balance.is_none() {
|
||||||
self.balance = Some(get_balance(self.wallet.clone()));
|
self.balance = Some(get_balance(self.wallet.clone()));
|
||||||
return None;
|
return None;
|
||||||
@@ -117,11 +117,51 @@ impl Wallet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_balance(nwc: Arc<RwLock<NWC>>) -> Promise<Result<u64, nwc::Error>> {
|
#[derive(Clone)]
|
||||||
|
pub enum NwcError {
|
||||||
|
/// NIP47 error
|
||||||
|
NIP47(String),
|
||||||
|
/// Relay
|
||||||
|
Relay(String),
|
||||||
|
/// Premature exit
|
||||||
|
PrematureExit,
|
||||||
|
/// Request timeout
|
||||||
|
Timeout,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<nwc::Error> for NwcError {
|
||||||
|
fn from(value: nwc::Error) -> Self {
|
||||||
|
match value {
|
||||||
|
nwc::error::Error::NIP47(error) => NwcError::NIP47(error.to_string()),
|
||||||
|
nwc::error::Error::Relay(error) => NwcError::Relay(error.to_string()),
|
||||||
|
nwc::error::Error::PrematureExit => NwcError::PrematureExit,
|
||||||
|
nwc::error::Error::Timeout => NwcError::Timeout,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for NwcError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
NwcError::NIP47(err) => write!(f, "NIP47 error: {}", err),
|
||||||
|
NwcError::Relay(err) => write!(f, "Relay error: {}", err),
|
||||||
|
NwcError::PrematureExit => write!(f, "Premature exit"),
|
||||||
|
NwcError::Timeout => write!(f, "Request timed out"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_balance(nwc: Arc<RwLock<NWC>>) -> Promise<Result<u64, NwcError>> {
|
||||||
let (sender, promise) = Promise::new();
|
let (sender, promise) = Promise::new();
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
sender.send(nwc.read().await.get_balance().await);
|
sender.send(
|
||||||
|
nwc.read()
|
||||||
|
.await
|
||||||
|
.get_balance()
|
||||||
|
.await
|
||||||
|
.map_err(nwc::Error::into),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
promise
|
promise
|
||||||
|
|||||||
Reference in New Issue
Block a user