This commit is contained in:
fosse 2024-02-14 13:21:37 -05:00
parent ce2a51835d
commit d309e528f6
5 changed files with 180 additions and 131 deletions

View file

@ -608,7 +608,6 @@ Future<void> setup({
authService: getIt.get<AuthService>(), authService: getIt.get<AuthService>(),
initialPaymentRequest: initialPaymentRequest, initialPaymentRequest: initialPaymentRequest,
)); ));
getIt.registerFactory( getIt.registerFactory(
() => SendTemplatePage(sendTemplateViewModel: getIt.get<SendTemplateViewModel>())); () => SendTemplatePage(sendTemplateViewModel: getIt.get<SendTemplateViewModel>()));
@ -1200,11 +1199,6 @@ Future<void> setup({
getIt.registerFactory(() => NFTViewModel(appStore, getIt.get<BottomSheetService>())); getIt.registerFactory(() => NFTViewModel(appStore, getIt.get<BottomSheetService>()));
getIt.registerFactory<TorPage>(() => TorPage(getIt.get<AppStore>())); getIt.registerFactory<TorPage>(() => TorPage(getIt.get<AppStore>()));
getIt.registerFactory<LightningReceiveOnchainPage>(() => LightningReceiveOnchainPage(
addressListViewModel: getIt.get<WalletAddressListViewModel>(),
receiveOptionViewModel: ReceiveOptionViewModel(getIt.get<AppStore>().wallet!, ReceivePageOption.lightningOnchain),
));
getIt.registerFactoryParam<LightningInvoicePageViewModel, List<dynamic>, void>((args, _) { getIt.registerFactoryParam<LightningInvoicePageViewModel, List<dynamic>, void>((args, _) {
final address = args.first as String; final address = args.first as String;
final pageOption = args.last as ReceivePageOption; final pageOption = args.last as ReceivePageOption;
@ -1213,29 +1207,38 @@ Future<void> setup({
getIt.get<SettingsStore>(), getIt.get<SettingsStore>(),
getIt.get<AppStore>().wallet!, getIt.get<AppStore>().wallet!,
getIt.get<SharedPreferences>(), getIt.get<SharedPreferences>(),
LightningViewModel(),
pageOption, pageOption,
); );
}); });
// getIt.registerFactory<LightningInvoicePage>(() => LightningInvoicePage( // getIt.registerFactory<LightningReceiveOnchainPage>(() => LightningReceiveOnchainPage(
// lightningInvoicePageViewModel: getIt.get<LightningInvoicePageViewModel>(), // addressListViewModel: getIt.get<WalletAddressListViewModel>(),
// lightningViewModel: LightningViewModel(), // receiveOptionViewModel: ReceiveOptionViewModel(getIt.get<AppStore>().wallet!, ReceivePageOption.lightningOnchain),
// receiveOptionViewModel: ReceiveOptionViewModel(getIt.get<AppStore>().wallet!, ReceivePageOption.lightningInvoice),
// )); // ));
getIt.registerFactoryParam<LightningReceiveOnchainPage, List<dynamic>, void>(
(List<dynamic> args, _) {
final pageOption = args.last as ReceivePageOption;
return LightningReceiveOnchainPage(
addressListViewModel: getIt.get<WalletAddressListViewModel>(),
// lightningViewModel: LightningViewModel(),
receiveOptionViewModel: getIt.get<ReceiveOptionViewModel>(param1: pageOption));
});
getIt.registerFactoryParam<LightningInvoicePage, List<dynamic>, void>((List<dynamic> args, _) { getIt.registerFactoryParam<LightningInvoicePage, List<dynamic>, void>((List<dynamic> args, _) {
final pageOption = args.last as ReceivePageOption; final pageOption = args.last as ReceivePageOption;
return LightningInvoicePage( return LightningInvoicePage(
lightningInvoicePageViewModel: getIt.get<LightningInvoicePageViewModel>(param1: args), lightningInvoicePageViewModel: getIt.get<LightningInvoicePageViewModel>(param1: args),
lightningViewModel: LightningViewModel(), lightningViewModel: LightningViewModel(),
receiveOptionViewModel: getIt.get<ReceiveOptionViewModel>(param1: pageOption)); receiveOptionViewModel: getIt.get<ReceiveOptionViewModel>(param1: pageOption));
}); });
getIt.registerFactory<LightningSendPage>(() => LightningSendPage(
// getIt.registerFactory<LightningSendPage>(() => LightningSendPage(authService: , sendViewModel: , initialPaymentRequest: ,)); sendViewModel: getIt.get<SendViewModel>(),
authService: getIt.get<AuthService>(),
initialPaymentRequest: null,
));
_isSetupFinished = true; _isSetupFinished = true;
} }

