Skip to content

Commit

Permalink
stash
Browse files Browse the repository at this point in the history
  • Loading branch information
MajorLift committed Jan 30, 2025
1 parent 67dbeac commit 68b0a52
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 832 deletions.
179 changes: 53 additions & 126 deletions app/scripts/controllers/metametrics-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -23,22 +22,16 @@ 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,
ControllerGetStateAction,
ControllerStateChangeEvent,
RestrictedControllerMessenger,
} from '@metamask/base-controller';
import { AddressBookControllerState } from '@metamask/address-book-controller';
import { ENVIRONMENT_TYPE_BACKGROUND } from '../../../shared/constants/app';
import {
METAMETRICS_ANONYMOUS_ID,
Expand All @@ -65,13 +58,13 @@ 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)
import { ENVIRONMENT } from '../../../development/build/constants';
///: END:ONLY_INCLUDE_IF

import { BackgroundStateProxy } from '../../../shared/types/background';
import type {
PreferencesControllerState,

Check failure on line 69 in app/scripts/controllers/metametrics-controller.ts

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

'PreferencesControllerState' is defined but never used
PreferencesControllerGetStateAction,
Expand Down Expand Up @@ -144,39 +137,6 @@ const exceptionsToFilter: Record<string, boolean> = {
*/
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.
*
Expand Down Expand Up @@ -358,10 +318,6 @@ export default class MetaMetricsController extends BaseController<

#environment: MetaMetricsControllerOptions['environment'];

///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
#selectedAddress: PreferencesControllerState['selectedAddress'];
///: END:ONLY_INCLUDE_IF

#segment: MetaMetricsControllerOptions['segment'];

/**
Expand Down Expand Up @@ -412,10 +368,6 @@ export default class MetaMetricsController extends BaseController<
this.#extension = extension;
this.#environment = environment;

///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
this.#selectedAddress = preferencesControllerState.selectedAddress;
///: END:ONLY_INCLUDE_IF

const abandonedFragments = omitBy(state.fragments, 'persist');

this.messagingSystem.subscribe(
Expand Down Expand Up @@ -584,6 +536,7 @@ export default class MetaMetricsController extends BaseController<
: {};

this.update((state) => {
// @ts-expect-error TODO: Fix `Type instantiation is excessively deep and possibly infinite.ts(2589)`
state.fragments[id] = merge({}, additionalFragmentProps, fragment);
});

Expand Down Expand Up @@ -1004,7 +957,7 @@ export default class MetaMetricsController extends BaseController<
}
}

handleMetaMaskStateUpdate(newState: MetaMaskState): void {
handleMetaMaskStateUpdate(newState: BackgroundStateProxy): void {
const userTraits = this._buildUserTraitsObject(newState);
if (userTraits) {
this.identify(userTraits);
Expand Down Expand Up @@ -1067,23 +1020,10 @@ export default class MetaMetricsController extends BaseController<
referrer: MetaMetricsContext['referrer'],
page: MetaMetricsContext['page'] = METAMETRICS_BACKGROUND_PAGE_OBJECT,
): MetaMetricsContext {
///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
const mmiProps: {
extensionId?: string;
} = {};

if (this.#extension?.runtime?.id) {
mmiProps.extensionId = this.#extension.runtime.id;
}
///: END:ONLY_INCLUDE_IF

return {
app: {
name: 'MetaMask Extension',
version: this.version,
///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
...mmiProps,
///: END:ONLY_INCLUDE_IF
},
userAgent: window.navigator.userAgent,
page,
Expand Down Expand Up @@ -1115,21 +1055,6 @@ export default class MetaMetricsController extends BaseController<
environmentType = ENVIRONMENT_TYPE_BACKGROUND,
} = rawPayload;

///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
const mmiProps: {
extensionId?: string;
accountAddress?: string;
} = {};

if (this.#extension?.runtime?.id) {
mmiProps.extensionId = this.#extension.runtime.id;
}

if (this.#selectedAddress) {
mmiProps.accountAddress = this.#selectedAddress;
}
///: END:ONLY_INCLUDE_IF

return {
event,
messageId: buildUniqueMessageId(rawPayload),
Expand All @@ -1153,9 +1078,6 @@ export default class MetaMetricsController extends BaseController<
? properties.chain_id
: this.chainId,
environment_type: environmentType,
///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
...mmiProps,
///: END:ONLY_INCLUDE_IF
},
context: this.#buildContext(referrer, page),
};
Expand All @@ -1169,73 +1091,73 @@ export default class MetaMetricsController extends BaseController<
* @returns traits that have changed since last update
*/
_buildUserTraitsObject(
metamaskState: MetaMaskState,
metamaskState: BackgroundStateProxy,
): Partial<MetaMetricsUserTraits> | null {
///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
const mmiAccountAddress =
metamaskState.custodyAccountDetails &&
Object.keys(metamaskState.custodyAccountDetails).length
? Object.keys(metamaskState.custodyAccountDetails)[0]
: null;
///: END:ONLY_INCLUDE_IF
const { traits, previousUserTraits } = this.state;

const currentTraits = {
[MetaMetricsUserTrait.AddressBookEntries]: sum(
Object.values(metamaskState.addressBook).map(size),
Object.values(metamaskState.AddressBookController.addressBook).map(
size,
),
),
[MetaMetricsUserTrait.InstallDateExt]:
traits[MetaMetricsUserTrait.InstallDateExt] || '',
[MetaMetricsUserTrait.LedgerConnectionType]:
metamaskState.ledgerTransportType,
metamaskState.PreferencesController.ledgerTransportType,
[MetaMetricsUserTrait.NetworksAdded]: Object.values(
metamaskState.networkConfigurationsByChainId,
metamaskState.NetworkController.networkConfigurationsByChainId,
).map((networkConfiguration) => networkConfiguration.chainId),
[MetaMetricsUserTrait.NetworksWithoutTicker]: Object.values(
metamaskState.networkConfigurationsByChainId,
metamaskState.NetworkController.networkConfigurationsByChainId,
)
.filter(({ nativeCurrency }) => !nativeCurrency)
.map(({ chainId }) => chainId),
[MetaMetricsUserTrait.NftAutodetectionEnabled]:
metamaskState.useNftDetection,
metamaskState.PreferencesController.useNftDetection,
[MetaMetricsUserTrait.NumberOfAccounts]: Object.values(
metamaskState.internalAccounts.accounts,
metamaskState.AccountsController.internalAccounts.accounts,
).length,
[MetaMetricsUserTrait.NumberOfNftCollections]:
this.#getAllUniqueNFTAddressesLength(metamaskState.allNfts),
this.#getAllUniqueNFTAddressesLength(
metamaskState.NftController.allNfts,
),
[MetaMetricsUserTrait.NumberOfNfts]: this.#getAllNFTsFlattened(
metamaskState.allNfts,
metamaskState.NftController.allNfts,
).length,
[MetaMetricsUserTrait.NumberOfTokens]: this.#getNumberOfTokens(
metamaskState.allTokens,
metamaskState.TokensController.allTokens,
),
[MetaMetricsUserTrait.OpenSeaApiEnabled]: metamaskState.openSeaEnabled,
[MetaMetricsUserTrait.OpenSeaApiEnabled]:
metamaskState.PreferencesController.openSeaEnabled,
[MetaMetricsUserTrait.ThreeBoxEnabled]: false, // deprecated, hard-coded as false
[MetaMetricsUserTrait.Theme]: metamaskState.theme || 'default',
[MetaMetricsUserTrait.Theme]:
metamaskState.PreferencesController.theme || 'default',
[MetaMetricsUserTrait.TokenDetectionEnabled]:
metamaskState.useTokenDetection,
metamaskState.PreferencesController.useTokenDetection,
[MetaMetricsUserTrait.ShowNativeTokenAsMainBalance]:
metamaskState.ShowNativeTokenAsMainBalance,
[MetaMetricsUserTrait.CurrentCurrency]: metamaskState.currentCurrency,
///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
[MetaMetricsUserTrait.MmiExtensionId]: this.#extension?.runtime?.id,
[MetaMetricsUserTrait.MmiAccountAddress]: mmiAccountAddress ?? null,
[MetaMetricsUserTrait.MmiIsCustodian]: Boolean(mmiAccountAddress),
///: END:ONLY_INCLUDE_IF
[MetaMetricsUserTrait.SecurityProviders]:
metamaskState.securityAlertsEnabled ? ['blockaid'] : [],
metamaskState.PreferencesController.preferences
?.showNativeTokenAsMainBalance,
[MetaMetricsUserTrait.CurrentCurrency]:
metamaskState.CurrencyController.currentCurrency,
[MetaMetricsUserTrait.SecurityProviders]: metamaskState
.PreferencesController.securityAlertsEnabled
? ['blockaid']
: [],
[MetaMetricsUserTrait.PetnameAddressCount]:
this.#getPetnameAddressCount(metamaskState),
[MetaMetricsUserTrait.IsMetricsOptedIn]:
metamaskState.participateInMetaMetrics,
metamaskState.MetaMetricsController.participateInMetaMetrics,
[MetaMetricsUserTrait.HasMarketingConsent]:
metamaskState.dataCollectionForMarketing,
metamaskState.MetaMetricsController.dataCollectionForMarketing,
[MetaMetricsUserTrait.TokenSortPreference]:
metamaskState.tokenSortConfig?.key || '',
metamaskState.PreferencesController.preferences?.tokenSortConfig?.key ||
'',
[MetaMetricsUserTrait.PrivacyModeEnabled]:
metamaskState.preferences.privacyMode,
metamaskState.PreferencesController.preferences?.privacyMode,
[MetaMetricsUserTrait.NetworkFilterPreference]: Object.keys(
metamaskState.preferences.tokenNetworkFilter || {},
metamaskState.PreferencesController.preferences?.tokenNetworkFilter ||
{},
),
};

