diff --git a/app/scripts/controllers/transactions/index.js b/app/scripts/controllers/transactions/index.js index ed851fcc9..41ddc70ad 100644 --- a/app/scripts/controllers/transactions/index.js +++ b/app/scripts/controllers/transactions/index.js @@ -793,4 +793,5 @@ export default class TransactionController extends EventEmitter { const currentNetworkTxList = this.txStateManager.getTxList(MAX_MEMSTORE_TX_LIST_SIZE) this.memStore.updateState({ unapprovedTxs, currentNetworkTxList }) } + } diff --git a/app/scripts/controllers/transactions/tx-state-manager.js b/app/scripts/controllers/transactions/tx-state-manager.js index ca07d3e88..7992a1d02 100644 --- a/app/scripts/controllers/transactions/tx-state-manager.js +++ b/app/scripts/controllers/transactions/tx-state-manager.js @@ -508,4 +508,14 @@ export default class TransactionStateManager extends EventEmitter { const transactionList = this.getFullTxList() this._saveTxList(transactionList.filter((txMeta) => txMeta.id !== txId)) } + + /** + * Filters out the unapproved transactions + */ + + clearUnapprovedTxs () { + const transactions = this.getFullTxList() + const nonUnapprovedTxs = transactions.filter((tx) => tx.status !== 'unapproved') + this._saveTxList(nonUnapprovedTxs) + } } diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 0b87206ca..c64e92ff2 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -631,6 +631,9 @@ export default class MetamaskController extends EventEmitter { // clear permissions this.permissionsController.clearPermissions() + // clear unapproved transactions + this.txController.txStateManager.clearUnapprovedTxs() + // create new vault const vault = await keyringController.createNewVaultAndRestore(password, seed) diff --git a/test/unit/app/controllers/transactions/tx-state-manager-test.js b/test/unit/app/controllers/transactions/tx-state-manager-test.js index 32acfb408..e4dd13515 100644 --- a/test/unit/app/controllers/transactions/tx-state-manager-test.js +++ b/test/unit/app/controllers/transactions/tx-state-manager-test.js @@ -598,4 +598,23 @@ describe('TransactionStateManager', function () { assert.equal(txStateManager.getFullTxList()[0].id, 2, 'txList should have a id of 2') }) }) + + describe('#clearUnapprovedTxs', function () { + it('removes unapproved transactions', function () { + const txMetas = [ + { id: 0, status: 'unapproved', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId }, + { id: 1, status: 'unapproved', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId }, + { id: 2, status: 'confirmed', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: otherNetworkId }, + { id: 3, status: 'confirmed', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: otherNetworkId }, + ] + + txMetas.forEach((txMeta) => txStateManager.addTx(txMeta, noop)) + + txStateManager.clearUnapprovedTxs() + + const unapprovedTxList = txStateManager.getFullTxList().filter((tx) => tx.status === 'unapproved') + + assert.equal(unapprovedTxList.length, 0) + }) + }) })