Skip to content

Commit

Permalink
Replace CombinedBackgroundAndReduxState type with fixed `MetaMaskRe…
Browse files Browse the repository at this point in the history
…duxState` type
  • Loading branch information
MajorLift committed Dec 4, 2024
1 parent a2e12f7 commit 32f90c5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import {
INITIAL_SEND_STATE_FOR_EXISTING_DRAFT,
createMockInternalAccount,
} from '../../../../../../test/jest/mocks';
import { CombinedBackgroundAndReduxState } from '../../../../../store/store';
import { MetaMaskReduxState } from '../../../../../store/store';
import { shortenAddress } from '../../../../../helpers/utils/util';
// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
import { normalizeSafeAddress } from '../../../../../../app/scripts/lib/multichain/address';
import { SendPageAccountPicker } from '.';

const render = (
state: Partial<CombinedBackgroundAndReduxState> = {},
state: Partial<MetaMaskReduxState> = {},
props = {},
sendStage = SEND_STAGES.ADD_RECIPIENT,
) => {
Expand Down Expand Up @@ -105,25 +105,29 @@ describe('SendPageAccountPicker', () => {
});
const { queryByText, queryAllByTestId, getByTestId } = render({
metamask: {
internalAccounts: {
accounts: {
[mockAccount.id]: mockAccount,
[mockBtcAccount.id]: mockBtcAccount,
AccountsController: {
internalAccounts: {
accounts: {
[mockAccount.id]: mockAccount,
[mockBtcAccount.id]: mockBtcAccount,
},
selectedAccount: mockAccount.id,
},
selectedAccount: mockAccount.id,
},
keyrings: [
{
type: 'HD Key Tree',
accounts: [mockAccount.address],
},
{
type: 'Snap Keyring',
accounts: [mockBtcAccount.address],
},
],
KeyringController: {
keyrings: [
{
type: 'HD Key Tree',
accounts: [mockAccount.address],
},
{
type: 'Snap Keyring',
accounts: [mockBtcAccount.address],
},
],
},
},
} as CombinedBackgroundAndReduxState);
} as MetaMaskReduxState);

expect(queryByText(mockAccount.metadata.name)).toBeInTheDocument();

Expand Down
6 changes: 3 additions & 3 deletions ui/components/ui/qr-code-view/qr-code-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { isHexPrefixed } from 'ethereumjs-util';
import { normalizeSafeAddress } from '../../../../app/scripts/lib/multichain/address';
import { Box, Icon, IconName, IconSize, Text } from '../../component-library';
import { MetaMetricsContext } from '../../../contexts/metametrics';
import type { CombinedBackgroundAndReduxState } from '../../../store/store';
import type { MetaMaskReduxState } from '../../../store/store';
import {
AlignItems,
Display,
Expand All @@ -25,7 +25,7 @@ import {
} from '../../../../shared/constants/metametrics';
import { useCopyToClipboard } from '../../../hooks/useCopyToClipboard';

function mapStateToProps(state: CombinedBackgroundAndReduxState) {
function mapStateToProps(state: MetaMaskReduxState) {
const { buyView, warning } = state.appState;
return {
buyView,
Expand All @@ -41,7 +41,7 @@ function QrCodeView({
accountName,
}: {
Qr: { message: string; data: string };
warning: null | string;
warning: string | null | undefined;
accountName?: string;
}) {
const trackEvent = useContext(MetaMetricsContext);
Expand Down
17 changes: 6 additions & 11 deletions ui/ducks/metamask/metamask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { getSelectedInternalAccount } from '../../selectors/accounts';
import * as actionConstants from '../../store/actionConstants';
import { updateTransactionGasFees } from '../../store/actions';
import { setCustomGasLimit, setCustomGasPrice } from '../gas/gas.duck';
import { CombinedBackgroundAndReduxState } from '../../store/store';
import { MetaMaskReduxState } from '../../store/store';
import type { BackgroundStateProxy } from '../../../shared/types/metamask';
import { initialMetamaskState } from './constants';

Expand Down Expand Up @@ -375,16 +375,14 @@ function getGasFeeControllerEstimatesByChainId(
}

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

function getTransactionGasFeeEstimatesByChainId(
state: MetamaskSliceState &
Pick<CombinedBackgroundAndReduxState, 'confirmTransaction'>,
state: MetamaskSliceState & Pick<MetaMaskReduxState, 'confirmTransaction'>,
chainId: Hex,
) {
const transactionMetadata = state.confirmTransaction?.txData;
Expand Down Expand Up @@ -482,8 +480,7 @@ export function getEstimatedGasFeeTimeBoundsByChainId(
}

export function getIsGasEstimatesLoading(
state: MetamaskSliceState &
Pick<CombinedBackgroundAndReduxState, 'confirmTransaction'>,
state: MetamaskSliceState & Pick<MetaMaskReduxState, 'confirmTransaction'>,
) {
const networkAndAccountSupports1559 =
checkNetworkAndAccountSupports1559(state);
Expand All @@ -505,8 +502,7 @@ export function getIsGasEstimatesLoading(
}

export function getIsGasEstimatesLoadingByChainId(
state: MetamaskSliceState &
Pick<CombinedBackgroundAndReduxState, 'confirmTransaction'>,
state: MetamaskSliceState & Pick<MetaMaskReduxState, 'confirmTransaction'>,
{ chainId, networkClientId }: { chainId: Hex; networkClientId: string },
) {
const networkAndAccountSupports1559 = checkNetworkAndAccountSupports1559(
Expand All @@ -531,8 +527,7 @@ export function getIsGasEstimatesLoadingByChainId(
}

export function getIsNetworkBusyByChainId(
state: MetamaskSliceState &
Pick<CombinedBackgroundAndReduxState, 'confirmTransaction'>,
state: MetamaskSliceState & Pick<MetaMaskReduxState, 'confirmTransaction'>,
chainId: Hex,
) {
const gasFeeEstimates = getGasFeeEstimatesByChainId(state, chainId);
Expand Down
19 changes: 10 additions & 9 deletions ui/store/institutional/institution-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
showModal,
} from '../actions';
import {
CombinedBackgroundAndReduxState,
MetaMaskReduxState,
TemporaryMessageDataType,
MessagesIndexedById,
Expand Down Expand Up @@ -56,13 +55,14 @@ export function showCustodyConfirmLink({
}

export function updateCustodyState(
dispatch: ThunkDispatch<CombinedBackgroundAndReduxState, unknown, AnyAction>,
dispatch: ThunkDispatch<MetaMaskReduxState, unknown, AnyAction>,
newState: MetaMaskReduxState['metamask'],
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
state: CombinedBackgroundAndReduxState & any,
state: MetaMaskReduxState,
) {
if (!newState.transactions || !state.metamask.transactions) {
if (
!newState.TxController.transactions ||
!state.metamask.TxController.transactions
) {
return;
}

Expand All @@ -86,7 +86,7 @@ export function updateCustodyState(
const txStateSaysDeepLinkShouldClose = Boolean(
differentTxs.find((tx: TransactionMeta) => {
const custodyAccountDetails =
state.metamask.custodyAccountDetails[
state.metamask.CustodyController.custodyAccountDetails[
toChecksumHexAddress(tx.txParams.from)
];
const custody = custodyAccountDetails?.custodyType
Expand All @@ -99,8 +99,9 @@ export function updateCustodyState(
return (
tx.custodyId === state.appState.modal.modalState.props?.custodyId &&
tx.custodyStatus &&
(state.metamask.custodyStatusMaps[custody][tx.custodyStatus]
?.mmStatus !== 'approved' ||
(state.metamask.CustodyController.custodyStatusMaps[custody][
tx.custodyStatus
]?.mmStatus !== 'approved' ||
tx.custodyStatus === CustodyStatus.CREATED)
);
}),
Expand Down
30 changes: 15 additions & 15 deletions ui/store/store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { StoreEnhancer } from 'redux';
import { Reducer, StoreEnhancer } from 'redux';
import { configureStore as baseConfigureStore } from '@reduxjs/toolkit';
import devtoolsEnhancer from 'remote-redux-devtools';
import rootReducer from '../ducks';
import { AppSliceState } from '../ducks/app/app';
import { MetamaskSliceState } from '../ducks/metamask/metamask';

/**
* This interface is temporary and is copied from the message-manager.js file
Expand Down Expand Up @@ -32,22 +34,20 @@ export type MessagesIndexedById = {

type RootReducerReturnType = ReturnType<typeof rootReducer>;

export type CombinedBackgroundAndReduxState = RootReducerReturnType & {
/**
* `ReduxState` overrides incorrectly typed properties of `RootReducerReturnType`, and is only intended to be used as an input for `configureStore`.
* The `MetaMaskReduxState` type (derived from the returned output of `configureStore`) is to be used consistently as the single source-of-truth and representation of Redux state shape.
*
* Redux slice reducers that are passed an `AnyAction`-type `action` parameter are inferred to have a return type of `never`.
* TODO: Supply exhaustive action types to all Redux slices (specifically `metamask` and `appState`)
*/
type ReduxState = {
activeTab: {
origin: string;
};
metamask: RootReducerReturnType['metamask'];
appState: RootReducerReturnType['appState'];
send: RootReducerReturnType['send'];
DNS: RootReducerReturnType['DNS'];
history: RootReducerReturnType['history'];
confirmAlerts: RootReducerReturnType['confirmAlerts'];
confirmTransaction: RootReducerReturnType['confirmTransaction'];
swaps: RootReducerReturnType['swaps'];
bridge: RootReducerReturnType['bridge'];
gas: RootReducerReturnType['gas'];
localeMessages: RootReducerReturnType['localeMessages'];
};
metamask: MetamaskSliceState['metamask'];
appState: AppSliceState['appState'];
} & Omit<RootReducerReturnType, 'activeTab' | 'metamask' | 'appState'>;

// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -68,7 +68,7 @@ export default function configureStore(preloadedState: any) {
}

return baseConfigureStore({
reducer: rootReducer as () => CombinedBackgroundAndReduxState,
reducer: rootReducer as unknown as Reducer<ReduxState>,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
/**
Expand Down

0 comments on commit 32f90c5

Please sign in to comment.