From 9033a25d29efb63b9a08c8fc2c26b294b95cd23e Mon Sep 17 00:00:00 2001 From: Jongsun Suh Date: Wed, 27 Nov 2024 10:55:40 -0500 Subject: [PATCH] Merge `MetamaskState` defined in `metametrics-controller` into `initialMetamaskState` --- .../controllers/metametrics-controller.ts | 46 ++----------------- shared/constants/metametrics.ts | 4 +- ui/ducks/metamask/constants.ts | 40 ++++++++++++++-- 3 files changed, 41 insertions(+), 49 deletions(-) diff --git a/app/scripts/controllers/metametrics-controller.ts b/app/scripts/controllers/metametrics-controller.ts index b2b78a4e6406..244a63ef13c7 100644 --- a/app/scripts/controllers/metametrics-controller.ts +++ b/app/scripts/controllers/metametrics-controller.ts @@ -10,8 +10,7 @@ import { } from 'lodash'; import { bufferToHex, keccak } from 'ethereumjs-util'; import { v4 as uuidv4 } from 'uuid'; -import { NameControllerState, NameType } from '@metamask/name-controller'; -import { AccountsControllerState } from '@metamask/accounts-controller'; +import { NameType } from '@metamask/name-controller'; import { getErrorMessage, Hex, @@ -23,14 +22,9 @@ import { NetworkControllerGetNetworkClientByIdAction, NetworkControllerGetStateAction, NetworkControllerNetworkDidChangeEvent, - NetworkState, } from '@metamask/network-controller'; import { Browser } from 'webextension-polyfill'; -import { - Nft, - NftControllerState, - TokensControllerState, -} from '@metamask/assets-controllers'; +import { Nft } from '@metamask/assets-controllers'; import { captureException as sentryCaptureException } from '@sentry/browser'; import { BaseController, @@ -38,7 +32,7 @@ import { ControllerStateChangeEvent, RestrictedControllerMessenger, } from '@metamask/base-controller'; -import { AddressBookControllerState } from '@metamask/address-book-controller'; +import type { MemStoreControllersComposedState } from '../metamask-controller-stores'; import { ENVIRONMENT_TYPE_BACKGROUND } from '../../../shared/constants/app'; import { METAMETRICS_ANONYMOUS_ID, @@ -65,7 +59,6 @@ import { AnonymousTransactionMetaMetricsEvent, TransactionMetaMetricsEvent, } from '../../../shared/constants/transaction'; -import { LedgerTransportTypes } from '../../../shared/constants/hardware-wallets'; import Analytics from '../lib/segment/analytics'; ///: BEGIN:ONLY_INCLUDE_IF(build-main) @@ -144,39 +137,6 @@ const exceptionsToFilter: Record = { */ type SegmentEventType = 'identify' | 'track' | 'page'; -// TODO: Complete MetaMaskState by adding the full state definition and relocate it after the background is converted to TypeScript. -export type MetaMaskState = { - ledgerTransportType: LedgerTransportTypes; - networkConfigurationsByChainId: NetworkState['networkConfigurationsByChainId']; - internalAccounts: AccountsControllerState['internalAccounts']; - allNfts: NftControllerState['allNfts']; - allTokens: TokensControllerState['allTokens']; - theme: string; - participateInMetaMetrics: boolean; - dataCollectionForMarketing: boolean; - ShowNativeTokenAsMainBalance: boolean; - useNftDetection: PreferencesControllerState['useNftDetection']; - openSeaEnabled: PreferencesControllerState['openSeaEnabled']; - securityAlertsEnabled: PreferencesControllerState['securityAlertsEnabled']; - useTokenDetection: PreferencesControllerState['useTokenDetection']; - tokenSortConfig: PreferencesControllerState['preferences']['tokenSortConfig']; - names: NameControllerState['names']; - security_providers: string[]; - addressBook: AddressBookControllerState['addressBook']; - currentCurrency: string; - preferences: { - privacyMode: PreferencesControllerState['preferences']['privacyMode']; - tokenNetworkFilter: string[]; - }; - ///: BEGIN:ONLY_INCLUDE_IF(build-mmi) - custodyAccountDetails: { - [address: string]: { - custodianName: string; - }; - }; - ///: END:ONLY_INCLUDE_IF -}; - /** * {@link MetaMetricsController}'s metadata. * diff --git a/shared/constants/metametrics.ts b/shared/constants/metametrics.ts index c87af10544c3..22998d09bf1d 100644 --- a/shared/constants/metametrics.ts +++ b/shared/constants/metametrics.ts @@ -455,11 +455,11 @@ export type MetaMetricsUserTraits = { /** * Does the user opt in for metrics */ - is_metrics_opted_in?: boolean; + is_metrics_opted_in?: boolean | null; /** * Does the user accepted marketing consent */ - has_marketing_consent?: boolean; + has_marketing_consent?: boolean | null; /** * The date the extension was installed. */ diff --git a/ui/ducks/metamask/constants.ts b/ui/ducks/metamask/constants.ts index d7b4961cf040..33105209263e 100644 --- a/ui/ducks/metamask/constants.ts +++ b/ui/ducks/metamask/constants.ts @@ -1,6 +1,11 @@ -import { Preferences } from '../../../app/scripts/controllers/preferences-controller'; +import { NameType } from '@metamask/name-controller'; +import { getDefaultPreferencesControllerState } from '../../../app/scripts/controllers/preferences-controller'; import { MemStoreControllersComposedState } from '../../../app/scripts/metamask-controller-stores'; -import { DEFAULT_AUTO_LOCK_TIME_LIMIT } from '../../../shared/constants/preferences'; +import { + DEFAULT_AUTO_LOCK_TIME_LIMIT, + ThemeType, +} from '../../../shared/constants/preferences'; +import { LedgerTransportTypes } from '../../../shared/constants/hardware-wallets'; export const initialMetamaskState: Partial<{ [ControllerName in keyof MemStoreControllersComposedState]: Partial< @@ -26,6 +31,7 @@ export const initialMetamaskState: Partial<{ approvalFlows: [], }, CurrencyController: { + currentCurrency: 'usd', currencyRates: { ETH: { conversionRate: null, @@ -50,33 +56,59 @@ export const initialMetamaskState: Partial<{ participateInMetaMetrics: null, dataCollectionForMarketing: null, }, + NameController: { + names: { + [NameType.ETHEREUM_ADDRESS]: {}, + }, + }, NetworkController: { selectedNetworkClientId: '', networkConfigurationsByChainId: {}, networksMetadata: {}, }, + NftController: { + allNfts: {}, + }, OnboardingController: { firstTimeFlowType: null, completedOnboarding: false, }, PreferencesController: { + ...getDefaultPreferencesControllerState(), + theme: ThemeType.os, useBlockie: false, use4ByteResolution: true, + useNftDetection: true, + useTokenDetection: true, + openSeaEnabled: true, + securityAlertsEnabled: true, featureFlags: {}, currentLocale: '', knownMethodData: {}, - ledgerTransportType: undefined, + // Ledger transport type is deprecated. We currently only support webhid + // on chrome, and u2f on firefox. + ledgerTransportType: window.navigator.hid + ? LedgerTransportTypes.webhid + : LedgerTransportTypes.u2f, preferences: { + ...getDefaultPreferencesControllerState().preferences, autoLockTimeLimit: DEFAULT_AUTO_LOCK_TIME_LIMIT, showExtensionInFullSizeView: false, showFiatInTestnets: false, + showNativeTokenAsMainBalance: true, showTestNetworks: false, smartTransactionsOptInStatus: true, petnamesEnabled: true, featureNotificationsEnabled: false, privacyMode: false, showMultiRpcModal: false, - } as Preferences, + tokenSortConfig: { + key: 'tokenFiatAmount', + order: 'dsc', + sortCallback: 'stringNumeric', + }, + tokenNetworkFilter: {}, + }, }, SignatureController: { unapprovedPersonalMsgs: {},