cake_wallet/lib/view_model/send/send_view_model.dart

386 lines
11 KiB
Dart
Raw Normal View History

2021-01-27 13:51:51 +00:00
import 'package:cake_wallet/bitcoin/bitcoin_amount_format.dart';
import 'package:cake_wallet/bitcoin/bitcoin_transaction_priority.dart';
import 'package:cake_wallet/bitcoin/electrum_wallet.dart';
import 'package:cake_wallet/entities/balance_display_mode.dart';
import 'package:cake_wallet/entities/calculate_fiat_amount_raw.dart';
2020-11-06 18:54:00 +00:00
import 'package:cake_wallet/entities/transaction_description.dart';
2021-01-27 13:51:51 +00:00
import 'package:cake_wallet/entities/transaction_priority.dart';
import 'package:cake_wallet/monero/monero_amount_format.dart';
import 'package:cake_wallet/view_model/settings/settings_view_model.dart';
2020-11-06 18:54:00 +00:00
import 'package:hive/hive.dart';
2020-10-30 16:32:21 +00:00
import 'package:intl/intl.dart';
import 'package:mobx/mobx.dart';
2020-10-02 17:28:29 +00:00
import 'package:cake_wallet/entities/openalias_record.dart';
import 'package:cake_wallet/entities/template.dart';
import 'package:cake_wallet/store/templates/send_template_store.dart';
2020-09-02 08:47:41 +00:00
import 'package:cake_wallet/core/template_validator.dart';
2020-08-25 16:32:40 +00:00
import 'package:cake_wallet/core/address_validator.dart';
import 'package:cake_wallet/core/amount_validator.dart';
import 'package:cake_wallet/core/pending_transaction.dart';
import 'package:cake_wallet/core/validator.dart';
import 'package:cake_wallet/core/wallet_base.dart';
2020-09-21 11:50:26 +00:00
import 'package:cake_wallet/core/execution_state.dart';
2020-08-25 16:32:40 +00:00
import 'package:cake_wallet/bitcoin/bitcoin_wallet.dart';
2020-09-21 11:50:26 +00:00
import 'package:cake_wallet/bitcoin/bitcoin_transaction_credentials.dart';
2020-08-25 16:32:40 +00:00
import 'package:cake_wallet/monero/monero_wallet.dart';
2020-09-21 11:50:26 +00:00
import 'package:cake_wallet/monero/monero_transaction_creation_credentials.dart';
import 'package:cake_wallet/entities/sync_status.dart';
import 'package:cake_wallet/entities/crypto_currency.dart';
import 'package:cake_wallet/entities/fiat_currency.dart';
2021-01-27 13:51:51 +00:00
import 'package:cake_wallet/entities/monero_transaction_priority.dart';
2020-09-21 11:50:26 +00:00
import 'package:cake_wallet/entities/calculate_fiat_amount.dart';
import 'package:cake_wallet/entities/wallet_type.dart';
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
2020-08-25 16:32:40 +00:00
import 'package:cake_wallet/store/settings_store.dart';
import 'package:cake_wallet/view_model/send/send_view_model_state.dart';
2020-09-21 11:50:26 +00:00
import 'package:cake_wallet/generated/i18n.dart';
2020-08-25 16:32:40 +00:00
part 'send_view_model.g.dart';
const String cryptoNumberPattern = '0.0';
2020-08-25 16:32:40 +00:00
class SendViewModel = SendViewModelBase with _$SendViewModel;
abstract class SendViewModelBase with Store {
2020-11-06 18:54:00 +00:00
SendViewModelBase(this._wallet, this._settingsStore, this._sendTemplateStore,
this._fiatConversationStore, this.transactionDescriptionBox)
2020-09-21 11:50:26 +00:00
: state = InitialExecutionState(),
_cryptoNumberFormat = NumberFormat(cryptoNumberPattern),
note = '',
2020-09-21 11:50:26 +00:00
sendAll = false {
2021-01-27 13:51:51 +00:00
final priority = _settingsStore.priority[_wallet.type];
final priorities = priorityForWalletType(_wallet.type);
2021-01-27 13:51:51 +00:00
if (!priorityForWalletType(_wallet.type).contains(priority)) {
_settingsStore.priority[_wallet.type] = priorities.first;
}
isBitcoinWallet = _wallet is BitcoinWallet;
2020-09-21 11:50:26 +00:00
_setCryptoNumMaximumFractionDigits();
}
2020-08-25 16:32:40 +00:00
@observable
2020-09-21 11:50:26 +00:00
ExecutionState state;
2020-08-25 16:32:40 +00:00
@observable
String fiatAmount;
@observable
String cryptoAmount;
@observable
String address;
@observable
String note;
2020-08-25 16:32:40 +00:00
@observable
2020-09-02 08:47:41 +00:00
bool sendAll;
2020-08-25 16:32:40 +00:00
2020-09-30 18:23:15 +00:00
@computed
2021-01-27 13:51:51 +00:00
double get estimatedFee {
int amount;
try {
if (cryptoAmount?.isNotEmpty ?? false) {
final _cryptoAmount = cryptoAmount.replaceAll(',', '.');
int _amount = 0;
switch (walletType) {
case WalletType.monero:
_amount = moneroParseAmount(amount: _cryptoAmount);
break;
case WalletType.bitcoin:
_amount = stringDoubleToBitcoinAmount(_cryptoAmount);
break;
case WalletType.litecoin:
_amount = stringDoubleToBitcoinAmount(_cryptoAmount);
break;
default:
break;
}
if (_amount > 0) {
amount = _amount;
}
2021-01-27 13:51:51 +00:00
}
final fee = _wallet.calculateEstimatedFee(
_settingsStore.priority[_wallet.type], amount);
2021-01-27 13:51:51 +00:00
if (_wallet is ElectrumWallet) {
return bitcoinAmountToDouble(amount: fee);
}
2021-01-27 13:51:51 +00:00
if (_wallet is MoneroWallet) {
return moneroAmountToDouble(amount: fee);
}
} catch (e) {
print(e.toString());
2021-01-27 13:51:51 +00:00
}
return 0;
}
2020-09-30 18:23:15 +00:00
@computed
String get estimatedFeeFiatAmount {
try {
final fiat = calculateFiatAmountRaw(
price: _fiatConversationStore.prices[_wallet.currency],
cryptoAmount: estimatedFee);
return fiat;
} catch (_) {
return '0.00';
}
}
@computed
String get pendingTransactionFiatAmount {
try {
if (pendingTransaction != null) {
final fiat = calculateFiatAmount(
price: _fiatConversationStore.prices[_wallet.currency],
cryptoAmount: pendingTransaction.amountFormatted);
return fiat;
} else {
return '0.00';
}
} catch (_) {
return '0.00';
}
}
@computed
String get pendingTransactionFeeFiatAmount {
try {
if (pendingTransaction != null) {
final fiat = calculateFiatAmount(
price: _fiatConversationStore.prices[_wallet.currency],
cryptoAmount: pendingTransaction.feeFormatted);
return fiat;
} else {
return '0.00';
}
} catch (_) {
return '0.00';
}
}
2020-08-25 16:32:40 +00:00
FiatCurrency get fiat => _settingsStore.fiatCurrency;
TransactionPriority get transactionPriority =>
2021-01-27 13:51:51 +00:00
_settingsStore.priority[_wallet.type];
2020-08-25 16:32:40 +00:00
CryptoCurrency get currency => _wallet.currency;
Validator get amountValidator => AmountValidator(type: _wallet.type);
Validator get allAmountValidator => AllAmountValidator();
2020-08-25 16:32:40 +00:00
Validator get addressValidator => AddressValidator(type: _wallet.currency);
2020-09-02 08:47:41 +00:00
Validator get templateValidator => TemplateValidator();
@observable
2020-08-25 16:32:40 +00:00
PendingTransaction pendingTransaction;
@observable
bool isBitcoinWallet;
2020-08-25 16:32:40 +00:00
@computed
2021-01-11 17:15:27 +00:00
String get balance => _wallet.balance.formattedAvailableBalance ?? '0.0';
2020-08-25 16:32:40 +00:00
@computed
bool get isReadyForSend => _wallet.syncStatus is SyncedSyncStatus;
@computed
ObservableList<Template> get templates => _sendTemplateStore.templates;
WalletType get walletType => _wallet.type;
2020-08-25 16:32:40 +00:00
final WalletBase _wallet;
final SettingsStore _settingsStore;
final SendTemplateStore _sendTemplateStore;
2020-09-21 11:50:26 +00:00
final FiatConversionStore _fiatConversationStore;
2020-08-27 16:54:34 +00:00
final NumberFormat _cryptoNumberFormat;
2020-11-06 18:54:00 +00:00
final Box<TransactionDescription> transactionDescriptionBox;
2020-08-25 16:32:40 +00:00
@action
2020-09-02 08:47:41 +00:00
void setSendAll() => sendAll = true;
2020-08-25 16:32:40 +00:00
@action
void reset() {
sendAll = false;
2020-08-25 16:32:40 +00:00
cryptoAmount = '';
fiatAmount = '';
address = '';
note = '';
2020-08-25 16:32:40 +00:00
}
@action
Future<void> createTransaction() async {
try {
2020-09-21 11:50:26 +00:00
state = IsExecutingState();
2020-08-25 16:32:40 +00:00
pendingTransaction = await _wallet.createTransaction(_credentials());
2020-09-21 11:50:26 +00:00
state = ExecutedSuccessfullyState();
2020-08-25 16:32:40 +00:00
} catch (e) {
2020-09-21 11:50:26 +00:00
state = FailureState(e.toString());
2020-08-25 16:32:40 +00:00
}
}
@action
Future<void> commitTransaction() async {
try {
state = TransactionCommitting();
await pendingTransaction.commit();
2020-11-06 18:54:00 +00:00
if (pendingTransaction.id?.isNotEmpty ?? false) {
_settingsStore.shouldSaveRecipientAddress
2021-01-11 17:15:27 +00:00
? await transactionDescriptionBox.add(TransactionDescription(
id: pendingTransaction.id,
recipientAddress: address,
transactionNote: note))
: await transactionDescriptionBox.add(TransactionDescription(
id: pendingTransaction.id, transactionNote: note));
2020-11-06 18:54:00 +00:00
}
2020-08-25 16:32:40 +00:00
state = TransactionCommitted();
} catch (e) {
2020-09-21 11:50:26 +00:00
state = FailureState(e.toString());
2020-08-25 16:32:40 +00:00
}
}
@action
void setCryptoAmount(String amount) {
2020-09-21 11:50:26 +00:00
if (amount.toUpperCase() != S.current.all) {
2020-09-02 08:47:41 +00:00
sendAll = false;
2020-08-29 10:19:27 +00:00
}
2020-08-25 16:32:40 +00:00
cryptoAmount = amount;
_updateFiatAmount();
}
@action
void setFiatAmount(String amount) {
fiatAmount = amount;
_updateCryptoAmount();
}
2020-09-30 18:23:15 +00:00
@action
void setTransactionPriority(TransactionPriority priority) =>
2021-01-27 13:51:51 +00:00
_settingsStore.priority[_wallet.type] = priority;
2020-09-30 18:23:15 +00:00
2020-10-02 17:28:29 +00:00
Future<OpenaliasRecord> decodeOpenaliasRecord(String name) async {
2020-11-06 18:54:00 +00:00
final record = await OpenaliasRecord.fetchAddressAndName(
OpenaliasRecord.formatDomainName(name));
2020-10-02 17:28:29 +00:00
return record.name != name ? record : null;
}
2020-08-25 16:32:40 +00:00
@action
void _updateFiatAmount() {
try {
final fiat = calculateFiatAmount(
price: _fiatConversationStore.prices[_wallet.currency],
2020-08-27 16:54:34 +00:00
cryptoAmount: cryptoAmount.replaceAll(',', '.'));
2020-08-25 16:32:40 +00:00
if (fiatAmount != fiat) {
fiatAmount = fiat;
}
} catch (_) {
fiatAmount = '';
}
}
@action
void _updateCryptoAmount() {
try {
2020-08-27 16:54:34 +00:00
final crypto = double.parse(fiatAmount.replaceAll(',', '.')) /
_fiatConversationStore.prices[_wallet.currency];
2020-08-25 16:32:40 +00:00
final cryptoAmountTmp = _cryptoNumberFormat.format(crypto);
if (cryptoAmount != cryptoAmountTmp) {
cryptoAmount = cryptoAmountTmp;
}
} catch (e) {
cryptoAmount = '';
}
}
Object _credentials() {
2020-09-21 11:50:26 +00:00
final _amount = cryptoAmount.replaceAll(',', '.');
2020-08-25 16:32:40 +00:00
switch (_wallet.type) {
case WalletType.bitcoin:
2021-01-05 18:31:03 +00:00
final amount = !sendAll ? _amount : null;
2021-01-27 13:51:51 +00:00
final priority = _settingsStore.priority[_wallet.type];
2020-09-21 11:50:26 +00:00
return BitcoinTransactionCredentials(
address, amount, priority as BitcoinTransactionPriority);
case WalletType.litecoin:
final amount = !sendAll ? _amount : null;
final priority = _settingsStore.priority[_wallet.type];
2021-02-12 22:38:34 +00:00
return BitcoinTransactionCredentials(
address, amount, priority as BitcoinTransactionPriority);
2020-08-25 16:32:40 +00:00
case WalletType.monero:
2020-09-21 11:50:26 +00:00
final amount = !sendAll ? _amount : null;
2021-01-27 13:51:51 +00:00
final priority = _settingsStore.priority[_wallet.type];
2020-09-21 11:50:26 +00:00
return MoneroTransactionCreationCredentials(
address: address,
paymentId: '',
2021-01-27 13:51:51 +00:00
priority: priority as MoneroTransactionPriority,
2020-09-21 11:50:26 +00:00
amount: amount);
2020-08-25 16:32:40 +00:00
default:
return null;
}
}
2020-09-21 11:50:26 +00:00
void _setCryptoNumMaximumFractionDigits() {
var maximumFractionDigits = 0;
switch (_wallet.type) {
case WalletType.monero:
maximumFractionDigits = 12;
break;
case WalletType.bitcoin:
maximumFractionDigits = 8;
break;
case WalletType.litecoin:
maximumFractionDigits = 8;
break;
2020-09-21 11:50:26 +00:00
default:
break;
}
_cryptoNumberFormat.maximumFractionDigits = maximumFractionDigits;
}
void updateTemplate() => _sendTemplateStore.update();
2020-11-06 18:54:00 +00:00
void addTemplate(
{String name,
String address,
String cryptoCurrency,
String amount}) =>
_sendTemplateStore.addTemplate(
name: name,
address: address,
cryptoCurrency: cryptoCurrency,
amount: amount);
void removeTemplate({Template template}) =>
_sendTemplateStore.remove(template: template);
2021-02-12 22:38:34 +00:00
String displayFeeRate(dynamic priority) {
final _priority = priority as TransactionPriority;
final wallet = _wallet;
if (wallet is ElectrumWallet) {
2021-02-12 22:38:34 +00:00
final rate = wallet.feeRate(_priority);
return '${priority.labelWithRate(rate)}';
2021-02-12 22:38:34 +00:00
}
return priority.toString();
}
2020-08-25 16:32:40 +00:00
}