Skip to content

Commit

Permalink
fix: send on-chain transaction and return to home screen
Browse files Browse the repository at this point in the history
It looks like the issue was a problem of poping the modal async. Since we don't do a lightning payment at the moment we can fall back to good ol sync payments.

Signed-off-by: Philipp Hoenisch <philipp@coblox.tech>
  • Loading branch information
bonomat committed Jan 25, 2024
1 parent 17599a8 commit 4fa3816
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
9 changes: 6 additions & 3 deletions mobile/lib/features/wallet/application/wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,12 @@ class WalletService {
};
}

Future<void> sendPayment(Destination destination, Amount? amount, {Fee? fee}) async {
logger.i("Sending payment of $amount");
await rust.api.sendPayment(payment: _createPayment(destination, amount, fee: fee));
String sendOnChainPayment(Destination destination, Amount? amount, {Fee? fee}) {
var feeApi = fee!.toAPI();
var sats = amount!.sats;
var address = destination.raw;
logger.i("Sending payment of $amount to $address with fee $feeApi");
return rust.api.sendOnChainPayment(address: address, amount: sats, fee: feeApi);
}

String getUnusedAddress() {
Expand Down
8 changes: 5 additions & 3 deletions mobile/lib/features/wallet/send/confirm_payment_modal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,14 @@ class ConfirmPayment extends StatelessWidget {
}
showExecuteUsdpPaymentModal(context, destination, amt, payWithUsdp);
} else {
walletService.sendPayment(destination, amt, fee: fee).then((value) {
try {
var txid = walletService.sendOnChainPayment(destination, amt, fee: fee);
showSnackBar(messenger, "Transaction broadcasted $txid");
GoRouter.of(context).pop();
}).catchError((error) {
} catch (error) {
logger.e("Failed to send payment: $error");
showSnackBar(messenger, error.toString());
});
}
}
}),
const SizedBox(height: 24),
Expand Down
7 changes: 5 additions & 2 deletions mobile/lib/features/wallet/send/execute_payment_modal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,13 @@ class _ExecuteUsdpPaymentState extends State<ExecuteUsdpPayment> {
if (widget.payWithUsdp) {
logger.d("Order has been filled, attempting to send payment");
}
walletService.sendPayment(widget.destination, widget.amount).catchError((error) {
try {
walletService.sendOnChainPayment(widget.destination, widget.amount);
setState(() => sent = true);
} catch (error) {
logger.e("Failed to send payment: $error");
context.read<PaymentChangeNotifier>().failPayment();
}).whenComplete(() => setState(() => sent = true));
}
}

switch (paymentChangeNotifier.getPaymentStatus()) {
Expand Down
4 changes: 4 additions & 0 deletions mobile/native/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,10 @@ pub fn send_payment(payment: SendPayment) -> Result<()> {
runtime.block_on(async { ln_dlc::send_payment(payment).await })
}

pub fn send_on_chain_payment(address: String, amount: u64, fee: Fee) -> Result<SyncReturn<String>> {
ln_dlc::send_on_chain_payment(address, amount, fee).map(|txid| SyncReturn(txid.to_string()))
}

pub fn send_preflight_probe(payment: SendPayment) -> Result<u64> {
let runtime = crate::state::get_or_create_tokio_runtime()?;
runtime.block_on(async { ln_dlc::estimate_payment_fee_msat(payment).await })
Expand Down
7 changes: 7 additions & 0 deletions mobile/native/src/ln_dlc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,13 @@ pub async fn send_payment(payment: SendPayment) -> Result<()> {
Ok(())
}

pub fn send_on_chain_payment(address: String, amount: u64, fee: Fee) -> Result<Txid> {
let address = Address::from_str(&address)?;
state::get_node()
.inner
.send_to_address(&address, amount, fee.into())
}

pub async fn estimate_payment_fee_msat(payment: SendPayment) -> Result<u64> {
match payment {
SendPayment::Lightning { invoice, amount } => {
Expand Down

0 comments on commit 4fa3816

Please sign in to comment.