dev
dart 223 lines 7.8 KB
Raw
1 import 'package:cake_wallet/bitcoin_cash/bitcoin_cash.dart';
2 import 'package:cake_wallet/core/amount_parsing_proxy.dart';
3 import 'package:cake_wallet/decred/decred.dart';
4 import 'package:cake_wallet/dogecoin/dogecoin.dart';
5 import 'package:cake_wallet/entities/priority_for_wallet_type.dart';
6 import 'package:cake_wallet/core/wallet_change_listener_view_model.dart';
7 import 'package:cake_wallet/evm/evm.dart';
8 import 'package:cake_wallet/monero/monero.dart';
9 import 'package:cake_wallet/zcash/zcash.dart';
10 import 'package:cake_wallet/store/app_store.dart';
11 import 'package:cake_wallet/view_model/dashboard/balance_view_model.dart';
12 import 'package:cw_core/crypto_currency.dart';
13 import 'package:cw_core/transaction_priority.dart';
14 import 'package:mobx/mobx.dart';
15 import 'package:cake_wallet/entities/fiat_currency.dart';
16 import 'package:cw_core/wallet_type.dart';
17 import 'package:cake_wallet/store/settings_store.dart';
18 import 'package:cake_wallet/bitcoin/bitcoin.dart';
19 import 'package:collection/collection.dart';
20
21 part 'fees_view_model.g.dart';
22
23 class FeesViewModel = FeesViewModelBase with _$FeesViewModel;
24
25 abstract class FeesViewModelBase extends WalletChangeListenerViewModel with Store {
26 FeesViewModelBase(
27 AppStore appStore,
28 this.balanceViewModel,
29 ) : _appStore = appStore,
30 super(appStore: appStore) {
31 final priority = _settingsStore.getPriority(wallet.type, chainId: wallet.chainId);
32
33 if (wallet.type == WalletType.bitcoin && priority == bitcoinTransactionPriorityCustom) {
34 setTransactionPriority(bitcoinTransactionPriorityMedium);
35 }
36
37 final priorities = priorityForWalletType(wallet.type);
38 if (!priorities.contains(priority) && priorities.isNotEmpty) {
39 _settingsStore.setPriority(wallet.type, priorities.first, chainId: wallet.chainId);
40 }
41 }
42
43 final AppStore _appStore;
44 SettingsStore get _settingsStore => _appStore.settingsStore;
45 AmountParsingProxy get amountParsingProxy => _appStore.amountParsingProxy;
46
47 @computed
48 WalletType get walletType => wallet.type;
49 CryptoCurrency get currency => wallet.currency;
50 FiatCurrency get fiat => _settingsStore.fiatCurrency;
51 bool get isFiatDisabled => balanceViewModel.isFiatDisabled;
52
53 final BalanceViewModel balanceViewModel;
54
55 TransactionPriority get transactionPriority {
56 final priority = _settingsStore.getPriority(wallet.type, chainId: wallet.chainId);
57
58 if (priority == null && hasFeesPriority) {
59 throw Exception('Unexpected type ${wallet.type}');
60 }
61
62 return priority!;
63 }
64
65 int? getCustomPriorityIndex(List<TransactionPriority> priorities) {
66 if (wallet.type == WalletType.bitcoin) {
67 final customItem = priorities
68 .firstWhereOrNull((element) => element == bitcoin!.getBitcoinTransactionPriorityCustom());
69
70 return customItem != null ? priorities.indexOf(customItem) : null;
71 }
72 return null;
73 }
74
75 int? get maxCustomFeeRate {
76 if (wallet.type == WalletType.bitcoin) {
77 return bitcoin!.getMaxCustomFeeRate(wallet);
78 }
79 return null;
80 }
81
82 bool get isLowFee {
83 if (wallet.chainId == 42161) return false;
84
85 switch (wallet.type) {
86 case WalletType.monero:
87 case WalletType.wownero:
88 case WalletType.haven:
89 case WalletType.zano:
90 return transactionPriority == monero!.getMoneroTransactionPrioritySlow();
91 case WalletType.bitcoin:
92 return transactionPriority == bitcoin!.getBitcoinTransactionPrioritySlow();
93 case WalletType.litecoin:
94 return transactionPriority == bitcoin!.getLitecoinTransactionPrioritySlow();
95 case WalletType.ethereum:
96 case WalletType.polygon:
97 case WalletType.base:
98 case WalletType.bsc:
99 return transactionPriority == evm!.getEVMTransactionPrioritySlow();
100 case WalletType.bitcoinCash:
101 return transactionPriority == bitcoinCash!.getBitcoinCashTransactionPrioritySlow();
102 case WalletType.decred:
103 return transactionPriority == decred!.getDecredTransactionPrioritySlow();
104 case WalletType.dogecoin:
105 return transactionPriority == dogecoin!.getDogeCoinTransactionPrioritySlow();
106 case WalletType.none:
107 case WalletType.nano:
108 case WalletType.banano:
109 case WalletType.solana:
110 case WalletType.tron:
111 case WalletType.arbitrum:
112 case WalletType.zcash:
113 return false;
114 }
115 }
116
117 @computed
118 int get customBitcoinFeeRate => _settingsStore.customBitcoinFeeRate;
119
120 void set customBitcoinFeeRate(int value) => _settingsStore.customBitcoinFeeRate = value;
121
122 @computed
123 bool get hasFees => wallet.type != WalletType.nano && wallet.type != WalletType.banano;
124
125 @computed
126 bool get hasFeesPriority =>
127 wallet.type != WalletType.nano &&
128 wallet.type != WalletType.banano &&
129 wallet.type != WalletType.solana &&
130 wallet.type != WalletType.tron &&
131 wallet.chainId !=
132 42161; // Wallet type is generic for all EVM chains, so we need to check the chainId
133
134 @computed
135 bool get isElectrumWallet =>
136 wallet.type == WalletType.bitcoin ||
137 wallet.type == WalletType.litecoin ||
138 wallet.type == WalletType.bitcoinCash ||
139 wallet.type == WalletType.dogecoin;
140
141 String? get walletCurrencyName => wallet.currency.fullName?.toLowerCase() ?? wallet.currency.name;
142
143 @computed
144 FiatCurrency get fiatCurrency => _settingsStore.fiatCurrency;
145
146 @action
147 void setTransactionPriority(TransactionPriority priority) =>
148 _settingsStore.setPriority(wallet.type, priority, chainId: wallet.chainId);
149
150 bool showAlertForCustomFeeRate() {
151 if (wallet.type != WalletType.bitcoin || isLowFee) {
152 return false;
153 }
154
155 if (transactionPriority != bitcoinTransactionPriorityCustom) {
156 return false;
157 }
158
159 final mediumRate = bitcoin!.getFeeRate(wallet, bitcoinTransactionPriorityMedium);
160 return customBitcoinFeeRate < mediumRate;
161 }
162
163 String displayFeeRate(dynamic priority, int? customValue) {
164 final _priority = priority as TransactionPriority;
165
166 if (wallet.type == WalletType.bitcoin) {
167 final rate = bitcoin!.getFeeRate(wallet, _priority);
168 return bitcoin!.bitcoinTransactionPriorityWithLabel(_priority, rate, customRate: customValue);
169 }
170
171 if (isElectrumWallet) {
172 final rate = bitcoin!.getFeeRate(wallet, _priority);
173 return bitcoin!.bitcoinTransactionPriorityWithLabel(_priority, rate);
174 }
175
176 return priority.toString();
177 }
178
179 TransactionPriority get bitcoinTransactionPriorityCustom =>
180 bitcoin!.getBitcoinTransactionPriorityCustom();
181
182 TransactionPriority get bitcoinTransactionPriorityMedium =>
183 bitcoin!.getBitcoinTransactionPriorityMedium();
184
185 @action
186 void setDefaultTransactionPriority() {
187 switch (wallet.type) {
188 case WalletType.monero:
189 case WalletType.haven:
190 case WalletType.wownero:
191 case WalletType.zano:
192 _settingsStore.setPriority(wallet.type, monero!.getMoneroTransactionPriorityAutomatic());
193 break;
194 case WalletType.zcash:
195 _settingsStore.setPriority(wallet.type, zcash!.getZcashTransactionPriorityAutomatic());
196 break;
197 case WalletType.bitcoin:
198 _settingsStore.setPriority(wallet.type, bitcoin!.getBitcoinTransactionPriorityMedium());
199 break;
200 case WalletType.litecoin:
201 _settingsStore.setPriority(wallet.type, bitcoin!.getLitecoinTransactionPriorityMedium());
202 break;
203 case WalletType.ethereum:
204 case WalletType.polygon:
205 case WalletType.base:
206 case WalletType.bsc:
207 _settingsStore.setPriority(
208 wallet.type,
209 evm!.getDefaultTransactionPriority(),
210 chainId: wallet.chainId,
211 );
212 break;
213 case WalletType.bitcoinCash:
214 _settingsStore.setPriority(wallet.type, bitcoinCash!.getDefaultTransactionPriority());
215 break;
216 case WalletType.dogecoin:
217 _settingsStore.setPriority(wallet.type, dogecoin!.getDefaultTransactionPriority());
218 break;
219 default:
220 break;
221 }
222 }
223 }