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-26 19:46:05 +00:00
|
|
|
import 'package:cw_bitcoin/bitcoin_amount_format.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';
|
2024-02-23 17:46:17 +00:00
|
|
|
import 'package:cw_lightning/lightning_receive_page_option.dart';
|
2024-02-14 17:34:52 +00:00
|
|
|
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
|
|
|
this.pageOption,
|
|
|
|
) : 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;
|
|
|
|
final ReceivePageOption pageOption;
|
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;
|
|
|
|
}
|
|
|
|
if (maximum != null && amountInCrypto > maximum!) {
|
|
|
|
state = FailureState('Amount is too big');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-14 18:21:37 +00:00
|
|
|
try {
|
|
|
|
String bolt11 =
|
|
|
|
await lightningViewModel.createInvoice(amount: amount, description: description);
|
|
|
|
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-02-26 19:46:05 +00:00
|
|
|
List<String> limits = await lightningViewModel.invoiceLimitsSats();
|
|
|
|
minimum = bitcoinAmountToDouble(amount: int.parse(limits[0]) ~/ 1000);
|
|
|
|
maximum = bitcoinAmountToDouble(amount: int.parse(limits[1]) ~/ 1000);
|
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 = '';
|
|
|
|
_fetchLimits();
|
|
|
|
}
|
|
|
|
}
|