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 Sep 26, 2024
1 parent 4d314b4 commit cba16ef
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 81 deletions.
7 changes: 7 additions & 0 deletions test/data/mock-state.json
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@
"address": "0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc",
"id": "cf8dace4-9439-4bd4-b3a8-88c821c8fcb3",
"metadata": {
"importTime": 0,
"name": "Test Account",
"keyring": {
"type": "HD Key Tree"
Expand All @@ -434,6 +435,7 @@
"address": "0xec1adf982415d2ef5ec55899b9bfb8bc0f29251b",
"id": "07c2cfec-36c9-46c4-8115-3836d3ac9047",
"metadata": {
"importTime": 0,
"name": "Test Account 2",
"keyring": {
"type": "HD Key Tree"
Expand All @@ -453,6 +455,7 @@
"address": "0xc42edfcc21ed14dda456aa0756c153f7985d8813",
"id": "15e69915-2a1a-4019-93b3-916e11fd432f",
"metadata": {
"importTime": 0,
"name": "Ledger Hardware 2",
"keyring": {
"type": "Ledger Hardware"
Expand All @@ -472,6 +475,7 @@
"address": "0xeb9e64b93097bc15f01f13eae97015c57ab64823",
"id": "784225f4-d30b-4e77-a900-c8bbce735b88",
"metadata": {
"importTime": 0,
"name": "Test Account 3",
"keyring": {
"type": "HD Key Tree"
Expand All @@ -491,6 +495,7 @@
"address": "0xca8f1F0245530118D0cf14a06b01Daf8f76Cf281",
"id": "694225f4-d30b-4e77-a900-c8bbce735b42",
"metadata": {
"importTime": 0,
"name": "Test Account 4",
"keyring": {
"type": "Custody test"
Expand All @@ -510,11 +515,13 @@
"address": "0xb552685e3d2790efd64a175b00d51f02cdafee5d",
"id": "c3deeb99-ba0d-4a4e-a0aa-033fc1f79ae3",
"metadata": {
"importTime": 0,
"name": "Snap Account 1",
"keyring": {
"type": "Snap Keyring"
},
"snap": {
"enabled": true,
"id": "snap-id",
"name": "snap-name"
}
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
2 changes: 1 addition & 1 deletion 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/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
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
Loading

0 comments on commit cba16ef

Please sign in to comment.