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: Fall back to token list for the token symbol #28003

Merged
merged 1 commit into from
Oct 24, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { TransactionMeta } from '@metamask/transaction-controller';
import { genUnapprovedTokenTransferConfirmation } from '../../../../../../../test/data/confirmations/token-transfer';
import mockState from '../../../../../../../test/data/mock-state.json';
import { renderHookWithProvider } from '../../../../../../../test/lib/render-helpers';
import { useTokenImage } from './use-token-image';
import { useTokenDetails } from './useTokenDetails';

describe('useTokenImage', () => {
describe('useTokenDetails', () => {
it('returns iconUrl from selected token if it exists', () => {
const transactionMeta = genUnapprovedTokenTransferConfirmation(
{},
Expand All @@ -19,11 +19,14 @@ describe('useTokenImage', () => {
};

const { result } = renderHookWithProvider(
() => useTokenImage(transactionMeta, TEST_SELECTED_TOKEN),
() => useTokenDetails(transactionMeta, TEST_SELECTED_TOKEN),
mockState,
);

expect(result.current).toEqual({ tokenImage: 'iconUrl' });
expect(result.current).toEqual({
tokenImage: 'iconUrl',
tokenSymbol: 'symbol',
});
});

it('returns selected token image if no iconUrl is included', () => {
Expand All @@ -39,11 +42,14 @@ describe('useTokenImage', () => {
};

const { result } = renderHookWithProvider(
() => useTokenImage(transactionMeta, TEST_SELECTED_TOKEN),
() => useTokenDetails(transactionMeta, TEST_SELECTED_TOKEN),
mockState,
);

expect(result.current).toEqual({ tokenImage: 'image' });
expect(result.current).toEqual({
tokenImage: 'image',
tokenSymbol: 'symbol',
});
});

it('returns token list icon url if no image is included in the token', () => {
Expand All @@ -58,7 +64,7 @@ describe('useTokenImage', () => {
};

const { result } = renderHookWithProvider(
() => useTokenImage(transactionMeta, TEST_SELECTED_TOKEN),
() => useTokenDetails(transactionMeta, TEST_SELECTED_TOKEN),
{
...mockState,
metamask: {
Expand All @@ -72,7 +78,10 @@ describe('useTokenImage', () => {
},
);

expect(result.current).toEqual({ tokenImage: 'tokenListIconUrl' });
expect(result.current).toEqual({
tokenImage: 'tokenListIconUrl',
tokenSymbol: 'symbol',
});
});

it('returns undefined if no image is found', () => {
Expand All @@ -87,10 +96,13 @@ describe('useTokenImage', () => {
};

const { result } = renderHookWithProvider(
() => useTokenImage(transactionMeta, TEST_SELECTED_TOKEN),
() => useTokenDetails(transactionMeta, TEST_SELECTED_TOKEN),
mockState,
);

expect(result.current).toEqual({ tokenImage: undefined });
expect(result.current).toEqual({
tokenImage: undefined,
tokenSymbol: 'symbol',
});
});
});
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import { TokenListMap } from '@metamask/assets-controllers';
import { TransactionMeta } from '@metamask/transaction-controller';
import { useSelector } from 'react-redux';
import { useI18nContext } from '../../../../../../hooks/useI18nContext';
import { getTokenList } from '../../../../../../selectors';
import { SelectedToken } from '../shared/selected-token';

export const useTokenImage = (
export const useTokenDetails = (
transactionMeta: TransactionMeta,
selectedToken: SelectedToken,
) => {
const t = useI18nContext();

const tokenList = useSelector(getTokenList) as TokenListMap;
Copy link
Member

Choose a reason for hiding this comment

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

One for future note but getTokenList is derived from the global network where as I just added a selector to use tokensChainsCache which is multi-chain compatible as it's a map of data per chain.


// TODO: Add support for NFT images in one of the following tasks
const tokenImage =
selectedToken?.iconUrl ||
selectedToken?.image ||
tokenList[transactionMeta?.txParams?.to as string]?.iconUrl;

return { tokenImage };
const tokenSymbol =
selectedToken?.symbol ||
tokenList[transactionMeta?.txParams?.to as string]?.symbol ||
Copy link
Member

Choose a reason for hiding this comment

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

Minor, worth extracting the to into a contractAddress variable?

t('unknown');

return { tokenImage, tokenSymbol };
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@ import {
TextColor,
TextVariant,
} from '../../../../../../../helpers/constants/design-system';
import { useI18nContext } from '../../../../../../../hooks/useI18nContext';
import { getWatchedToken } from '../../../../../../../selectors';
import { MultichainState } from '../../../../../../../selectors/multichain';
import { useConfirmContext } from '../../../../../context/confirm';
import { useTokenImage } from '../../hooks/use-token-image';
import { useTokenDetails } from '../../hooks/useTokenDetails';
import { useTokenValues } from '../../hooks/use-token-values';
import { ConfirmLoader } from '../confirm-loader/confirm-loader';

const SendHeading = () => {
const t = useI18nContext();
const { currentConfirmation: transactionMeta } =
useConfirmContext<TransactionMeta>();
const selectedToken = useSelector((state: MultichainState) =>
getWatchedToken(transactionMeta)(state),
);
const { tokenImage } = useTokenImage(transactionMeta, selectedToken);
const { tokenImage, tokenSymbol } = useTokenDetails(
transactionMeta,
selectedToken,
);
const { decodedTransferValue, fiatDisplayValue, pending } =
useTokenValues(transactionMeta);

Expand All @@ -57,9 +58,7 @@ const SendHeading = () => {
variant={TextVariant.headingLg}
color={TextColor.inherit}
marginTop={3}
>{`${decodedTransferValue || ''} ${
selectedToken?.symbol || t('unknown')
}`}</Text>
>{`${decodedTransferValue || ''} ${tokenSymbol}`}</Text>
{fiatDisplayValue && (
<Text variant={TextVariant.bodyMd} color={TextColor.textAlternative}>
{fiatDisplayValue}
Expand Down