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

Commit 6209c60

Browse files
committed
Try mutiny2mutiny offers
1 parent f4f9d87 commit 6209c60

File tree

3 files changed

+98
-10
lines changed

3 files changed

+98
-10
lines changed

mutiny-core/src/node.rs

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use crate::utils::get_monitor_version;
4343
use bitcoin::util::bip32::ExtendedPrivKey;
4444
use lightning::events::bump_transaction::{BumpTransactionEventHandler, Wallet};
4545
use lightning::ln::PaymentSecret;
46-
use lightning::offers::offer::Offer;
46+
use lightning::offers::offer::{Offer, Quantity};
4747
use lightning::onion_message::OnionMessenger as LdkOnionMessenger;
4848
use lightning::sign::{EntropySource, InMemorySigner, NodeSigner, Recipient};
4949
use lightning::util::config::MaxDustHTLCExposure;
@@ -944,6 +944,43 @@ impl<S: MutinyStorage> Node<S> {
944944
Ok(invoice)
945945
}
946946

947+
pub async fn create_offer(
948+
&self,
949+
amount_sat: Option<u64>,
950+
labels: Vec<String>,
951+
) -> Result<Offer, MutinyError> {
952+
// wait for first sync to complete
953+
for _ in 0..60 {
954+
// check if we've been stopped
955+
if self.stop.load(Ordering::Relaxed) {
956+
return Err(MutinyError::NotRunning);
957+
}
958+
959+
if let Ok(true) = self.persister.storage.has_done_first_sync() {
960+
break;
961+
}
962+
963+
sleep(1_000).await;
964+
}
965+
966+
let mut builder = self
967+
.channel_manager
968+
.create_offer_builder("".to_string())
969+
.supported_quantity(Quantity::Unbounded);
970+
971+
if let Some(amount_sats) = amount_sat {
972+
builder = builder.amount_msats(amount_sats * 1_000);
973+
}
974+
975+
let offer = builder.build()?;
976+
977+
self.persister
978+
.storage
979+
.set_invoice_labels(offer.to_string(), labels)?;
980+
981+
Ok(offer)
982+
}
983+
947984
pub fn get_invoice(&self, invoice: &Bolt11Invoice) -> Result<MutinyInvoice, MutinyError> {
948985
self.get_invoice_by_hash(invoice.payment_hash())
949986
}
@@ -1225,16 +1262,32 @@ impl<S: MutinyStorage> Node<S> {
12251262
getrandom::getrandom(&mut bytes).map_err(|_| MutinyError::SeedGenerationFailed)?;
12261263
let payment_id = PaymentId(bytes);
12271264

1265+
let quantity = match quantity {
1266+
None => {
1267+
if offer.expects_quantity() {
1268+
Some(1)
1269+
} else {
1270+
None
1271+
}
1272+
}
1273+
Some(q) => Some(q),
1274+
};
1275+
12281276
let amount_msats = amt_sats.map(|a| a * 1_000);
1229-
self.channel_manager.pay_for_offer(
1230-
&offer,
1231-
quantity,
1232-
amount_msats,
1233-
payer_note,
1234-
payment_id,
1235-
Self::retry_strategy(),
1236-
None,
1237-
)?;
1277+
self.channel_manager
1278+
.pay_for_offer(
1279+
&offer,
1280+
quantity,
1281+
amount_msats,
1282+
payer_note,
1283+
payment_id,
1284+
Self::retry_strategy(),
1285+
None,
1286+
)
1287+
.map_err(|e| {
1288+
log_error!(self.logger, "failed to make payment: {e:?}");
1289+
e
1290+
})?;
12381291

12391292
// persist and label offer after calling pay_for_offer, it only fails if we can't initiate a payment
12401293

mutiny-core/src/nodemanager.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,20 @@ impl<S: MutinyStorage> NodeManager<S> {
16621662
Ok(invoice.into())
16631663
}
16641664

1665+
/// Creates a lightning offer. The amount should be in satoshis.
1666+
/// If no amount is provided, the offer will be created with no amount.
1667+
pub async fn create_offer(
1668+
&self,
1669+
from_node: &PublicKey,
1670+
amount: Option<u64>,
1671+
labels: Vec<String>,
1672+
) -> Result<Offer, MutinyError> {
1673+
let node = self.get_node(from_node).await?;
1674+
let offer = node.create_offer(amount, labels).await?;
1675+
1676+
Ok(offer)
1677+
}
1678+
16651679
/// Pays a lightning invoice from the selected node.
16661680
/// An amount should only be provided if the invoice does not have an amount.
16671681
/// The amount should be in satoshis.

mutiny-wasm/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,27 @@ impl MutinyWallet {
646646
.into())
647647
}
648648

649+
/// Creates a lightning offer. The amount should be in satoshis.
650+
/// If no amount is provided, the offer will be created with no amount.
651+
#[wasm_bindgen]
652+
pub async fn create_offer(
653+
&self,
654+
from_node: String,
655+
amount: Option<u64>,
656+
labels: &JsValue, /* Vec<String> */
657+
) -> Result<String, MutinyJsError> {
658+
let from_node = PublicKey::from_str(&from_node)?;
659+
let labels: Vec<String> = labels
660+
.into_serde()
661+
.map_err(|_| MutinyJsError::InvalidArgumentsError)?;
662+
Ok(self
663+
.inner
664+
.node_manager
665+
.create_offer(&from_node, amount, labels)
666+
.await?
667+
.to_string())
668+
}
669+
649670
/// Pays a lightning invoice from the selected node.
650671
/// An amount should only be provided if the invoice does not have an amount.
651672
/// The amount should be in satoshis.

0 commit comments

Comments
 (0)