Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ function waitForTransactionFinished(
});
}

const MOCK_PRFERENCES = { state: { selectedAddress: 'foo' } };
const MOCK_PREFERENCES = { state: { selectedAddress: 'foo' } };
const INFURA_PROJECT_ID = '341eacb578dd44a1a049cbc5f6fd4035';
const GOERLI_PROVIDER = new HttpProvider(
`https://goerli.infura.io/v3/${INFURA_PROJECT_ID}`,
Expand Down Expand Up @@ -1039,7 +1039,7 @@ describe('TransactionController', () => {
controller.wipeTransactions();

controller.state.transactions.push({
from: MOCK_PRFERENCES.state.selectedAddress,
from: MOCK_PREFERENCES.state.selectedAddress,
id: 'foo',
networkID: '5',
status: TransactionStatus.submitted,
Expand All @@ -1050,14 +1050,76 @@ describe('TransactionController', () => {

expect(controller.state.transactions).toHaveLength(0);
});

it('removes only txs with given address', async () => {
const controller = newController();

controller.wipeTransactions();

const mockFromAccount1 = '0x1bf137f335ea1b8f193b8f6ea92561a60d23a207';
const mockFromAccount2 = '0x2bf137f335ea1b8f193b8f6ea92561a60d23a207';
const mockCurrentChainId = toHex(5);

controller.state.transactions.push({
id: '1',
chainId: mockCurrentChainId,
transaction: {
from: mockFromAccount1,
},
} as any);

controller.state.transactions.push({
id: '2',
chainId: mockCurrentChainId,
transaction: {
from: mockFromAccount2,
},
} as any);

controller.wipeTransactions(true, mockFromAccount2);

expect(controller.state.transactions).toHaveLength(1);
expect(controller.state.transactions[0].id).toBe('1');
});

it('removes only txs with given address only on current network', async () => {
const controller = newController();

controller.wipeTransactions();

const mockFromAccount1 = '0x1bf137f335ea1b8f193b8f6ea92561a60d23a207';
const mockDifferentChainId = toHex(1);
const mockCurrentChainId = toHex(5);

controller.state.transactions.push({
id: '1',
chainId: mockCurrentChainId,
transaction: {
from: mockFromAccount1,
},
} as any);

controller.state.transactions.push({
id: '4',
chainId: mockDifferentChainId,
transaction: {
from: mockFromAccount1,
},
} as any);

controller.wipeTransactions(false, mockFromAccount1);

expect(controller.state.transactions).toHaveLength(1);
expect(controller.state.transactions[0].id).toBe('4');
});
});

describe('queryTransactionStatus', () => {
it('updates transaction status to confirmed', async () => {
const controller = newController();

controller.state.transactions.push({
from: MOCK_PRFERENCES.state.selectedAddress,
from: MOCK_PREFERENCES.state.selectedAddress,
id: 'foo',
networkID: '5',
chainId: toHex(5),
Expand All @@ -1083,7 +1145,7 @@ describe('TransactionController', () => {
const controller = newController();

controller.state.transactions.push({
from: MOCK_PRFERENCES.state.selectedAddress,
from: MOCK_PREFERENCES.state.selectedAddress,
id: 'foo',
networkID: '5',
status: TransactionStatus.submitted,
Expand All @@ -1106,7 +1168,7 @@ describe('TransactionController', () => {
const controller = newController();

controller.state.transactions.push({
from: MOCK_PRFERENCES.state.selectedAddress,
from: MOCK_PREFERENCES.state.selectedAddress,
id: 'foo',
networkID: '5',
status: TransactionStatus.submitted,
Expand All @@ -1123,7 +1185,7 @@ describe('TransactionController', () => {
const controller = newController();

controller.state.transactions.push({
from: MOCK_PRFERENCES.state.selectedAddress,
from: MOCK_PREFERENCES.state.selectedAddress,
id: 'foo',
networkID: '5',
chainId: toHex(5),
Expand Down
21 changes: 16 additions & 5 deletions packages/transaction-controller/src/TransactionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -789,23 +789,34 @@ export class TransactionController extends BaseController<
*
* @param ignoreNetwork - Determines whether to wipe all transactions, or just those on the
* current network. If `true`, all transactions are wiped.
* @param address - If specified, only transactions originating from this address will be
* wiped on current network.
*/
wipeTransactions(ignoreNetwork?: boolean) {
wipeTransactions(ignoreNetwork?: boolean, address?: string) {
/* istanbul ignore next */
if (ignoreNetwork) {
if (ignoreNetwork && !address) {
this.update({ transactions: [] });
return;
}
const { providerConfig, networkId: currentNetworkID } =
this.getNetworkState();
const { chainId: currentChainId } = providerConfig;
const newTransactions = this.state.transactions.filter(
({ networkID, chainId }) => {
({ networkID, chainId, transaction }) => {
// Using fallback to networkID only when there is no chainId present. Should be removed when networkID is completely removed.
const isCurrentNetwork =
const isMatchingNetwork =
ignoreNetwork ||
chainId === currentChainId ||
(!chainId && networkID === currentNetworkID);
return !isCurrentNetwork;

if (!isMatchingNetwork) {
return true;
}

const isMatchingAddress =
!address || transaction.from?.toLowerCase() === address.toLowerCase();

return !isMatchingAddress;
},
);

Expand Down