Skip to content

Commit df896e5

Browse files
committed
refactor: getSeletedAccount to messenger action
1 parent 45a3819 commit df896e5

File tree

8 files changed

+113
-62
lines changed

8 files changed

+113
-62
lines changed

packages/transaction-controller/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@
4747
"@ethersproject/abi": "^5.7.0",
4848
"@ethersproject/contracts": "^5.7.0",
4949
"@ethersproject/providers": "^5.7.0",
50+
"@metamask/accounts-controller": "^16.0.0",
5051
"@metamask/approval-controller": "^7.0.0",
5152
"@metamask/base-controller": "^6.0.0",
5253
"@metamask/controller-utils": "^11.0.0",
5354
"@metamask/eth-query": "^4.0.0",
5455
"@metamask/gas-fee-controller": "^17.0.0",
55-
"@metamask/keyring-api": "6.4.0",
5656
"@metamask/metamask-eth-abis": "^3.1.1",
5757
"@metamask/network-controller": "^19.0.0",
5858
"@metamask/nonce-tracker": "^5.0.0",
@@ -69,6 +69,7 @@
6969
"@babel/runtime": "^7.23.9",
7070
"@metamask/auto-changelog": "^3.4.4",
7171
"@metamask/ethjs-provider-http": "^0.3.0",
72+
"@metamask/keyring-api": "^6.4.0",
7273
"@types/bn.js": "^5.1.5",
7374
"@types/jest": "^27.4.1",
7475
"@types/node": "^16.18.54",
@@ -84,6 +85,7 @@
8485
},
8586
"peerDependencies": {
8687
"@babel/runtime": "^7.23.9",
88+
"@metamask/accounts-controller": "^16.0.0",
8789
"@metamask/approval-controller": "^7.0.0",
8890
"@metamask/gas-fee-controller": "^17.0.0",
8991
"@metamask/network-controller": "^19.0.0"

packages/transaction-controller/src/TransactionController.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,6 @@ describe('TransactionController', () => {
597597
// eslint-disable-next-line @typescript-eslint/no-explicit-any
598598
getNetworkClientRegistry: () => ({} as any),
599599
getPermittedAccounts: async () => [ACCOUNT_MOCK],
600-
getSelectedAccount: () => INTERNAL_ACCOUNT_MOCK,
601600
isMultichainEnabled: false,
602601
hooks: {},
603602
onNetworkStateChange: network.subscribe,
@@ -615,10 +614,19 @@ describe('TransactionController', () => {
615614
'ApprovalController:addRequest',
616615
'NetworkController:getNetworkClientById',
617616
'NetworkController:findNetworkClientIdByChainId',
617+
'AccountsController:getSelectedAccount',
618618
],
619619
allowedEvents: [],
620620
});
621621

622+
const mockSelectedAccountCall = jest
623+
.fn()
624+
.mockReturnValue(INTERNAL_ACCOUNT_MOCK);
625+
unrestrictedMessenger.registerActionHandler(
626+
'AccountsController:getSelectedAccount',
627+
mockSelectedAccountCall,
628+
);
629+
622630
const controller = new TransactionController({
623631
...otherOptions,
624632
messenger: restrictedMessenger,
@@ -628,6 +636,7 @@ describe('TransactionController', () => {
628636
controller,
629637
messenger: unrestrictedMessenger,
630638
mockTransactionApprovalRequest,
639+
mockSelectedAccountCall,
631640
};
632641
}
633642

packages/transaction-controller/src/TransactionController.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Hardfork, Common, type ChainConfig } from '@ethereumjs/common';
22
import type { TypedTransaction } from '@ethereumjs/tx';
33
import { TransactionFactory } from '@ethereumjs/tx';
44
import { bufferToHex } from '@ethereumjs/util';
5+
import type { AccountsControllerGetSelectedAccountAction } from '@metamask/accounts-controller';
56
import type {
67
AcceptResultCallbacks,
78
AddApprovalRequest,
@@ -25,7 +26,6 @@ import type {
2526
FetchGasFeeEstimateOptions,
2627
GasFeeState,
2728
} from '@metamask/gas-fee-controller';
28-
import type { InternalAccount } from '@metamask/keyring-api';
2929
import type {
3030
BlockTracker,
3131
NetworkClientId,
@@ -298,7 +298,6 @@ export type TransactionControllerOptions = {
298298
getNetworkState: () => NetworkState;
299299
getPermittedAccounts: (origin?: string) => Promise<string[]>;
300300
getSavedGasFees?: (chainId: Hex) => SavedGasFees | undefined;
301-
getSelectedAccount: () => InternalAccount;
302301
incomingTransactions?: IncomingTransactionOptions;
303302
isMultichainEnabled: boolean;
304303
isSimulationEnabled?: () => boolean;
@@ -345,7 +344,8 @@ const controllerName = 'TransactionController';
345344
export type AllowedActions =
346345
| AddApprovalRequest
347346
| NetworkControllerFindNetworkClientIdByChainIdAction
348-
| NetworkControllerGetNetworkClientByIdAction;
347+
| NetworkControllerGetNetworkClientByIdAction
348+
| AccountsControllerGetSelectedAccountAction;
349349

350350
/**
351351
* The external events available to the {@link TransactionController}.
@@ -615,8 +615,6 @@ export class TransactionController extends BaseController<
615615

616616
private readonly getPermittedAccounts: (origin?: string) => Promise<string[]>;
617617

618-
private readonly getSelectedAccount: () => InternalAccount;
619-
620618
private readonly getExternalPendingTransactions: (
621619
address: string,
622620
chainId?: string,
@@ -734,7 +732,6 @@ export class TransactionController extends BaseController<
734732
* @param options.getNetworkState - Gets the state of the network controller.
735733
* @param options.getPermittedAccounts - Get accounts that a given origin has permissions for.
736734
* @param options.getSavedGasFees - Gets the saved gas fee config.
737-
* @param options.getSelectedAccount - Gets the currently selected account.
738735
* @param options.incomingTransactions - Configuration options for incoming transaction support.
739736
* @param options.isMultichainEnabled - Enable multichain support.
740737
* @param options.isSimulationEnabled - Whether new transactions will be automatically simulated.
@@ -762,7 +759,6 @@ export class TransactionController extends BaseController<
762759
getNetworkState,
763760
getPermittedAccounts,
764761
getSavedGasFees,
765-
getSelectedAccount,
766762
incomingTransactions = {},
767763
isMultichainEnabled = false,
768764
isSimulationEnabled,
@@ -803,7 +799,6 @@ export class TransactionController extends BaseController<
803799
this.getGasFeeEstimates =
804800
getGasFeeEstimates || (() => Promise.resolve({} as GasFeeState));
805801
this.getPermittedAccounts = getPermittedAccounts;
806-
this.getSelectedAccount = getSelectedAccount;
807802
this.getExternalPendingTransactions =
808803
getExternalPendingTransactions ?? (() => []);
809804
this.securityProviderRequest = securityProviderRequest;
@@ -1036,7 +1031,8 @@ export class TransactionController extends BaseController<
10361031
if (origin) {
10371032
await validateTransactionOrigin(
10381033
await this.getPermittedAccounts(origin),
1039-
this.getSelectedAccount().address,
1034+
this.messagingSystem.call('AccountsController:getSelectedAccount')
1035+
.address,
10401036
txParams.from,
10411037
origin,
10421038
);
@@ -3431,7 +3427,8 @@ export class TransactionController extends BaseController<
34313427
}): IncomingTransactionHelper {
34323428
const incomingTransactionHelper = new IncomingTransactionHelper({
34333429
blockTracker,
3434-
getCurrentAccount: this.getSelectedAccount,
3430+
getCurrentAccount: () =>
3431+
this.messagingSystem.call('AccountsController:getSelectedAccount'),
34353432
getLastFetchedBlockNumbers: () => this.state.lastFetchedBlockNumbers,
34363433
getChainId: chainId ? () => chainId : this.getChainId.bind(this),
34373434
isEnabled: this.#incomingTransactionOptions.isEnabled,

packages/transaction-controller/src/TransactionControllerIntegration.test.ts

Lines changed: 80 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { TypedTransaction } from '@ethereumjs/tx';
2+
import type { AccountsControllerGetSelectedAccountAction } from '@metamask/accounts-controller';
23
import type {
34
ApprovalControllerActions,
45
ApprovalControllerEvents,
@@ -61,7 +62,8 @@ import * as etherscanUtils from './utils/etherscan';
6162
type UnrestrictedControllerMessenger = ControllerMessenger<
6263
| NetworkControllerActions
6364
| ApprovalControllerActions
64-
| TransactionControllerActions,
65+
| TransactionControllerActions
66+
| AccountsControllerGetSelectedAccountAction,
6567
| NetworkControllerEvents
6668
| ApprovalControllerEvents
6769
| TransactionControllerEvents
@@ -188,10 +190,20 @@ const setupController = async (
188190
'ApprovalController:addRequest',
189191
'NetworkController:getNetworkClientById',
190192
'NetworkController:findNetworkClientIdByChainId',
193+
'AccountsController:getSelectedAccount',
191194
],
192195
allowedEvents: ['NetworkController:stateChange'],
193196
});
194197

198+
const mockSelectedAccountCall = jest
199+
.fn()
200+
.mockReturnValue(createMockInternalAccount({ address: '0xdeadbeef' }));
201+
202+
unrestrictedMessenger.registerActionHandler(
203+
'AccountsController:getSelectedAccount',
204+
mockSelectedAccountCall,
205+
);
206+
195207
const options = {
196208
blockTracker,
197209
disableHistory: false,
@@ -230,6 +242,7 @@ const setupController = async (
230242
approvalController,
231243
networkController,
232244
messenger,
245+
mockSelectedAccountCall,
233246
};
234247
};
235248

@@ -841,12 +854,16 @@ describe('TransactionController Integration', () => {
841854
],
842855
});
843856

844-
const { approvalController, networkController, transactionController } =
845-
await setupController({
846-
isMultichainEnabled: true,
847-
getPermittedAccounts: async () => [ACCOUNT_MOCK],
848-
getSelectedAccount: () => INTERNAL_ACCOUNT_MOCK,
849-
});
857+
const {
858+
approvalController,
859+
networkController,
860+
transactionController,
861+
mockSelectedAccountCall,
862+
} = await setupController({
863+
isMultichainEnabled: true,
864+
getPermittedAccounts: async () => [ACCOUNT_MOCK],
865+
});
866+
mockSelectedAccountCall.mockReturnValue(INTERNAL_ACCOUNT_MOCK);
850867
const otherNetworkClientIdOnGoerli =
851868
await networkController.upsertNetworkConfiguration(
852869
{
@@ -922,12 +939,15 @@ describe('TransactionController Integration', () => {
922939
buildEthGetTransactionReceiptRequestMock('0x2', '0x2', '0x4'),
923940
],
924941
});
925-
const { approvalController, transactionController } =
926-
await setupController({
927-
isMultichainEnabled: true,
928-
getPermittedAccounts: async () => [ACCOUNT_MOCK],
929-
getSelectedAccount: () => INTERNAL_ACCOUNT_MOCK,
930-
});
942+
const {
943+
approvalController,
944+
transactionController,
945+
mockSelectedAccountCall,
946+
} = await setupController({
947+
isMultichainEnabled: true,
948+
getPermittedAccounts: async () => [ACCOUNT_MOCK],
949+
});
950+
mockSelectedAccountCall.mockReturnValue(INTERNAL_ACCOUNT_MOCK);
931951

932952
const addTx1 = await transactionController.addTransaction(
933953
{
@@ -1187,11 +1207,14 @@ describe('TransactionController Integration', () => {
11871207
address: selectedAddress,
11881208
});
11891209

1190-
const { networkController, transactionController } =
1191-
await setupController({
1192-
getSelectedAccount: () => selectedAccountMock,
1193-
isMultichainEnabled: true,
1194-
});
1210+
const {
1211+
networkController,
1212+
transactionController,
1213+
mockSelectedAccountCall,
1214+
} = await setupController({
1215+
isMultichainEnabled: true,
1216+
});
1217+
mockSelectedAccountCall.mockReturnValue(selectedAccountMock);
11951218

11961219
const expectedLastFetchedBlockNumbers: Record<string, number> = {};
11971220
const expectedTransactions: Partial<TransactionMeta>[] = [];
@@ -1274,9 +1297,10 @@ describe('TransactionController Integration', () => {
12741297
)
12751298
.reply(200, ETHERSCAN_TRANSACTION_RESPONSE_MOCK);
12761299

1277-
const { transactionController } = await setupController({
1278-
getSelectedAccount: () => selectedAccountMock,
1279-
});
1300+
const { transactionController, mockSelectedAccountCall } =
1301+
await setupController({});
1302+
1303+
mockSelectedAccountCall.mockReturnValue(selectedAccountMock);
12801304

12811305
transactionController.startIncomingTransactionPolling();
12821306

@@ -1367,11 +1391,14 @@ describe('TransactionController Integration', () => {
13671391
address: selectedAddress,
13681392
});
13691393

1370-
const { networkController, transactionController } =
1371-
await setupController({
1372-
getSelectedAccount: () => selectedAccountMock,
1373-
isMultichainEnabled: true,
1374-
});
1394+
const {
1395+
networkController,
1396+
transactionController,
1397+
mockSelectedAccountCall,
1398+
} = await setupController({
1399+
isMultichainEnabled: true,
1400+
});
1401+
mockSelectedAccountCall.mockReturnValue(selectedAccountMock);
13751402

13761403
const otherGoerliClientNetworkClientId =
13771404
await networkController.upsertNetworkConfiguration(
@@ -1466,10 +1493,12 @@ describe('TransactionController Integration', () => {
14661493
address: selectedAddress,
14671494
});
14681495

1469-
const { networkController, transactionController } =
1470-
await setupController({
1471-
getSelectedAccount: () => selectedAccountMock,
1472-
});
1496+
const {
1497+
networkController,
1498+
transactionController,
1499+
mockSelectedAccountCall,
1500+
} = await setupController({});
1501+
mockSelectedAccountCall.mockReturnValue(selectedAccountMock);
14731502

14741503
const networkClients = networkController.getNetworkClientRegistry();
14751504
const networkClientIds = Object.keys(networkClients);
@@ -1513,9 +1542,10 @@ describe('TransactionController Integration', () => {
15131542
address: selectedAddress,
15141543
});
15151544

1516-
const { transactionController } = await setupController({
1517-
getSelectedAccount: () => selectedAccountMock,
1518-
});
1545+
const { transactionController, mockSelectedAccountCall } =
1546+
await setupController({});
1547+
1548+
mockSelectedAccountCall.mockReturnValue(selectedAccountMock);
15191549

15201550
mockNetwork({
15211551
networkClientConfiguration: buildInfuraNetworkClientConfiguration(
@@ -1552,10 +1582,12 @@ describe('TransactionController Integration', () => {
15521582
address: selectedAddress,
15531583
});
15541584

1555-
const { networkController, transactionController } =
1556-
await setupController({
1557-
getSelectedAccount: () => selectedAccountMock,
1558-
});
1585+
const {
1586+
networkController,
1587+
transactionController,
1588+
mockSelectedAccountCall,
1589+
} = await setupController({});
1590+
mockSelectedAccountCall.mockReturnValue(selectedAccountMock);
15591591

15601592
const networkClients = networkController.getNetworkClientRegistry();
15611593
const networkClientIds = Object.keys(networkClients);
@@ -1599,11 +1631,14 @@ describe('TransactionController Integration', () => {
15991631
address: selectedAddress,
16001632
});
16011633

1602-
const { networkController, transactionController } =
1603-
await setupController({
1604-
getSelectedAccount: () => selectedAccountMock,
1605-
isMultichainEnabled: true,
1606-
});
1634+
const {
1635+
networkController,
1636+
transactionController,
1637+
mockSelectedAccountCall,
1638+
} = await setupController({
1639+
isMultichainEnabled: true,
1640+
});
1641+
mockSelectedAccountCall.mockReturnValue(selectedAccountMock);
16071642

16081643
const expectedLastFetchedBlockNumbers: Record<string, number> = {};
16091644
const expectedTransactions: Partial<TransactionMeta>[] = [];
@@ -1668,9 +1703,9 @@ describe('TransactionController Integration', () => {
16681703
address: selectedAddress,
16691704
});
16701705

1671-
const { transactionController } = await setupController({
1672-
getSelectedAccount: () => selectedAccountMock,
1673-
});
1706+
const { transactionController, mockSelectedAccountCall } =
1707+
await setupController({});
1708+
mockSelectedAccountCall.mockReturnValue(selectedAccountMock);
16741709

16751710
mockNetwork({
16761711
networkClientConfiguration: buildInfuraNetworkClientConfiguration(

0 commit comments

Comments
 (0)