Skip to content

Commit

Permalink
fix: Fall back to token list for the token symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronfigueiredo committed Oct 22, 2024
1 parent f995e3c commit 37a22d4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
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;

// 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 ||
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

0 comments on commit 37a22d4

Please sign in to comment.