diff --git a/app/components/Nav/Main/MainNavigator.js b/app/components/Nav/Main/MainNavigator.js index 54dec72c894..519942d55a7 100644 --- a/app/components/Nav/Main/MainNavigator.js +++ b/app/components/Nav/Main/MainNavigator.js @@ -81,6 +81,7 @@ import DeprecatedNetworkDetails from '../../UI/DeprecatedNetworkModal'; import ConfirmAddAsset from '../../UI/ConfirmAddAsset'; import { AesCryptoTestForm } from '../../Views/AesCryptoTestForm'; import { isTest } from '../../../util/test/utils'; +import { selectPermissionControllerState } from '../../../selectors/snaps/permissionController'; const Stack = createStackNavigator(); const Tab = createBottomTabNavigator(); @@ -395,8 +396,7 @@ const HomeTabs = () => { const activeTabUrl = getActiveTabUrl(state); if (!isUrl(activeTabUrl)) return []; try { - const permissionsControllerState = - state.engine.backgroundState.PermissionController; + const permissionsControllerState = selectPermissionControllerState(state); const hostname = new URL(activeTabUrl).hostname; const permittedAcc = getPermittedAccountsByHostname( permissionsControllerState, diff --git a/app/components/Nav/Main/RootRPCMethodsUI.js b/app/components/Nav/Main/RootRPCMethodsUI.js index 47a2e5bf552..c602a8acebf 100644 --- a/app/components/Nav/Main/RootRPCMethodsUI.js +++ b/app/components/Nav/Main/RootRPCMethodsUI.js @@ -67,6 +67,7 @@ import { selectShouldUseSmartTransaction } from '../../../selectors/smartTransac import { STX_NO_HASH_ERROR } from '../../../util/smart-transactions/smart-publish-hook'; import { getSmartTransactionMetricsProperties } from '../../../util/smart-transactions'; import { cloneDeep, isEqual } from 'lodash'; +import { selectSwapsTransactions } from '../../../selectors/transactionController'; ///: BEGIN:ONLY_INCLUDE_IF(preinstalled-snaps,external-snaps) import InstallSnapApproval from '../../Approvals/InstallSnapApproval'; @@ -75,11 +76,7 @@ import InstallSnapApproval from '../../Approvals/InstallSnapApproval'; const hstInterface = new ethers.utils.Interface(abi); function useSwapsTransactions() { - const swapTransactions = useSelector( - (state) => - state.engine.backgroundState.TransactionController.swapsTransactions, - isEqual, - ); + const swapTransactions = useSelector(selectSwapsTransactions, isEqual); // Memo prevents fresh fallback empty object on every render. return useMemo(() => swapTransactions ?? {}, [swapTransactions]); @@ -98,7 +95,6 @@ export const useSwapConfirmedEvent = ({ trackSwaps }) => { }, [transactionMetaIdsForListening], ); - const swapsTransactions = useSwapsTransactions(); useEffect(() => { @@ -260,7 +256,6 @@ const RootRPCMethodsUI = (props) => { const { addTransactionMetaIdForListening } = useSwapConfirmedEvent({ trackSwaps, }); - const swapsTransactions = useSwapsTransactions(); const autoSign = useCallback( diff --git a/app/components/Views/AccountPermissions/AccountPermissions.tsx b/app/components/Views/AccountPermissions/AccountPermissions.tsx index d450e93dffa..bc92ccc9cc7 100755 --- a/app/components/Views/AccountPermissions/AccountPermissions.tsx +++ b/app/components/Views/AccountPermissions/AccountPermissions.tsx @@ -50,6 +50,7 @@ import useFavicon from '../../hooks/useFavicon/useFavicon'; import URLParse from 'url-parse'; import { useMetrics } from '../../../components/hooks/useMetrics'; import { selectInternalAccounts } from '../../../selectors/accountsController'; +import { selectPermissionControllerState } from '../../../selectors/snaps/permissionController'; const AccountPermissions = (props: AccountPermissionsProps) => { const navigation = useNavigation(); @@ -94,11 +95,7 @@ const AccountPermissions = (props: AccountPermissionsProps) => { const { toastRef } = useContext(ToastContext); const [isLoading, setIsLoading] = useState(false); - const permittedAccountsList = useSelector( - // TODO: Replace "any" with type - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (state: any) => state.engine.backgroundState.PermissionController, - ); + const permittedAccountsList = useSelector(selectPermissionControllerState); const permittedAccountsByHostname = getPermittedAccountsByHostname( permittedAccountsList, hostname, diff --git a/app/components/Views/Asset/index.js b/app/components/Views/Asset/index.js index ed29e1b8ae9..f29480e4a32 100644 --- a/app/components/Views/Asset/index.js +++ b/app/components/Views/Asset/index.js @@ -64,6 +64,7 @@ import { withMetricsAwareness } from '../../../components/hooks/useMetrics'; import { store } from '../../../store'; import { createBuyNavigationDetails } from '../../UI/Ramp/routes/utils'; import { toChecksumHexAddress } from '@metamask/controller-utils'; +import { selectSwapsTransactions } from '../../../selectors/transactionController'; const createStyles = (colors) => StyleSheet.create({ @@ -577,8 +578,7 @@ Asset.contextType = ThemeContext; const mapStateToProps = (state) => ({ swapsIsLive: swapsLivenessSelector(state), swapsTokens: swapsTokensObjectSelector(state), - swapsTransactions: - state.engine.backgroundState.TransactionController.swapsTransactions || {}, + swapsTransactions: selectSwapsTransactions(state), conversionRate: selectConversionRate(state), currentCurrency: selectCurrentCurrency(state), selectedInternalAccount: selectSelectedInternalAccount(state), diff --git a/app/components/Views/Browser/index.js b/app/components/Views/Browser/index.js index 7f08eaa9acf..f67f0700bd4 100644 --- a/app/components/Views/Browser/index.js +++ b/app/components/Views/Browser/index.js @@ -39,6 +39,7 @@ import URL from 'url-parse'; import { useMetrics } from '../../../components/hooks/useMetrics'; import { selectNetworkConfigurations } from '../../../selectors/networkController'; import { getBrowserViewNavbarOptions } from '../../UI/Navbar'; +import { selectPermissionControllerState } from '../../../selectors/snaps/permissionController'; const margin = 16; const THUMB_WIDTH = Dimensions.get('window').width / 2 - margin * 2; @@ -82,8 +83,7 @@ export const Browser = (props) => { const permittedAccountsList = useSelector((state) => { if (!activeTab) return []; - const permissionsControllerState = - state.engine.backgroundState.PermissionController; + const permissionsControllerState = selectPermissionControllerState(state); const hostname = new URL(activeTab.url).hostname; const permittedAcc = getPermittedAccountsByHostname( permissionsControllerState, diff --git a/app/components/Views/BrowserTab/index.js b/app/components/Views/BrowserTab/index.js index 0198d6fdf62..5d8ca238d4b 100644 --- a/app/components/Views/BrowserTab/index.js +++ b/app/components/Views/BrowserTab/index.js @@ -101,6 +101,7 @@ import { BrowserViewSelectorsIDs } from '../../../../e2e/selectors/Browser/Brows import { useMetrics } from '../../../components/hooks/useMetrics'; import { trackDappViewedEvent } from '../../../util/metrics'; import trackErrorAsAnalytics from '../../../util/metrics/TrackError/trackErrorAsAnalytics'; +import { selectPermissionControllerState } from '../../../selectors/snaps/permissionController'; const { HOMEPAGE_URL, NOTIFICATION_NAMES } = AppConstants; const HOMEPAGE_HOST = new URL(HOMEPAGE_URL)?.hostname; @@ -276,8 +277,7 @@ export const BrowserTab = (props) => { const fromHomepage = useRef(false); const wizardScrollAdjusted = useRef(false); const permittedAccountsList = useSelector((state) => { - const permissionsControllerState = - state.engine.backgroundState.PermissionController; + const permissionsControllerState = selectPermissionControllerState(state); const hostname = new URL(url.current).hostname; const permittedAcc = getPermittedAccountsByHostname( permissionsControllerState, diff --git a/app/components/hooks/useExistingAddress.ts b/app/components/hooks/useExistingAddress.ts index 77b75ef7771..e1d8c93cf0d 100644 --- a/app/components/hooks/useExistingAddress.ts +++ b/app/components/hooks/useExistingAddress.ts @@ -5,16 +5,15 @@ import { selectChainId } from '../../selectors/networkController'; import { selectInternalAccounts } from '../../selectors/accountsController'; import { toLowerCaseEquals } from '../../util/general'; import { AddressBookEntry } from '@metamask/address-book-controller'; -import { RootState } from '../../reducers'; +import { selectAddressBook } from '../../selectors/addressBookController'; type AccountInfo = Pick; const useExistingAddress = (address?: string): AccountInfo | undefined => { const chainId = useSelector(selectChainId); - const { addressBook, internalAccounts } = useSelector((state: RootState) => ({ - addressBook: state.engine.backgroundState.AddressBookController.addressBook, - internalAccounts: selectInternalAccounts(state), - })); + + const addressBook = useSelector(selectAddressBook); + const internalAccounts = useSelector(selectInternalAccounts); if (!address) return; diff --git a/app/selectors/transactionController.ts b/app/selectors/transactionController.ts index ce8ba2ec0b5..7cfa4d6cb35 100644 --- a/app/selectors/transactionController.ts +++ b/app/selectors/transactionController.ts @@ -20,3 +20,10 @@ export const selectNonReplacedTransactions = createDeepEqualSelector( (transactions) => transactions.filter((tx) => !(tx.replacedBy && tx.replacedById && tx.hash)), ); + +export const selectSwapsTransactions = createSelector( + selectTransactionControllerState, + (transactionControllerState) => + //@ts-expect-error - This is populated at the app level, the TransactionController is not aware of this property + transactionControllerState.swapsTransactions ?? {}, +);