Skip to content

Commit

Permalink
Add metrics for blockaid settings and user metrics for security alerts.
Browse files Browse the repository at this point in the history
Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com>

lint fixes

Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com>

use preferences controller

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Remove array props

Use CSV instead of list

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

use the list without spreading.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

use strings to store security providers key

lint fix.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Add metrics for blockaid provider calls count.

track all methods
tests for blockaid utils

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>
  • Loading branch information
segun committed Oct 11, 2023
1 parent bab39f1 commit 85aa789
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/components/UI/BlockaidBanner/BlockaidBanner.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface SecurityAlertResponse {
reason: Reason;
features: string[];
resultType: ResultType;
providerRequestsCount: Record<string, number>;
}

type BlockaidBannerAllProps = BannerAlertProps & {
Expand Down
4 changes: 3 additions & 1 deletion app/components/UI/TransactionReview/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ describe('TransactionReview', () => {
const securityAlertResponse = {
resultType: 'Malicious',
reason: 'blur_farming',
providerRequestsCount: {},
};
const trackEventSypy = jest
.spyOn(analyticsV2, 'trackEvent')
Expand All @@ -140,9 +141,10 @@ describe('TransactionReview', () => {

const blockaidMetricsParamsSpy = jest
.spyOn(BlockaidUtils, 'getBlockaidMetricsParams')
.mockImplementation(({ resultType, reason }) => ({
.mockImplementation(({ resultType, reason, providerRequestsCount }) => ({
security_alert_response: resultType,
security_alert_reason: reason,
security_alert_provider_requests_count: providerRequestsCount,
}));
const { queryByText, queryByTestId, getByText } = renderWithProvider(
<TransactionReview
Expand Down
34 changes: 34 additions & 0 deletions app/util/blockaid/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { getBlockaidMetricsParams } from '.';
import {
Reason,
ResultType,
SecurityAlertResponse,
} from '../../components/UI/BlockaidBanner/BlockaidBanner.types';

describe('getBlockaidMetricsParams', () => {
it('should return empty object when securityAlertResponse is not defined', () => {
const result = getBlockaidMetricsParams(undefined);
expect(result).toStrictEqual({});
});

it('should return additionalParams object when securityAlertResponse defined', () => {
const securityAlertResponse: SecurityAlertResponse = {
resultType: ResultType.Malicious,
reason: Reason.notApplicable,
providerRequestsCount: {
eth_call: 5,
eth_getCode: 3,
},
features: [],
};

const result = getBlockaidMetricsParams(securityAlertResponse);
expect(result).toEqual({
ui_customizations: ['flagged_as_malicious'],
security_alert_response: ResultType.Malicious,
security_alert_reason: Reason.notApplicable,
ppom_eth_call_count: 5,
ppom_eth_getCode_count: 3,
});
});
});
17 changes: 11 additions & 6 deletions app/util/blockaid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,17 @@ export const isBlockaidFeatureEnabled = () =>
process.env.MM_BLOCKAID_UI_ENABLED === 'true';

export const getBlockaidMetricsParams = (
securityAlertResponse: SecurityAlertResponse,
securityAlertResponse?: SecurityAlertResponse,
) => {
const additionalParams: Record<string, any> = {};

if (securityAlertResponse && isBlockaidFeatureEnabled()) {
const { resultType, reason } = securityAlertResponse;
let uiCustomizations;
const { resultType, reason, providerRequestsCount } = securityAlertResponse;

if (resultType === ResultType.Malicious) {
uiCustomizations = ['flagged_as_malicious'];
additionalParams.ui_customizations = ['flagged_as_malicious'];
}

additionalParams.ui_customizations = uiCustomizations;

if (resultType !== ResultType.Benign) {
additionalParams.security_alert_reason = Reason.notApplicable;

Expand All @@ -31,6 +28,14 @@ export const getBlockaidMetricsParams = (
additionalParams.security_alert_reason = reason;
}
}

// add counts of each RPC call
if (providerRequestsCount) {
Object.keys(providerRequestsCount).forEach((key: string) => {
const metricKey = `ppom_${key}_count`;
additionalParams[metricKey] = providerRequestsCount[key];
});
}
}

return additionalParams;
Expand Down

0 comments on commit 85aa789

Please sign in to comment.