Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issue where queued request counts were not included in logic to reroute from home screen and close notification #25454

Merged
merged 8 commits into from
Jun 21, 2024
1 change: 1 addition & 0 deletions app/scripts/controllers/app-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default class AppStateController extends EventEmitter {
...initState,
qrHardware: {},
nftsDropdownState: {},
queuedRequestCount: 0,
usedNetworks: {
'0x1': true,
'0x5': true,
Expand Down
1 change: 1 addition & 0 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2285,6 +2285,7 @@ export default class MetamaskController extends EventEmitter {
AuthenticationController: this.authenticationController,
UserStorageController: this.userStorageController,
MetamaskNotificationsController: this.metamaskNotificationsController,
QueuedRequestController: this.queuedRequestController,
PushPlatformNotificationsController:
this.pushPlatformNotificationsController,
...resetOnRestartStore,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@
"selectedAddress": "string"
},
"PushPlatformNotificationsController": { "fcmToken": "string" },
"QueuedRequestController": "object",
"SelectedNetworkController": { "domains": "object" },
"SignatureController": {
"unapprovedMsgs": "object",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"nftsDropdownState": {},
"termsOfUseLastAgreed": "number",
"qrHardware": {},
"queuedRequestCount": "number",
"usedNetworks": { "0x1": true, "0x5": true, "0x539": true },
"snapsInstallPrivacyWarningShown": true,
"surveyLinkLastClickedOrClosed": "object",
Expand Down
6 changes: 4 additions & 2 deletions ui/pages/home/home.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,19 @@ import FlaskHomeFooter from './flask/flask-home-footer.component';

function shouldCloseNotificationPopup({
isNotification,
totalUnapprovedCount,
totalUnapprovedAndQueuedRequestCount,
hasApprovalFlows,
isSigningQRHardwareTransaction,
///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
waitForConfirmDeepLinkDialog,
institutionalConnectRequests,
///: END:ONLY_INCLUDE_IF
}) {
// we can't use totalUnapproved because there are also queued requests

let shouldClose =
isNotification &&
totalUnapprovedCount === 0 &&
totalUnapprovedAndQueuedRequestCount === 0 &&
!hasApprovalFlows &&
!isSigningQRHardwareTransaction;

Expand Down
5 changes: 5 additions & 0 deletions ui/pages/home/home.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
getNewTokensImportedError,
hasPendingApprovals,
getSelectedInternalAccount,
getQueuedRequestCount,
///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
getAccountType,
///: END:ONLY_INCLUDE_IF
Expand Down Expand Up @@ -114,6 +115,9 @@ const mapStateToProps = (state) => {
const { address: selectedAddress } = getSelectedInternalAccount(state);
const { forgottenPassword } = metamask;
const totalUnapprovedCount = getTotalUnapprovedCount(state);
const queuedRequestCount = getQueuedRequestCount(state);
const totalUnapprovedAndQueuedRequestCount =
totalUnapprovedCount + queuedRequestCount;
const swapsEnabled = getSwapsFeatureIsLive(state);
const pendingConfirmations = getUnapprovedTemplatedConfirmations(state);
///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
Expand Down Expand Up @@ -175,6 +179,7 @@ const mapStateToProps = (state) => {
selectedAddress,
firstPermissionsRequestId,
totalUnapprovedCount,
totalUnapprovedAndQueuedRequestCount,
participateInMetaMetrics,
hasApprovalFlows: getApprovalFlows(state)?.length > 0,
connectedStatusPopoverHasBeenShown,
Expand Down
4 changes: 4 additions & 0 deletions ui/selectors/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,10 @@ export function getTotalUnapprovedCount(state) {
return state.metamask.pendingApprovalCount ?? 0;
}

export function getQueuedRequestCount(state) {
return state.metamask.queuedRequestCount ?? 0;
}

export function getTotalUnapprovedMessagesCount(state) {
const {
unapprovedMsgCount = 0,
Expand Down
22 changes: 5 additions & 17 deletions ui/selectors/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,22 +618,6 @@ export const submittedPendingTransactionsSelector = createSelector(
),
);

const hasUnapprovedTransactionsInCurrentNetwork = (state) => {
const unapprovedTxs = getUnapprovedTransactions(state);
const unapprovedTxRequests = getApprovalRequestsByType(
state,
ApprovalType.Transaction,
);

const chainId = getCurrentChainId(state);

const filteredUnapprovedTxInCurrentNetwork = unapprovedTxRequests.filter(
({ id }) => unapprovedTxs[id] && unapprovedTxs[id].chainId === chainId,
);

return filteredUnapprovedTxInCurrentNetwork.length > 0;
};

const TRANSACTION_APPROVAL_TYPES = [
ApprovalType.EthDecrypt,
ApprovalType.EthGetEncryptionPublicKey,
Expand All @@ -643,8 +627,12 @@ const TRANSACTION_APPROVAL_TYPES = [
];

export function hasTransactionPendingApprovals(state) {
const unapprovedTxRequests = getApprovalRequestsByType(
state,
ApprovalType.Transaction,
);
return (
hasUnapprovedTransactionsInCurrentNetwork(state) ||
unapprovedTxRequests.length > 0 ||
Comment on lines +630 to +635
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we queue requests on different dapps which can be on different networks now we don't want to restrict this boolean check to only transactions that match the currently selected network. I'm not actually sure how there could have been pending transactions on different networks previously that necessitated this check 🤔... but I might be forgetting something

hasPendingApprovals(state, TRANSACTION_APPROVAL_TYPES)
);
}
4 changes: 2 additions & 2 deletions ui/selectors/transactions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -767,10 +767,10 @@ describe('Transaction Selectors', () => {
expect(result).toBe(true);
});

it('should return false if there is a pending transaction on different network', () => {
it('should return true if there is a pending transaction on different network', () => {
mockedState.metamask.transactions[0].chainId = 'differentChainId';
const result = hasTransactionPendingApprovals(mockedState);
expect(result).toBe(false);
expect(result).toBe(true);
});

it.each([
Expand Down