Skip to content

Commit

Permalink
Unflatten selectors and fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
MajorLift committed Dec 10, 2024
1 parent 3f8df5c commit 63d8618
Show file tree
Hide file tree
Showing 28 changed files with 1,730 additions and 997 deletions.
8 changes: 4 additions & 4 deletions app/scripts/controllers/account-tracker-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
NetworkControllerGetStateAction,
Provider,
} from '@metamask/network-controller';
import { hasProperty, Hex } from '@metamask/utils';
import { getKnownPropertyNames, hasProperty, Hex } from '@metamask/utils';
import {
BaseController,
ControllerGetStateAction,
Expand Down Expand Up @@ -66,7 +66,7 @@ type Account = {
export type AccountTrackerControllerState = {
accounts: Record<string, Account | Record<string, never>>;
currentBlockGasLimit: string;
accountsByChainId: Record<string, AccountTrackerControllerState['accounts']>;
accountsByChainId: Record<Hex, AccountTrackerControllerState['accounts']>;
currentBlockGasLimitByChainId: Record<Hex, string>;
};

Expand Down Expand Up @@ -524,7 +524,7 @@ export default class AccountTrackerController extends BaseController<
addresses.forEach((address) => {
accounts[address] = {};
});
Object.keys(accountsByChainId).forEach((chainId) => {
getKnownPropertyNames(accountsByChainId).forEach((chainId) => {
addresses.forEach((address) => {
accountsByChainId[chainId][address] = {};
});
Expand Down Expand Up @@ -562,7 +562,7 @@ export default class AccountTrackerController extends BaseController<
addresses.forEach((address) => {
delete accounts[address];
});
Object.keys(accountsByChainId).forEach((chainId) => {
getKnownPropertyNames(accountsByChainId).forEach((chainId) => {
addresses.forEach((address) => {
delete accountsByChainId[chainId][address];
});
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/controllers/app-state-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export type AppStateControllerState = {
// prior to Migration 92.3 where we split out the setting to support
// multiple networks.
hadAdvancedGasFeesSetPriorToMigration92_3: boolean;
qrHardware: Json;
qrHardware: { sign?: { request?: Json } } | Json;
nftsDropdownState: Json;
surveyLinkLastClickedOrClosed: number | null;
signatureSecurityAlertResponses: Record<string, SecurityAlertResponse>;
Expand Down
6 changes: 5 additions & 1 deletion app/scripts/controllers/swaps/swaps.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
MetaMetricsEventName,
} from '../../../../shared/constants/metametrics';
import { fetchTradesInfo as defaultFetchTradesInfo } from '../../../../shared/lib/swaps-utils';
import type { FeatureFlags } from '../../lib/transaction/smart-transactions';
import { controllerName } from './swaps.constants';
import SwapsController from '.';

Expand Down Expand Up @@ -50,7 +51,10 @@ export type SwapsControllerState = {
swapsStxStatusDeadline?: number;
swapsStxGetTransactionsRefreshTime: number;
swapsStxMaxFeeMultiplier: number;
swapsFeatureFlags: Record<string, boolean>;
swapsFeatureFlags: { smartTransactions?: Partial<FeatureFlags> } & Record<
string,
boolean
>;
};
};

Expand Down
2 changes: 1 addition & 1 deletion shared/modules/selectors/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export type ProviderConfigState = NetworkConfigurationsByChainIdState &
SelectedNetworkClientIdState;

export const getNetworkConfigurationsByChainId = createDeepEqualSelector(
(state: NetworkConfigurationsByChainIdState) =>
(state: ProviderConfigState) =>
state.metamask.NetworkController.networkConfigurationsByChainId,
(networkConfigurationsByChainId) => networkConfigurationsByChainId,
);
Expand Down
54 changes: 13 additions & 41 deletions shared/modules/selectors/smart-transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,18 @@ import {
// eslint-disable-next-line import/no-restricted-paths
} from '../../../ui/selectors/selectors'; // TODO: Migrate shared selectors to this file.
import { isProduction } from '../environment';
import { BackgroundStateProxy } from '../../types/metamask';
import { getCurrentChainId, NetworkState } from './networks';

type SmartTransactionsMetaMaskState = {
metamask: {
preferences: {
smartTransactionsOptInStatus?: boolean;
};
internalAccounts: {
selectedAccount: string;
accounts: {
[key: string]: {
metadata: {
keyring: {
type: string;
};
};
};
};
};
swapsState: {
swapsFeatureFlags: {
ethereum: {
extensionActive: boolean;
mobileActive: boolean;
smartTransactions: {
expectedDeadline?: number;
maxDeadline?: number;
extensionReturnTxHashAsap?: boolean;
};
};
smartTransactions: {
extensionActive: boolean;
mobileActive: boolean;
};
};
};
smartTransactionsState: {
liveness: boolean;
};
};
export type SmartTransactionsMetaMaskState = {
metamask: Pick<
BackgroundStateProxy,
| 'PreferencesController'
| 'AccountsController'
| 'KeyringController'
| 'SwapsController'
| 'SmartTransactionsController'
>;
};

/**
Expand Down Expand Up @@ -134,10 +106,10 @@ export const getSmartTransactionsEnabled = (
const supportedAccount = accountSupportsSmartTx(state);
// TODO: Create a new proxy service only for MM feature flags.
const smartTransactionsFeatureFlagEnabled =
state.metamask.swapsState?.swapsFeatureFlags?.smartTransactions
?.extensionActive;
state.metamask.SwapsController.swapsState?.swapsFeatureFlags
?.smartTransactions?.extensionActive;
const smartTransactionsLiveness =
state.metamask.smartTransactionsState?.liveness;
state.metamask.SmartTransactionsController.smartTransactionsState?.liveness;
return Boolean(
getCurrentChainSupportsSmartTransactions(state) &&
getIsAllowedRpcUrlForSmartTransactions(state) &&
Expand Down
95 changes: 74 additions & 21 deletions ui/ducks/metamask/metamask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ import {
import {
getProviderConfig,
getSelectedNetworkClientId,
NetworkState,
ProviderConfigState,
} from '../../../shared/modules/selectors/networks';
import { getSelectedInternalAccount } from '../../selectors/accounts';
import { MultichainState } from '../../selectors/multichain';
import * as actionConstants from '../../store/actionConstants';
import { updateTransactionGasFees } from '../../store/actions';
import { setCustomGasLimit, setCustomGasPrice } from '../gas/gas.duck';
Expand Down Expand Up @@ -266,7 +269,10 @@ export const getNfts = (state: MetamaskSliceState) => {
return allNfts?.[selectedAddress]?.[chainId] ?? [];
};

export const getNFTsByChainId = (state: MetamaskSliceState, chainId: Hex) => {
export const getNFTsByChainId = (state: MetamaskSliceState, chainId?: Hex) => {
if (!chainId) {
return [];
}
const {
metamask: {
NftController: { allNfts },
Expand All @@ -292,25 +298,46 @@ export function getBlockGasLimit(state: MetamaskSliceState) {
return state.metamask.AccountTracker.currentBlockGasLimit;
}

export function getNativeCurrency(state: MetamaskSliceState) {
export function getNativeCurrency(state: ProviderConfigState) {
return getProviderConfig(state).ticker;
}

export function getConversionRate(state: MetamaskSliceState) {
export function getConversionRate(
state: ProviderConfigState & {
metamask: Pick<BackgroundStateProxy, 'CurrencyController'>;
},
) {
return state.metamask.CurrencyController.currencyRates[
getProviderConfig(state).ticker
]?.conversionRate;
}

export function getCurrencyRates(state: MetamaskSliceState) {
export function getCurrencyRates(state: {
metamask: Pick<BackgroundStateProxy, 'CurrencyController'>;
}) {
return state.metamask.CurrencyController.currencyRates;
}

export function getSendHexDataFeatureFlagState(state: MetamaskSliceState) {
export function getSendHexDataFeatureFlagState(state: {
metamask: Pick<BackgroundStateProxy, 'PreferencesController'>;
}) {
return state.metamask.PreferencesController.featureFlags.sendHexData;
}

export function getSendToAccounts(state: MetamaskSliceState) {
export function getSendToAccounts(
state: MultichainState & {
metamask: Pick<
BackgroundStateProxy,
| 'AccountTracker'
| 'AddressBookController'
| 'PermissionController'
| 'SubjectMetadataController'
| 'OnboardingController'
| 'SnapController'
| 'PreferencesController'
>;
} & ProviderConfigState,
) {
const fromAccounts = accountsWithSendEtherInfoSelector(state);
const addressBookAccounts = getAddressBook(state);
return [...fromAccounts, ...addressBookAccounts];
Expand All @@ -321,7 +348,7 @@ export function getSendToAccounts(state: MetamaskSliceState) {
*
* @param state
*/
export function isNotEIP1559Network(state: MetamaskSliceState) {
export function isNotEIP1559Network(state: NetworkState) {
const selectedNetworkClientId = getSelectedNetworkClientId(state);
return (
state.metamask.NetworkController.networksMetadata[selectedNetworkClientId]
Expand All @@ -336,8 +363,8 @@ export function isNotEIP1559Network(state: MetamaskSliceState) {
* @param networkClientId - The optional network client ID to check for EIP-1559 support. Defaults to the currently selected network.
*/
export function isEIP1559Network(
state: MetamaskSliceState,
networkClientId: string,
state: NetworkState,
networkClientId?: string,
) {
const selectedNetworkClientId = getSelectedNetworkClientId(state);

Expand All @@ -348,24 +375,32 @@ export function isEIP1559Network(
);
}

function getGasFeeControllerEstimateType(state: MetamaskSliceState) {
function getGasFeeControllerEstimateType(state: {
metamask: Pick<BackgroundStateProxy, 'GasFeeController'>;
}) {
return state.metamask.GasFeeController.gasEstimateType;
}

function getGasFeeControllerEstimateTypeByChainId(
state: MetamaskSliceState,
state: {
metamask: Pick<BackgroundStateProxy, 'GasFeeController'>;
},
chainId: Hex,
) {
return state.metamask.GasFeeController.gasFeeEstimatesByChainId?.[chainId]
?.gasEstimateType;
}

function getGasFeeControllerEstimates(state: MetamaskSliceState) {
function getGasFeeControllerEstimates(state: {
metamask: Pick<BackgroundStateProxy, 'GasFeeController'>;
}) {
return state.metamask.GasFeeController.gasFeeEstimates;
}

function getGasFeeControllerEstimatesByChainId(
state: MetamaskSliceState,
state: {
metamask: Pick<BackgroundStateProxy, 'GasFeeController'>;
},
chainId: Hex,
) {
return (
Expand All @@ -375,14 +410,19 @@ function getGasFeeControllerEstimatesByChainId(
}

function getTransactionGasFeeEstimates(
state: MetamaskSliceState & Pick<MetaMaskReduxState, 'confirmTransaction'>,
state: { metamask: Pick<BackgroundStateProxy, 'GasFeeController'> } & Pick<
MetaMaskReduxState,
'confirmTransaction'
>,
) {
const transactionMetadata = state.confirmTransaction?.txData;
return transactionMetadata?.gasFeeEstimates;
}

function getTransactionGasFeeEstimatesByChainId(
state: MetamaskSliceState & Pick<MetaMaskReduxState, 'confirmTransaction'>,
state: {
metamask: Pick<BackgroundStateProxy, 'GasFeeController'>;
} & Pick<MetaMaskReduxState, 'confirmTransaction'>,
chainId: Hex,
) {
const transactionMetadata = state.confirmTransaction?.txData;
Expand Down Expand Up @@ -467,20 +507,27 @@ export const getGasFeeEstimates = createSelector(
},
);

export function getEstimatedGasFeeTimeBounds(state: MetamaskSliceState) {
export function getEstimatedGasFeeTimeBounds(state: {
metamask: Pick<BackgroundStateProxy, 'GasFeeController'>;
}) {
return state.metamask.GasFeeController.estimatedGasFeeTimeBounds;
}

export function getEstimatedGasFeeTimeBoundsByChainId(
state: MetamaskSliceState,
state: {
metamask: Pick<BackgroundStateProxy, 'GasFeeController'>;
},
chainId: Hex,
) {
return state.metamask.GasFeeController.gasFeeEstimatesByChainId?.[chainId]
?.estimatedGasFeeTimeBounds;
}

export function getIsGasEstimatesLoading(
state: MetamaskSliceState & Pick<MetaMaskReduxState, 'confirmTransaction'>,
state: {
metamask: Pick<BackgroundStateProxy, 'GasFeeController'>;
} & NetworkState &
Pick<MetaMaskReduxState, 'confirmTransaction'>,
) {
const networkAndAccountSupports1559 =
checkNetworkAndAccountSupports1559(state);
Expand Down Expand Up @@ -537,18 +584,24 @@ export function getIsNetworkBusyByChainId(
: false;
}

export function getCompletedOnboarding(state: MetamaskSliceState) {
export function getCompletedOnboarding(state: {
metamask: Pick<BackgroundStateProxy, 'OnboardingController'>;
}) {
return state.metamask.OnboardingController.completedOnboarding;
}
export function getIsInitialized(state: MetamaskSliceState) {
return state.metamask.isInitialized;
}

export function getIsUnlocked(state: MetamaskSliceState) {
export function getIsUnlocked(state: {
metamask: Pick<BackgroundStateProxy, 'KeyringController'>;
}) {
return state.metamask.KeyringController.isUnlocked;
}

export function getSeedPhraseBackedUp(state: MetamaskSliceState) {
export function getSeedPhraseBackedUp(state: {
metamask: Pick<BackgroundStateProxy, 'OnboardingController'>;
}) {
return state.metamask.OnboardingController.seedPhraseBackedUp;
}

Expand Down
4 changes: 2 additions & 2 deletions ui/helpers/utils/confirm-tx.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ export function getTransactionFee({
}

export function formatCurrency(
value: string,
value: Numeric | string,
currencyCode: string,
precision?: number,
): string {
): string | Numeric {
const upperCaseCurrencyCode = currencyCode.toUpperCase();

return currencies.find((currency) => currency.code === upperCaseCurrencyCode)
Expand Down
Loading

0 comments on commit 63d8618

Please sign in to comment.