cake_wallet/lib/view_model/lightning_invoice_page_view_model.dart

132 lines
3.5 KiB
Dart
Raw Normal View History

2024-02-14 17:34:52 +00:00
import 'package:cake_wallet/core/execution_state.dart';
import 'package:cake_wallet/entities/fiat_currency.dart';
import 'package:cake_wallet/store/settings_store.dart';
2024-02-14 18:21:37 +00:00
import 'package:cake_wallet/view_model/lightning_view_model.dart';
2024-02-14 17:34:52 +00:00
import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/currency.dart';
2024-02-23 17:46:17 +00:00
import 'package:cw_core/receive_page_option.dart';
2024-02-14 17:34:52 +00:00
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:mobx/mobx.dart';
import 'package:shared_preferences/shared_preferences.dart';
part 'lightning_invoice_page_view_model.g.dart';
2024-02-14 18:21:37 +00:00
class LightningInvoicePageViewModel = LightningInvoicePageViewModelBase
with _$LightningInvoicePageViewModel;
2024-02-14 17:34:52 +00:00
abstract class LightningInvoicePageViewModelBase with Store {
LightningInvoicePageViewModelBase(
this.settingsStore,
this._wallet,
this.sharedPreferences,
2024-02-14 18:21:37 +00:00
this.lightningViewModel,
2024-02-14 17:34:52 +00:00
) : description = '',
amount = '',
state = InitialExecutionState(),
selectedCurrency = walletTypeToCryptoCurrency(_wallet.type),
cryptoCurrency = walletTypeToCryptoCurrency(_wallet.type) {
2024-02-14 18:21:37 +00:00
_fetchLimits();
2024-02-14 17:34:52 +00:00
}
List<Currency> get currencies => [walletTypeToCryptoCurrency(_wallet.type), ...FiatCurrency.all];
final SettingsStore settingsStore;
final WalletBase _wallet;
final SharedPreferences sharedPreferences;
2024-02-14 18:21:37 +00:00
final LightningViewModel lightningViewModel;
2024-02-14 17:34:52 +00:00
@observable
Currency selectedCurrency;
CryptoCurrency cryptoCurrency;
@observable
String description;
@observable
String amount;
@observable
ExecutionState state;
@computed
int get selectedCurrencyIndex => currencies.indexOf(selectedCurrency);
@observable
double? minimum;
@observable
double? maximum;
@action
void selectCurrency(Currency currency) {
selectedCurrency = currency;
maximum = minimum = null;
if (currency is CryptoCurrency) {
cryptoCurrency = currency;
} else {
cryptoCurrency = walletTypeToCryptoCurrency(_wallet.type);
}
_fetchLimits();
}
@action
Future<void> createInvoice() async {
state = IsExecutingState();
if (amount.isNotEmpty) {
final amountInCrypto = double.tryParse(amount);
if (amountInCrypto == null) {
state = FailureState('Amount is invalid');
return;
}
if (minimum != null && amountInCrypto < minimum!) {
state = FailureState('Amount is too small');
return;
}
2024-03-21 15:36:14 +00:00
if (maximum == 0) {
state = FailureState('Your inbound liquidity is 0!');
return;
}
2024-02-14 17:34:52 +00:00
if (maximum != null && amountInCrypto > maximum!) {
state = FailureState('Amount is too big');
return;
}
}
2024-02-14 18:21:37 +00:00
try {
String bolt11 =
2024-03-04 17:47:39 +00:00
await lightningViewModel.createInvoice(amountSats: amount, description: description);
2024-02-14 18:21:37 +00:00
state = ExecutedSuccessfullyState(payload: bolt11);
} catch (e) {
state = FailureState(e.toString());
}
2024-02-14 17:34:52 +00:00
}
@action
void setRequestParams({
required String inputAmount,
required String inputDescription,
}) {
description = inputDescription;
amount = inputAmount;
}
Future<void> _fetchLimits() async {
2024-03-07 06:10:40 +00:00
final limits = await lightningViewModel.invoiceSoftLimitsSats();
minimum = limits.minFee.toDouble();
maximum = limits.inboundLiquidity.toDouble();
2024-02-14 17:34:52 +00:00
}
@action
void reset() {
selectedCurrency = walletTypeToCryptoCurrency(_wallet.type);
2024-02-14 18:21:37 +00:00
cryptoCurrency = walletTypeToCryptoCurrency(_wallet.type);
2024-02-14 17:34:52 +00:00
description = '';
amount = '';
2024-02-29 18:29:01 +00:00
try {
_fetchLimits();
} catch (_) {}
2024-02-14 17:34:52 +00:00
}
}