| 1 | import 'package:cake_wallet/evm/evm.dart'; |
| 2 | import 'package:cake_wallet/reactions/wallet_connect.dart'; |
| 3 | import 'package:cake_wallet/store/app_store.dart'; |
| 4 | import 'package:cake_wallet/view_model/send/template_view_model.dart'; |
| 5 | import 'package:cw_core/crypto_currency.dart'; |
| 6 | import 'package:cw_core/currency_for_wallet_type.dart'; |
| 7 | import 'package:cw_core/wallet_type.dart'; |
| 8 | import 'package:mobx/mobx.dart'; |
| 9 | import 'package:cake_wallet/entities/template.dart'; |
| 10 | import 'package:cake_wallet/store/templates/send_template_store.dart'; |
| 11 | import 'package:cake_wallet/core/template_validator.dart'; |
| 12 | import 'package:cake_wallet/core/address_validator.dart'; |
| 13 | import 'package:cake_wallet/core/amount_validator.dart'; |
| 14 | import 'package:cw_core/wallet_base.dart'; |
| 15 | import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart'; |
| 16 | |
| 17 | part 'send_template_view_model.g.dart'; |
| 18 | |
| 19 | class SendTemplateViewModel = SendTemplateViewModelBase with _$SendTemplateViewModel; |
| 20 | |
| 21 | abstract class SendTemplateViewModelBase with Store { |
| 22 | final WalletBase _wallet; |
| 23 | final AppStore _appStore; |
| 24 | final SendTemplateStore _sendTemplateStore; |
| 25 | final FiatConversionStore _fiatConversationStore; |
| 26 | |
| 27 | SendTemplateViewModelBase( |
| 28 | this._wallet, this._appStore, this._sendTemplateStore, this._fiatConversationStore) |
| 29 | : recipients = ObservableList<TemplateViewModel>() { |
| 30 | addRecipient(); |
| 31 | } |
| 32 | |
| 33 | ObservableList<TemplateViewModel> recipients; |
| 34 | |
| 35 | @action |
| 36 | void addRecipient() { |
| 37 | recipients.add(TemplateViewModel( |
| 38 | wallet: _wallet, appStore: _appStore, fiatConversationStore: _fiatConversationStore)); |
| 39 | } |
| 40 | |
| 41 | @action |
| 42 | void removeRecipient(TemplateViewModel recipient) { |
| 43 | recipients.remove(recipient); |
| 44 | } |
| 45 | |
| 46 | AmountValidator get amountValidator => AmountValidator( |
| 47 | currency: walletTypeToCryptoCurrency(_wallet.type, chainId: _wallet.chainId), |
| 48 | amountParsingProxy: _appStore.amountParsingProxy, |
| 49 | ); |
| 50 | |
| 51 | AddressValidator get addressValidator => AddressValidator( |
| 52 | type: _wallet.currency, |
| 53 | isTestnet: _wallet.isTestnet, |
| 54 | network: _wallet.walletInfo.network, |
| 55 | ); |
| 56 | |
| 57 | TemplateValidator get templateValidator => TemplateValidator(); |
| 58 | |
| 59 | bool get hasMultiRecipient => |
| 60 | _wallet.type != WalletType.haven && |
| 61 | _wallet.type != WalletType.solana && |
| 62 | _wallet.type != WalletType.tron && |
| 63 | !isEVMCompatibleChain(_wallet.type); |
| 64 | |
| 65 | @computed |
| 66 | CryptoCurrency get cryptoCurrency => _wallet.currency; |
| 67 | |
| 68 | @computed |
| 69 | String get fiatCurrency => _appStore.settingsStore.fiatCurrency.title; |
| 70 | |
| 71 | @computed |
| 72 | int get fiatCurrencyDecimals => _appStore.settingsStore.fiatCurrency.decimals; |
| 73 | |
| 74 | @computed |
| 75 | ObservableList<Template> get templates => _sendTemplateStore.templates; |
| 76 | |
| 77 | @action |
| 78 | void updateTemplate() => _sendTemplateStore.update(); |
| 79 | |
| 80 | @action |
| 81 | void addTemplate( |
| 82 | {required String name, |
| 83 | required bool isCurrencySelected, |
| 84 | required String cryptoCurrency, |
| 85 | required String address, |
| 86 | required String amount, |
| 87 | required String amountFiat, |
| 88 | required List<Template> additionalRecipients}) { |
| 89 | _sendTemplateStore.addTemplate( |
| 90 | name: name, |
| 91 | isCurrencySelected: isCurrencySelected, |
| 92 | address: address, |
| 93 | cryptoCurrency: cryptoCurrency, |
| 94 | fiatCurrency: fiatCurrency, |
| 95 | amount: amount, |
| 96 | amountFiat: amountFiat, |
| 97 | additionalRecipients: additionalRecipients); |
| 98 | updateTemplate(); |
| 99 | } |
| 100 | |
| 101 | @action |
| 102 | void removeTemplate({required Template template}) { |
| 103 | _sendTemplateStore.remove(template: template); |
| 104 | updateTemplate(); |
| 105 | } |
| 106 | |
| 107 | @computed |
| 108 | List<CryptoCurrency> get walletCurrencies => _wallet.balance.keys.toList(); |
| 109 | |
| 110 | bool get hasMultipleTokens => |
| 111 | isEVMCompatibleChain(_wallet.type) || |
| 112 | _wallet.type == WalletType.solana || |
| 113 | _wallet.type == WalletType.tron; |
| 114 | } |