View file

@ -641,8 +641,9 @@ Route<dynamic> createRoute(RouteSettings settings) {
fullscreenDialog: true, builder: (_) => getIt.get<LightningSendPage>()); fullscreenDialog: true, builder: (_) => getIt.get<LightningSendPage>());
case Routes.lightningReceiveOnchain: case Routes.lightningReceiveOnchain:
final args = settings.arguments as List;
return CupertinoPageRoute<void>( return CupertinoPageRoute<void>(
fullscreenDialog: true, builder: (_) => getIt.get<LightningReceiveOnchainPage>()); fullscreenDialog: true, builder: (_) => getIt.get<LightningReceiveOnchainPage>(param1: args));
case Routes.lightningInvoice: case Routes.lightningInvoice:
final args = settings.arguments as List; final args = settings.arguments as List;

View file

@ -35,8 +35,6 @@ class LightningInvoicePage extends BasePage {
required this.receiveOptionViewModel, required this.receiveOptionViewModel,
}) : _amountFocusNode = FocusNode() {} }) : _amountFocusNode = FocusNode() {}
final _nameController = TextEditingController();
final _emailController = TextEditingController();
final _descriptionController = TextEditingController(); final _descriptionController = TextEditingController();
final _amountController = TextEditingController(); final _amountController = TextEditingController();
final FocusNode _amountFocusNode; final FocusNode _amountFocusNode;
@ -138,48 +136,36 @@ class LightningInvoicePage extends BasePage {
bottomSection: Observer(builder: (_) { bottomSection: Observer(builder: (_) {
return Column( return Column(
children: <Widget>[ children: <Widget>[
Padding( // Padding(
padding: EdgeInsets.only(bottom: 15), // padding: EdgeInsets.only(bottom: 15),
child: Center( // child: Center(
child: Text( // child: Text(
S.of(context).anonpay_description("an invoice", "pay"), // S.of(context).anonpay_description("an invoice", "pay"),
textAlign: TextAlign.center, // textAlign: TextAlign.center,
style: TextStyle( // style: TextStyle(
color: Theme.of(context) // color: Theme.of(context)
.extension<ExchangePageTheme>()! // .extension<ExchangePageTheme>()!
.receiveAmountColor, // .receiveAmountColor,
fontWeight: FontWeight.w500, // fontWeight: FontWeight.w500,
fontSize: 12), // 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,
// ), // ),
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; return;
} }
reaction((_) => receiveOptionViewModel.selectedReceiveOption, (ReceivePageOption option) { reaction((_) => receiveOptionViewModel.selectedReceiveOption, (ReceivePageOption option) async {
switch (option) { switch (option) {
case ReceivePageOption.lightningInvoice: case ReceivePageOption.lightningInvoice:
break; break;
case ReceivePageOption.lightningOnchain: case ReceivePageOption.lightningOnchain:
Navigator.popAndPushNamed(context, Routes.lightningReceiveOnchain); final address = await lightningViewModel.receiveOnchain();
Navigator.popAndPushNamed(
context,
Routes.lightningReceiveOnchain,
arguments: [address, ReceivePageOption.lightningInvoice],
);
break; break;
default: default:
} }
}); });
// reaction((_) => anonInvoicePageViewModel.state, (ExecutionState state) { reaction((_) => lightningInvoicePageViewModel.state, (ExecutionState state) {
// if (state is ExecutedSuccessfullyState) { if (state is ExecutedSuccessfullyState) {
// Navigator.pushNamed(context, Routes.anonPayReceivePage, arguments: state.payload); // Navigator.pushNamed(context, Routes.anonPayReceivePage, arguments: state.payload);
// } lightningViewModel.createInvoice(
// if (state is FailureState) { amount: state.payload["amount"] as String,
// showPopUp<void>( description: state.payload["description"] as String?,
// context: context, );
// builder: (BuildContext context) { }
// return AlertWithOneAction(
// alertTitle: S.of(context).error, if (state is ExecutedSuccessfullyState) {
// alertContent: state.error.toString(), showPopUp<void>(
// buttonText: S.of(context).ok, context: context,
// buttonAction: () => Navigator.of(context).pop()); 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<void>(
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; effectsInstalled = true;
} }

View file

@ -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/preferences_key.dart';
import 'package:cake_wallet/entities/receive_page_option.dart'; import 'package:cake_wallet/entities/receive_page_option.dart';
import 'package:cake_wallet/store/settings_store.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/crypto_currency.dart';
import 'package:cw_core/currency.dart'; import 'package:cw_core/currency.dart';
import 'package:cw_core/wallet_base.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'; part 'lightning_invoice_page_view_model.g.dart';
class LightningInvoicePageViewModel = LightningInvoicePageViewModelBase with _$LightningInvoicePageViewModel; class LightningInvoicePageViewModel = LightningInvoicePageViewModelBase
with _$LightningInvoicePageViewModel;
abstract class LightningInvoicePageViewModelBase with Store { abstract class LightningInvoicePageViewModelBase with Store {
LightningInvoicePageViewModelBase( LightningInvoicePageViewModelBase(
this.address, this.address,
this.settingsStore, this.settingsStore,
this._wallet, this._wallet,
// this._anonpayInvoiceInfoSource,
this.sharedPreferences, this.sharedPreferences,
this.lightningViewModel,
this.pageOption, this.pageOption,
) : description = '', ) : description = '',
amount = '', amount = '',
state = InitialExecutionState(), state = InitialExecutionState(),
selectedCurrency = walletTypeToCryptoCurrency(_wallet.type), selectedCurrency = walletTypeToCryptoCurrency(_wallet.type),
cryptoCurrency = walletTypeToCryptoCurrency(_wallet.type) { cryptoCurrency = walletTypeToCryptoCurrency(_wallet.type) {
// _getPreviousDonationLink(); _fetchLimits();
// _fetchLimits();
} }
List<Currency> get currencies => [walletTypeToCryptoCurrency(_wallet.type), ...FiatCurrency.all]; List<Currency> get currencies => [walletTypeToCryptoCurrency(_wallet.type), ...FiatCurrency.all];
@ -42,6 +43,7 @@ abstract class LightningInvoicePageViewModelBase with Store {
// final Box<AnonpayInvoiceInfo> _anonpayInvoiceInfoSource; // final Box<AnonpayInvoiceInfo> _anonpayInvoiceInfoSource;
final SharedPreferences sharedPreferences; final SharedPreferences sharedPreferences;
final ReceivePageOption pageOption; final ReceivePageOption pageOption;
final LightningViewModel lightningViewModel;
@observable @observable
Currency selectedCurrency; Currency selectedCurrency;
@ -97,18 +99,14 @@ abstract class LightningInvoicePageViewModelBase with Store {
return; 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); try {
String bolt11 =
// state = ExecutedSuccessfullyState(payload: result); await lightningViewModel.createInvoice(amount: amount, description: description);
state = ExecutedSuccessfullyState(payload: bolt11);
} catch (e) {
state = FailureState(e.toString());
}
} }
@action @action
@ -121,6 +119,7 @@ abstract class LightningInvoicePageViewModelBase with Store {
} }
Future<void> _fetchLimits() async { Future<void> _fetchLimits() async {
List<String> limits = await lightningViewModel.invoiceLimits();
// final limit = await anonPayApi.fetchLimits( // final limit = await anonPayApi.fetchLimits(
// cryptoCurrency: cryptoCurrency, // cryptoCurrency: cryptoCurrency,
// fiatCurrency: selectedCurrency is FiatCurrency ? selectedCurrency as FiatCurrency : null, // fiatCurrency: selectedCurrency is FiatCurrency ? selectedCurrency as FiatCurrency : null,
@ -132,7 +131,7 @@ abstract class LightningInvoicePageViewModelBase with Store {
@action @action
void reset() { void reset() {
selectedCurrency = walletTypeToCryptoCurrency(_wallet.type); selectedCurrency = walletTypeToCryptoCurrency(_wallet.type);
cryptoCurrency = walletTypeToCryptoCurrency(_wallet.type); cryptoCurrency = walletTypeToCryptoCurrency(_wallet.type);
description = ''; description = '';
amount = ''; amount = '';
_fetchLimits(); _fetchLimits();

View file

@ -22,52 +22,89 @@ abstract class LightningViewModelBase with Store {
// ExecutionState state; // ExecutionState state;
@action @action
Future<void> receiveOnChain() async { Future<void> stuff() async {
print("pressed"); print("pressed");
// ReceivePaymentRequest req = const ReceivePaymentRequest( // ReceivePaymentRequest req = const ReceivePaymentRequest(
// amountMsat: 3000000, // amountMsat: 3000000,
// description: "Invoice for 3000 sats", // description: "Invoice for 3000 sats",
// ); // );
// ReceivePaymentResponse receivePaymentResponse = // ReceivePaymentResponse receivePaymentResponse =
// await BreezSDK().receivePayment(req: req); // await BreezSDK().receivePayment(req: req);
// print(receivePaymentResponse.lnInvoice); // print(receivePaymentResponse.lnInvoice);
final sdk = await BreezSDK(); final sdk = await BreezSDK();
sdk.nodeStateStream.listen((event) { sdk.nodeStateStream.listen((event) {
// print("Node state: $event"); // print("Node state: $event");
if (event == null) return; if (event == null) return;
int balanceSat = event.maxPayableMsat ~/ 1000; int balanceSat = event.maxPayableMsat ~/ 1000;
print("sats: $balanceSat"); print("sats: $balanceSat");
}); });
// ServiceHealthCheckResponse healthCheck = await sdk.serviceHealthCheck(); // ServiceHealthCheckResponse healthCheck = await sdk.serviceHealthCheck();
// print("Current service status is: ${healthCheck.status}"); // print("Current service status is: ${healthCheck.status}");
// ReceivePaymentRequest req = ReceivePaymentRequest( // ReceivePaymentRequest req = ReceivePaymentRequest(
// amountMsat: 123 * 1000, // amountMsat: 123 * 1000,
// description: "Invoice for 123 sats", // description: "Invoice for 123 sats",
// ); // );
// final s = await sdk.receivePayment(req: req); // final s = await sdk.receivePayment(req: req);
// print(s.lnInvoice.bolt11); // print(s.lnInvoice.bolt11);
// ReceiveOnchainRequest req = const ReceiveOnchainRequest(); // ReceiveOnchainRequest req = const ReceiveOnchainRequest();
// SwapInfo swapInfo = await sdk.receiveOnchain(req: req); // SwapInfo swapInfo = await sdk.receiveOnchain(req: req);
// // Send your funds to the below bitcoin address // // Send your funds to the below bitcoin address
// String address = swapInfo.bitcoinAddress; // String address = swapInfo.bitcoinAddress;
// print(address); // print(address);
// print("Minimum amount allowed to deposit in sats: ${swapInfo.minAllowedDeposit}"); // print("Minimum amount allowed to deposit in sats: ${swapInfo.minAllowedDeposit}");
// print("Maximum amount allowed to deposit in sats: ${swapInfo.maxAllowedDeposit}"); // print("Maximum amount allowed to deposit in sats: ${swapInfo.maxAllowedDeposit}");
ListPaymentsRequest lReq = ListPaymentsRequest(); ListPaymentsRequest lReq = ListPaymentsRequest();
var list = await sdk.listPayments(req: lReq); var list = await sdk.listPayments(req: lReq);
print(list[0].amountMsat); print(list[0].amountMsat);
var data = await sdk.fetchNodeData(); var data = await sdk.fetchNodeData();
print(data); print(data);
}
Future<List<String>> 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<String> 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<List<String>> 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'];
} }
} }