Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit 9aa46e6

Browse files
Merge pull request #800 from MutinyWallet/autopay-subscription
Add option to autopay subscriptions
2 parents c80660d + b711f88 commit 9aa46e6

2 files changed

Lines changed: 47 additions & 15 deletions

File tree

mutiny-core/src/lib.rs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ pub use crate::ldkstorage::{CHANNEL_MANAGER_KEY, MONITORS_PREFIX_KEY};
5151

5252
use crate::auth::MutinyAuthClient;
5353
use crate::labels::{Contact, LabelStorage};
54-
use crate::nostr::nwc::{NwcProfileTag, SpendingConditions};
54+
use crate::nostr::nwc::{
55+
BudgetPeriod, BudgetedSpendingConditions, NwcProfileTag, SpendingConditions,
56+
};
5557
use crate::storage::{MutinyStorage, DEVICE_ID_KEY, EXPECTED_NETWORK_KEY, NEED_FULL_SYNC_KEY};
5658
use crate::{error::MutinyError, nostr::ReservedProfile};
5759
use crate::{nodemanager::NodeManager, nostr::ProfileType};
@@ -357,7 +359,8 @@ impl<S: MutinyStorage> MutinyWallet<S> {
357359
// account for 3 day grace period
358360
if expired_time + 86_400 * 3 > crate::utils::now().as_secs() {
359361
// now submit the NWC string if never created before
360-
self.ensure_mutiny_nwc_profile(subscription_client).await?;
362+
self.ensure_mutiny_nwc_profile(subscription_client, false)
363+
.await?;
361364
}
362365
}
363366
Ok(expired)
@@ -367,7 +370,11 @@ impl<S: MutinyStorage> MutinyWallet<S> {
367370
}
368371

369372
/// Pay the subscription invoice. This will post a NWC automatically afterwards.
370-
pub async fn pay_subscription_invoice(&self, inv: &Bolt11Invoice) -> Result<(), MutinyError> {
373+
pub async fn pay_subscription_invoice(
374+
&self,
375+
inv: &Bolt11Invoice,
376+
autopay: bool,
377+
) -> Result<(), MutinyError> {
371378
if let Some(subscription_client) = self.node_manager.subscription_client.clone() {
372379
let nodes = self.node_manager.nodes.lock().await;
373380
let first_node_pubkey = if let Some(node) = nodes.values().next() {
@@ -388,7 +395,8 @@ impl<S: MutinyStorage> MutinyWallet<S> {
388395
.await?;
389396

390397
// now submit the NWC string if never created before
391-
self.ensure_mutiny_nwc_profile(subscription_client).await?;
398+
self.ensure_mutiny_nwc_profile(subscription_client, autopay)
399+
.await?;
392400

393401
Ok(())
394402
} else {
@@ -399,6 +407,7 @@ impl<S: MutinyStorage> MutinyWallet<S> {
399407
async fn ensure_mutiny_nwc_profile(
400408
&self,
401409
subscription_client: Arc<subscription::MutinySubscriptionClient>,
410+
autopay: bool,
402411
) -> Result<(), MutinyError> {
403412
let nwc_profiles = self.nostr.profiles();
404413
let reserved_profile_index = ReservedProfile::MutinySubscription.info().1;
@@ -409,16 +418,33 @@ impl<S: MutinyStorage> MutinyWallet<S> {
409418
match profile_opt {
410419
None => {
411420
// profile with the reserved index does not exist, create a new one
412-
let profile = self
413-
.nostr
414-
.create_new_nwc_profile(
415-
ProfileType::Reserved(ReservedProfile::MutinySubscription),
416-
SpendingConditions::RequireApproval,
417-
NwcProfileTag::Subscription,
418-
)
419-
.await?;
421+
let nwc = if autopay {
422+
self.nostr
423+
.create_new_nwc_profile(
424+
ProfileType::Reserved(ReservedProfile::MutinySubscription),
425+
SpendingConditions::Budget(BudgetedSpendingConditions {
426+
budget: 21_000,
427+
single_max: None,
428+
payments: vec![],
429+
period: BudgetPeriod::Month,
430+
}),
431+
NwcProfileTag::Subscription,
432+
)
433+
.await?
434+
.nwc_uri
435+
} else {
436+
self.nostr
437+
.create_new_nwc_profile(
438+
ProfileType::Reserved(ReservedProfile::MutinySubscription),
439+
SpendingConditions::RequireApproval,
440+
NwcProfileTag::Subscription,
441+
)
442+
.await?
443+
.nwc_uri
444+
};
445+
420446
// only should have to submit the NWC if never created locally before
421-
subscription_client.submit_nwc(profile.nwc_uri).await?;
447+
subscription_client.submit_nwc(nwc).await?;
422448
}
423449
Some(profile) => {
424450
if profile.tag != NwcProfileTag::Subscription {

mutiny-wasm/src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,9 +1345,15 @@ impl MutinyWallet {
13451345
}
13461346

13471347
/// Pay the subscription invoice. This will post a NWC automatically afterwards.
1348-
pub async fn pay_subscription_invoice(&self, invoice_str: String) -> Result<(), MutinyJsError> {
1348+
pub async fn pay_subscription_invoice(
1349+
&self,
1350+
invoice_str: String,
1351+
autopay: bool,
1352+
) -> Result<(), MutinyJsError> {
13491353
let invoice = Bolt11Invoice::from_str(&invoice_str)?;
1350-
self.inner.pay_subscription_invoice(&invoice).await?;
1354+
self.inner
1355+
.pay_subscription_invoice(&invoice, autopay)
1356+
.await?;
13511357
Ok(())
13521358
}
13531359

0 commit comments

Comments
 (0)