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';
|
2021-05-07 07:36:38 +00:00
|
|
|
import 'package:cake_wallet/bitcoin/electrum_wallet.dart';
|
2020-12-15 16:29:10 +00:00
|
|
|
import 'package:cake_wallet/entities/balance_display_mode.dart';
|
2020-12-29 18:48:57 +00:00
|
|
|
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';
|
2020-10-07 05:58:22 +00:00
|
|
|
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';
|
|
|
|
|
2021-02-04 16:37:43 +00:00
|
|
|
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(),
|
2021-02-04 16:37:43 +00:00
|
|
|
_cryptoNumberFormat = NumberFormat(cryptoNumberPattern),
|
2020-12-22 18:42:30 +00:00
|
|
|
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-06 08:42:21 +00:00
|
|
|
|
2021-01-27 13:51:51 +00:00
|
|
|
if (!priorityForWalletType(_wallet.type).contains(priority)) {
|
|
|
|
_settingsStore.priority[_wallet.type] = priorities.first;
|
2021-01-06 08:42:21 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2020-12-22 18:42:30 +00:00
|
|
|
@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;
|
|
|
|
|
2021-03-26 18:16:10 +00:00
|
|
|
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;
|
2021-05-07 07:36:38 +00:00
|
|
|
case WalletType.litecoin:
|
|
|
|
_amount = stringDoubleToBitcoinAmount(_cryptoAmount);
|
|
|
|
break;
|
2021-03-26 18:16:10 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_amount > 0) {
|
|
|
|
amount = _amount;
|
|
|
|
}
|
2021-01-27 13:51:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-26 18:16:10 +00:00
|
|
|
final fee = _wallet.calculateEstimatedFee(
|
|
|
|
_settingsStore.priority[_wallet.type], amount);
|
2021-01-27 13:51:51 +00:00
|
|
|
|
2021-05-07 07:36:38 +00:00
|
|
|
if (_wallet is ElectrumWallet) {
|
2021-03-26 18:16:10 +00:00
|
|
|
return bitcoinAmountToDouble(amount: fee);
|
|
|
|
}
|
2021-01-27 13:51:51 +00:00
|
|
|
|
2021-03-26 18:16:10 +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
|
|
|
|
2020-12-29 18:48:57 +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';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-30 14:54:55 +00: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);
|
|
|
|
|
2020-10-09 07:41:29 +00:00
|
|
|
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();
|
|
|
|
|
2021-01-29 20:18:07 +00:00
|
|
|
@observable
|
2020-08-25 16:32:40 +00:00
|
|
|
PendingTransaction pendingTransaction;
|
|
|
|
|
|
|
|
@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;
|
|
|
|
|
2020-10-07 05:58:22 +00:00
|
|
|
@computed
|
|
|
|
ObservableList<Template> get templates => _sendTemplateStore.templates;
|
|
|
|
|
2020-12-03 19:34:56 +00:00
|
|
|
WalletType get walletType => _wallet.type;
|
2020-08-25 16:32:40 +00:00
|
|
|
final WalletBase _wallet;
|
|
|
|
final SettingsStore _settingsStore;
|
2020-10-07 05:58:22 +00:00
|
|
|
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() {
|
2020-10-30 12:42:23 +00:00
|
|
|
sendAll = false;
|
2020-08-25 16:32:40 +00:00
|
|
|
cryptoAmount = '';
|
|
|
|
fiatAmount = '';
|
|
|
|
address = '';
|
2020-12-22 18:42:30 +00:00
|
|
|
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
|
|
|
|
2020-12-24 09:01:27 +00:00
|
|
|
if (pendingTransaction.id?.isNotEmpty ?? false) {
|
2020-12-24 10:04:23 +00:00
|
|
|
_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(
|
2020-12-15 16:29:10 +00:00
|
|
|
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(',', '.')) /
|
2020-12-15 16:29:10 +00:00
|
|
|
_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
|
|
|
|
2021-05-07 07:36:38 +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;
|
2021-05-07 07:36:38 +00:00
|
|
|
case WalletType.litecoin:
|
|
|
|
maximumFractionDigits = 8;
|
|
|
|
break;
|
2020-09-21 11:50:26 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_cryptoNumberFormat.maximumFractionDigits = maximumFractionDigits;
|
|
|
|
}
|
2020-10-07 05:58:22 +00:00
|
|
|
|
|
|
|
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);
|
2020-10-07 05:58:22 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-05-07 07:36:38 +00:00
|
|
|
if (wallet is ElectrumWallet) {
|
2021-02-12 22:38:34 +00:00
|
|
|
final rate = wallet.feeRate(_priority);
|
2021-05-11 13:52:48 +00:00
|
|
|
return '${priority.labelWithRate(rate)}';
|
2021-02-12 22:38:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return priority.toString();
|
|
|
|
}
|
2020-08-25 16:32:40 +00:00
|
|
|
}
|