Skip to content
Merged
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
13 changes: 5 additions & 8 deletions app/scripts/constants/sentry-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,14 +399,11 @@ export const SENTRY_BACKGROUND_STATE = {
},
};

const flattenedBackgroundStateMask = Object.values(
SENTRY_BACKGROUND_STATE,
).reduce((partialBackgroundState, controllerState: object) => {
return {
...partialBackgroundState,
...controllerState,
};
}, {});
const flattenedBackgroundStateMask: Record<string, unknown> = {};

for (const controllerState of Object.values(SENTRY_BACKGROUND_STATE)) {
Object.assign(flattenedBackgroundStateMask, controllerState);
}

// This describes the subset of Redux state attached to errors sent to Sentry
// These properties have some potential to be useful for debugging, and they do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ export const NetworkEnablementControllerInit: ControllerInitFunction<
);
///: END:ONLY_INCLUDE_IF

const allEnabledNetworks = Object.values(
controller.state.enabledNetworkMap,
).reduce((acc, curr) => {
return { ...acc, ...curr };
}, {});
const allEnabledNetworks = {};

for (const network of Object.values(controller.state.enabledNetworkMap)) {
Object.assign(allEnabledNetworks, network);
}

if (Object.keys(allEnabledNetworks).length === 1) {
const chainId = Object.keys(allEnabledNetworks)[0];
Expand Down
30 changes: 12 additions & 18 deletions app/scripts/controllers/metametrics-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1471,27 +1471,21 @@ export default class MetaMetricsController extends BaseController<
#buildValidTraits(
userTraits: Partial<MetaMetricsUserTraits>,
): MetaMetricsUserTraits {
return Object.entries(userTraits).reduce(
(validTraits: MetaMetricsUserTraits, [key, value]) => {
if (this.#isValidTraitDate(value)) {
return {
...validTraits,
[key]: value.toISOString(),
};
} else if (this.#isValidTrait(value)) {
return {
...validTraits,
[key]: value,
};
}

const validTraits: Record<string, string> = {};

for (const [key, value] of Object.entries(userTraits)) {
if (this.#isValidTraitDate(value)) {
validTraits[key] = value.toISOString();
} else if (this.#isValidTrait(value)) {
(validTraits as Record<string, typeof value>)[key] = value;
} else {
console.warn(
`MetaMetricsController: "${key}" value is not a valid trait type`,
);
return validTraits;
},
{},
);
}
}

return validTraits;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/lib/ComposableObservableStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ export default class ComposableObservableStore extends ObservableStore {
if (!this.config) {
return {};
}
let flatState = {};
const flatState = {};
for (const key of Object.keys(this.config)) {
const controller = this.config[key];
const state = controller.getState
? controller.getState()
: controller.state;
flatState = { ...flatState, ...state };
Object.assign(flatState, state);
}
return flatState;
}
Expand Down
28 changes: 13 additions & 15 deletions shared/constants/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,19 @@ export const LISTED_CONTRACT_ADDRESSES = Object.keys(contractMap).map(
* @property {boolean} [isERC721] - True when the asset is a ERC721 token.
*/

export const STATIC_MAINNET_TOKEN_LIST = Object.keys(contractMap).reduce(
(acc, base) => {
const { logo, ...tokenMetadata } = contractMap[base];
return {
...acc,
[base.toLowerCase()]: {
...tokenMetadata,
address: base.toLowerCase(),
iconUrl: `images/contract/${logo}`,
aggregators: [],
},
};
},
{},
);
const STATIC_MAINNET_TOKEN_LIST = {};

for (const base of Object.keys(contractMap)) {
const { logo, ...tokenMetadata } = contractMap[base];
STATIC_MAINNET_TOKEN_LIST[base.toLowerCase()] = {
...tokenMetadata,
address: base.toLowerCase(),
iconUrl: `images/contract/${logo}`,
aggregators: [],
};
}

export { STATIC_MAINNET_TOKEN_LIST };

export const TOKEN_API_METASWAP_CODEFI_URL =
'https://token.api.cx.metamask.io/tokens/';
Expand Down
37 changes: 17 additions & 20 deletions shared/modules/selectors/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,18 @@ export const getNetworkConfigurationsByChainId = (
export const selectDefaultNetworkClientIdsByChainId = createSelector(
getNetworkConfigurationsByChainId,
(networkConfigurationsByChainId) => {
return Object.entries(networkConfigurationsByChainId).reduce<
Record<Hex, NetworkClientId>
>(
(obj, [chainId, networkConfiguration]) => ({
...obj,
[chainId]:
networkConfiguration.rpcEndpoints[
networkConfiguration.defaultRpcEndpointIndex
].networkClientId,
}),
{},
);
const clientIdsByChain: Record<Hex, NetworkClientId> = {};

for (const [chainId, networkConfiguration] of Object.entries(
networkConfigurationsByChainId,
)) {
clientIdsByChain[chainId as Hex] =
networkConfiguration.rpcEndpoints[
networkConfiguration.defaultRpcEndpointIndex
].networkClientId;
}

return clientIdsByChain;
},
);

Expand Down Expand Up @@ -366,21 +366,18 @@ export const getNetworksByScopes = createSelector(
name: network.name,
}));

return [...result, ...evmNetworks];
return result.concat(evmNetworks);
}

const matchingNetwork = nonTestNetworks.find(
(network) => network.caipChainId === scope,
);

if (matchingNetwork) {
return [
...result,
{
chainId: matchingNetwork.chainId,
name: matchingNetwork.name,
},
];
return result.concat({
chainId: matchingNetwork.chainId,
name: matchingNetwork.name,
});
}

