| 1 | import 'package:cake_wallet/bitcoin/bitcoin.dart'; |
| 2 | import 'package:cake_wallet/di.dart'; |
| 3 | import 'package:cake_wallet/entities/auto_generate_subaddress_status.dart'; |
| 4 | import 'package:cake_wallet/entities/fiat_api_mode.dart'; |
| 5 | import 'package:cake_wallet/entities/wallet_manager.dart'; |
| 6 | import 'package:cake_wallet/evm/evm.dart'; |
| 7 | import 'package:cake_wallet/reactions/wallet_connect.dart'; |
| 8 | import 'package:cake_wallet/solana/solana.dart'; |
| 9 | import 'package:cake_wallet/tron/tron.dart'; |
| 10 | import 'package:cake_wallet/utils/tor.dart'; |
| 11 | import 'package:cw_core/crypto_currency.dart'; |
| 12 | import 'package:cw_core/sync_status.dart'; |
| 13 | import 'package:cw_core/transaction_history.dart'; |
| 14 | import 'package:cw_core/balance.dart'; |
| 15 | import 'package:cw_core/transaction_info.dart'; |
| 16 | import 'package:cw_core/utils/print_verbose.dart'; |
| 17 | import 'package:mobx/mobx.dart'; |
| 18 | import 'package:cake_wallet/reactions/check_connection.dart'; |
| 19 | import 'package:cake_wallet/reactions/on_wallet_sync_status_change.dart'; |
| 20 | import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart'; |
| 21 | import 'package:cake_wallet/store/app_store.dart'; |
| 22 | import 'package:cake_wallet/store/settings_store.dart'; |
| 23 | import 'package:cake_wallet/core/fiat_conversion_service.dart'; |
| 24 | import 'package:cw_core/wallet_base.dart'; |
| 25 | import 'package:cw_core/wallet_type.dart'; |
| 26 | |
| 27 | ReactionDisposer? _onCurrentWalletChangeReaction; |
| 28 | ReactionDisposer? _onCurrentWalletChangeFiatRateUpdateReaction; |
| 29 | //ReactionDisposer _onCurrentWalletAddressChangeReaction; |
| 30 | |
| 31 | void startCurrentWalletChangeReaction( |
| 32 | AppStore appStore, SettingsStore settingsStore, FiatConversionStore fiatConversionStore) { |
| 33 | _onCurrentWalletChangeReaction?.reaction.dispose(); |
| 34 | _onCurrentWalletChangeFiatRateUpdateReaction?.reaction.dispose(); |
| 35 | //_onCurrentWalletAddressChangeReaction?.reaction?dispose(); |
| 36 | |
| 37 | //_onCurrentWalletAddressChangeReaction = reaction((_) => appStore.wallet.walletAddresses.address, |
| 38 | //(String address) async { |
| 39 | //if (address == appStore.wallet.walletInfo.yatLastUsedAddress) { |
| 40 | // return; |
| 41 | //} |
| 42 | |
| 43 | //try { |
| 44 | // final yatStore = getIt.get<YatStore>(); |
| 45 | // await updateEmojiIdAddress( |
| 46 | // appStore.wallet.walletInfo.yatEmojiId, |
| 47 | // appStore.wallet.walletAddresses.address, |
| 48 | // yatStore.apiKey, |
| 49 | // appStore.wallet.type |
| 50 | // ); |
| 51 | // appStore.wallet.walletInfo.yatLastUsedAddress = address; |
| 52 | // await appStore.wallet.walletInfo.save(); |
| 53 | //} catch (e) { |
| 54 | // printV(e.toString()); |
| 55 | //} |
| 56 | //}); |
| 57 | |
| 58 | _onCurrentWalletChangeReaction = reaction((_) => appStore.wallet, |
| 59 | (WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo>? |
| 60 | wallet) async { |
| 61 | try { |
| 62 | if (wallet == null) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | await getIt.get<WalletManager>().ensureGroupHasHashedIdentifier(wallet); |
| 67 | |
| 68 | int? chainId; |
| 69 | if (isEVMCompatibleChain(wallet.type)) { |
| 70 | chainId = evm!.getSelectedChainId(wallet); |
| 71 | } |
| 72 | |
| 73 | final node = settingsStore.getCurrentNode(wallet.type, chainId: chainId); |
| 74 | |
| 75 | startWalletSyncStatusChangeReaction(wallet, settingsStore); |
| 76 | startCheckConnectionReaction(wallet, settingsStore); |
| 77 | |
| 78 | await Future.delayed(Duration.zero); |
| 79 | |
| 80 | if (wallet.type == WalletType.monero || |
| 81 | wallet.type == WalletType.wownero || |
| 82 | wallet.type == WalletType.bitcoin || |
| 83 | wallet.type == WalletType.litecoin || |
| 84 | wallet.type == WalletType.bitcoinCash || |
| 85 | wallet.type == WalletType.dogecoin || |
| 86 | wallet.type == WalletType.decred) { |
| 87 | _setAutoGenerateSubaddressStatus(wallet, settingsStore); |
| 88 | } |
| 89 | |
| 90 | if (wallet.type == WalletType.bitcoin) { |
| 91 | bitcoin!.updatePayjoinState(wallet, settingsStore.usePayjoin); |
| 92 | } |
| 93 | |
| 94 | if (settingsStore.currentBuiltinTor) { |
| 95 | await ensureTorStarted(context: null); |
| 96 | } |
| 97 | |
| 98 | await wallet.connectToNode(node: node); |
| 99 | SyncingSyncStatus.blockHistory.clear(); |
| 100 | if (wallet.type == WalletType.nano || wallet.type == WalletType.banano) { |
| 101 | final powNode = settingsStore.getCurrentPowNode(wallet.type); |
| 102 | await wallet.connectToPowNode(node: powNode); |
| 103 | } |
| 104 | |
| 105 | if (wallet.walletInfo.address.isEmpty) { |
| 106 | wallet.walletInfo.address = wallet.walletAddresses.address; |
| 107 | |
| 108 | await wallet.walletInfo.save(); |
| 109 | } |
| 110 | |
| 111 | if (isEVMCompatibleChain(wallet.type)) { |
| 112 | await evm!.discoverAndAddWalletTokens(wallet); |
| 113 | } |
| 114 | |
| 115 | if (wallet.type == WalletType.solana) { |
| 116 | await solana!.discoverAndAddWalletTokens(wallet); |
| 117 | } |
| 118 | } catch (e) { |
| 119 | printV(e.toString()); |
| 120 | } |
| 121 | }); |
| 122 | |
| 123 | _onCurrentWalletChangeFiatRateUpdateReaction = reaction((_) => appStore.wallet, |
| 124 | (WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo>? |
| 125 | wallet) async { |
| 126 | try { |
| 127 | if (wallet == null || settingsStore.fiatApiMode == FiatApiMode.disabled) { |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | fiatConversionStore.prices[wallet.currency] = 0; |
| 132 | fiatConversionStore.prices[wallet.currency] = await FiatConversionService.fetchPrice( |
| 133 | crypto: wallet.currency, |
| 134 | fiat: settingsStore.fiatCurrency, |
| 135 | torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly); |
| 136 | |
| 137 | Iterable<CryptoCurrency>? currencies; |
| 138 | if (isEVMCompatibleChain(wallet.type)) { |
| 139 | currencies = evm!.getERC20Currencies(appStore.wallet!).where((element) => element.enabled); |
| 140 | } |
| 141 | if (wallet.type == WalletType.solana) { |
| 142 | currencies = |
| 143 | solana!.getSPLTokenCurrencies(appStore.wallet!).where((element) => element.enabled); |
| 144 | } |
| 145 | if (wallet.type == WalletType.tron) { |
| 146 | currencies = |
| 147 | tron!.getTronTokenCurrencies(appStore.wallet!).where((element) => element.enabled); |
| 148 | } |
| 149 | |
| 150 | if (currencies != null) { |
| 151 | for (final currency in currencies) { |
| 152 | () async { |
| 153 | fiatConversionStore.prices[currency] = await FiatConversionService.fetchPrice( |
| 154 | crypto: currency, |
| 155 | fiat: settingsStore.fiatCurrency, |
| 156 | torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly); |
| 157 | }.call(); |
| 158 | } |
| 159 | } |
| 160 | } catch (e) { |
| 161 | printV(e.toString()); |
| 162 | } |
| 163 | }); |
| 164 | } |
| 165 | |
| 166 | void _setAutoGenerateSubaddressStatus( |
| 167 | WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo> wallet, |
| 168 | SettingsStore settingsStore, |
| 169 | ) async { |
| 170 | wallet.isEnabledAutoGenerateSubaddress = |
| 171 | settingsStore.autoGenerateSubaddressStatus == AutoGenerateSubaddressStatus.enabled || |
| 172 | settingsStore.autoGenerateSubaddressStatus == AutoGenerateSubaddressStatus.initialized; |
| 173 | } |