2023-03-14 11:57:05 +01:00
|
|
|
import { ThunkAction, ThunkDispatch } from 'redux-thunk';
|
|
|
|
import { AnyAction } from 'redux';
|
|
|
|
import {
|
|
|
|
closeCurrentNotificationWindow,
|
|
|
|
hideModal,
|
|
|
|
showModal,
|
|
|
|
} from '../actions';
|
|
|
|
import {
|
|
|
|
CombinedBackgroundAndReduxState,
|
|
|
|
MetaMaskReduxState,
|
|
|
|
TemporaryMessageDataType,
|
2023-06-20 15:37:09 +02:00
|
|
|
MessagesIndexedById,
|
2023-03-14 11:57:05 +01:00
|
|
|
} from '../store';
|
|
|
|
import { toChecksumHexAddress } from '../../../shared/modules/hexstring-utils';
|
2023-09-04 17:48:25 +02:00
|
|
|
import { getCurrentNetworkTransactions } from '../../selectors';
|
|
|
|
import { TransactionMeta } from '../../../shared/constants/transaction';
|
2023-03-14 11:57:05 +01:00
|
|
|
|
|
|
|
export function showInteractiveReplacementTokenModal(): ThunkAction<
|
|
|
|
void,
|
|
|
|
MetaMaskReduxState,
|
|
|
|
unknown,
|
|
|
|
AnyAction
|
|
|
|
> {
|
|
|
|
return (dispatch) => {
|
|
|
|
dispatch(
|
|
|
|
showModal({
|
|
|
|
name: 'INTERACTIVE_REPLACEMENT_TOKEN_MODAL',
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-07-13 10:42:08 +02:00
|
|
|
export function showCustodyConfirmLink({
|
|
|
|
link,
|
|
|
|
address,
|
|
|
|
closeNotification,
|
|
|
|
custodyId,
|
|
|
|
}: {
|
|
|
|
link: string;
|
|
|
|
address: string;
|
|
|
|
closeNotification: boolean;
|
|
|
|
custodyId: string;
|
|
|
|
}): ThunkAction<void, MetaMaskReduxState, unknown, AnyAction> {
|
2023-03-14 11:57:05 +01:00
|
|
|
return (dispatch) => {
|
|
|
|
dispatch(
|
|
|
|
showModal({
|
|
|
|
name: 'CUSTODY_CONFIRM_LINK',
|
|
|
|
link,
|
|
|
|
address,
|
|
|
|
closeNotification,
|
|
|
|
custodyId,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function updateCustodyState(
|
|
|
|
dispatch: ThunkDispatch<CombinedBackgroundAndReduxState, unknown, AnyAction>,
|
|
|
|
newState: MetaMaskReduxState['metamask'],
|
|
|
|
state: CombinedBackgroundAndReduxState & any,
|
|
|
|
) {
|
2023-09-04 17:48:25 +02:00
|
|
|
if (!newState.transactions || !state.metamask.transactions) {
|
2023-03-14 11:57:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-04 17:48:25 +02:00
|
|
|
const newCurrentNetworkTxList = getCurrentNetworkTransactions({
|
|
|
|
metamask: newState,
|
|
|
|
});
|
|
|
|
|
|
|
|
const oldCurrentNetworkTxList = getCurrentNetworkTransactions(state);
|
|
|
|
|
|
|
|
const differentTxs = newCurrentNetworkTxList.filter(
|
|
|
|
(item: TransactionMeta) =>
|
|
|
|
oldCurrentNetworkTxList.filter(
|
2023-03-14 11:57:05 +01:00
|
|
|
(tx: { [key: string]: any }) =>
|
|
|
|
tx.custodyId === item.custodyId &&
|
|
|
|
tx.custodyStatus !== item.custodyStatus,
|
|
|
|
).length > 0,
|
|
|
|
);
|
|
|
|
|
|
|
|
const txStateSaysDeepLinkShouldClose = Boolean(
|
2023-09-04 17:48:25 +02:00
|
|
|
differentTxs.find((tx: TransactionMeta) => {
|
2023-03-14 11:57:05 +01:00
|
|
|
const custodyAccountDetails =
|
|
|
|
state.metamask.custodyAccountDetails[
|
|
|
|
toChecksumHexAddress(tx.txParams.from)
|
|
|
|
];
|
|
|
|
const custody = custodyAccountDetails?.custodyType
|
|
|
|
.split(' - ')[1]
|
|
|
|
.toLowerCase();
|
|
|
|
if (!custody) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
tx.custodyId === state.appState.modal.modalState.props?.custodyId &&
|
|
|
|
(state.metamask.custodyStatusMaps[custody][tx.custodyStatus]
|
|
|
|
?.mmStatus !== 'approved' ||
|
|
|
|
tx.custodyStatus === 'created')
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (
|
|
|
|
state.appState.modal.open &&
|
|
|
|
state.appState.modal.modalState.name === 'CUSTODY_CONFIRM_LINK' &&
|
|
|
|
txStateSaysDeepLinkShouldClose
|
|
|
|
) {
|
|
|
|
if (state.appState.modal.modalState.props?.closeNotification) {
|
|
|
|
dispatch(closeCurrentNotificationWindow());
|
|
|
|
}
|
|
|
|
dispatch(hideModal());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
state.appState.modal.open &&
|
|
|
|
state.appState.modal.modalState.name ===
|
|
|
|
'INTERACTIVE_REPLACEMENT_TOKEN_MODAL'
|
|
|
|
) {
|
|
|
|
if (state.appState.modal.modalState.props?.closeNotification) {
|
|
|
|
dispatch(closeCurrentNotificationWindow());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-06 17:10:03 +02:00
|
|
|
export function checkForUnapprovedMessages(
|
2023-07-21 17:52:47 +02:00
|
|
|
msgData: TemporaryMessageDataType,
|
2023-07-06 17:10:03 +02:00
|
|
|
unapprovedMessages: MessagesIndexedById,
|
2023-03-14 11:57:05 +01:00
|
|
|
) {
|
2023-07-06 17:10:03 +02:00
|
|
|
const custodianUnapprovedMessages = Object.keys(unapprovedMessages)
|
|
|
|
.map((key) => unapprovedMessages[key])
|
2023-07-21 17:52:47 +02:00
|
|
|
.filter((message) => {
|
|
|
|
return message.metadata?.custodyId && message.status === 'unapproved';
|
|
|
|
});
|
2023-03-14 11:57:05 +01:00
|
|
|
|
|
|
|
if (custodianUnapprovedMessages && custodianUnapprovedMessages.length > 0) {
|
|
|
|
return {
|
|
|
|
...msgData,
|
2023-07-21 17:52:47 +02:00
|
|
|
custodyId: unapprovedMessages[msgData.id]?.metadata?.custodyId,
|
2023-03-14 11:57:05 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return msgData;
|
|
|
|
}
|