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

Fix: send on-chain #1900

Merged
merged 3 commits into from
Jan 25, 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
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
73 changes: 45 additions & 28 deletions mobile/lib/features/wallet/receive_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -241,34 +241,51 @@ class BitcoinAddress extends StatelessWidget {
var address = this.address.replaceAll("bitcoin:", '');

return Container(
margin: const EdgeInsets.fromLTRB(0, 15, 0, 0),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey.shade200),
borderRadius: BorderRadius.circular(10),
color: tenTenOnePurple.shade200.withOpacity(0.1)),
child: Container(
padding: const EdgeInsets.all(20),
child: Opacity(
opacity: 1.0,
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
const Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [Text("Address", style: TextStyle(fontSize: 18))]),
const SizedBox(height: 5),
Row(
children: [
Expanded(
child: Text(
address,
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
),
],
)
])),
),
);
margin: const EdgeInsets.fromLTRB(0, 15, 0, 0),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey.shade200),
borderRadius: BorderRadius.circular(10),
color: tenTenOnePurple.shade200.withOpacity(0.1)),
child: GestureDetector(
onTap: () {
Clipboard.setData(ClipboardData(text: address)).then((_) {
showSnackBar(ScaffoldMessenger.of(context), "Address copied to clipboard");
});
},
child: Container(
padding: const EdgeInsets.all(20),
child: Opacity(
opacity: 1.0,
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
const Text("Address", style: TextStyle(fontSize: 18)),
IconButton(
onPressed: () {
Clipboard.setData(ClipboardData(text: address)).then((_) {
showSnackBar(
ScaffoldMessenger.of(context), "Address copied to clipboard");
});
},
icon: const Icon(
Icons.copy,
size: 16,
))
]),
const SizedBox(height: 5),
Row(
children: [
Expanded(
child: Text(
address,
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
),
],
)
])),
),
));
}
}

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: To be consistent with the previous implementation this should go after the try catch.

However, this will anyways not be needed when we drop uspd and lightning payments.

} catch (error) {
logger.e("Failed to send payment: $error");
context.read<PaymentChangeNotifier>().failPayment();
}).whenComplete(() => setState(() => sent = true));
}
}

switch (paymentChangeNotifier.getPaymentStatus()) {
Expand Down
5 changes: 2 additions & 3 deletions mobile/lib/features/wallet/send/fee_picker.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:get_10101/common/amount_and_fiat_text.dart';
import 'package:get_10101/common/amount_text.dart';
import 'package:get_10101/common/amount_text_input_form_field.dart';
import 'package:get_10101/common/color.dart';
import 'package:get_10101/common/domain/model.dart';
Expand Down Expand Up @@ -153,8 +152,8 @@ class _FeePickerModalState extends State<_FeePickerModal> {

final amt = Amount.parseAmount(val);

if (minFee > amt.sats) {
return "The minimum fee to broadcast the transaction is ${formatSats(Amount(minFee))}.";
if (amt.sats < 1) {
return "The minimum fee to broadcast the transaction is 1 sat/vbyte)}.";
}

return null;
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
Loading