Skip to content

Commit eca308d

Browse files
authored
feat: Add new metadata properties (#531)
* add metadata properties to `SmartTransactionsController` * update CHANGELOG
1 parent a1ca475 commit eca308d

File tree

3 files changed

+143
-2
lines changed

3 files changed

+143
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

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

1415
### Changed
1516

src/SmartTransactionsController.test.ts

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Messenger } from '@metamask/base-controller';
1+
import { deriveStateFromMetadata, Messenger } from '@metamask/base-controller';
22
import {
33
NetworkType,
44
convertHexToDecimal,
@@ -2531,6 +2531,143 @@ describe('SmartTransactionsController', () => {
25312531
);
25322532
});
25332533
});
2534+
2535+
describe('metadata', () => {
2536+
it('includes expected state in debug snapshots', async () => {
2537+
await withController(({ controller }) => {
2538+
expect(
2539+
deriveStateFromMetadata(
2540+
controller.state,
2541+
controller.metadata,
2542+
'anonymous',
2543+
),
2544+
).toMatchInlineSnapshot(`
2545+
{
2546+
"smartTransactionsState": {
2547+
"fees": {
2548+
"approvalTxFees": null,
2549+
"tradeTxFees": null,
2550+
},
2551+
"feesByChainId": {
2552+
"0x1": {
2553+
"approvalTxFees": null,
2554+
"tradeTxFees": null,
2555+
},
2556+
"0xaa36a7": {
2557+
"approvalTxFees": null,
2558+
"tradeTxFees": null,
2559+
},
2560+
},
2561+
"liveness": true,
2562+
"livenessByChainId": {
2563+
"0x1": true,
2564+
"0xaa36a7": true,
2565+
},
2566+
"smartTransactions": {
2567+
"0x1": [],
2568+
},
2569+
"userOptIn": null,
2570+
"userOptInV2": null,
2571+
},
2572+
}
2573+
`);
2574+
});
2575+
});
2576+
2577+
it('includes expected state in state logs', async () => {
2578+
await withController(({ controller }) => {
2579+
expect(
2580+
deriveStateFromMetadata(
2581+
controller.state,
2582+
controller.metadata,
2583+
'includeInStateLogs',
2584+
),
2585+
).toMatchInlineSnapshot(`
2586+
{
2587+
"smartTransactionsState": {
2588+
"fees": {
2589+
"approvalTxFees": null,
2590+
"tradeTxFees": null,
2591+
},
2592+
"feesByChainId": {
2593+
"0x1": {
2594+
"approvalTxFees": null,
2595+
"tradeTxFees": null,
2596+
},
2597+
"0xaa36a7": {
2598+
"approvalTxFees": null,
2599+
"tradeTxFees": null,
2600+
},
2601+
},
2602+
"liveness": true,
2603+
"livenessByChainId": {
2604+
"0x1": true,
2605+
"0xaa36a7": true,
2606+
},
2607+
"smartTransactions": {
2608+
"0x1": [],
2609+
},
2610+
"userOptIn": null,
2611+
"userOptInV2": null,
2612+
},
2613+
}
2614+
`);
2615+
});
2616+
});
2617+
2618+
it('persists expected state', async () => {
2619+
await withController(({ controller }) => {
2620+
expect(
2621+
deriveStateFromMetadata(
2622+
controller.state,
2623+
controller.metadata,
2624+
'persist',
2625+
),
2626+
).toMatchInlineSnapshot(`{}`);
2627+
});
2628+
});
2629+
2630+
it('includes expected state in UI', async () => {
2631+
await withController(({ controller }) => {
2632+
expect(
2633+
deriveStateFromMetadata(
2634+
controller.state,
2635+
controller.metadata,
2636+
'usedInUi',
2637+
),
2638+
).toMatchInlineSnapshot(`
2639+
{
2640+
"smartTransactionsState": {
2641+
"fees": {
2642+
"approvalTxFees": null,
2643+
"tradeTxFees": null,
2644+
},
2645+
"feesByChainId": {
2646+
"0x1": {
2647+
"approvalTxFees": null,
2648+
"tradeTxFees": null,
2649+
},
2650+
"0xaa36a7": {
2651+
"approvalTxFees": null,
2652+
"tradeTxFees": null,
2653+
},
2654+
},
2655+
"liveness": true,
2656+
"livenessByChainId": {
2657+
"0x1": true,
2658+
"0xaa36a7": true,
2659+
},
2660+
"smartTransactions": {
2661+
"0x1": [],
2662+
},
2663+
"userOptIn": null,
2664+
"userOptInV2": null,
2665+
},
2666+
}
2667+
`);
2668+
});
2669+
});
2670+
});
25342671
});
25352672

25362673
type WithControllerCallback<ReturnValue> = ({

src/SmartTransactionsController.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
ControllerGetStateAction,
44
ControllerStateChangeEvent,
55
RestrictedMessenger,
6+
StateMetadata,
67
} from '@metamask/base-controller';
78
import {
89
query,
@@ -75,10 +76,12 @@ const ETH_QUERY_ERROR_MSG =
7576
*/
7677
const controllerName = 'SmartTransactionsController';
7778

78-
const controllerMetadata = {
79+
const controllerMetadata: StateMetadata<SmartTransactionsControllerState> = {
7980
smartTransactionsState: {
81+
includeInStateLogs: true,
8082
persist: false,
8183
anonymous: true,
84+
usedInUi: true,
8285
},
8386
};
8487

0 commit comments

Comments
 (0)