cake_wallet/lib/view_model/lightning_send_view_model.dart

94 lines
2.6 KiB
Dart
Raw Normal View History

2024-03-07 06:10:40 +00:00
import 'dart:async';
import 'package:breez_sdk/breez_sdk.dart';
import 'package:breez_sdk/bridge_generated.dart' as BZG;
import 'package:cake_wallet/entities/calculate_fiat_amount_raw.dart';
import 'package:cake_wallet/entities/fiat_currency.dart';
import 'package:cake_wallet/lightning/lightning.dart';
2024-03-07 06:47:24 +00:00
import 'package:cake_wallet/routes.dart';
2024-03-07 06:10:40 +00:00
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
import 'package:cake_wallet/store/settings_store.dart';
import 'package:cw_core/crypto_currency.dart';
2024-03-07 06:47:24 +00:00
import 'package:flutter/widgets.dart';
2024-03-07 06:10:40 +00:00
import 'package:mobx/mobx.dart';
part 'lightning_send_view_model.g.dart';
class LightningSendViewModel = LightningSendViewModelBase with _$LightningSendViewModel;
abstract class LightningSendViewModelBase with Store {
LightningSendViewModelBase({
required this.settingsStore,
required this.fiatConversionStore,
}) {}
final SettingsStore settingsStore;
final FiatConversionStore fiatConversionStore;
2024-03-07 06:47:24 +00:00
2024-03-07 06:10:40 +00:00
@observable
bool loading = false;
@action
void setLoading(bool value) {
loading = value;
}
FiatCurrency get fiat => settingsStore.fiatCurrency;
String formattedFiatAmount(int sats) {
String amount = calculateFiatAmountRaw(
cryptoAmount: lightning!.formatterLightningAmountToDouble(amount: sats),
price: fiatConversionStore.prices[CryptoCurrency.btcln],
);
return amount;
}
2024-03-07 06:47:24 +00:00
@action
Future<void> send(BZG.LNInvoice invoice, int satAmount) async {
try {
setLoading(true);
final sdk = await BreezSDK();
late BZG.SendPaymentRequest req;
if (invoice.amountMsat == null) {
req = BZG.SendPaymentRequest(
bolt11: invoice.bolt11,
amountMsat: satAmount * 1000,
);
} else {
req = BZG.SendPaymentRequest(bolt11: invoice.bolt11);
}
await sdk.sendPayment(req: req);
setLoading(false);
} catch (e) {
setLoading(false);
}
}
@action
Future<void> processInput(BuildContext context, String input) async {
FocusScope.of(context).unfocus();
final sdk = await BreezSDK();
2024-03-07 15:38:27 +00:00
late BZG.InputType inputType;
2024-03-07 06:47:24 +00:00
try {
inputType = await sdk.parseInput(input: input);
} catch (_) {
throw Exception("Unknown input type");
}
2024-03-07 15:38:27 +00:00
if (inputType is BZG.InputType_Bolt11) {
2024-03-07 06:47:24 +00:00
final bolt11 = await sdk.parseInvoice(input);
Navigator.of(context).pushNamed(Routes.lightningSendConfirm, arguments: bolt11);
2024-03-07 15:38:27 +00:00
} else if (inputType is BZG.InputType_LnUrlPay) {
2024-03-07 06:47:24 +00:00
throw Exception("Unsupported input type");
} else {
throw Exception("Unknown input type");
}
}
2024-03-07 06:10:40 +00:00
}