Skip to content

Commit ecf91cb

Browse files
committed
Refactor receive with supported_quantity
We now only build an offer with a supported_quantity when the user set the quantity to Some. To fix the tests I set the quantity to 1 while building the offers.
1 parent 09c4735 commit ecf91cb

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

src/payment/bolt12.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -255,27 +255,23 @@ impl Bolt12Payment {
255255
Error::OfferCreationFailed
256256
})?;
257257

258-
let user_set_qty = if let Some(qty) = quantity {
258+
let mut offer =
259+
offer_builder.amount_msats(amount_msat).description(description.to_string());
260+
261+
if let Some(qty) = quantity {
259262
if qty == 0 {
260263
return Err(Error::InvalidQuantity);
261264
} else {
262-
Quantity::Bounded(NonZeroU64::new(qty).unwrap())
263-
}
264-
} else {
265-
Quantity::Unbounded
265+
offer = offer.supported_quantity(Quantity::Bounded(NonZeroU64::new(qty).unwrap()))
266+
};
266267
};
267268

268-
let offer = offer_builder
269-
.amount_msats(amount_msat)
270-
.description(description.to_string())
271-
.supported_quantity(user_set_qty)
272-
.build()
273-
.map_err(|e| {
274-
log_error!(self.logger, "Failed to create offer: {:?}", e);
275-
Error::OfferCreationFailed
276-
})?;
269+
let finalized_offer = offer.build().map_err(|e| {
270+
log_error!(self.logger, "Failed to create offer: {:?}", e);
271+
Error::OfferCreationFailed
272+
})?;
277273

278-
Ok(offer)
274+
Ok(finalized_offer)
279275
}
280276

281277
/// Returns a payable offer that can be used to request and receive a payment for which the

tests/integration_tests_rust.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ fn simple_bolt12_send_receive() {
424424
std::thread::sleep(std::time::Duration::from_secs(1));
425425

426426
let expected_amount_msat = 100_000_000;
427-
let offer = node_b.bolt12_payment().receive(expected_amount_msat, "asdf", None).unwrap();
427+
let offer = node_b.bolt12_payment().receive(expected_amount_msat, "asdf", Some(1)).unwrap();
428428
let expected_quantity = Some(1);
429429
let expected_payer_note = Some("Test".to_string());
430430
let payment_id = node_a
@@ -478,7 +478,7 @@ fn simple_bolt12_send_receive() {
478478
let offer_amount_msat = 100_000_000;
479479
let less_than_offer_amount = offer_amount_msat - 10_000;
480480
let expected_amount_msat = offer_amount_msat + 10_000;
481-
let offer = node_b.bolt12_payment().receive(offer_amount_msat, "asdf", None).unwrap();
481+
let offer = node_b.bolt12_payment().receive(offer_amount_msat, "asdf", Some(1)).unwrap();
482482
let expected_quantity = Some(1);
483483
let expected_payer_note = Some("Test".to_string());
484484
assert!(node_a

0 commit comments

Comments
 (0)