Expand Down Expand Up @@ -1299,11 +1221,13 @@ export default class MetaMetricsController extends BaseController<
*
* @param allNfts
*/
#getAllNFTsFlattened = memoize((allNfts: MetaMaskState['allNfts'] = {}) => {
return Object.values(allNfts).reduce((result: Nft[], chainNFTs) => {
return result.concat(...Object.values(chainNFTs));
}, []);
});
#getAllNFTsFlattened = memoize(
(allNfts: BackgroundStateProxy['NftController']['allNfts'] = {}) => {
return Object.values(allNfts).reduce((result: Nft[], chainNFTs) => {
return result.concat(...Object.values(chainNFTs));
}, []);
},
);

/**
* Returns the number of unique NFT addresses the user
Expand All @@ -1312,7 +1236,7 @@ export default class MetaMetricsController extends BaseController<
* @param allNfts
*/
#getAllUniqueNFTAddressesLength(
allNfts: MetaMaskState['allNfts'] = {},
allNfts: BackgroundStateProxy['NftController']['allNfts'] = {},
): number {
const allNFTAddresses = this.#getAllNFTsFlattened(allNfts).map(
(nft) => nft.address,
Expand All @@ -1325,7 +1249,9 @@ export default class MetaMetricsController extends BaseController<
* @param allTokens
* @returns number of unique token addresses
*/
#getNumberOfTokens(allTokens: MetaMaskState['allTokens']): number {
#getNumberOfTokens(
allTokens: BackgroundStateProxy['TokensController']['allTokens'],
): number {
return Object.values(allTokens).reduce((result, accountsByChain) => {
return result + sum(Object.values(accountsByChain).map(size));
}, 0);
Expand Down Expand Up @@ -1549,8 +1475,9 @@ export default class MetaMetricsController extends BaseController<
*
* @param metamaskState
*/
#getPetnameAddressCount(metamaskState: MetaMaskState): number {
const addressNames = metamaskState.names?.[NameType.ETHEREUM_ADDRESS] ?? {};
#getPetnameAddressCount(metamaskState: BackgroundStateProxy): number {
const addressNames =
metamaskState.NameController.names?.[NameType.ETHEREUM_ADDRESS] ?? {};

return Object.keys(addressNames).reduce((totalCount, address) => {
const addressEntry = addressNames[address];
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3279,7 +3279,7 @@ export default class MetamaskController extends EventEmitter {

const updatePublicConfigStore = async (memState) => {
const networkStatus =
memState.networksMetadata[memState.selectedNetworkClientId]?.status;
memState.networksMetadata?.[memState.selectedNetworkClientId]?.status;
if (networkStatus === NetworkStatus.Available) {
publicConfigStore.putState(await selectPublicState(memState));
}
Expand Down
Loading

0 comments on commit 68b0a52

Please sign in to comment.