Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add send/receive subroutes to wallet page #24

Merged
merged 3 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ resources = ["assets/fonts/**/*.*"]

[dependencies]
anyhow = "1.0.80"
arboard = { version = "3.4.0", default-features = false }
async-stream = "0.3.5"
async-trait = "0.1.81"
chrono = { version = "0.4.34", features = ["alloc"] }
Expand All @@ -29,6 +30,7 @@ fedimint-mint-client = "0.4.0"
fedimint-rocksdb = "0.4.0"
iced = { git = "https://github.com/iced-rs/iced", rev = "e50aa03", features = [
"advanced",
"qr_code",
"svg",
"tokio",
] }
Expand Down
1 change: 1 addition & 0 deletions assets/icons/arrow_downward.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions assets/icons/arrow_upward.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions assets/icons/content_copy.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions assets/icons/send.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions src/fedimint.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt::Display;
use std::pin::Pin;
use std::{
collections::{BTreeMap, HashMap},
Expand Down Expand Up @@ -29,6 +30,8 @@ use nostr_sdk::{
};
use secp256k1::rand::{seq::SliceRandom, thread_rng};

use crate::util::format_amount;

const FEDIMINT_CLIENTS_DATA_DIR_NAME: &str = "fedimint_clients";
// TODO: Figure out if we even want this. If we do, it probably shouldn't live here.
// It'd make more sense for it to live wherever the key is maintained elsewhere, and
Expand All @@ -48,6 +51,19 @@ pub struct FederationView {
pub gateways: Vec<LightningGatewayAnnouncement>,
}

impl Display for FederationView {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let name_or_id = self
.name_or
.clone()
.unwrap_or_else(|| self.federation_id.to_string());

let balance = format_amount(self.balance);

write!(f, "{name_or_id} ({balance})")
}
}

pub struct Wallet {
derivable_secret: DerivableSecret,
clients: Arc<Mutex<HashMap<FederationId, ClientHandle>>>,
Expand Down Expand Up @@ -187,6 +203,32 @@ impl Wallet {
state
}

pub async fn pay_invoice(
&self,
invoice: Bolt11Invoice,
federation_id: FederationId,
) -> anyhow::Result<()> {
let clients = self.clients.lock().await;

let client = clients
.get(&federation_id)
.ok_or_else(|| anyhow::anyhow!("Client for federation {} not found", federation_id))?;

let lightning_module = client.get_first_module::<LightningClientModule>();

let gateways = lightning_module.list_gateways().await;

let payment_info = lightning_module
.pay_bolt11_invoice(Self::select_gateway(&gateways), invoice, ())
.await?;

lightning_module
.wait_for_ln_payment(payment_info.payment_type, payment_info.contract_id, false)
.await?;

Ok(())
}

pub async fn receive_payment(
&self,
federation_id: FederationId,
Expand Down
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Keystache {

while let Some(views) = wallet_update_stream.next().await {
output
.send(KeystacheMessage::FederationViewsUpdate { views })
.send(KeystacheMessage::UpdateFederationViews { views })
.await
.unwrap();
}
Expand Down Expand Up @@ -159,10 +159,12 @@ enum KeystacheMessage {

DbDeleteAllData,

FederationViewsUpdate {
UpdateFederationViews {
views: BTreeMap<FederationId, FederationView>,
},

CopyStringToClipboard(String),

IncomingNip46Request(
Arc<(
Vec<nostr_sdk::nips::nip46::Request>,
Expand Down
Loading
Loading