diff --git a/lib/di.dart b/lib/di.dart index 40d75008e..e5f84d889 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -608,7 +608,6 @@ Future setup({ authService: getIt.get(), initialPaymentRequest: initialPaymentRequest, )); - getIt.registerFactory( () => SendTemplatePage(sendTemplateViewModel: getIt.get())); @@ -1200,11 +1199,6 @@ Future setup({ getIt.registerFactory(() => NFTViewModel(appStore, getIt.get())); getIt.registerFactory(() => TorPage(getIt.get())); - getIt.registerFactory(() => LightningReceiveOnchainPage( - addressListViewModel: getIt.get(), - receiveOptionViewModel: ReceiveOptionViewModel(getIt.get().wallet!, ReceivePageOption.lightningOnchain), - )); - getIt.registerFactoryParam, void>((args, _) { final address = args.first as String; final pageOption = args.last as ReceivePageOption; @@ -1213,29 +1207,38 @@ Future setup({ getIt.get(), getIt.get().wallet!, getIt.get(), + LightningViewModel(), pageOption, ); }); - // getIt.registerFactory(() => LightningInvoicePage( - // lightningInvoicePageViewModel: getIt.get(), - // lightningViewModel: LightningViewModel(), - // receiveOptionViewModel: ReceiveOptionViewModel(getIt.get().wallet!, ReceivePageOption.lightningInvoice), + // getIt.registerFactory(() => LightningReceiveOnchainPage( + // addressListViewModel: getIt.get(), + // receiveOptionViewModel: ReceiveOptionViewModel(getIt.get().wallet!, ReceivePageOption.lightningOnchain), // )); - - + getIt.registerFactoryParam, void>( + (List args, _) { + final pageOption = args.last as ReceivePageOption; + return LightningReceiveOnchainPage( + addressListViewModel: getIt.get(), + // lightningViewModel: LightningViewModel(), + receiveOptionViewModel: getIt.get(param1: pageOption)); + }); getIt.registerFactoryParam, void>((List args, _) { final pageOption = args.last as ReceivePageOption; return LightningInvoicePage( - lightningInvoicePageViewModel: getIt.get(param1: args), - lightningViewModel: LightningViewModel(), - receiveOptionViewModel: getIt.get(param1: pageOption)); + lightningInvoicePageViewModel: getIt.get(param1: args), + lightningViewModel: LightningViewModel(), + receiveOptionViewModel: getIt.get(param1: pageOption)); }); - - // getIt.registerFactory(() => LightningSendPage(authService: , sendViewModel: , initialPaymentRequest: ,)); + getIt.registerFactory(() => LightningSendPage( + sendViewModel: getIt.get(), + authService: getIt.get(), + initialPaymentRequest: null, + )); _isSetupFinished = true; } diff --git a/lib/router.dart b/lib/router.dart index 93be92f88..4eece2b84 100644 --- a/lib/router.dart +++ b/lib/router.dart @@ -641,8 +641,9 @@ Route createRoute(RouteSettings settings) { fullscreenDialog: true, builder: (_) => getIt.get()); case Routes.lightningReceiveOnchain: + final args = settings.arguments as List; return CupertinoPageRoute( - fullscreenDialog: true, builder: (_) => getIt.get()); + fullscreenDialog: true, builder: (_) => getIt.get(param1: args)); case Routes.lightningInvoice: final args = settings.arguments as List; diff --git a/lib/src/screens/receive/lightning_invoice_page.dart b/lib/src/screens/receive/lightning_invoice_page.dart index a352348e5..5a971b096 100644 --- a/lib/src/screens/receive/lightning_invoice_page.dart +++ b/lib/src/screens/receive/lightning_invoice_page.dart @@ -35,8 +35,6 @@ class LightningInvoicePage extends BasePage { required this.receiveOptionViewModel, }) : _amountFocusNode = FocusNode() {} - final _nameController = TextEditingController(); - final _emailController = TextEditingController(); final _descriptionController = TextEditingController(); final _amountController = TextEditingController(); final FocusNode _amountFocusNode; @@ -138,48 +136,36 @@ class LightningInvoicePage extends BasePage { bottomSection: Observer(builder: (_) { return Column( children: [ - Padding( - padding: EdgeInsets.only(bottom: 15), - child: Center( - child: Text( - S.of(context).anonpay_description("an invoice", "pay"), - textAlign: TextAlign.center, - style: TextStyle( - color: Theme.of(context) - .extension()! - .receiveAmountColor, - fontWeight: FontWeight.w500, - fontSize: 12), - ), - ), - ), - // LoadingPrimaryButton( - // text: isInvoice - // ? S.of(context).create_invoice - // : S.of(context).create_donation_link, - // onPressed: () { - // FocusScope.of(context).unfocus(); - // anonInvoicePageViewModel.setRequestParams( - // inputAmount: _amountController.text, - // inputName: _nameController.text, - // inputEmail: _emailController.text, - // inputDescription: _descriptionController.text, - // ); - // if (anonInvoicePageViewModel.receipientEmail.isNotEmpty && - // _formKey.currentState != null && - // !_formKey.currentState!.validate()) { - // return; - // } - // if (isInvoice) { - // anonInvoicePageViewModel.createInvoice(); - // } else { - // anonInvoicePageViewModel.generateDonationLink(); - // } - // }, - // color: Theme.of(context).primaryColor, - // textColor: Colors.white, - // isLoading: anonInvoicePageViewModel.state is IsExecutingState, + // Padding( + // padding: EdgeInsets.only(bottom: 15), + // child: Center( + // child: Text( + // S.of(context).anonpay_description("an invoice", "pay"), + // textAlign: TextAlign.center, + // style: TextStyle( + // color: Theme.of(context) + // .extension()! + // .receiveAmountColor, + // fontWeight: FontWeight.w500, + // fontSize: 12), + // ), + // ), // ), + LoadingPrimaryButton( + text: S.of(context).create_invoice, + onPressed: () { + FocusScope.of(context).unfocus(); + lightningViewModel.createInvoice(amount: _amountController.text, description: _descriptionController.text); + lightningInvoicePageViewModel.setRequestParams( + inputAmount: _amountController.text, + inputDescription: _descriptionController.text, + ); + lightningInvoicePageViewModel.createInvoice(); + }, + color: Theme.of(context).primaryColor, + textColor: Colors.white, + isLoading: lightningInvoicePageViewModel.state is IsExecutingState, + ), ], ); }), @@ -194,33 +180,56 @@ class LightningInvoicePage extends BasePage { return; } - reaction((_) => receiveOptionViewModel.selectedReceiveOption, (ReceivePageOption option) { + reaction((_) => receiveOptionViewModel.selectedReceiveOption, (ReceivePageOption option) async { switch (option) { case ReceivePageOption.lightningInvoice: break; case ReceivePageOption.lightningOnchain: - Navigator.popAndPushNamed(context, Routes.lightningReceiveOnchain); + final address = await lightningViewModel.receiveOnchain(); + Navigator.popAndPushNamed( + context, + Routes.lightningReceiveOnchain, + arguments: [address, ReceivePageOption.lightningInvoice], + ); break; default: } }); - // reaction((_) => anonInvoicePageViewModel.state, (ExecutionState state) { - // if (state is ExecutedSuccessfullyState) { - // Navigator.pushNamed(context, Routes.anonPayReceivePage, arguments: state.payload); - // } - // if (state is FailureState) { - // showPopUp( - // context: context, - // builder: (BuildContext context) { - // return AlertWithOneAction( - // alertTitle: S.of(context).error, - // alertContent: state.error.toString(), - // buttonText: S.of(context).ok, - // buttonAction: () => Navigator.of(context).pop()); - // }); - // } - // }); + reaction((_) => lightningInvoicePageViewModel.state, (ExecutionState state) { + if (state is ExecutedSuccessfullyState) { + // Navigator.pushNamed(context, Routes.anonPayReceivePage, arguments: state.payload); + lightningViewModel.createInvoice( + amount: state.payload["amount"] as String, + description: state.payload["description"] as String?, + ); + } + + if (state is ExecutedSuccessfullyState) { + showPopUp( + context: context, + builder: (BuildContext context) { + return AlertWithOneAction( + // alertTitle: S.of(context).invoice_created, + alertTitle: "Invoice created TODO", + alertContent: state.payload as String, + buttonText: S.of(context).ok, + buttonAction: () => Navigator.of(context).pop()); + }); + } + + if (state is FailureState) { + showPopUp( + context: context, + builder: (BuildContext context) { + return AlertWithOneAction( + alertTitle: S.of(context).error, + alertContent: state.error.toString(), + buttonText: S.of(context).ok, + buttonAction: () => Navigator.of(context).pop()); + }); + } + }); effectsInstalled = true; } diff --git a/lib/view_model/lightning_invoice_page_view_model.dart b/lib/view_model/lightning_invoice_page_view_model.dart index 656f8e05a..2cadf89da 100644 --- a/lib/view_model/lightning_invoice_page_view_model.dart +++ b/lib/view_model/lightning_invoice_page_view_model.dart @@ -6,6 +6,7 @@ import 'package:cake_wallet/entities/fiat_currency.dart'; import 'package:cake_wallet/entities/preferences_key.dart'; import 'package:cake_wallet/entities/receive_page_option.dart'; import 'package:cake_wallet/store/settings_store.dart'; +import 'package:cake_wallet/view_model/lightning_view_model.dart'; import 'package:cw_core/crypto_currency.dart'; import 'package:cw_core/currency.dart'; import 'package:cw_core/wallet_base.dart'; @@ -16,23 +17,23 @@ import 'package:shared_preferences/shared_preferences.dart'; part 'lightning_invoice_page_view_model.g.dart'; -class LightningInvoicePageViewModel = LightningInvoicePageViewModelBase with _$LightningInvoicePageViewModel; +class LightningInvoicePageViewModel = LightningInvoicePageViewModelBase + with _$LightningInvoicePageViewModel; abstract class LightningInvoicePageViewModelBase with Store { LightningInvoicePageViewModelBase( this.address, this.settingsStore, this._wallet, - // this._anonpayInvoiceInfoSource, this.sharedPreferences, + this.lightningViewModel, this.pageOption, ) : description = '', amount = '', state = InitialExecutionState(), selectedCurrency = walletTypeToCryptoCurrency(_wallet.type), cryptoCurrency = walletTypeToCryptoCurrency(_wallet.type) { - // _getPreviousDonationLink(); - // _fetchLimits(); + _fetchLimits(); } List get currencies => [walletTypeToCryptoCurrency(_wallet.type), ...FiatCurrency.all]; @@ -42,6 +43,7 @@ abstract class LightningInvoicePageViewModelBase with Store { // final Box _anonpayInvoiceInfoSource; final SharedPreferences sharedPreferences; final ReceivePageOption pageOption; + final LightningViewModel lightningViewModel; @observable Currency selectedCurrency; @@ -97,18 +99,14 @@ abstract class LightningInvoicePageViewModelBase with Store { return; } } - // final result = await anonPayApi.createInvoice(AnonPayRequest( - // cryptoCurrency: cryptoCurrency, - // address: address, - // amount: amount.isEmpty ? null : amount, - // description: description, - // fiatEquivalent: - // selectedCurrency is FiatCurrency ? (selectedCurrency as FiatCurrency).raw : null, - // )); - // _anonpayInvoiceInfoSource.add(result); - - // state = ExecutedSuccessfullyState(payload: result); + try { + String bolt11 = + await lightningViewModel.createInvoice(amount: amount, description: description); + state = ExecutedSuccessfullyState(payload: bolt11); + } catch (e) { + state = FailureState(e.toString()); + } } @action @@ -121,6 +119,7 @@ abstract class LightningInvoicePageViewModelBase with Store { } Future _fetchLimits() async { + List limits = await lightningViewModel.invoiceLimits(); // final limit = await anonPayApi.fetchLimits( // cryptoCurrency: cryptoCurrency, // fiatCurrency: selectedCurrency is FiatCurrency ? selectedCurrency as FiatCurrency : null, @@ -132,7 +131,7 @@ abstract class LightningInvoicePageViewModelBase with Store { @action void reset() { selectedCurrency = walletTypeToCryptoCurrency(_wallet.type); - cryptoCurrency = walletTypeToCryptoCurrency(_wallet.type); + cryptoCurrency = walletTypeToCryptoCurrency(_wallet.type); description = ''; amount = ''; _fetchLimits(); diff --git a/lib/view_model/lightning_view_model.dart b/lib/view_model/lightning_view_model.dart index 5c3c0ae48..7e8094c8a 100644 --- a/lib/view_model/lightning_view_model.dart +++ b/lib/view_model/lightning_view_model.dart @@ -22,52 +22,89 @@ abstract class LightningViewModelBase with Store { // ExecutionState state; @action - Future receiveOnChain() async { - print("pressed"); - // ReceivePaymentRequest req = const ReceivePaymentRequest( - // amountMsat: 3000000, - // description: "Invoice for 3000 sats", - // ); - // ReceivePaymentResponse receivePaymentResponse = - // await BreezSDK().receivePayment(req: req); + Future stuff() async { + print("pressed"); + // ReceivePaymentRequest req = const ReceivePaymentRequest( + // amountMsat: 3000000, + // description: "Invoice for 3000 sats", + // ); + // ReceivePaymentResponse receivePaymentResponse = + // await BreezSDK().receivePayment(req: req); - // print(receivePaymentResponse.lnInvoice); + // print(receivePaymentResponse.lnInvoice); - final sdk = await BreezSDK(); + final sdk = await BreezSDK(); - sdk.nodeStateStream.listen((event) { - // print("Node state: $event"); - if (event == null) return; - int balanceSat = event.maxPayableMsat ~/ 1000; - print("sats: $balanceSat"); - }); + sdk.nodeStateStream.listen((event) { + // print("Node state: $event"); + if (event == null) return; + int balanceSat = event.maxPayableMsat ~/ 1000; + print("sats: $balanceSat"); + }); - // ServiceHealthCheckResponse healthCheck = await sdk.serviceHealthCheck(); - // print("Current service status is: ${healthCheck.status}"); + // ServiceHealthCheckResponse healthCheck = await sdk.serviceHealthCheck(); + // print("Current service status is: ${healthCheck.status}"); - // ReceivePaymentRequest req = ReceivePaymentRequest( - // amountMsat: 123 * 1000, - // description: "Invoice for 123 sats", - // ); - // final s = await sdk.receivePayment(req: req); - // print(s.lnInvoice.bolt11); + // ReceivePaymentRequest req = ReceivePaymentRequest( + // amountMsat: 123 * 1000, + // description: "Invoice for 123 sats", + // ); + // final s = await sdk.receivePayment(req: req); + // print(s.lnInvoice.bolt11); - // ReceiveOnchainRequest req = const ReceiveOnchainRequest(); - // SwapInfo swapInfo = await sdk.receiveOnchain(req: req); - // // Send your funds to the below bitcoin address - // String address = swapInfo.bitcoinAddress; - // print(address); - // print("Minimum amount allowed to deposit in sats: ${swapInfo.minAllowedDeposit}"); - // print("Maximum amount allowed to deposit in sats: ${swapInfo.maxAllowedDeposit}"); + // ReceiveOnchainRequest req = const ReceiveOnchainRequest(); + // SwapInfo swapInfo = await sdk.receiveOnchain(req: req); + // // Send your funds to the below bitcoin address + // String address = swapInfo.bitcoinAddress; + // print(address); + // print("Minimum amount allowed to deposit in sats: ${swapInfo.minAllowedDeposit}"); + // print("Maximum amount allowed to deposit in sats: ${swapInfo.maxAllowedDeposit}"); - ListPaymentsRequest lReq = ListPaymentsRequest(); + ListPaymentsRequest lReq = ListPaymentsRequest(); - var list = await sdk.listPayments(req: lReq); - print(list[0].amountMsat); + var list = await sdk.listPayments(req: lReq); + print(list[0].amountMsat); - var data = await sdk.fetchNodeData(); - print(data); + var data = await sdk.fetchNodeData(); + print(data); + } + Future> receiveOnchain() async { + final sdk = await BreezSDK(); + ReceiveOnchainRequest req = const ReceiveOnchainRequest(); + SwapInfo swapInfo = await sdk.receiveOnchain(req: req); + print(swapInfo.bitcoinAddress); + print("Minimum amount allowed to deposit in sats: ${swapInfo.minAllowedDeposit}"); + print("Maximum amount allowed to deposit in sats: ${swapInfo.maxAllowedDeposit}"); + return [ + swapInfo.bitcoinAddress, + swapInfo.minAllowedDeposit.toString(), + swapInfo.maxAllowedDeposit.toString() + ]; + } + + Future createInvoice({required String amount, String? description}) async { + final sdk = await BreezSDK(); + final req = ReceivePaymentRequest( + amountMsat: int.parse(amount) * 1000, + description: description ?? '', + ); + final res = await sdk.receivePayment(req: req); + print(res.lnInvoice.bolt11); + return res.lnInvoice.bolt11; + } + + Future> invoiceLimits() async { + final sdk = await BreezSDK(); + final req = ReceivePaymentRequest( + amountMsat: 1000, + description: 'limits', + ); + // final res = await sdk.receivePayment(req: req); + // print(res.lnInvoice.); + // return res.lnInvoice.bolt11; + // TODO: figure out how to get the limits + return ['1000', '20000']; } }