Skip to content

Commit 5026a6c

Browse files
committed
refactor: migrate SmartTransactionController to @metamask/messenger
1 parent db3f00f commit 5026a6c

File tree

5 files changed

+80
-80
lines changed

5 files changed

+80
-80
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,21 @@
3838
"test": "jest && attw --pack",
3939
"test:watch": "jest --watchAll"
4040
},
41+
"resolutions": {
42+
"@metamask/polling-controller": "npm:@metamask-previews/[email protected]"
43+
},
4144
"dependencies": {
4245
"@babel/runtime": "^7.24.1",
4346
"@ethereumjs/tx": "^5.2.1",
4447
"@ethereumjs/util": "^9.0.2",
4548
"@ethersproject/bytes": "^5.7.0",
4649
"@ethersproject/keccak256": "^5.8.0",
4750
"@ethersproject/transactions": "^5.7.0",
48-
"@metamask/base-controller": "^8.3.0",
51+
"@metamask/base-controller": "^8.4.0",
4952
"@metamask/controller-utils": "^11.0.0",
5053
"@metamask/eth-json-rpc-provider": "^4.1.6",
5154
"@metamask/eth-query": "^4.0.0",
55+
"@metamask/messenger": "^0.3.0",
5256
"@metamask/polling-controller": "^14.0.0",
5357
"bignumber.js": "^9.0.1",
5458
"fast-json-patch": "^3.1.0",

src/SmartTransactionsController.test.ts

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
import { deriveStateFromMetadata, Messenger } from '@metamask/base-controller';
1+
import { deriveStateFromMetadata } from '@metamask/base-controller/next';
22
import {
33
NetworkType,
44
convertHexToDecimal,
55
ChainId,
66
} from '@metamask/controller-utils';
7+
import {
8+
Messenger,
9+
MessengerActions,
10+
MessengerEvents,
11+
MockAnyNamespace,
12+
MOCK_ANY_NAMESPACE,
13+
} from '@metamask/messenger';
714
import {
815
type NetworkControllerGetNetworkClientByIdAction,
916
type NetworkControllerGetStateAction,
@@ -37,6 +44,7 @@ import {
3744
DEFAULT_INTERVAL,
3845
SmartTransactionsController,
3946
getDefaultSmartTransactionsControllerState,
47+
type SmartTransactionsControllerMessenger,
4048
} from './SmartTransactionsController';
4149
import type {
4250
SmartTransactionsControllerActions,
@@ -46,6 +54,12 @@ import type { SmartTransaction, UnsignedTransaction, Hex } from './types';
4654
import { SmartTransactionStatuses, ClientId } from './types';
4755
import * as utils from './utils';
4856

57+
type AllActions = MessengerActions<SmartTransactionsControllerMessenger>;
58+
59+
type AllEvents = MessengerEvents<SmartTransactionsControllerMessenger>;
60+
61+
type RootMessenger = Messenger<MockAnyNamespace, AllActions, AllEvents>;
62+
4963
jest.mock('@metamask/eth-query', () => {
5064
const EthQuery = jest.requireActual('@metamask/eth-query');
5165
return class FakeEthQuery extends EthQuery {
@@ -2540,7 +2554,7 @@ describe('SmartTransactionsController', () => {
25402554
deriveStateFromMetadata(
25412555
controller.state,
25422556
controller.metadata,
2543-
'anonymous',
2557+
'includeInDebugSnapshot',
25442558
),
25452559
).toMatchInlineSnapshot(`
25462560
{
@@ -2717,17 +2731,10 @@ async function withController<ReturnValue>(
27172731
updateTransaction = jest.fn(),
27182732
} = rest;
27192733

2720-
const controllerMessenger = new Messenger<
2721-
| SmartTransactionsControllerActions
2722-
| NetworkControllerGetNetworkClientByIdAction
2723-
| NetworkControllerGetStateAction
2724-
| TransactionControllerGetNonceLockAction
2725-
| TransactionControllerConfirmExternalTransactionAction
2726-
| TransactionControllerGetTransactionsAction
2727-
| TransactionControllerUpdateTransactionAction,
2728-
SmartTransactionsControllerEvents | NetworkControllerStateChangeEvent
2729-
>();
2730-
controllerMessenger.registerActionHandler(
2734+
const rootMessenger: RootMessenger = new Messenger({
2735+
namespace: MOCK_ANY_NAMESPACE,
2736+
});
2737+
rootMessenger.registerActionHandler(
27312738
'NetworkController:getNetworkClientById',
27322739
jest.fn().mockImplementation((networkClientId) => {
27332740
switch (networkClientId) {
@@ -2750,7 +2757,7 @@ async function withController<ReturnValue>(
27502757
}
27512758
}),
27522759
);
2753-
controllerMessenger.registerActionHandler(
2760+
rootMessenger.registerActionHandler(
27542761
'NetworkController:getState',
27552762
jest.fn().mockReturnValue({
27562763
selectedNetworkClientId: NetworkType.mainnet,
@@ -2788,34 +2795,43 @@ async function withController<ReturnValue>(
27882795
},
27892796
}),
27902797
);
2791-
controllerMessenger.registerActionHandler(
2798+
rootMessenger.registerActionHandler(
27922799
'TransactionController:getNonceLock',
27932800
getNonceLock,
27942801
);
2795-
controllerMessenger.registerActionHandler(
2802+
rootMessenger.registerActionHandler(
27962803
'TransactionController:confirmExternalTransaction',
27972804
confirmExternalTransaction,
27982805
);
2799-
controllerMessenger.registerActionHandler(
2806+
rootMessenger.registerActionHandler(
28002807
'TransactionController:getTransactions',
28012808
getTransactions,
28022809
);
2803-
controllerMessenger.registerActionHandler(
2810+
rootMessenger.registerActionHandler(
28042811
'TransactionController:updateTransaction',
28052812
updateTransaction,
28062813
);
28072814

2808-
const messenger = controllerMessenger.getRestricted({
2809-
name: 'SmartTransactionsController',
2810-
allowedActions: [
2815+
const messenger = new Messenger<
2816+
'SmartTransactionsController',
2817+
AllActions,
2818+
AllEvents,
2819+
RootMessenger
2820+
>({
2821+
namespace: 'SmartTransactionsController',
2822+
parent: rootMessenger,
2823+
});
2824+
rootMessenger.delegate({
2825+
messenger,
2826+
actions: [
28112827
'NetworkController:getNetworkClientById',
28122828
'NetworkController:getState',
28132829
'TransactionController:getNonceLock',
28142830
'TransactionController:confirmExternalTransaction',
28152831
'TransactionController:getTransactions',
28162832
'TransactionController:updateTransaction',
28172833
],
2818-
allowedEvents: ['NetworkController:stateChange'],
2834+
events: ['NetworkController:stateChange'],
28192835
});
28202836

28212837
const controller = new SmartTransactionsController({
@@ -2834,7 +2850,7 @@ async function withController<ReturnValue>(
28342850
});
28352851

28362852
function triggerNetworStateChange(state: NetworkState) {
2837-
controllerMessenger.publish('NetworkController:stateChange', state, []);
2853+
rootMessenger.publish('NetworkController:stateChange', state, []);
28382854
}
28392855

28402856
triggerNetworStateChange({

src/SmartTransactionsController.ts

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import { hexlify } from '@ethersproject/bytes';
22
import type {
33
ControllerGetStateAction,
44
ControllerStateChangeEvent,
5-
RestrictedMessenger,
65
StateMetadata,
7-
} from '@metamask/base-controller';
6+
} from '@metamask/base-controller/next';
87
import {
98
query,
109
safelyExecute,
@@ -13,6 +12,7 @@ import {
1312
type TraceCallback,
1413
} from '@metamask/controller-utils';
1514
import EthQuery from '@metamask/eth-query';
15+
import type { Messenger } from '@metamask/messenger';
1616
import type {
1717
NetworkClientId,
1818
NetworkControllerGetNetworkClientByIdAction,
@@ -82,7 +82,7 @@ const controllerMetadata: StateMetadata<SmartTransactionsControllerState> = {
8282
smartTransactionsState: {
8383
includeInStateLogs: true,
8484
persist: false,
85-
anonymous: true,
85+
includeInDebugSnapshot: true,
8686
usedInUi: true,
8787
},
8888
};
@@ -187,12 +187,10 @@ type AllowedEvents = NetworkControllerStateChangeEvent;
187187
/**
188188
* The messenger of the {@link SmartTransactionsController}.
189189
*/
190-
export type SmartTransactionsControllerMessenger = RestrictedMessenger<
190+
export type SmartTransactionsControllerMessenger = Messenger<
191191
typeof controllerName,
192192
SmartTransactionsControllerActions | AllowedActions,
193-
SmartTransactionsControllerEvents | AllowedEvents,
194-
AllowedActions['type'],
195-
AllowedEvents['type']
193+
SmartTransactionsControllerEvents | AllowedEvents
196194
>;
197195

198196
type SmartTransactionsControllerOptions = {
@@ -294,13 +292,13 @@ export class SmartTransactionsController extends StaticIntervalPollingController
294292

295293
this.initializeSmartTransactionsForChainId();
296294

297-
this.messagingSystem.subscribe(
295+
this.messenger.subscribe(
298296
'NetworkController:stateChange',
299297
({ selectedNetworkClientId }) => {
300298
const {
301299
configuration: { chainId },
302300
provider,
303-
} = this.messagingSystem.call(
301+
} = this.messenger.call(
304302
'NetworkController:getNetworkClientById',
305303
selectedNetworkClientId,
306304
);
@@ -311,9 +309,8 @@ export class SmartTransactionsController extends StaticIntervalPollingController
311309
},
312310
);
313311

314-
this.messagingSystem.subscribe(
315-
`${controllerName}:stateChange`,
316-
(currentState) => this.checkPoll(currentState),
312+
this.messenger.subscribe(`${controllerName}:stateChange`, (currentState) =>
313+
this.checkPoll(currentState),
317314
);
318315
}
319316

@@ -430,7 +427,7 @@ export class SmartTransactionsController extends StaticIntervalPollingController
430427
let ethQuery = this.#ethQuery;
431428
let chainId = this.#chainId;
432429
if (networkClientId) {
433-
const { configuration, provider } = this.messagingSystem.call(
430+
const { configuration, provider } = this.messenger.call(
434431
'NetworkController:getNetworkClientById',
435432
networkClientId,
436433
);
@@ -565,7 +562,7 @@ export class SmartTransactionsController extends StaticIntervalPollingController
565562

566563
// We have to emit this event here, because then a txHash is returned to the TransactionController once it's available
567564
// and the #doesTransactionNeedConfirmation function will work properly, since it will find the txHash in the regular transactions list.
568-
this.messagingSystem.publish(
565+
this.messenger.publish(
569566
`SmartTransactionsController:smartTransaction`,
570567
nextSmartTransaction,
571568
);
@@ -580,9 +577,9 @@ export class SmartTransactionsController extends StaticIntervalPollingController
580577
markRegularTransactionAsFailed({
581578
smartTransaction: nextSmartTransaction,
582579
getRegularTransactions: () =>
583-
this.messagingSystem.call('TransactionController:getTransactions'),
580+
this.messenger.call('TransactionController:getTransactions'),
584581
updateTransaction: (transactionMeta: TransactionMeta, note: string) =>
585-
this.messagingSystem.call(
582+
this.messenger.call(
586583
'TransactionController:updateTransaction',
587584
transactionMeta,
588585
note,
@@ -650,7 +647,7 @@ export class SmartTransactionsController extends StaticIntervalPollingController
650647
if (!txHash) {
651648
return true;
652649
}
653-
const transactions = this.messagingSystem.call(
650+
const transactions = this.messenger.call(
654651
'TransactionController:getTransactions',
655652
);
656653
const foundTransaction = transactions?.find((tx) => {
@@ -733,7 +730,7 @@ export class SmartTransactionsController extends StaticIntervalPollingController
733730
: originalTxMeta;
734731

735732
if (this.#doesTransactionNeedConfirmation(txHash)) {
736-
this.messagingSystem.call(
733+
this.messenger.call(
737734
'TransactionController:confirmExternalTransaction',
738735
// TODO: Replace 'as' assertion with correct typing for `txMeta`
739736
txMeta as TransactionMeta,
@@ -763,7 +760,7 @@ export class SmartTransactionsController extends StaticIntervalPollingController
763760
});
764761
console.error('confirm error', error);
765762
} finally {
766-
this.messagingSystem.publish(
763+
this.messenger.publish(
767764
`SmartTransactionsController:smartTransactionConfirmationDone`,
768765
smartTransaction,
769766
);
@@ -829,7 +826,7 @@ export class SmartTransactionsController extends StaticIntervalPollingController
829826
transaction: UnsignedTransaction,
830827
networkClientId: NetworkClientId,
831828
): Promise<UnsignedTransaction> {
832-
const nonceLock = await this.messagingSystem.call(
829+
const nonceLock = await this.messenger.call(
833830
'TransactionController:getNonceLock',
834831
transaction.from,
835832
networkClientId,
@@ -861,8 +858,7 @@ export class SmartTransactionsController extends StaticIntervalPollingController
861858
): Promise<Fees> {
862859
const selectedNetworkClientId =
863860
networkClientId ??
864-
this.messagingSystem.call('NetworkController:getState')
865-
.selectedNetworkClientId;
861+
this.messenger.call('NetworkController:getState').selectedNetworkClientId;
866862
const chainId = this.#getChainId({
867863
networkClientId: selectedNetworkClientId,
868864
});
@@ -942,8 +938,7 @@ export class SmartTransactionsController extends StaticIntervalPollingController
942938
}) {
943939
const selectedNetworkClientId =
944940
networkClientId ??
945-
this.messagingSystem.call('NetworkController:getState')
946-
.selectedNetworkClientId;
941+
this.messenger.call('NetworkController:getState').selectedNetworkClientId;
947942
const chainId = this.#getChainId({
948943
networkClientId: selectedNetworkClientId,
949944
});
@@ -985,7 +980,7 @@ export class SmartTransactionsController extends StaticIntervalPollingController
985980
// This should only happen for Swaps. Non-swaps transactions should already have a nonce
986981
if (requiresNonce) {
987982
try {
988-
nonceLock = await this.messagingSystem.call(
983+
nonceLock = await this.messenger.call(
989984
'TransactionController:getNonceLock',
990985
txParams.from,
991986
selectedNetworkClientId,
@@ -1041,7 +1036,7 @@ export class SmartTransactionsController extends StaticIntervalPollingController
10411036
networkClientId,
10421037
}: { networkClientId?: NetworkClientId } = {}): Hex {
10431038
if (networkClientId) {
1044-
return this.messagingSystem.call(
1039+
return this.messenger.call(
10451040
'NetworkController:getNetworkClientById',
10461041
networkClientId,
10471042
).configuration.chainId;
@@ -1051,7 +1046,7 @@ export class SmartTransactionsController extends StaticIntervalPollingController
10511046
}
10521047

10531048
#getChainIds(): Hex[] {
1054-
const { networkConfigurationsByChainId } = this.messagingSystem.call(
1049+
const { networkConfigurationsByChainId } = this.messenger.call(
10551050
'NetworkController:getState',
10561051
);
10571052
return Object.keys(networkConfigurationsByChainId).filter(
@@ -1061,7 +1056,7 @@ export class SmartTransactionsController extends StaticIntervalPollingController
10611056
}
10621057

10631058
#getNetworkClientId({ chainId }: { chainId: string }): string {
1064-
const { networkConfigurationsByChainId } = this.messagingSystem.call(
1059+
const { networkConfigurationsByChainId } = this.messenger.call(
10651060
'NetworkController:getState',
10661061
);
10671062
return networkConfigurationsByChainId[chainId as Hex].rpcEndpoints[
@@ -1075,7 +1070,7 @@ export class SmartTransactionsController extends StaticIntervalPollingController
10751070
networkClientId?: NetworkClientId;
10761071
} = {}): EthQuery {
10771072
if (networkClientId) {
1078-
const { provider } = this.messagingSystem.call(
1073+
const { provider } = this.messenger.call(
10791074
'NetworkController:getNetworkClientById',
10801075
networkClientId,
10811076
);

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"esModuleInterop": true,
44
"lib": ["DOM", "ES2020"],
55
"module": "CommonJS",
6-
"moduleResolution": "node",
6+
"moduleResolution": "Node16",
77
"noEmit": true,
88
"noErrorTruncation": true,
99
"resolveJsonModule": true,

0 commit comments

Comments
 (0)