Skip to content

Commit

Permalink
Add WebLN::get_balance
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Jan 19, 2024
1 parent 4d7a5ee commit 48c0f66
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
29 changes: 29 additions & 0 deletions webln-js/src/balance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2024 Yuki Kishimoto
// Distributed under the MIT software license

use alloc::string::String;

use wasm_bindgen::prelude::*;
use webln::BalanceResponse;

#[wasm_bindgen(js_name = BalanceResponse)]
pub struct JsBalanceResponse {
inner: BalanceResponse,
}

impl From<BalanceResponse> for JsBalanceResponse {
fn from(inner: BalanceResponse) -> Self {
Self { inner }
}
}

#[wasm_bindgen(js_class = BalanceResponse)]
impl JsBalanceResponse {
pub fn balance(&self) -> f64 {
self.inner.balance
}

pub fn currency(&self) -> Option<String> {
self.inner.currency.clone()
}
}
8 changes: 8 additions & 0 deletions webln-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ use core::ops::Deref;
use wasm_bindgen::prelude::*;
use webln::WebLN;

pub mod balance;
pub mod error;
pub mod get_info;
pub mod keysend;
pub mod request_invoice;
pub mod send_payment;
pub mod sign_message;

use self::balance::JsBalanceResponse;
use self::error::{into_err, Result};
use self::get_info::JsGetInfoResponse;
use self::keysend::JsKeysendArgs;
Expand Down Expand Up @@ -127,4 +129,10 @@ impl JsWebLN {
.map_err(into_err)?
.into())
}

/// Fetch the balance of the current account.
#[wasm_bindgen(js_name = getBalance)]
pub async fn get_balance(&self) -> Result<JsBalanceResponse> {
Ok(self.inner.get_balance().await.map_err(into_err)?.into())
}
}
29 changes: 29 additions & 0 deletions webln/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,15 @@ pub struct SignMessageResponse {
pub signature: String,
}

/// Balance Response
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct BalanceResponse {
/// Balance
pub balance: f64,
/// Currency
pub currency: Option<String>,
}

/// WebLN instance
#[derive(Debug, Clone)]
pub struct WebLN {
Expand Down Expand Up @@ -539,4 +548,24 @@ impl WebLN {
signature,
})
}

/// Fetch the balance of the current account.
pub async fn get_balance(&self) -> Result<BalanceResponse, Error> {
let func: Function = self.get_func(&self.webln_obj, GET_BALANCE)?;
let promise: Promise = Promise::resolve(&func.call0(&self.webln_obj)?);
let result: JsValue = JsFuture::from(promise).await?;
let balance_response_obj: Object =
result.dyn_into().map_err(|_| Error::SomethingGoneWrong)?;

// Extract data
let balance: f64 = self
.get_value_by_key(&balance_response_obj, "balance")?
.as_f64()
.ok_or_else(|| Error::TypeMismatch(String::from("expected a number [balance]")))?;
let currency: Option<String> = self
.get_value_by_key(&balance_response_obj, "currency")?
.as_string();

Ok(BalanceResponse { balance, currency })
}
}

0 comments on commit 48c0f66

Please sign in to comment.