2020-08-25 16:32:40 +00:00
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
import 'package:mobx/mobx.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';
|
|
|
|
import 'package:cake_wallet/entities/transaction_priority.dart';
|
|
|
|
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';
|
|
|
|
|
|
|
|
class SendViewModel = SendViewModelBase with _$SendViewModel;
|
|
|
|
|
|
|
|
abstract class SendViewModelBase with Store {
|
|
|
|
SendViewModelBase(
|
|
|
|
this._wallet, this._settingsStore, this._fiatConversationStore)
|
2020-09-21 11:50:26 +00:00
|
|
|
: state = InitialExecutionState(),
|
|
|
|
_cryptoNumberFormat = NumberFormat(),
|
|
|
|
sendAll = false {
|
|
|
|
_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
|
2020-09-02 08:47:41 +00:00
|
|
|
bool sendAll;
|
2020-08-25 16:32:40 +00:00
|
|
|
|
|
|
|
FiatCurrency get fiat => _settingsStore.fiatCurrency;
|
|
|
|
|
|
|
|
TransactionPriority get transactionPriority =>
|
|
|
|
_settingsStore.transactionPriority;
|
|
|
|
|
|
|
|
double get estimatedFee =>
|
|
|
|
_wallet.calculateEstimatedFee(_settingsStore.transactionPriority);
|
|
|
|
|
|
|
|
CryptoCurrency get currency => _wallet.currency;
|
|
|
|
|
|
|
|
Validator get amountValidator => AmountValidator(type: _wallet.type);
|
|
|
|
|
|
|
|
Validator get addressValidator => AddressValidator(type: _wallet.currency);
|
|
|
|
|
2020-09-02 08:47:41 +00:00
|
|
|
Validator get templateValidator => TemplateValidator();
|
|
|
|
|
2020-08-25 16:32:40 +00:00
|
|
|
PendingTransaction pendingTransaction;
|
|
|
|
|
|
|
|
@computed
|
|
|
|
String get balance {
|
|
|
|
if (_wallet is MoneroWallet) {
|
|
|
|
_wallet.balance.formattedUnlockedBalance;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_wallet is BitcoinWallet) {
|
|
|
|
_wallet.balance.confirmedFormatted;
|
|
|
|
}
|
|
|
|
|
|
|
|
return '0.0';
|
|
|
|
}
|
|
|
|
|
|
|
|
@computed
|
|
|
|
bool get isReadyForSend => _wallet.syncStatus is SyncedSyncStatus;
|
|
|
|
|
|
|
|
final WalletBase _wallet;
|
|
|
|
final SettingsStore _settingsStore;
|
2020-09-21 11:50:26 +00:00
|
|
|
final FiatConversionStore _fiatConversationStore;
|
2020-08-27 16:54:34 +00:00
|
|
|
final NumberFormat _cryptoNumberFormat;
|
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() {
|
|
|
|
cryptoAmount = '';
|
|
|
|
fiatAmount = '';
|
|
|
|
address = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
@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();
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
void _updateFiatAmount() {
|
|
|
|
try {
|
|
|
|
final fiat = calculateFiatAmount(
|
2020-08-27 16:54:34 +00:00
|
|
|
price: _fiatConversationStore.price,
|
|
|
|
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.price;
|
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:
|
2020-09-21 11:50:26 +00:00
|
|
|
final amount = !sendAll ? double.parse(_amount) : null;
|
|
|
|
|
2020-08-25 16:32:40 +00:00
|
|
|
return BitcoinTransactionCredentials(
|
|
|
|
address, amount, _settingsStore.transactionPriority);
|
|
|
|
case WalletType.monero:
|
2020-09-21 11:50:26 +00:00
|
|
|
final amount = !sendAll ? _amount : null;
|
|
|
|
|
|
|
|
return MoneroTransactionCreationCredentials(
|
|
|
|
address: address,
|
|
|
|
paymentId: '',
|
|
|
|
priority: _settingsStore.transactionPriority,
|
|
|
|
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;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_cryptoNumberFormat.maximumFractionDigits = maximumFractionDigits;
|
|
|
|
}
|
2020-08-25 16:32:40 +00:00
|
|
|
}
|