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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- This package can now be used in ESM environments as well as CommonJS ([#469](https://github.com/MetaMask/smart-transactions-controller/pull/469))
- Add two new controller state metadata properties: `includeInStateLogs` and `usedInUi` ([#531](https://github.com/MetaMask/smart-transactions-controller/pull/531))

### Changed

Expand Down
139 changes: 138 additions & 1 deletion src/SmartTransactionsController.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Messenger } from '@metamask/base-controller';
import { deriveStateFromMetadata, Messenger } from '@metamask/base-controller';
import {
NetworkType,
convertHexToDecimal,
Expand Down Expand Up @@ -446,7 +446,7 @@

controller.timeoutHandle = setTimeout(() => ({}));

controller.poll(1000);

Check warning on line 449 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

Check warning on line 449 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

expect(updateSmartTransactionsSpy).toHaveBeenCalled();
});
Expand Down Expand Up @@ -2531,6 +2531,143 @@
);
});
});

describe('metadata', () => {
it('includes expected state in debug snapshots', async () => {
await withController(({ controller }) => {
expect(
deriveStateFromMetadata(
controller.state,
controller.metadata,
'anonymous',
),
).toMatchInlineSnapshot(`
{
"smartTransactionsState": {
"fees": {
"approvalTxFees": null,
"tradeTxFees": null,
},
"feesByChainId": {
"0x1": {
"approvalTxFees": null,
"tradeTxFees": null,
},
"0xaa36a7": {
"approvalTxFees": null,
"tradeTxFees": null,
},
},
"liveness": true,
"livenessByChainId": {
"0x1": true,
"0xaa36a7": true,
},
"smartTransactions": {
"0x1": [],
},
"userOptIn": null,
"userOptInV2": null,
},
}
`);
});
});

it('includes expected state in state logs', async () => {
await withController(({ controller }) => {
expect(
deriveStateFromMetadata(
controller.state,
controller.metadata,
'includeInStateLogs',
),
).toMatchInlineSnapshot(`
{
"smartTransactionsState": {
"fees": {
"approvalTxFees": null,
"tradeTxFees": null,
},
"feesByChainId": {
"0x1": {
"approvalTxFees": null,
"tradeTxFees": null,
},
"0xaa36a7": {
"approvalTxFees": null,
"tradeTxFees": null,
},
},
"liveness": true,
"livenessByChainId": {
"0x1": true,
"0xaa36a7": true,
},
"smartTransactions": {
"0x1": [],
},
"userOptIn": null,
"userOptInV2": null,
},
}
`);
});
});

it('persists expected state', async () => {
await withController(({ controller }) => {
expect(
deriveStateFromMetadata(
controller.state,
controller.metadata,
'persist',
),
).toMatchInlineSnapshot(`{}`);
});
});

it('includes expected state in UI', async () => {
await withController(({ controller }) => {
expect(
deriveStateFromMetadata(
controller.state,
controller.metadata,
'usedInUi',
),
).toMatchInlineSnapshot(`
{
"smartTransactionsState": {
"fees": {
"approvalTxFees": null,
"tradeTxFees": null,
},
"feesByChainId": {
"0x1": {
"approvalTxFees": null,
"tradeTxFees": null,
},
"0xaa36a7": {
"approvalTxFees": null,
"tradeTxFees": null,
},
},
"liveness": true,
"livenessByChainId": {
"0x1": true,
"0xaa36a7": true,
},
"smartTransactions": {
"0x1": [],
},
"userOptIn": null,
"userOptInV2": null,
},
}
`);
});
});
});
});

type WithControllerCallback<ReturnValue> = ({
Expand Down Expand Up @@ -2703,7 +2840,7 @@
triggerNetworStateChange,
});
} finally {
controller.stop();

Check warning on line 2843 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

Check warning on line 2843 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
controller.stopAllPolling();
}
}
5 changes: 4 additions & 1 deletion src/SmartTransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
ControllerGetStateAction,
ControllerStateChangeEvent,
RestrictedMessenger,
StateMetadata,
} from '@metamask/base-controller';
import {
query,
Expand Down Expand Up @@ -75,10 +76,12 @@
*/
const controllerName = 'SmartTransactionsController';

const controllerMetadata = {
const controllerMetadata: StateMetadata<SmartTransactionsControllerState> = {
smartTransactionsState: {
includeInStateLogs: true,
persist: false,
anonymous: true,
usedInUi: true,
},
};

Expand Down Expand Up @@ -357,9 +360,9 @@
isSmartTransactionPending,
);
if (!this.timeoutHandle && pendingTransactions?.length > 0) {
this.poll();

Check warning on line 363 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

Check warning on line 363 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
} else if (this.timeoutHandle && pendingTransactions?.length === 0) {
this.stop();

Check warning on line 365 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

Check warning on line 365 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
}
}

Expand All @@ -384,7 +387,7 @@
}

this.timeoutHandle = setInterval(() => {
safelyExecute(async () => this.updateSmartTransactions());

Check warning on line 390 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

Check warning on line 390 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
}, this.#interval);
await safelyExecute(async () => this.updateSmartTransactions());
}
Expand Down Expand Up @@ -451,7 +454,7 @@
ethQuery = new EthQuery(provider);
}

this.#createOrUpdateSmartTransaction(smartTransaction, {

Check warning on line 457 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

Check warning on line 457 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
chainId,
ethQuery,
});
Expand Down Expand Up @@ -738,7 +741,7 @@
: originalTxMeta;

if (this.#doesTransactionNeedConfirmation(txHash)) {
this.#confirmExternalTransaction(

Check warning on line 744 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

Check warning on line 744 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
// TODO: Replace 'as' assertion with correct typing for `txMeta`
txMeta as TransactionMeta,
transactionReceipt,
Expand Down
Loading