Skip to content

Commit

Permalink
fix: Warning when rejecting an approval request with id XYZ not found (
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistevam authored Sep 13, 2023
1 parent 2fbafdf commit b23a5f4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
23 changes: 13 additions & 10 deletions app/components/Views/Approval/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,18 @@ class Approval extends PureComponent {
try {
const { transactionHandled } = this.state;
const { transaction, selectedAddress } = this.props;
const { KeyringController, ApprovalController } = Engine.context;
const { KeyringController } = Engine.context;
if (!transactionHandled) {
if (isQRHardwareAccount(selectedAddress)) {
KeyringController.cancelQRSignRequest();
} else if (ApprovalController.has({ id: transaction?.id })) {
ApprovalController.reject(
} else {
Engine.rejectPendingApproval(
transaction?.id,
ethErrors.provider.userRejectedRequest(),
{
ignoreMissing: true,
logErrors: false,
},
);
}
Engine.context.TransactionController.hub.removeAllListeners(
Expand Down Expand Up @@ -175,19 +179,18 @@ class Approval extends PureComponent {
try {
if (appState !== 'active') {
const { transaction, transactions } = this.props;
const { ApprovalController } = Engine.context;
const currentTransaction = transactions.find(
(tx) => tx.id === transaction.id,
);

if (
transaction?.id &&
ApprovalController.has({ id: transaction?.id }) &&
this.isTxStatusCancellable(currentTransaction)
) {
ApprovalController.reject(
if (transaction?.id && this.isTxStatusCancellable(currentTransaction)) {
Engine.rejectPendingApproval(
transaction.id,
ethErrors.provider.userRejectedRequest(),
{
ignoreMissing: true,
logErrors: false,
},
);
}
this.props.hideModal();
Expand Down
26 changes: 16 additions & 10 deletions app/components/Views/ApproveView/Approve/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,30 +304,36 @@ class Approve extends PureComponent {
};

componentWillUnmount = async () => {
const { TransactionController } = Engine.context;
const { approved } = this.state;
const { transaction } = this.props;

await stopGasPolling(this.state.pollToken);
this.appStateListener?.remove();
Engine.context.TransactionController.hub.removeAllListeners(
`${transaction.id}:finished`,
);
TransactionController.hub.removeAllListeners(`${transaction.id}:finished`);
if (!approved)
Engine.context.ApprovalController.reject(
Engine.rejectPendingApproval(
transaction.id,
ethErrors.provider.userRejectedRequest(),
{
ignoreMissing: true,
logErrors: false,
},
);
};

handleAppStateChange = (appState) => {
if (appState !== 'active') {
const { transaction } = this.props;
transaction &&
transaction.id &&
Engine.context.ApprovalController.reject(
transaction.id,
ethErrors.provider.userRejectedRequest(),
);
Engine.rejectPendingApproval(
transaction?.id,
ethErrors.provider.userRejectedRequest(),
{
ignoreMissing: true,
logErrors: false,
},
);

this.props.hideModal();
}
};
Expand Down
28 changes: 24 additions & 4 deletions app/core/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ import {
import { hasProperty, Json } from '@metamask/controller-utils';
// TODO: Export this type from the package directly
import { SwapsState } from '@metamask/swaps-controller/dist/SwapsController';
import { ethErrors } from 'eth-rpc-errors';

import { PPOM, ppomInit } from '../lib/ppom/PPOMView';
import RNFSStorageBackend from '../lib/ppom/rnfs-storage-backend';
Expand Down Expand Up @@ -935,13 +936,26 @@ class Engine {
Engine.instance = null;
}

rejectPendingApproval(id: string, reason: Error) {
rejectPendingApproval(
id: string,
reason: Error = ethErrors.provider.userRejectedRequest(),
opts: { ignoreMissing?: boolean; logErrors?: boolean } = {},
) {
const { ApprovalController } = this.context;

if (opts.ignoreMissing && !ApprovalController.has({ id })) {
return;
}

try {
ApprovalController.reject(id, reason);
} catch (error: any) {
Logger.error(error, 'Reject while rejecting pending connection request');
if (opts.logErrors !== false) {
Logger.error(
error,
'Reject while rejecting pending connection request',
);
}
}
}

Expand Down Expand Up @@ -1083,6 +1097,12 @@ export default {
requestData?: Record<string, Json>,
opts?: AcceptOptions & { handleErrors?: boolean },
) => instance?.acceptPendingApproval(id, requestData, opts),
rejectPendingApproval: (id: string, reason: Error) =>
instance?.rejectPendingApproval(id, reason),
rejectPendingApproval: (
id: string,
reason: Error,
opts: {
ignoreMissing?: boolean;
logErrors?: boolean;
} = {},
) => instance?.rejectPendingApproval(id, reason, opts),
};

0 comments on commit b23a5f4

Please sign in to comment.