Skip to content

Commit

Permalink
refactor: remove metametrics circular dependency (#30037)
Browse files Browse the repository at this point in the history
This PR solves an issue with circular dependencies. It does not change
the behavior of the application. It only rearranges functions/files to
avoid circular references.

<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.


## **Description**


[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/30037?quickstart=1)

## **Related issues**

Fixes:

## **Manual testing steps**

1. Go to this page...
2.
3.

## **Screenshots/Recordings**

### **Before**

### **After**

## **Pre-merge author checklist**

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
-->
  • Loading branch information
davidmurdoch authored Feb 12, 2025
1 parent 042d7cb commit 103d92c
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import {
handleTransactionFailed,
handleTransactionRejected,
handleTransactionSubmitted,
TransactionMetricsRequest,
} from '../../lib/transaction/metrics';
import {
ControllerInitFunction,
Expand All @@ -45,6 +44,7 @@ import {
} from '../types';
import { TransactionControllerInitMessenger } from '../messengers/transaction-controller-messenger';
import { ControllerFlatState } from '../controller-list';
import { TransactionMetricsRequest } from '../../../../shared/types/metametrics';

export const TransactionControllerInit: ControllerInitFunction<
TransactionController,
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/controller-init/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { Hex } from '@metamask/utils';
import { Duplex } from 'readable-stream';
import { SubjectType } from '@metamask/permission-controller';
import { TransactionMetricsRequest } from '../lib/transaction/metrics';
import type { TransactionMetricsRequest } from '../../../shared/types/metametrics';
import { MessageSender } from '../../../types/global';
import { Controller, ControllerFlatState } from './controller-list';

Expand Down
2 changes: 1 addition & 1 deletion app/scripts/lib/transaction/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
BlockaidResultType,
} from '../../../../shared/constants/security-provider';
import { decimalToHex } from '../../../../shared/modules/conversion.utils';
import { TransactionMetricsRequest } from '../../../../shared/types/metametrics';
import {
handleTransactionAdded,
handleTransactionApproved,
Expand All @@ -35,7 +36,6 @@ import {
handleTransactionRejected,
handleTransactionSubmitted,
METRICS_STATUS_FAILED,
TransactionMetricsRequest,
} from './metrics';

const providerResultStub = {
Expand Down
81 changes: 6 additions & 75 deletions app/scripts/lib/transaction/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import type { Provider } from '@metamask/network-controller';
import { FetchGasFeeEstimateOptions } from '@metamask/gas-fee-controller';
import { BigNumber } from 'bignumber.js';
import { isHexString } from 'ethereumjs-util';

import { SmartTransaction } from '@metamask/smart-transactions-controller/dist/types';
import {
TransactionMeta,
TransactionType,
Expand All @@ -15,12 +11,9 @@ import {
} from '../../../../shared/constants/gas';
import {
MetaMetricsEventCategory,
MetaMetricsEventFragment,
MetaMetricsEventName,
MetaMetricsEventUiCustomization,
MetaMetricsEventTransactionEstimateType,
MetaMetricsPageObject,
MetaMetricsReferrerObject,
} from '../../../../shared/constants/metametrics';
import {
TokenStandard,
Expand Down Expand Up @@ -48,79 +41,17 @@ import {
// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
} from '../../../../ui/helpers/utils/metrics';
import type {
TransactionEventPayload,
TransactionMetaEventPayload,
TransactionMetricsRequest,
} from '../../../../shared/types/metametrics';

import {
getSnapAndHardwareInfoForMetrics,
type SnapAndHardwareMessenger,
} from '../snap-keyring/metrics';
import { getSnapAndHardwareInfoForMetrics } from '../snap-keyring/metrics';
import { shouldUseRedesignForTransactions } from '../../../../shared/lib/confirmation.utils';
import { HardwareKeyringType } from '../../../../shared/constants/hardware-wallets';

export type TransactionMetricsRequest = {
createEventFragment: (
options: Omit<MetaMetricsEventFragment, 'id'>,
) => MetaMetricsEventFragment;
finalizeEventFragment: (
fragmentId: string,
options?: {
abandoned?: boolean;
page?: MetaMetricsPageObject;
referrer?: MetaMetricsReferrerObject;
},
) => void;
getEventFragmentById: (fragmentId: string) => MetaMetricsEventFragment;
updateEventFragment: (
fragmentId: string,
payload: Partial<MetaMetricsEventFragment>,
) => void;
getAccountType: (
address: string,
) => Promise<'hardware' | 'imported' | 'MetaMask'>;
getDeviceModel: (
address: string,
) => Promise<'ledger' | 'lattice' | 'N/A' | string>;
getHardwareTypeForMetric: (address: string) => Promise<HardwareKeyringType>;
// According to the type GasFeeState returned from getEIP1559GasFeeEstimates
// doesn't include some properties used in buildEventFragmentProperties,
// hence returning any here to avoid type errors.
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getEIP1559GasFeeEstimates(options?: FetchGasFeeEstimateOptions): Promise<any>;
getParticipateInMetrics: () => boolean;
getSelectedAddress: () => string;
getTokenStandardAndDetails: () => Promise<{
decimals?: string;
balance?: string;
symbol?: string;
standard?: TokenStandard;
}>;
getTransaction: (transactionId: string) => TransactionMeta;
provider: Provider;
snapAndHardwareMessenger: SnapAndHardwareMessenger;
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
trackEvent: (payload: any) => void;
getIsSmartTransaction: () => boolean;
getSmartTransactionByMinedTxHash: (
txhash: string | undefined,
) => SmartTransaction;
getMethodData: (data: string) => Promise<{ name: string }>;
getIsConfirmationAdvancedDetailsOpen: () => boolean;
};

export const METRICS_STATUS_FAILED = 'failed on-chain';

export type TransactionEventPayload = {
transactionMeta: TransactionMeta;
actionId?: string;
error?: string;
};

export type TransactionMetaEventPayload = TransactionMeta & {
actionId?: string;
error?: string;
};

/**
* This function is called when a transaction is added to the controller.
*
Expand Down
4 changes: 0 additions & 4 deletions development/circular-deps.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
"app/scripts/controllers/bridge-status/utils.ts",
"app/scripts/controllers/bridge-status/validators.ts"
],
[
"app/scripts/lib/transaction/metrics.ts",
"shared/modules/metametrics.ts"
],
[
"ui/components/app/alert-system/confirm-alert-modal/confirm-alert-modal.tsx",
"ui/components/app/alert-system/confirm-alert-modal/index.tsx",
Expand Down
4 changes: 1 addition & 3 deletions shared/modules/metametrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import {
TransactionType,
} from '@metamask/transaction-controller';
import { createTestProviderTools } from '../../test/stub/provider';
// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
import { TransactionMetricsRequest } from '../../app/scripts/lib/transaction/metrics';
import { CHAIN_IDS } from '../constants/network';
import { TransactionMetricsRequest } from '../types/metametrics';
import { getSmartTransactionMetricsProperties } from './metametrics';

const txHash =
Expand Down
4 changes: 1 addition & 3 deletions shared/modules/metametrics.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { TransactionMeta } from '@metamask/transaction-controller';
// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
import { TransactionMetricsRequest } from '../../app/scripts/lib/transaction/metrics';
import { TransactionMetricsRequest } from '../types/metametrics';

type SmartTransactionMetricsProperties = {
is_smart_transaction: boolean;
Expand Down
77 changes: 77 additions & 0 deletions shared/types/metametrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import type { Provider } from '@metamask/network-controller';
import type { FetchGasFeeEstimateOptions } from '@metamask/gas-fee-controller';
import type { SmartTransaction } from '@metamask/smart-transactions-controller/dist/types';
import type { TransactionMeta } from '@metamask/transaction-controller';
import type {
MetaMetricsEventFragment,
MetaMetricsPageObject,
MetaMetricsReferrerObject,
} from '../constants/metametrics';
import type { TokenStandard } from '../constants/transaction';
import type { HardwareKeyringType } from '../constants/hardware-wallets';
// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
import type { SnapAndHardwareMessenger } from '../../app/scripts/lib/snap-keyring/metrics';

export type TransactionMetricsRequest = {
createEventFragment: (
options: Omit<MetaMetricsEventFragment, 'id'>,
) => MetaMetricsEventFragment;
finalizeEventFragment: (
fragmentId: string,
options?: {
abandoned?: boolean;
page?: MetaMetricsPageObject;
referrer?: MetaMetricsReferrerObject;
},
) => void;
getEventFragmentById: (fragmentId: string) => MetaMetricsEventFragment;
updateEventFragment: (
fragmentId: string,
payload: Partial<MetaMetricsEventFragment>,
) => void;
getAccountType: (
address: string,
) => Promise<'hardware' | 'imported' | 'MetaMask'>;
getDeviceModel: (
address: string,
) => Promise<'ledger' | 'lattice' | 'N/A' | string>;
getHardwareTypeForMetric: (address: string) => Promise<HardwareKeyringType>;
// According to the type GasFeeState returned from getEIP1559GasFeeEstimates
// doesn't include some properties used in buildEventFragmentProperties,
// hence returning any here to avoid type errors.
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getEIP1559GasFeeEstimates(options?: FetchGasFeeEstimateOptions): Promise<any>;
getParticipateInMetrics: () => boolean;
getSelectedAddress: () => string;
getTokenStandardAndDetails: () => Promise<{
decimals?: string;
balance?: string;
symbol?: string;
standard?: TokenStandard;
}>;
getTransaction: (transactionId: string) => TransactionMeta;
provider: Provider;
snapAndHardwareMessenger: SnapAndHardwareMessenger;
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
trackEvent: (payload: any) => void;
getIsSmartTransaction: () => boolean;
getSmartTransactionByMinedTxHash: (
txhash: string | undefined,
) => SmartTransaction;
getMethodData: (data: string) => Promise<{ name: string }>;
getIsConfirmationAdvancedDetailsOpen: () => boolean;
};

export type TransactionEventPayload = {
transactionMeta: TransactionMeta;
actionId?: string;
error?: string;
};

export type TransactionMetaEventPayload = TransactionMeta & {
actionId?: string;
error?: string;
};

0 comments on commit 103d92c

Please sign in to comment.