Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove metametrics circular dependency #30037

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};
Loading