Skip to content
Merged
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
51 changes: 37 additions & 14 deletions ui/pages/asset/components/asset-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PropTypes from 'prop-types';
import { useNavigate } from 'react-router-dom-v5-compat';
import { useSelector } from 'react-redux';
import { I18nContext } from '../../../contexts/i18n';
import { MetaMetricsContext } from '../../../contexts/metametrics';
import { Menu, MenuItem } from '../../../components/ui/menu';
import { getBlockExplorerLinkText } from '../../../selectors';
import { NETWORKS_ROUTE } from '../../../helpers/constants/routes';
Expand All @@ -13,15 +14,25 @@ import {
IconName,
} from '../../../components/component-library';
import { Color } from '../../../helpers/constants/design-system';
import {
MetaMetricsEventName,
MetaMetricsEventCategory,
MetaMetricsEventLocation,
} from '../../../../shared/constants/metametrics';
import {
AssetType,
TokenStandard,
} from '../../../../shared/constants/transaction';

const AssetOptions = ({
onRemove,
onClickBlockExplorer,
onViewTokenDetails,
tokenSymbol,
token,
isNativeAsset,
}) => {
const t = useContext(I18nContext);
const trackEvent = useContext(MetaMetricsContext);
const [assetOptionsOpen, setAssetOptionsOpen] = useState(false);
const navigate = useNavigate();
const blockExplorerLinkText = useSelector(getBlockExplorerLinkText);
Expand All @@ -36,6 +47,26 @@ const AssetOptions = ({
onClickBlockExplorer();
};

const handleRemoveToken = () => {
// Track the TokenHidden event before calling onRemove
trackEvent({
event: MetaMetricsEventName.TokenHidden,
category: MetaMetricsEventCategory.Wallet,
sensitiveProperties: {
token_symbol: token?.symbol,
token_contract_address: token?.address,
token_decimal_precision: token?.decimals,
location: MetaMetricsEventLocation.TokenDetails,
token_standard: TokenStandard.ERC20,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Hardcoded ERC20 token standard ignores ERC721 tokens

The token_standard in the TokenHidden event is hardcoded as TokenStandard.ERC20, but the token object can also represent ERC721 tokens (it has an isERC721 property). When users hide an ERC721 token, the event will incorrectly report it as ERC20, causing inaccurate analytics data. The token standard could be determined dynamically using token?.isERC721 since TokenStandard.ERC721 is available in the same imported enum.

Fix in Cursor Fix in Web

asset_type: AssetType.token,
chain_id: token?.chainId,
},
});

setAssetOptionsOpen(false);
onRemove();
};

return (
<div ref={ref}>
<ButtonIcon
Expand Down Expand Up @@ -72,12 +103,9 @@ const AssetOptions = ({
<MenuItem
iconName={IconName.Trash}
data-testid="asset-options__hide"
onClick={() => {
setAssetOptionsOpen(false);
onRemove();
}}
onClick={handleRemoveToken}
>
{t('hideTokenSymbol', [tokenSymbol])}
{t('hideTokenSymbol', [token?.symbol])}
</MenuItem>
)}
{isNativeAsset || !onViewTokenDetails ? null : (
Expand Down Expand Up @@ -113,14 +141,9 @@ AssetOptions.propTypes = {
}
},
onViewTokenDetails: PropTypes.func,
tokenSymbol: (props) => {
if (
props.isNativeAsset === false &&
typeof props.tokenSymbol !== 'string'
) {
throw new Error(
'When isNativeAsset is true, tokenSymbol is a required prop',
);
token: (props) => {
if (props.isNativeAsset === false && typeof props.token !== 'object') {
throw new Error('When isNativeAsset is false, token is a required prop');
}
},
};
Expand Down
2 changes: 1 addition & 1 deletion ui/pages/asset/components/token-asset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ const TokenAsset = ({ token, chainId }: { token: Token; chainId: Hex }) => {
});
global.platform.openTab({ url: blockExplorerLink });
}}
tokenSymbol={token.symbol}
token={token}
/>
}
/>
Expand Down
Loading