-
Notifications
You must be signed in to change notification settings - Fork 23
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: Pay lightning invoice with USDP #1706
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,18 +2,24 @@ import 'package:flutter/material.dart'; | |||||
import 'package:get_10101/common/color.dart'; | ||||||
import 'package:get_10101/common/domain/model.dart'; | ||||||
import 'package:get_10101/common/snack_bar.dart'; | ||||||
import 'package:get_10101/features/trade/domain/direction.dart'; | ||||||
import 'package:get_10101/features/trade/domain/leverage.dart'; | ||||||
import 'package:get_10101/features/trade/submit_order_change_notifier.dart'; | ||||||
import 'package:get_10101/features/trade/trade_value_change_notifier.dart'; | ||||||
import 'package:get_10101/features/wallet/application/util.dart'; | ||||||
import 'package:get_10101/features/wallet/domain/destination.dart'; | ||||||
import 'package:get_10101/features/wallet/domain/wallet_type.dart'; | ||||||
import 'package:get_10101/features/wallet/send/execute_payment_modal.dart'; | ||||||
import 'package:get_10101/features/wallet/send/payment_sent_change_notifier.dart'; | ||||||
import 'package:get_10101/features/wallet/send/send_dialog.dart'; | ||||||
import 'package:get_10101/features/wallet/wallet_change_notifier.dart'; | ||||||
import 'package:get_10101/logger/logger.dart'; | ||||||
import 'package:go_router/go_router.dart'; | ||||||
import 'package:intl/intl.dart'; | ||||||
import 'package:provider/provider.dart'; | ||||||
import 'package:slide_to_confirm/slide_to_confirm.dart'; | ||||||
|
||||||
void showConfirmPaymentModal(BuildContext context, Destination destination, Amount? amount) { | ||||||
void showConfirmPaymentModal( | ||||||
BuildContext context, Destination destination, bool payWithUsdp, Amount sats, Amount usdp) { | ||||||
showModalBottomSheet<void>( | ||||||
shape: const RoundedRectangleBorder( | ||||||
borderRadius: BorderRadius.vertical( | ||||||
|
@@ -27,45 +33,110 @@ void showConfirmPaymentModal(BuildContext context, Destination destination, Amou | |||||
builder: (BuildContext context) { | ||||||
return SingleChildScrollView( | ||||||
child: SizedBox( | ||||||
height: 320, | ||||||
height: 420, | ||||||
child: Scaffold( | ||||||
body: ConfirmPayment( | ||||||
payWithUsdp: payWithUsdp, | ||||||
destination: destination, | ||||||
amount: amount, | ||||||
sats: sats, | ||||||
usdp: usdp, | ||||||
)))); | ||||||
}); | ||||||
} | ||||||
|
||||||
class ConfirmPayment extends StatelessWidget { | ||||||
final Destination destination; | ||||||
final Amount? amount; | ||||||
final bool payWithUsdp; | ||||||
final Amount sats; | ||||||
final Amount usdp; | ||||||
|
||||||
const ConfirmPayment({super.key, required this.destination, this.amount}); | ||||||
const ConfirmPayment( | ||||||
{super.key, | ||||||
required this.destination, | ||||||
required this.payWithUsdp, | ||||||
required this.sats, | ||||||
required this.usdp}); | ||||||
|
||||||
@override | ||||||
Widget build(BuildContext context) { | ||||||
final walletService = context.read<WalletChangeNotifier>().service; | ||||||
final submitOderChangeNotifier = context.read<SubmitOrderChangeNotifier>(); | ||||||
final formatter = NumberFormat("#,###,##0.00", "en"); | ||||||
|
||||||
final amt = destination.amount.sats > 0 ? destination.amount : amount!; | ||||||
final tradeValuesChangeNotifier = context.watch<TradeValuesChangeNotifier>(); | ||||||
|
||||||
final tradeValues = tradeValuesChangeNotifier.fromDirection(Direction.long); | ||||||
tradeValues.updateLeverage(Leverage(1)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need to update the leverage? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the default leverage is 2 and if I wouldn't set it to 1 we would get an incorrect sats amount from the quantity. |
||||||
|
||||||
Amount amt = destination.amount; | ||||||
if (destination.amount.sats == 0) { | ||||||
if (payWithUsdp) { | ||||||
// if the destination does not specify an amount and we ar paying with the usdp balance we | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🙃
Suggested change
|
||||||
// calculate the amount from the quantity point of view. | ||||||
tradeValues.updateQuantity(usdp); | ||||||
amt = tradeValues.margin!; | ||||||
} else { | ||||||
// Otherwise it is a regular lightning payment and we just pay the given amount. | ||||||
amt = sats; | ||||||
} | ||||||
} else { | ||||||
// if the amount is set on the invoice we need to pay the amount no matter what. That might | ||||||
// lead to the usdp amount to jump by one dollar depending on the current bid price | ||||||
tradeValues.updateMargin(destination.amount); | ||||||
} | ||||||
|
||||||
return SafeArea( | ||||||
child: Padding( | ||||||
child: Container( | ||||||
color: Colors.white, | ||||||
padding: const EdgeInsets.only(left: 20.0, top: 35.0, right: 20.0), | ||||||
child: Column( | ||||||
crossAxisAlignment: CrossAxisAlignment.stretch, | ||||||
crossAxisAlignment: CrossAxisAlignment.center, | ||||||
children: [ | ||||||
const Text("Destination:", style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16)), | ||||||
const SizedBox(height: 2), | ||||||
Text(truncateWithEllipsis(32, destination.raw), style: const TextStyle(fontSize: 16)), | ||||||
const SizedBox(height: 20), | ||||||
const Text("Payee:", style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16)), | ||||||
const SizedBox(height: 2), | ||||||
Text(truncateWithEllipsis(32, destination.payee), style: const TextStyle(fontSize: 16)), | ||||||
const SizedBox(height: 20), | ||||||
const Text("Amount:", style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16)), | ||||||
const SizedBox(height: 2), | ||||||
Text(amt.toString(), style: const TextStyle(fontSize: 16)), | ||||||
const SizedBox(height: 25), | ||||||
Column( | ||||||
crossAxisAlignment: CrossAxisAlignment.start, | ||||||
children: [ | ||||||
const Text("Summary", style: TextStyle(fontSize: 20)), | ||||||
const SizedBox(height: 10), | ||||||
Container( | ||||||
padding: const EdgeInsets.all(20), | ||||||
decoration: BoxDecoration( | ||||||
color: tenTenOnePurple.shade200.withOpacity(0.1), | ||||||
borderRadius: BorderRadius.circular(8)), | ||||||
child: Column( | ||||||
crossAxisAlignment: CrossAxisAlignment.stretch, | ||||||
children: [ | ||||||
const Text("Amount", style: TextStyle(color: Colors.grey, fontSize: 16)), | ||||||
const SizedBox(height: 5), | ||||||
Visibility( | ||||||
visible: payWithUsdp, | ||||||
replacement: Text(amt.sats == 0 ? "Max" : amt.toString(), | ||||||
style: const TextStyle(fontSize: 16)), | ||||||
child: Row( | ||||||
mainAxisAlignment: MainAxisAlignment.spaceBetween, | ||||||
children: [ | ||||||
Text("~ \$ ${formatter.format(tradeValues.quantity?.toInt ?? 0)}", | ||||||
style: const TextStyle(fontSize: 16)), | ||||||
Text(amt.toString(), | ||||||
style: const TextStyle(fontSize: 16, color: Colors.grey)) | ||||||
], | ||||||
)), | ||||||
const Divider(height: 40, indent: 0, endIndent: 0), | ||||||
const Text("Destination", style: TextStyle(color: Colors.grey, fontSize: 16)), | ||||||
const SizedBox(height: 5), | ||||||
Text(truncateWithEllipsis(26, destination.raw), | ||||||
style: const TextStyle(fontSize: 16)), | ||||||
const Divider(height: 40, indent: 0, endIndent: 0), | ||||||
const Text("Payee", style: TextStyle(color: Colors.grey, fontSize: 16)), | ||||||
const SizedBox(height: 5), | ||||||
Text(truncateWithEllipsis(26, destination.payee), | ||||||
style: const TextStyle(fontSize: 16)), | ||||||
const SizedBox(height: 10), | ||||||
], | ||||||
), | ||||||
) | ||||||
], | ||||||
), | ||||||
const SizedBox(height: 15), | ||||||
ConfirmationSlider( | ||||||
text: "Swipe to confirm", | ||||||
textStyle: const TextStyle(color: Colors.black87), | ||||||
|
@@ -77,30 +148,22 @@ class ConfirmPayment extends StatelessWidget { | |||||
size: 20, | ||||||
), | ||||||
onConfirmation: () async { | ||||||
context.read<PaymentChangeNotifier>().waitForPayment(); | ||||||
GoRouter.of(context).pop(); | ||||||
final messenger = ScaffoldMessenger.of(context); | ||||||
if (destination.getWalletType() == WalletType.lightning) { | ||||||
showDialog( | ||||||
context: context, | ||||||
useRootNavigator: true, | ||||||
barrierDismissible: false, // Prevent user from leaving | ||||||
builder: (BuildContext context) { | ||||||
return SendDialog(destination: destination, amount: amt); | ||||||
}); | ||||||
} | ||||||
|
||||||
walletService.sendPayment(destination, amt).then((value) { | ||||||
if (destination.getWalletType() == WalletType.onChain) { | ||||||
GoRouter.of(context).pop(); | ||||||
context.read<PaymentChangeNotifier>().waitForPayment(); | ||||||
if (payWithUsdp) { | ||||||
submitOderChangeNotifier.submitPendingOrder(tradeValues, PositionAction.open); | ||||||
} | ||||||
}).catchError((error) { | ||||||
logger.e("Failed to send payment: $error"); | ||||||
if (destination.getWalletType() == WalletType.onChain) { | ||||||
showExecuteUsdpPaymentModal(context, destination, amt, payWithUsdp); | ||||||
} else { | ||||||
walletService.sendPayment(destination, amt).then((value) { | ||||||
GoRouter.of(context).pop(); | ||||||
}).catchError((error) { | ||||||
logger.e("Failed to send payment: $error"); | ||||||
showSnackBar(messenger, error.toString()); | ||||||
} | ||||||
context.read<PaymentChangeNotifier>().failPayment(); | ||||||
}); | ||||||
}); | ||||||
} | ||||||
}) | ||||||
], | ||||||
), | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
of course 420, it's a multiple of 42!