Skip to content

Commit

Permalink
move getSelectedInternalAccount from selectors.js to accounts.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmurdoch committed Oct 8, 2024
1 parent 699334b commit 23628c4
Show file tree
Hide file tree
Showing 18 changed files with 101 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe('SelectedAccount Component', () => {

const tooltipTitle = await waitFor(() =>
container.querySelector(
'[data-original-title="This account is not set up for use with goerli"]',
'[data-original-title="This account is not set up for use with Goerli"]',
),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ICustodianType } from '@metamask-institutional/types';
import { useI18nContext } from '../../../hooks/useI18nContext';
import { MetaMetricsContext } from '../../../contexts/metametrics';
import { hideModal } from '../../../store/actions';
import { getSelectedInternalAccount } from '../../../selectors/selectors';
import { getSelectedInternalAccount } from '../../../selectors/accounts';
import { toChecksumHexAddress } from '../../../../shared/modules/hexstring-utils';
import {
Box,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const InteractiveReplacementTokenNotification: React.FC<
interactiveReplacementToken &&
Boolean(Object.keys(interactiveReplacementToken).length);

// @ts-expect-error keyring type is wrong maybe?
if (!/^Custody/u.test(keyring.type) || !hasInteractiveReplacementToken) {
setShowNotification(false);
return;
Expand All @@ -66,6 +67,7 @@ const InteractiveReplacementTokenNotification: React.FC<
)) as unknown as string;
const custodyAccountDetails = await dispatch(
mmiActions.getAllCustodianAccountsWithToken(
// @ts-expect-error keyring type is wrong maybe?
keyring.type.split(' - ')[1],
token,
),
Expand Down Expand Up @@ -105,6 +107,7 @@ const InteractiveReplacementTokenNotification: React.FC<
interactiveReplacementToken?.oldRefreshToken,
isUnlocked,
dispatch,
// @ts-expect-error keyring type is wrong maybe?
keyring.type,
interactiveReplacementToken,
mmiActions,
Expand Down
4 changes: 2 additions & 2 deletions ui/ducks/metamask/metamask.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import {
checkNetworkAndAccountSupports1559,
getAddressBook,
getSelectedNetworkClientId,
getSelectedInternalAccount,
getNetworkConfigurationsByChainId,
} from '../../selectors';
} from '../../selectors/selectors';
import { getSelectedInternalAccount } from '../../selectors/accounts';
import * as actionConstants from '../../store/actionConstants';
import { updateTransactionGasFees } from '../../store/actions';
import { setCustomGasLimit, setCustomGasPrice } from '../gas/gas.duck';
Expand Down
1 change: 1 addition & 0 deletions ui/hooks/bridge/useBridging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const useBridging = () => {
const isMarketingEnabled = useSelector(getDataCollectionForMarketing);
const chainId = useSelector(getCurrentChainId);
const keyring = useSelector(getCurrentKeyring);
// @ts-expect-error keyring type is wrong maybe?
const usingHardwareWallet = isHardwareKeyring(keyring.type);

const isBridgeSupported = useSelector(getIsBridgeEnabled);
Expand Down
1 change: 1 addition & 0 deletions ui/hooks/useMultichainSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function useMultichainSelector<
) {
return useSelector((state: TState) => {
// We either pass an account or fallback to the currently selected one
// @ts-expect-error state types don't match
return selector(state, account || getSelectedInternalAccount(state));
});
}
1 change: 1 addition & 0 deletions ui/pages/asset/components/token-buttons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const TokenButtons = ({

///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta,build-flask)
const keyring = useSelector(getCurrentKeyring);
// @ts-expect-error keyring type is wrong maybe?
const usingHardwareWallet = isHardwareKeyring(keyring.type);
///: END:ONLY_INCLUDE_IF

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { useI18nContext } from '../../../hooks/useI18nContext';
import { useCopyToClipboard } from '../../../hooks/useCopyToClipboard';
import { getMetaMaskAccounts } from '../../../selectors';
import { getInstitutionalConnectRequests } from '../../../ducks/institutional/institutional';
import { getSelectedInternalAccount } from '../../../selectors/selectors';
import { getSelectedInternalAccount } from '../../../selectors/accounts';
import { toChecksumHexAddress } from '../../../../shared/modules/hexstring-utils';
import { SWAPS_CHAINID_DEFAULT_BLOCK_EXPLORER_URL_MAP } from '../../../../shared/constants/swaps';
import { CHAIN_IDS } from '../../../../shared/constants/network';
Expand Down
2 changes: 1 addition & 1 deletion ui/pages/settings/security-tab/security-tab.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
getIsSecurityAlertsEnabled,
getNetworkConfigurationsByChainId,
getPetnamesEnabled,
} from '../../../selectors';
} from '../../../selectors/selectors';
import { openBasicFunctionalityModal } from '../../../ducks/app/app';
import SecurityTab from './security-tab.component';

Expand Down
72 changes: 72 additions & 0 deletions ui/selectors/accounts.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { EthAccountType } from '@metamask/keyring-api';
import { ETH_EOA_METHODS } from '../../shared/constants/eth-methods';
import {
MOCK_ACCOUNTS,
MOCK_ACCOUNT_EOA,
MOCK_ACCOUNT_ERC4337,
MOCK_ACCOUNT_BIP122_P2WPKH,
MOCK_ACCOUNT_BIP122_P2WPKH_TESTNET,
} from '../../test/data/mock-accounts';
import mockState from '../../test/data/mock-state.json';
import {
AccountsState,
isSelectedInternalAccountEth,
isSelectedInternalAccountBtc,
hasCreatedBtcMainnetAccount,
hasCreatedBtcTestnetAccount,
getSelectedInternalAccount,
} from './accounts';

const MOCK_STATE: AccountsState = {
Expand All @@ -23,6 +27,74 @@ const MOCK_STATE: AccountsState = {
};

describe('Accounts Selectors', () => {
describe('#getSelectedInternalAccount', () => {
it('returns selected internalAccount', () => {
expect(
getSelectedInternalAccount(mockState as AccountsState),
).toStrictEqual({
address: '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc',
id: 'cf8dace4-9439-4bd4-b3a8-88c821c8fcb3',
metadata: {
name: 'Test Account',
keyring: {
type: 'HD Key Tree',
},
},
options: {},
methods: [
'personal_sign',
'eth_signTransaction',
'eth_signTypedData_v1',
'eth_signTypedData_v3',
'eth_signTypedData_v4',
],
type: 'eip155:eoa',
});
});

it('returns undefined if selectedAccount is undefined', () => {
expect(
getSelectedInternalAccount({
metamask: {
internalAccounts: {
accounts: {},
selectedAccount: '',
},
},
}),
).toBeUndefined();
});

it('returns selectedAccount', () => {
const mockInternalAccount = {
address: '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc',
id: 'cf8dace4-9439-4bd4-b3a8-88c821c8fcb3',
metadata: {
importTime: 0,
name: 'Test Account',
keyring: {
type: 'HD Key Tree',
},
},
options: {},
methods: ETH_EOA_METHODS,
type: EthAccountType.Eoa,
};
expect(
getSelectedInternalAccount({
metamask: {
internalAccounts: {
accounts: {
[mockInternalAccount.id]: mockInternalAccount,
},
selectedAccount: mockInternalAccount.id,
},
},
}),
).toStrictEqual(mockInternalAccount);
});
});

describe('isSelectedInternalAccountEth', () => {
// @ts-expect-error This is missing from the Mocha type definitions
it.each([
Expand Down
7 changes: 6 additions & 1 deletion ui/selectors/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
isBtcMainnetAddress,
isBtcTestnetAddress,
} from '../../shared/lib/multichain';
import { getSelectedInternalAccount, getInternalAccounts } from './selectors';
import { getInternalAccounts } from './selectors';

export type AccountsState = {
metamask: AccountsControllerState;
Expand All @@ -20,6 +20,11 @@ function isBtcAccount(account: InternalAccount) {
return Boolean(account && account.type === P2wpkh);
}

export function getSelectedInternalAccount(state: AccountsState) {
const accountId = state.metamask.internalAccounts.selectedAccount;
return state.metamask.internalAccounts.accounts[accountId];
}

export function isSelectedInternalAccountEth(state: AccountsState) {
const account = getSelectedInternalAccount(state);
const { Eoa, Erc4337 } = EthAccountType;
Expand Down
1 change: 1 addition & 0 deletions ui/selectors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './permissions';
export * from './selectors';
export * from './transactions';
export * from './approvals';
export * from './accounts';
5 changes: 4 additions & 1 deletion ui/selectors/institutional/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { toChecksumAddress } from 'ethereumjs-util';
import { getAccountType, getSelectedInternalAccount } from '../selectors';
import { getAccountType } from '../selectors';
import { getSelectedInternalAccount } from '../accounts';
import { getProviderConfig } from '../../ducks/metamask/metamask';
import { hexToDecimal } from '../../../shared/modules/conversion.utils';
// TODO: Remove restricted import
Expand Down Expand Up @@ -166,6 +167,7 @@ export function getCustodianIconForAddress(state: State, address: string) {

export function getIsCustodianSupportedChain(state: State) {
try {
// @ts-expect-error state types don't match
const selectedAccount = getSelectedInternalAccount(state);
const accountType = getAccountType(state);
const providerConfig = getProviderConfig(state);
Expand Down Expand Up @@ -207,6 +209,7 @@ export function getIsCustodianSupportedChain(state: State) {

export function getMMIAddressFromModalOrAddress(state: State) {
const modalAddress = state?.appState?.modal?.modalState?.props?.address;
// @ts-expect-error state types don't match
const selectedAddress = getSelectedInternalAccount(state)?.address;

return modalAddress || selectedAddress;
Expand Down
3 changes: 1 addition & 2 deletions ui/selectors/multichain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
CHAIN_ID_TO_NETWORK_IMAGE_URL_MAP,
TEST_NETWORK_IDS,
} from '../../shared/constants/network';
import { AccountsState } from './accounts';
import { AccountsState, getSelectedInternalAccount } from './accounts';
import {
getCurrentChainId,
getCurrentCurrency,
Expand All @@ -33,7 +33,6 @@ import {
getNativeCurrencyImage,
getNetworkConfigurationsByChainId,
getSelectedAccountCachedBalance,
getSelectedInternalAccount,
getShouldShowFiat,
getShowFiatInTestnets,
} from './selectors';
Expand Down
2 changes: 1 addition & 1 deletion ui/selectors/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {
getInternalAccount,
getMetaMaskAccountsOrdered,
getOriginOfCurrentTab,
getSelectedInternalAccount,
getTargetSubjectMetadata,
} from './selectors';
import { getSelectedInternalAccount } from './accounts';

// selectors

Expand Down
6 changes: 1 addition & 5 deletions ui/selectors/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ import {
getOrderedConnectedAccountsForConnectedDapp,
getSubjectMetadata,
} from './permissions';
import { getSelectedInternalAccount } from './accounts';
import { createDeepEqualSelector } from './util';
import { getMultichainBalances, getMultichainNetwork } from './multichain';

Expand Down Expand Up @@ -357,11 +358,6 @@ export function getMaybeSelectedInternalAccount(state) {
: undefined;
}

export function getSelectedInternalAccount(state) {
const accountId = state.metamask.internalAccounts.selectedAccount;
return state.metamask.internalAccounts.accounts[accountId];
}

export function checkIfMethodIsEnabled(state, methodName) {
const internalAccount = getSelectedInternalAccount(state);
return Boolean(internalAccount.methods.includes(methodName));
Expand Down
68 changes: 1 addition & 67 deletions ui/selectors/selectors.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { deepClone } from '@metamask/snaps-utils';
import { ApprovalType } from '@metamask/controller-utils';
import { EthAccountType, EthMethod } from '@metamask/keyring-api';
import { EthMethod } from '@metamask/keyring-api';
import { TransactionStatus } from '@metamask/transaction-controller';
import mockState from '../../test/data/mock-state.json';
import { KeyringType } from '../../shared/constants/keyring';
import { CHAIN_IDS, NETWORK_TYPES } from '../../shared/constants/network';
import { SURVEY_DATE, SURVEY_GMT } from '../helpers/constants/survey';
import { PRIVACY_POLICY_DATE } from '../helpers/constants/privacy-policy';
import { createMockInternalAccount } from '../../test/jest/mocks';
import { ETH_EOA_METHODS } from '../../shared/constants/eth-methods';
import { getProviderConfig } from '../ducks/metamask/metamask';
import { mockNetworkState } from '../../test/stub/networks';
import * as selectors from './selectors';
Expand Down Expand Up @@ -60,49 +59,6 @@ describe('Selectors', () => {
});
});

describe('#getSelectedInternalAccount', () => {
it('returns undefined if selectedAccount is undefined', () => {
expect(
selectors.getSelectedInternalAccount({
metamask: {
internalAccounts: {
accounts: {},
selectedAccount: '',
},
},
}),
).toBeUndefined();
});

it('returns selectedAccount', () => {
const mockInternalAccount = {
address: '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc',
id: 'cf8dace4-9439-4bd4-b3a8-88c821c8fcb3',
metadata: {
name: 'Test Account',
keyring: {
type: 'HD Key Tree',
},
},
options: {},
methods: ETH_EOA_METHODS,
type: EthAccountType.Eoa,
};
expect(
selectors.getSelectedInternalAccount({
metamask: {
internalAccounts: {
accounts: {
[mockInternalAccount.id]: mockInternalAccount,
},
selectedAccount: mockInternalAccount.id,
},
},
}),
).toStrictEqual(mockInternalAccount);
});
});

describe('#checkIfMethodIsEnabled', () => {
it('returns true if the method is enabled', () => {
expect(
Expand Down Expand Up @@ -955,28 +911,6 @@ describe('Selectors', () => {
});
});

it('returns selected internalAccount', () => {
expect(selectors.getSelectedInternalAccount(mockState)).toStrictEqual({
address: '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc',
id: 'cf8dace4-9439-4bd4-b3a8-88c821c8fcb3',
metadata: {
name: 'Test Account',
keyring: {
type: 'HD Key Tree',
},
},
options: {},
methods: [
'personal_sign',
'eth_signTransaction',
'eth_signTypedData_v1',
'eth_signTypedData_v3',
'eth_signTypedData_v4',
],
type: 'eip155:eoa',
});
});

it('returns selected account', () => {
const account = selectors.getSelectedAccount(mockState);
expect(account.balance).toStrictEqual('0x346ba7725f412cbfdb');
Expand Down
3 changes: 2 additions & 1 deletion ui/selectors/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import txHelper from '../helpers/utils/tx-helper';
import { SmartTransactionStatus } from '../../shared/constants/transaction';
import { hexToDecimal } from '../../shared/modules/conversion.utils';
import { getProviderConfig } from '../ducks/metamask/metamask';
import { getCurrentChainId, getSelectedInternalAccount } from './selectors';
import { getCurrentChainId } from './selectors';
import { getSelectedInternalAccount } from './accounts';
import { hasPendingApprovals, getApprovalRequestsByType } from './approvals';
import {
createDeepEqualSelector,
Expand Down

0 comments on commit 23628c4

Please sign in to comment.