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 a condition in the "wipeSmartTransations" function #323

Merged
merged 4 commits into from
May 2, 2024
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
61 changes: 51 additions & 10 deletions src/SmartTransactionsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
DEFAULT_INTERVAL,
} from './SmartTransactionsController';
import { advanceTime, flushPromises } from './test-helpers';
import type { SmartTransaction, UnsignedTransaction } from './types';
import type { SmartTransaction, UnsignedTransaction, Hex } from './types';
import { SmartTransactionStatuses } from './types';
import * as utils from './utils';
import packageJson from '../package.json';
Expand Down Expand Up @@ -1139,13 +1139,13 @@

describe('setStatusRefreshInterval', () => {
it('sets refresh interval if different', () => {
smartTransactionsController.setStatusRefreshInterval(100);

Check warning on line 1142 in src/SmartTransactionsController.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (18.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check warning on line 1142 in src/SmartTransactionsController.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
expect(smartTransactionsController.config.interval).toBe(100);
});

it('does not set refresh interval if they are the same', () => {
const configureSpy = jest.spyOn(smartTransactionsController, 'configure');
smartTransactionsController.setStatusRefreshInterval(DEFAULT_INTERVAL);

Check warning on line 1148 in src/SmartTransactionsController.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (18.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check warning on line 1148 in src/SmartTransactionsController.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
expect(configureSpy).toHaveBeenCalledTimes(0);
});
});
Expand Down Expand Up @@ -1415,12 +1415,29 @@
expect(smartTransactionsController.state).toStrictEqual(prevState);
});

it('removes transactions from current chainId if ignoreNetwork is true', () => {
it('removes transactions from all chains saved in the smartTransactionsState if ignoreNetwork is true', () => {
const address = '0x123';
smartTransactionsController.wipeSmartTransactions({
address,
ignoreNetwork: true,
});
const { smartTransactions } =
smartTransactionsController.state.smartTransactionsState;
Object.keys(smartTransactions).forEach((chainId) => {
const chainIdHex: Hex = chainId as Hex;
expect(
smartTransactionsController.state.smartTransactionsState
.smartTransactions[chainIdHex],
).not.toContainEqual({ txParams: { from: address } });
});
});

it('removes transactions only from the current chainId if ignoreNetwork is false', () => {
const address = '0x123';
smartTransactionsController.wipeSmartTransactions({
address,
ignoreNetwork: false,
});
expect(
smartTransactionsController.state.smartTransactionsState
.smartTransactions[smartTransactionsController.config.chainId],
Expand All @@ -1435,20 +1452,44 @@
);
});

it('removes transactions from all supported chainIds if ignoreNetwork is false', () => {
it('removes transactions from the current chainId (even if it is not in supportedChainIds) if ignoreNetwork is false', () => {
const address = '0x123';
smartTransactionsController.config.supportedChainIds = [ChainId.mainnet];
smartTransactionsController.config.chainId = ChainId.sepolia;
smartTransactionsController.wipeSmartTransactions({
address,
ignoreNetwork: false,
});
smartTransactionsController.config.supportedChainIds.forEach(
(chainId) => {
expect(
smartTransactionsController.state.smartTransactionsState
.smartTransactions[chainId],
).not.toContainEqual({ txParams: { from: address } });
},
expect(
smartTransactionsController.state.smartTransactionsState
.smartTransactions[smartTransactionsController.config.chainId],
).not.toContainEqual({ txParams: { from: address } });
expect(
smartTransactionsController.state.smartTransactionsState
.smartTransactions[ChainId.mainnet],
).toContainEqual(
expect.objectContaining({
txParams: expect.objectContaining({ from: address }),
}),
);
});

it('removes transactions from all chains (even if they are not in supportedChainIds) if ignoreNetwork is true', () => {
const address = '0x123';
smartTransactionsController.config.supportedChainIds = [];
smartTransactionsController.wipeSmartTransactions({
address,
ignoreNetwork: true,
});
const { smartTransactions } =
smartTransactionsController.state.smartTransactionsState;
Object.keys(smartTransactions).forEach((chainId) => {
const chainIdHex: Hex = chainId as Hex;
expect(
smartTransactionsController.state.smartTransactionsState
.smartTransactions[chainIdHex],
).not.toContainEqual({ txParams: { from: address } });
});
});
});
});
10 changes: 5 additions & 5 deletions src/SmartTransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@
isSmartTransactionPending,
);
if (!this.timeoutHandle && pendingTransactions?.length > 0) {
this.poll();

Check warning on line 218 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (18.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check warning on line 218 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
} else if (this.timeoutHandle && pendingTransactions?.length === 0) {
this.stop();

Check warning on line 220 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (18.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check warning on line 220 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
}
}

Expand Down Expand Up @@ -247,7 +247,7 @@
}
await safelyExecute(async () => this.updateSmartTransactions());
this.timeoutHandle = setInterval(() => {
safelyExecute(async () => this.updateSmartTransactions());

Check warning on line 250 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (18.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check warning on line 250 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
}, this.config.interval);
}

Expand Down Expand Up @@ -329,7 +329,7 @@
ethQuery = new EthQuery(networkClient.provider);
}

this.#updateSmartTransaction(smartTransaction, {

Check warning on line 332 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (18.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check warning on line 332 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
chainId,
ethQuery,
});
Expand Down Expand Up @@ -447,7 +447,7 @@
.map((smartTransaction) => smartTransaction.uuid);

if (transactionsToUpdate.length > 0) {
this.fetchSmartTransactionsStatus(transactionsToUpdate, {

Check warning on line 450 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (18.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check warning on line 450 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
networkClientId,
});
}
Expand Down Expand Up @@ -550,7 +550,7 @@
category: MetaMetricsEventCategory.Transactions,
});

this.#updateSmartTransaction(

Check warning on line 553 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (18.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check warning on line 553 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
{
...smartTransaction,
confirmed: true,
Expand Down Expand Up @@ -594,7 +594,7 @@
cancellable: isSmartTransactionCancellable(stxStatus),
uuid,
};
this.#updateSmartTransaction(smartTransaction, { chainId, ethQuery });

Check warning on line 597 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (18.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check warning on line 597 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
});

return data;
Expand All @@ -608,7 +608,7 @@
nonceLock.releaseLock();
return {
...transaction,
nonce: `0x${nonce.toString(16)}`,

Check warning on line 611 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (18.x)

Invalid type "any" of template literal expression

Check warning on line 611 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (20.x)

Invalid type "any" of template literal expression
};
}

Expand Down Expand Up @@ -898,11 +898,6 @@
}
const addressLowerCase = address.toLowerCase();
if (ignoreNetwork) {
this.#wipeSmartTransactionsPerChainId({
chainId: this.config.chainId,
addressLowerCase,
});
} else {
const { smartTransactions } = this.state.smartTransactionsState;
Object.keys(smartTransactions).forEach((chainId) => {
const chainIdHex: Hex = chainId as Hex;
Expand All @@ -911,6 +906,11 @@
addressLowerCase,
});
});
} else {
this.#wipeSmartTransactionsPerChainId({
chainId: this.config.chainId,
addressLowerCase,
});
}
}

Expand Down
Loading