Skip to content

Commit ecf4d30

Browse files
committed
cleanup
1 parent 2614f64 commit ecf4d30

5 files changed

Lines changed: 47 additions & 102 deletions

File tree

ui/selectors/activity.test.ts

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AccountGroupId } from '@metamask/account-api';
22
import type { Transaction } from '@metamask/keyring-api';
33
import type { MultichainTransactionsControllerState } from '@metamask/multichain-transactions-controller';
4+
import { MultichainNetworks } from '../../shared/constants/multichain/networks';
45
import type { MetaMaskReduxState as _MetaMaskReduxState } from '../store/store';
56
import { generateTokenCacheKey } from '../helpers/utils/token-scan';
67
import type { AccountTreeState } from './multichain-accounts/account-tree.types';
@@ -18,8 +19,6 @@ jest.mock('./multichain-accounts/account-tree', () => {
1819
};
1920
});
2021

21-
const SOLANA_MAINNET = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp';
22-
2322
type NonEvmTransactionsMap =
2423
MultichainTransactionsControllerState['nonEvmTransactions'];
2524

@@ -46,7 +45,7 @@ function buildState(
4645
metamask: {
4746
nonEvmTransactions: nonEvmTransactions as NonEvmTransactionsMap,
4847
enabledNetworkMap: {
49-
solana: { [SOLANA_MAINNET]: true },
48+
solana: { [MultichainNetworks.SOLANA]: true },
5049
},
5150
tokenScanCache: tokenScanCache ?? {},
5251
},
@@ -57,25 +56,25 @@ describe('selectNonEvmTransactionsForActivity', () => {
5756
it('filters malicious non-EVM token transactions', () => {
5857
const maliciousTx = {
5958
id: 'bad-tx',
60-
chain: SOLANA_MAINNET,
59+
chain: MultichainNetworks.SOLANA,
6160
from: [
6261
{
6362
asset: {
6463
fungible: true,
65-
type: `${SOLANA_MAINNET}/token:BadMint111`,
64+
type: `${MultichainNetworks.SOLANA}/token:BadMint111`,
6665
},
6766
},
6867
],
6968
to: [],
7069
} as unknown as Transaction;
7170
const benignTx = {
7271
id: 'good-tx',
73-
chain: SOLANA_MAINNET,
72+
chain: MultichainNetworks.SOLANA,
7473
from: [
7574
{
7675
asset: {
7776
fungible: true,
78-
type: `${SOLANA_MAINNET}/token:GoodMint222`,
77+
type: `${MultichainNetworks.SOLANA}/token:GoodMint222`,
7978
},
8079
},
8180
],
@@ -85,15 +84,15 @@ describe('selectNonEvmTransactionsForActivity', () => {
8584
const state = buildState(
8685
{
8786
'acc-1': {
88-
[SOLANA_MAINNET]: {
87+
[MultichainNetworks.SOLANA]: {
8988
transactions: [maliciousTx, benignTx],
9089
next: null,
9190
lastUpdated: 0,
9291
},
9392
},
9493
},
9594
{
96-
[generateTokenCacheKey(SOLANA_MAINNET, 'BadMint111')]: {
95+
[generateTokenCacheKey(MultichainNetworks.SOLANA, 'BadMint111')]: {
9796
data: {
9897
// eslint-disable-next-line @typescript-eslint/naming-convention
9998
result_type: 'Malicious',
@@ -104,48 +103,4 @@ describe('selectNonEvmTransactionsForActivity', () => {
104103

105104
expect(selectNonEvmTransactionsForActivity(state)).toEqual([benignTx]);
106105
});
107-
108-
it('keeps native-only and uncached token transactions visible', () => {
109-
const nativeOnlyTx = {
110-
id: 'native-tx',
111-
chain: SOLANA_MAINNET,
112-
from: [
113-
{
114-
asset: {
115-
fungible: true,
116-
type: `${SOLANA_MAINNET}/slip44:501`,
117-
},
118-
},
119-
],
120-
to: [],
121-
} as unknown as Transaction;
122-
const uncachedTokenTx = {
123-
id: 'uncached-token-tx',
124-
chain: SOLANA_MAINNET,
125-
from: [
126-
{
127-
asset: {
128-
fungible: true,
129-
type: `${SOLANA_MAINNET}/token:UnknownMint333`,
130-
},
131-
},
132-
],
133-
to: [],
134-
} as unknown as Transaction;
135-
136-
const state = buildState({
137-
'acc-1': {
138-
[SOLANA_MAINNET]: {
139-
transactions: [nativeOnlyTx, uncachedTokenTx],
140-
next: null,
141-
lastUpdated: 0,
142-
},
143-
},
144-
});
145-
146-
expect(selectNonEvmTransactionsForActivity(state)).toEqual([
147-
nativeOnlyTx,
148-
uncachedTokenTx,
149-
]);
150-
});
151106
});

ui/selectors/activity.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import type { TransactionGroup } from '../../shared/lib/multichain/types';
1111
import { CHAIN_ID_TO_CURRENCY_SYMBOL_MAP } from '../../shared/constants/network';
1212
import { NATIVE_TOKEN_ADDRESS } from '../../shared/constants/transaction';
13+
import type { MetaMaskReduxState } from '../store/store';
1314
import {
1415
groupAndSortTransactionsByNonce,
1516
smartTransactionsListSelector,
@@ -19,10 +20,14 @@ import {
1920
selectOrderedTransactions,
2021
selectRequiredTransactionHashes,
2122
} from './transactionController';
22-
import { getTokenScanResultsForCacheKeys } from './token-trust-signals';
23-
import { getMarketData, getCurrencyRates } from './selectors';
23+
import type { TokenScanCacheResults } from './token-scan';
24+
import {
25+
getMarketData,
26+
getCurrencyRates,
27+
getTokenScanCache,
28+
} from './selectors';
2429
import { getSelectedInternalAccount } from './accounts';
25-
import { EMPTY_ARRAY } from './shared';
30+
import { EMPTY_ARRAY, EMPTY_OBJECT } from './shared';
2631

2732
function isFromSelectedAccount(tx: TransactionMeta, selectedAddress: string) {
2833
// Ported from selectedAddressTxListSelector
@@ -75,32 +80,32 @@ export const selectLocalTransactions = createSelector(
7580
);
7681

7782
export const selectNonEvmTransactionsForActivity = createSelector(
78-
[selectCurrentAccountNonEvmTransactions, (state) => state],
79-
(nonEvmTransactions, state) => {
80-
const tokenScanCacheKeys: MultichainTokenScanKey[] = [
83+
[
84+
selectCurrentAccountNonEvmTransactions,
85+
(state: MetaMaskReduxState) =>
86+
(getTokenScanCache(state) as TokenScanCacheResults | undefined) ??
87+
(EMPTY_OBJECT as TokenScanCacheResults),
88+
],
89+
(nonEvmTransactions, tokenScanCache) => {
90+
const tokenScanKeys: MultichainTokenScanKey[] = [
8191
...new Set(
8292
nonEvmTransactions.flatMap((transaction) =>
8393
collectTransactionTokenScanKeys(transaction),
8494
),
8595
),
8696
];
8797

88-
const tokenScanResults = getTokenScanResultsForCacheKeys(
89-
state,
90-
tokenScanCacheKeys,
91-
);
92-
9398
const maliciousTokenKeys = new Set<MultichainTokenScanKey>(
94-
Object.entries(tokenScanResults)
95-
.filter(
96-
([, tokenScanResult]) =>
97-
tokenScanResult.data?.result_type === ResultType.Malicious,
98-
)
99-
.map(
100-
([tokenScanCacheKey]) => tokenScanCacheKey as MultichainTokenScanKey,
101-
),
99+
tokenScanKeys.filter(
100+
(key) =>
101+
tokenScanCache[key]?.data?.result_type === ResultType.Malicious,
102+
),
102103
);
103104

105+
if (maliciousTokenKeys.size === 0) {
106+
return nonEvmTransactions;
107+
}
108+
104109
return filterMaliciousTransactions(nonEvmTransactions, maliciousTokenKeys);
105110
},
106111
);

ui/selectors/selectors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2960,7 +2960,7 @@ export function getUrlScanCacheResult(state, hostname) {
29602960
* @param {*} state
29612961
* @returns The token scan cache object
29622962
*/
2963-
function getTokenScanCache(state) {
2963+
export function getTokenScanCache(state) {
29642964
return state.metamask.tokenScanCache;
29652965
}
29662966

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getTokenScanResultsForCacheKeys } from './token-trust-signals';
1+
import { selectTokenScanResults } from './token-scan';
22

33
describe('token trust signal selectors', () => {
44
it('returns only entries matching the requested cache keys', () => {
@@ -22,7 +22,7 @@ describe('token trust signal selectors', () => {
2222
} as never;
2323

2424
expect(
25-
getTokenScanResultsForCacheKeys(state, [
25+
selectTokenScanResults(state, [
2626
'solana:mainnet:badmint111',
2727
'solana:mainnet:missing333',
2828
]),
@@ -50,7 +50,7 @@ describe('token trust signal selectors', () => {
5050
},
5151
} as never;
5252

53-
expect(getTokenScanResultsForCacheKeys(state, [])).toEqual({});
54-
expect(getTokenScanResultsForCacheKeys(state, undefined)).toEqual({});
53+
expect(selectTokenScanResults(state, [])).toEqual({});
54+
expect(selectTokenScanResults(state, undefined)).toEqual({});
5555
});
5656
});
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createDeepEqualSelector } from '../../shared/lib/selectors/selector-creators';
22
import type { MultichainTokenScanKey } from '../helpers/utils/token-scan';
33
import type { MetaMaskReduxState } from '../store/store';
4+
import { getTokenScanCache } from './selectors';
45
import { EMPTY_OBJECT } from './shared';
56

67
type TokenScanCacheResult = {
@@ -16,43 +17,27 @@ export type TokenScanCacheResults = Record<
1617
TokenScanCacheResult
1718
>;
1819

19-
type MetaMaskWithTokenScanCache = {
20-
tokenScanCache?: TokenScanCacheResults;
21-
};
22-
23-
const getTokenScanCache = (
24-
state: MetaMaskReduxState,
25-
): TokenScanCacheResults => {
26-
const metamaskState = state.metamask as unknown as
27-
| MetaMaskWithTokenScanCache
28-
| undefined;
29-
30-
return (
31-
metamaskState?.tokenScanCache ?? (EMPTY_OBJECT as TokenScanCacheResults)
32-
);
33-
};
34-
35-
export const getTokenScanResultsForCacheKeys = createDeepEqualSelector(
36-
getTokenScanCache,
20+
export const selectTokenScanResults = createDeepEqualSelector(
21+
(state: MetaMaskReduxState) =>
22+
(getTokenScanCache(state) as TokenScanCacheResults | undefined) ??
23+
(EMPTY_OBJECT as TokenScanCacheResults),
3724
(_state: MetaMaskReduxState, tokenScanCacheKeys?: MultichainTokenScanKey[]) =>
3825
tokenScanCacheKeys,
3926
(tokenScanCache, tokenScanCacheKeys): TokenScanCacheResults => {
4027
if (!tokenScanCacheKeys?.length) {
4128
return EMPTY_OBJECT as TokenScanCacheResults;
4229
}
4330

44-
const tokenScanResults: TokenScanCacheResults = {};
31+
const results: TokenScanCacheResults = {};
4532

46-
for (const tokenScanCacheKey of tokenScanCacheKeys) {
47-
const tokenScanResult = tokenScanCacheKey
48-
? tokenScanCache[tokenScanCacheKey]
49-
: undefined;
33+
for (const key of tokenScanCacheKeys) {
34+
const tokenScanResult = key ? tokenScanCache[key] : undefined;
5035

5136
if (tokenScanResult) {
52-
tokenScanResults[tokenScanCacheKey] = tokenScanResult;
37+
results[key] = tokenScanResult;
5338
}
5439
}
5540

56-
return tokenScanResults;
41+
return results;
5742
},
5843
);

0 commit comments

Comments
 (0)