2023-04-25 11:47:49 +02:00
|
|
|
import { ApprovalControllerState } from '@metamask/approval-controller';
|
|
|
|
import { ApprovalType } from '@metamask/controller-utils';
|
2023-05-11 07:56:17 +02:00
|
|
|
import { TransactionMeta } from '../../shared/constants/transaction';
|
2023-04-25 11:47:49 +02:00
|
|
|
|
|
|
|
type ApprovalsMetaMaskState = {
|
|
|
|
metamask: {
|
|
|
|
pendingApprovals: ApprovalControllerState['pendingApprovals'];
|
2023-05-11 07:56:17 +02:00
|
|
|
unapprovedTxs: {
|
|
|
|
[transactionId: string]: TransactionMeta;
|
|
|
|
};
|
2023-04-25 11:47:49 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-06-15 22:18:12 +02:00
|
|
|
export function hasPendingApprovals(
|
2023-04-25 11:47:49 +02:00
|
|
|
state: ApprovalsMetaMaskState,
|
|
|
|
approvalType: ApprovalType,
|
2023-06-15 22:18:12 +02:00
|
|
|
predicate?: (
|
|
|
|
approval: ApprovalControllerState['pendingApprovals'][string],
|
|
|
|
) => boolean,
|
|
|
|
) {
|
2023-04-25 11:47:49 +02:00
|
|
|
const pendingApprovalRequests = Object.values(
|
|
|
|
state.metamask.pendingApprovals,
|
|
|
|
).filter(({ type }) => type === approvalType);
|
|
|
|
|
2023-06-15 22:18:12 +02:00
|
|
|
if (predicate) {
|
|
|
|
return pendingApprovalRequests.some(predicate);
|
|
|
|
}
|
2023-05-11 07:56:17 +02:00
|
|
|
|
2023-06-15 22:18:12 +02:00
|
|
|
return pendingApprovalRequests.length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getApprovalRequestsByType = (
|
2023-05-11 07:56:17 +02:00
|
|
|
state: ApprovalsMetaMaskState,
|
|
|
|
approvalType: ApprovalType,
|
2023-06-15 22:18:12 +02:00
|
|
|
predicate?: (
|
|
|
|
approval: ApprovalControllerState['pendingApprovals'][string],
|
|
|
|
) => boolean,
|
|
|
|
) => {
|
|
|
|
const pendingApprovalRequests = Object.values(
|
|
|
|
state.metamask.pendingApprovals,
|
|
|
|
).filter(({ type }) => type === approvalType);
|
2023-05-11 07:56:17 +02:00
|
|
|
|
2023-06-15 22:18:12 +02:00
|
|
|
if (predicate) {
|
|
|
|
return pendingApprovalRequests.filter(predicate);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pendingApprovalRequests;
|
|
|
|
};
|