return result;
Expand Down
22 changes: 10 additions & 12 deletions test/e2e/fixture-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,18 +586,16 @@ class FixtureBuilder {
withPermissionControllerConnectedToMultichainTestDappWithTwoAccounts({
scopes = ['eip155:1337'],
}) {
const optionalScopes = scopes
.map((scope) => ({
[scope]: {
accounts: [
`${scope}:0x5cfe73b6021e818b776b421b1c4db2474086a7e1`,
`${scope}:0x09781764c08de8ca82e156bbf156a3ca217c7950`,
],
},
}))
.reduce((acc, curr) => {
return { ...acc, ...curr };
}, {});
const optionalScopes = {};

for (const scope of scopes) {
optionalScopes[scope] = {
accounts: [
`${scope}:0x5cfe73b6021e818b776b421b1c4db2474086a7e1`,
`${scope}:0x09781764c08de8ca82e156bbf156a3ca217c7950`,
],
};
}

const subjects = {
[DAPP_URL]: {
Expand Down
39 changes: 18 additions & 21 deletions ui/components/app/contact-list/contact-list.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,24 @@ export default class ContactList extends PureComponent {
internalAccounts,
);

const unsortedContactsByLetter = searchForContacts().reduce(
(obj, contact) => {
const firstLetter = contact.name[0].toUpperCase();

const isDuplicate =
(duplicateContactMap.get(contact.name.trim().toLowerCase()) ?? [])
.length > 1;

return {
...obj,
[firstLetter]: [
...(obj[firstLetter] || []),
{
...contact,
isDuplicate,
},
],
};
},
{},
);
const unsortedContactsByLetter = {};

for (const contact of searchForContacts()) {
const firstLetter = contact.name[0].toUpperCase();

const isDuplicate =
(duplicateContactMap.get(contact.name.trim().toLowerCase()) ?? [])
.length > 1;

if (!unsortedContactsByLetter[firstLetter]) {
unsortedContactsByLetter[firstLetter] = [];
}

unsortedContactsByLetter[firstLetter].push({
...contact,
isDuplicate,
});
}

const letters = Object.keys(unsortedContactsByLetter).sort();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,32 +124,35 @@ export const AssetPickerModalNetwork = ({
// Initialized with the selectedChainIds if provided
const [checkedChainIds, setCheckedChainIds] = useState<
Record<string, boolean>
>(
networksList?.reduce(
(acc, { chainId }) => ({
...acc,
[chainId]: selectedChainIds
? selectedChainIds.includes(chainId)
: false,
}),
{},
) ?? {},
);
>(() => {
if (!networksList) {
return {};
}

const initialState: Record<string, boolean> = {};

for (const { chainId } of networksList) {
initialState[chainId] = selectedChainIds
? selectedChainIds.includes(chainId)
: false;
}

return initialState;
});

// Reset checkedChainIds if selectedChainIds change in parent component
useEffect(() => {
networksList &&
setCheckedChainIds(
networksList.reduce(
(acc, { chainId }) => ({
...acc,
[chainId]: selectedChainIds
? selectedChainIds.includes(chainId)
: false,
}),
{},
),
);
if (networksList) {
const updatedState: Record<string, boolean> = {};

for (const { chainId } of networksList) {
updatedState[chainId] = selectedChainIds
? selectedChainIds.includes(chainId)
: false;
}

setCheckedChainIds(updatedState);
}
}, [networksList, selectedChainIds]);

const handleToggleNetwork = useCallback((chainId: string) => {
Expand All @@ -161,15 +164,14 @@ export const AssetPickerModalNetwork = ({

// Toggles all networks to be checked or unchecked
const handleToggleAllNetworks = useCallback(() => {
setCheckedChainIds(
Object.keys(checkedChainIds)?.reduce(
(agg, chainId) => ({
...agg,
[chainId]: !Object.values(checkedChainIds).every((v) => v),
}),
{},
),
);
const toggledState: Record<string, boolean> = {};
const allChecked = Object.values(checkedChainIds).every((v) => v);

for (const chainId of Object.keys(checkedChainIds)) {
toggledState[chainId] = !allChecked;
}

setCheckedChainIds(toggledState);
}, [checkedChainIds]);

return (
Expand Down
27 changes: 15 additions & 12 deletions ui/selectors/multichain/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,21 @@ export const getMultichainNetworkConfigurationsByChainId =
// There's a fallback for EVM network names/nicknames, in case the network
// does not have a name/nickname the fallback is the first rpc endpoint url.
// TODO: Update toMultichainNetworkConfigurationsByChainId to handle this case.
const evmNetworks = Object.entries(networkConfigurationsByChainId).reduce(
(acc, [, network]) => ({
...acc,
[toEvmCaipChainId(network.chainId)]: {
...toMultichainNetworkConfiguration(network),
name:
network.name ||
network.rpcEndpoints[network.defaultRpcEndpointIndex].url,
},
}),
{},
);
const evmNetworks: Record<
CaipChainId,
InternalMultichainNetworkConfiguration
> = {};

for (const [, network] of Object.entries(
networkConfigurationsByChainId,
)) {
evmNetworks[toEvmCaipChainId(network.chainId)] = {
...toMultichainNetworkConfiguration(network),
name:
network.name ||
network.rpcEndpoints[network.defaultRpcEndpointIndex].url,
};
}

const networks = {
///: BEGIN:ONLY_INCLUDE_IF(multichain)
Expand Down
6 changes: 2 additions & 4 deletions ui/selectors/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,10 +497,8 @@ export const getMetaMaskAccounts = createDeepEqualSelector(
};
}

return {
...accounts,
[internalAccount.address]: account,
};
accounts[internalAccount.address] = account;
return accounts;
}, {}),
);
/**
Expand Down
Loading