Skip to content
Open
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
15 changes: 9 additions & 6 deletions ui/pages/confirmations/hooks/send/useContactRecipients.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { isAddress as isEvmAddress } from 'ethers/lib/utils';
import { renderHookWithProvider } from '../../../../../test/lib/render-helpers';
import mockState from '../../../../../test/data/mock-state.json';
import * as selectors from '../../../../selectors';
import * as SendContext from '../../context/send';
import { useContactRecipients } from './useContactRecipients';
import * as useSendTypeModule from './useSendType';
import { useSendType } from './useSendType';
Expand Down Expand Up @@ -47,7 +48,10 @@ describe('useContactRecipients', () => {
mockGetCompleteAddressBook.mockReturnValue(mockAddressBookEntries);
});

it('returns EVM contacts when isEvmSendType is true', () => {
it('returns EVM contacts filtered by chainId when isEvmSendType is true', () => {
jest.spyOn(SendContext, 'useSendContext').mockReturnValue({
chainId: '0x1',
} as unknown as SendContext.SendContextType);
mockUseSendType.mockReturnValue({
isEvmSendType: true,
isSolanaSendType: false,
Expand All @@ -64,11 +68,7 @@ describe('useContactRecipients', () => {
address: '0x1234567890abcdef1234567890abcdef12345678',
contactName: 'John Doe',
isContact: true,
},
{
address: '0xabcdef1234567890abcdef1234567890abcdef12',
contactName: 'Bob Wilson',
isContact: true,
seedIcon: undefined,
},
]);
});
Expand Down Expand Up @@ -104,6 +104,9 @@ describe('useContactRecipients', () => {
});

it('filters out non-EVM addresses when isEvmSendType is true', () => {
jest.spyOn(SendContext, 'useSendContext').mockReturnValue({
chainId: '0x1',
} as unknown as SendContext.SendContextType);
mockUseSendType.mockReturnValue({
isEvmSendType: true,
isSolanaSendType: false,
Expand Down
42 changes: 21 additions & 21 deletions ui/pages/confirmations/hooks/send/useContactRecipients.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { useCallback } from 'react';
import { useMemo } from 'react';
import { useSelector } from 'react-redux';
import { isAddress as isEvmAddress } from 'ethers/lib/utils';
import { AddressBookEntry } from '@metamask/address-book-controller';

import { getCompleteAddressBook } from '../../../../selectors';
import { useSendContext } from '../../context/send';
import { type Recipient } from './useRecipients';
import { useSendType } from './useSendType';
import { useAccountAddressSeedIconMap } from './useAccountAddressSeedIconMap';
Expand All @@ -12,25 +11,26 @@ export const useContactRecipients = (): Recipient[] => {
const { isEvmSendType } = useSendType();
const addressBook = useSelector(getCompleteAddressBook);
const { accountAddressSeedIconMap } = useAccountAddressSeedIconMap();
const { chainId } = useSendContext();

const processContacts = useCallback(
(contact: AddressBookEntry) => {
return {
address: contact.address,
contactName: contact.name,
isContact: true,
seedIcon: accountAddressSeedIconMap.get(contact.address.toLowerCase()),
};
},
[accountAddressSeedIconMap],
);
const contacts = useMemo(() => {
if (!isEvmSendType) {
return [];
}

// Contacts are only supported for EVM chains today - hence we only return contacts for EVM chains
if (isEvmSendType) {
return addressBook
.filter((contact) => isEvmAddress(contact.address))
.map(processContacts);
}
return (
addressBook
?.filter((contact) => contact.chainId === chainId)
?.map((contact) => ({
address: contact.address,
contactName: contact.name,
isContact: true,
seedIcon: accountAddressSeedIconMap.get(
contact.address.toLowerCase(),
),
})) ?? []
);
}, [addressBook, chainId, isEvmSendType]);

return [];
return contacts;
};
Loading