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

fix: refactor blockaid metrics sync functions to async #10518

Merged
merged 37 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
c752ab6
feat: add supported chains utils
vinistevam Jul 9, 2024
fec9d5c
Merge branch 'main' into feat/add-utility-function-supported-chains
vinistevam Jul 10, 2024
a5129ef
fix unit test
vinistevam Jul 10, 2024
ad145f4
refactor request function
vinistevam Jul 11, 2024
88c4c71
Merge branch 'main' into feat/add-utility-function-supported-chains
vinistevam Jul 11, 2024
ee0185f
add missing options
vinistevam Jul 11, 2024
b5b8db1
not export isChainSupported
vinistevam Jul 16, 2024
aeea378
Merge branch 'main' into feat/add-utility-function-supported-chains
vinistevam Jul 19, 2024
9352e4d
fix conflict
vinistevam Jul 19, 2024
2a87725
Merge branch 'main' into feat/add-utility-function-supported-chains
vinistevam Jul 22, 2024
5235069
Merge branch 'main' into feat/add-utility-function-supported-chains
vinistevam Jul 30, 2024
8bbffeb
Merge branch 'main' into feat/add-utility-function-supported-chains
vinistevam Jul 31, 2024
3dcb75e
Merge branch 'main' into feat/add-utility-function-supported-chains
vinistevam Aug 1, 2024
a8fec3f
refactor blockaid metrics to async
vinistevam Aug 1, 2024
0cf3b46
Merge branch 'main' into feat/add-utility-function-supported-chains
vinistevam Aug 1, 2024
ea1f753
Merge branch 'feat/add-utility-function-supported-chains' into fix/re…
vinistevam Aug 1, 2024
d84dccb
Merge branch 'main' into fix/refacor-blockaid-metrics-to-async
vinistevam Aug 1, 2024
0d268d4
fix: unit tests
vinistevam Aug 2, 2024
c29298c
Merge branch 'main' into fix/refacor-blockaid-metrics-to-async
vinistevam Aug 2, 2024
a561c43
Merge branch 'main' into fix/refacor-blockaid-metrics-to-async
vinistevam Aug 12, 2024
6e420ab
fix snapshots
vinistevam Aug 12, 2024
53e710f
clean up
vinistevam Aug 13, 2024
0afdd1c
Merge branch 'main' into fix/refacor-blockaid-metrics-to-async
vinistevam Aug 13, 2024
d927345
clean up
vinistevam Aug 13, 2024
bcdf808
Merge branch 'main' into fix/refacor-blockaid-metrics-to-async
vinistevam Aug 19, 2024
d3a2f3f
Merge branch 'main' into fix/refacor-blockaid-metrics-to-async
vinistevam Aug 20, 2024
64837b2
reduce changes
vinistevam Aug 20, 2024
d24ee84
clean up
vinistevam Aug 20, 2024
7e1bfa1
Merge branch 'main' into fix/refacor-blockaid-metrics-to-async
vinistevam Aug 22, 2024
f19c87b
fix: merge conflicts
vinistevam Aug 22, 2024
63c22cd
Merge branch 'main' into fix/refacor-blockaid-metrics-to-async
vinistevam Aug 23, 2024
fee520b
Merge branch 'main' into fix/refacor-blockaid-metrics-to-async
vinistevam Aug 26, 2024
7f8d052
Merge branch 'main' into fix/refacor-blockaid-metrics-to-async
vinistevam Aug 27, 2024
8ee73b8
apply suggestions
vinistevam Sep 6, 2024
cb5c855
Merge branch 'main' into fix/refacor-blockaid-metrics-to-async
vinistevam Sep 6, 2024
bf51625
fix lint
vinistevam Sep 6, 2024
5bd1bec
Merge branch 'main' into fix/refacor-blockaid-metrics-to-async
vinistevam Sep 6, 2024
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
4 changes: 2 additions & 2 deletions app/components/UI/Navbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,10 @@ export function getSendFlowTitle(
elevation: 0,
},
});
const rightAction = () => {
const rightAction = async () => {
const providerType = route?.params?.providerType ?? '';
const additionalTransactionMetricsParams =
getBlockaidTransactionMetricsParams(transaction);
await getBlockaidTransactionMetricsParams(transaction);
trackEvent(MetaMetricsEvents.SEND_FLOW_CANCEL, {
view: title.split('.')[1],
network: providerType,
Expand Down
4 changes: 4 additions & 0 deletions app/components/Views/Notifications/Details/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const mockInitialState = {
},
};

jest.mock('../../../../lib/ppom/ppom-util', () => ({
isChainSupported: jest.fn().mockResolvedValue(true),
}));

jest.mock('../../../../actions/alert', () => ({
showAlert: jest.fn(),
}));
Expand Down
25 changes: 5 additions & 20 deletions app/components/Views/confirmations/Approval/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import { providerErrors } from '@metamask/rpc-errors';
import { getDeviceId } from '../../../../core/Ledger/Ledger';
import { selectShouldUseSmartTransaction } from '../../../../selectors/smartTransactionsController';
import ExtendedKeyringTypes from '../../../../constants/keyringTypes';
import { getBlockaidMetricsParams } from '../../../../util/blockaid';
import { getBlockaidTransactionMetricsParams } from '../../../../util/blockaid';
import { getDecimalChainId } from '../../../../util/networks';

import { updateTransaction } from '../../../../util/transaction-controller';
Expand Down Expand Up @@ -305,22 +305,6 @@ class Approval extends PureComponent {
};
};

getBlockaidMetricsParams = () => {
const { transaction } = this.props;

let blockaidParams = {};

if (
transaction.id === transaction.currentTransactionSecurityAlertResponse?.id
) {
blockaidParams = getBlockaidMetricsParams(
transaction.currentTransactionSecurityAlertResponse?.response,
);
}

return blockaidParams;
};

getAnalyticsParams = ({ gasEstimateType, gasSelected } = {}) => {
try {
const {
Expand Down Expand Up @@ -388,15 +372,16 @@ class Approval extends PureComponent {
});
};

onCancel = () => {
onCancel = async () => {
const { transaction } = this.props;
this.props.hideModal();
this.state.mode === REVIEW && this.trackOnCancel();
this.showWalletConnectNotification();
this.props.metrics.trackEvent(
MetaMetricsEvents.DAPP_TRANSACTION_CANCELLED,
{
...this.getAnalyticsParams(),
...this.getBlockaidMetricsParams(),
...(await getBlockaidTransactionMetricsParams(transaction)),
...this.getTransactionMetrics(),
},
);
Expand Down Expand Up @@ -553,7 +538,7 @@ class Approval extends PureComponent {
gasEstimateType,
gasSelected,
}),
...this.getBlockaidMetricsParams(),
...(await getBlockaidTransactionMetricsParams(transaction)),
...this.getTransactionMetrics(),
},
);
Expand Down
4 changes: 2 additions & 2 deletions app/components/Views/confirmations/Send/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ class Send extends PureComponent {
*
* @return {object} - Object containing view, network type, activeCurrency and assetType
*/
getTrackingParams = () => {
getTrackingParams = async () => {
const {
networkType,
transaction,
Expand All @@ -714,7 +714,7 @@ class Send extends PureComponent {
(selectedAsset.symbol || selectedAsset.contractName)) ||
'ETH',
assetType,
...getBlockaidTransactionMetricsParams(transaction),
...(await getBlockaidTransactionMetricsParams(transaction)),
is_smart_transaction: shouldUseSmartTransaction,
};
};
Expand Down
18 changes: 11 additions & 7 deletions app/components/Views/confirmations/SendFlow/Confirm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,10 @@ class Confirm extends PureComponent {

this.setState({ result, transactionMeta });

if (isBlockaidFeatureEnabled()) {
const isBlockaidEnabled = await isBlockaidFeatureEnabled();
this.setState({ isBlockaidFeatureEnabled: isBlockaidEnabled });

if (isBlockaidEnabled) {
vinistevam marked this conversation as resolved.
Show resolved Hide resolved
// start validate ppom
const id = transactionMeta.id;
const reqObject = {
Expand Down Expand Up @@ -899,7 +902,7 @@ class Confirm extends PureComponent {
assetType,
{
...this.getAnalyticsParams(),
...getBlockaidTransactionMetricsParams(transaction),
...(await getBlockaidTransactionMetricsParams(transaction)),
...this.getTransactionMetrics(),
},
),
Expand Down Expand Up @@ -928,7 +931,7 @@ class Confirm extends PureComponent {
throw transactionMeta.error;
}

InteractionManager.runAfterInteractions(() => {
InteractionManager.runAfterInteractions(async () => {
NotificationManager.watchSubmittedTransaction({
...transactionMeta,
assetType,
Expand All @@ -938,7 +941,7 @@ class Confirm extends PureComponent {
MetaMetricsEvents.SEND_TRANSACTION_COMPLETED,
{
...this.getAnalyticsParams(transactionMeta),
...getBlockaidTransactionMetricsParams(transaction),
...(await getBlockaidTransactionMetricsParams(transaction)),
...this.getTransactionMetrics(),
},
);
Expand Down Expand Up @@ -1158,11 +1161,11 @@ class Confirm extends PureComponent {
});
};

onContactUsClicked = () => {
onContactUsClicked = async () => {
const { transaction } = this.props;
const analyticsParams = {
...this.getAnalyticsParams(),
...getBlockaidTransactionMetricsParams(transaction),
...(await getBlockaidTransactionMetricsParams(transaction)),
external_link_clicked: 'security_alert_support_link',
};
this.props.metrics.trackEvent(
Expand Down Expand Up @@ -1259,6 +1262,7 @@ class Confirm extends PureComponent {
EIP1559GasObject,
EIP1559GasTransaction,
legacyGasObject,
isBlockaidFeatureEnabled,
} = this.state;
const colors = this.context.colors || mockTheme.colors;
const styles = createStyles(colors);
Expand Down Expand Up @@ -1292,7 +1296,7 @@ class Confirm extends PureComponent {
layout="vertical"
/>
<ScrollView style={baseStyles.flexGrow} ref={this.setScrollViewRef}>
{isBlockaidFeatureEnabled() && this.state.transactionMeta?.id && (
vinistevam marked this conversation as resolved.
Show resolved Hide resolved
{isBlockaidFeatureEnabled && this.state.transactionMeta?.id && (
<TransactionBlockaidBanner
transactionId={this.state.transactionMeta.id}
style={styles.blockaidBanner}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ jest.mock('../../../../../util/ENSUtils', () => ({
jest.mock('../../../../../lib/ppom/ppom-util', () => ({
...jest.requireActual('../../../../../lib/ppom/ppom-util'),
validateRequest: jest.fn(),
isChainSupported: jest.fn(),
}));

jest.mock('../../../../../core/Engine', () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
import Icon from '../../../../../component-library/components/Icons/Icon/Icon';
import Text from '../../../../../component-library/components/Texts/Text/Text';
import { useStyles } from '../../../../../component-library/hooks/useStyles';
import { isBlockaidFeatureEnabled } from '../../../../../util/blockaid';
import {
FALSE_POSITIVE_REPOST_LINE_TEST_ID,
REASON_DESCRIPTION_I18N_KEY_MAP,
Expand Down Expand Up @@ -109,7 +108,7 @@ const BlockaidBanner = (bannerProps: BlockaidBannerProps) => {
})();
}, [securityAlertResponse]);

if (!securityAlertResponse || !isBlockaidFeatureEnabled()) {
if (!securityAlertResponse) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface SecurityAlertResponse {
type BlockaidBannerAllProps = BannerAlertProps & {
securityAlertResponse?: SecurityAlertResponse;
onToggleShowDetails?: () => void;
onContactUsClicked?: () => void;
onContactUsClicked?: () => Promise<void>;
};

export type BlockaidBannerProps = Omit<BlockaidBannerAllProps, 'severity'>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@ const BlockaidBannerLink = ({
}: {
text: string;
link: string;
onContactUsClicked?: () => void | undefined;
onContactUsClicked?: () => Promise<void>;
}) => {
const { colors } = useTheme();
const styles = createStyles(colors);

const handlePress = async () => {
if (onContactUsClicked) {
await onContactUsClicked();
}
Linking.openURL(link);
};

return (
<Text
suppressHighlighting
style={styles.attributionLink}
variant={DEFAULT_BANNERBASE_DESCRIPTION_TEXTVARIANT}
onPress={() => {
onContactUsClicked?.();
Linking.openURL(link);
}}
onPress={handlePress}
>
{text}
</Text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,27 @@ const MessageSign = ({
const styles = createStyles(colors);

useEffect(() => {
trackEvent(
MetaMetricsEvents.SIGNATURE_REQUESTED,
getAnalyticsParams(messageParams, 'eth_sign'),
);
const trackSignatureRequested = async () => {
const analyticsParams = await getAnalyticsParams(
messageParams,
'eth_sign',
);
trackEvent(MetaMetricsEvents.SIGNATURE_REQUESTED, analyticsParams);
};

trackSignatureRequested();

// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const onSignatureError = ({ error }: any) => {
const onSignatureError = async ({ error }: any) => {
if (error?.message.startsWith(KEYSTONE_TX_CANCELED)) {
const analyticsParams = await getAnalyticsParams(
messageParams,
'eth_sign',
);
trackEvent(
MetaMetricsEvents.QR_HARDWARE_TRANSACTION_CANCELED,
getAnalyticsParams(messageParams, 'eth_sign'),
analyticsParams,
);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const PersonalSign = ({
[key: string]: string | undefined;
}

const getAnalyticsParams = useCallback((): AnalyticsParams => {
const getAnalyticsParams = useCallback(async (): Promise<AnalyticsParams> => {
try {
const chainId = selectChainId(store.getState());
const pageInfo = currentPageInformation || messageParams.meta;
Expand All @@ -91,7 +91,7 @@ const PersonalSign = ({
let blockaidParams = {};

if (securityAlertResponse) {
blockaidParams = getBlockaidMetricsParams(
blockaidParams = await getBlockaidMetricsParams(
securityAlertResponse as SecurityAlertResponse,
);
}
Expand All @@ -110,11 +110,11 @@ const PersonalSign = ({
}, [currentPageInformation, messageParams, securityAlertResponse]);

useEffect(() => {
const onSignatureError = ({ error }: { error: Error }) => {
const onSignatureError = async ({ error }: { error: Error }) => {
if (error?.message.startsWith(KEYSTONE_TX_CANCELED)) {
trackEvent(
MetaMetricsEvents.QR_HARDWARE_TRANSACTION_CANCELED,
getAnalyticsParams(),
await getAnalyticsParams(),
);
}
};
Expand Down Expand Up @@ -151,14 +151,20 @@ const PersonalSign = ({
const rejectSignature = async () => {
await onReject();
showWalletConnectNotification(false);
trackEvent(MetaMetricsEvents.SIGNATURE_REJECTED, getAnalyticsParams());
trackEvent(
MetaMetricsEvents.SIGNATURE_REJECTED,
await getAnalyticsParams(),
);
};

const confirmSignature = async () => {
if (!isExternalHardwareAccount(messageParams.from)) {
await onConfirm();
showWalletConnectNotification(true);
trackEvent(MetaMetricsEvents.SIGNATURE_APPROVED, getAnalyticsParams());
trackEvent(
MetaMetricsEvents.SIGNATURE_APPROVED,
await getAnalyticsParams(),
);
} else {
navigation.navigate(
...(await createExternalSignModelNav(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ jest.mock('../../../../../core/NotificationManager', () => ({
showSimpleNotification: jest.fn(),
}));

jest.mock('../../../../../lib/ppom/ppom-util', () => ({
...jest.requireActual('../../../../../lib/ppom/ppom-util'),
isChainSupported: jest.fn().mockResolvedValue(true),
}));

jest.mock('@react-navigation/native');

const messageParamsMock = {
Expand Down Expand Up @@ -202,6 +207,7 @@ describe('PersonalSign', () => {
});
});
});

describe('trackEvent', () => {
it('tracks event for rejected requests', async () => {
const wrapper = createWrapper().dive();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,15 @@ class SignatureRequest extends PureComponent {
);
};

onContactUsClicked = () => {
onContactUsClicked = async () => {
const { fromAddress, type } = this.props;
const analyticsParams = {
...getAnalyticsParams(
...(await getAnalyticsParams(
{
from: fromAddress,
},
type,
),
)),
external_link_clicked: 'security_alert_support_link',
};
this.props.metrics.trackEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,12 @@ class TransactionReview extends PureComponent {
}
};

onContactUsClicked = () => {
onContactUsClicked = async () => {
const { transaction, metrics } = this.props;
const additionalParams = {
...getBlockaidMetricsParams(
...(await getBlockaidMetricsParams(
transaction?.currentTransactionSecurityAlertResponse,
),
)),
external_link_clicked: 'security_alert_support_link',
};
metrics.trackEvent(
Expand Down
Loading
Loading