cake_wallet/lib/view_model/lightning_send_view_model.dart

97 lines
2.7 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;
2024-03-07 06:47:24 +00:00
import 'package:breez_sdk/bridge_generated.dart';
2024-03-07 06:10:40 +00:00
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';
import 'package:cake_wallet/src/widgets/alert_with_one_action.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';
2024-03-07 06:47:24 +00:00
import 'package:cake_wallet/utils/show_pop_up.dart';
2024-03-07 06:10:40 +00:00
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();
late InputType inputType;
try {
inputType = await sdk.parseInput(input: input);
} catch (_) {
throw Exception("Unknown input type");
}
if (inputType is InputType_Bolt11) {
final bolt11 = await sdk.parseInvoice(input);
Navigator.of(context).pushNamed(Routes.lightningSendConfirm, arguments: bolt11);
} else if (inputType is InputType_LnUrlPay) {
throw Exception("Unsupported input type");
} else {
throw Exception("Unknown input type");
}
}
2024-03-07 06:10:40 +00:00
}