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: Handle error when requesting a hodl invoice #2573

Merged
merged 1 commit into from
May 29, 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
4 changes: 2 additions & 2 deletions mobile/lib/features/trade/application/order_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import 'package:get_10101/ffi.dart' as rust;

class ExternalFunding {
final String bitcoinAddress;
final String paymentRequest;
final String? paymentRequest;

const ExternalFunding({required this.bitcoinAddress, required this.paymentRequest});
const ExternalFunding({required this.bitcoinAddress, this.paymentRequest});

static ExternalFunding fromApi(rust.ExternalFunding funding) {
return ExternalFunding(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ class _ChannelFunding extends State<ChannelFunding> {

final qrCode = switch (selectedBox) {
FundingType.lightning => CustomQrCode(
data: widget.funding.paymentRequest,
embeddedImage: const AssetImage("assets/10101_logo_icon_white_background.png"),
data: widget.funding.paymentRequest ?? "https://x.com/get10101",
embeddedImage: widget.funding.paymentRequest != null
? const AssetImage("assets/10101_logo_icon_white_background.png")
: const AssetImage("assets/coming_soon.png"),
embeddedImageSizeHeight: widget.funding.paymentRequest != null ? 50 : 350,
embeddedImageSizeWidth: widget.funding.paymentRequest != null ? 50 : 350,
dimension: 300,
),
FundingType.onchain => CustomQrCode(
Expand All @@ -87,9 +91,10 @@ class _ChannelFunding extends State<ChannelFunding> {
};

final qrCodeSubTitle = switch (selectedBox) {
FundingType.lightning => widget.funding.paymentRequest,
FundingType.lightning =>
widget.funding.paymentRequest ?? "Currently not available. Try again later.",
FundingType.onchain => widget.funding.bitcoinAddress,
FundingType.unified || FundingType.external => "Follow us on social media for updates",
FundingType.unified || FundingType.external => "Follow us on social media for updates.",
};

return Scaffold(
Expand Down
1 change: 1 addition & 0 deletions mobile/native/src/hodl_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use bitcoin::Amount;
use reqwest::Url;
use xxi_node::commons;

#[derive(Clone)]
pub struct HodlInvoice {
pub payment_request: String,
pub pre_image: String,
Expand Down
16 changes: 11 additions & 5 deletions mobile/native/src/unfunded_channel_opening_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use xxi_node::commons::ChannelOpeningParams;

pub struct ExternalFunding {
pub bitcoin_address: String,
pub payment_request: String,
/// The payment request of the hodl invoice. Could be none if the lnd node is down.
pub payment_request: Option<String>,
}

/// handles orders which would open a channel where the user does not have funds in his wallets
Expand All @@ -39,15 +40,20 @@ pub async fn submit_unfunded_channel_opening_order(
+ crate::dlc::estimated_funding_tx_fee()?;

let funding_amount = Amount::from_sat(estimated_margin + trader_reserve) + fees;
let hodl_invoice = hodl_invoice::get_hodl_invoice_from_coordinator(funding_amount).await?;
let pre_image = hodl_invoice.pre_image;
let hodl_invoice = hodl_invoice::get_hodl_invoice_from_coordinator(funding_amount)
.await
.inspect_err(|e| tracing::error!("Failed to get hodl invoice. Error: {e:#}"))
.ok();

let payment_request = hodl_invoice.clone().map(|invoice| invoice.payment_request);

// abort previous watcher before starting new task.
abort_watcher().await?;

let runtime = crate::state::get_or_create_tokio_runtime()?;
let watch_handle = runtime.spawn({
let bitcoin_address = bitcoin_address.clone();
let pre_image = hodl_invoice.map(|invoice| invoice.pre_image);
async move {
event::publish(&EventInternal::FundingChannelNotification(
FundingChannelTask::Pending,
Expand All @@ -63,7 +69,7 @@ pub async fn submit_unfunded_channel_opening_order(
_ = watcher::watch_lightning_payment() => {
// received lightning payment.
tracing::info!(%funding_amount, "Found lighting payment.");
Some(pre_image)
pre_image
}
};

Expand Down Expand Up @@ -109,7 +115,7 @@ pub async fn submit_unfunded_channel_opening_order(

Ok(ExternalFunding {
bitcoin_address: bitcoin_address.to_string(),
payment_request: hodl_invoice.payment_request,
payment_request,
})
}

Expand Down
Loading