Skip to content

Commit c0f03c4

Browse files
authored
chore: Update @metamask/transaction-controller peer dependency to 42.0.0 (#482)
1 parent 8da8f78 commit c0f03c4

7 files changed

+115
-56
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Changed
9+
- **BREAKING** Update `@metamask/transaction-controller` peer dependency from `^38.0.0` to `^42.0.0` ([#482](https://github.com/MetaMask/smart-transactions-controller/pull/482))
10+
11+
### Removed
12+
- **BREAKING** Remove exports for `AllowedActions` and `AllowedEvents` types ([#482](https://github.com/MetaMask/smart-transactions-controller/pull/482))
813

914
## [15.1.0]
1015
### Changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"@metamask/gas-fee-controller": "^22.0.0",
5050
"@metamask/json-rpc-engine": "^10.0.1",
5151
"@metamask/network-controller": "^22.0.0",
52-
"@metamask/transaction-controller": "^38.0.0",
52+
"@metamask/transaction-controller": "^42.0.0",
5353
"@types/jest": "^26.0.24",
5454
"@types/lodash": "^4.14.194",
5555
"@types/node": "^18.19.17",
@@ -75,7 +75,7 @@
7575
},
7676
"peerDependencies": {
7777
"@metamask/network-controller": "^22.0.0",
78-
"@metamask/transaction-controller": "^38.0.0"
78+
"@metamask/transaction-controller": "^42.0.0"
7979
},
8080
"peerDependenciesMeta": {
8181
"@metamask/accounts-controller": {

src/SmartTransactionsController.test.ts

+22-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import {
55
ChainId,
66
} from '@metamask/controller-utils';
77
import {
8+
type NetworkControllerGetNetworkClientByIdAction,
9+
type NetworkControllerGetStateAction,
10+
type NetworkControllerStateChangeEvent,
811
NetworkStatus,
912
RpcEndpointType,
1013
type NetworkState,
@@ -25,8 +28,6 @@ import SmartTransactionsController, {
2528
getDefaultSmartTransactionsControllerState,
2629
} from './SmartTransactionsController';
2730
import type {
28-
AllowedActions,
29-
AllowedEvents,
3031
SmartTransactionsControllerActions,
3132
SmartTransactionsControllerEvents,
3233
} from './SmartTransactionsController';
@@ -1237,6 +1238,7 @@ describe('SmartTransactionsController', () => {
12371238
txParams: {
12381239
from: '0x123',
12391240
},
1241+
networkClientId: NetworkType.mainnet,
12401242
},
12411243
],
12421244
state: {
@@ -1267,6 +1269,7 @@ describe('SmartTransactionsController', () => {
12671269
txParams: {
12681270
from: '0x123',
12691271
},
1272+
networkClientId: NetworkType.mainnet,
12701273
},
12711274
'Smart transaction cancelled',
12721275
);
@@ -1294,6 +1297,7 @@ describe('SmartTransactionsController', () => {
12941297
txParams: {
12951298
from: '0x123',
12961299
},
1300+
networkClientId: NetworkType.mainnet,
12971301
},
12981302
],
12991303
},
@@ -1361,6 +1365,7 @@ describe('SmartTransactionsController', () => {
13611365
txParams: {
13621366
from: '0x123',
13631367
},
1368+
networkClientId: NetworkType.mainnet,
13641369
},
13651370
],
13661371
},
@@ -1942,8 +1947,10 @@ async function withController<ReturnValue>(
19421947
const [{ ...rest }, fn] = args.length === 2 ? args : [{}, args[0]];
19431948
const { options } = rest;
19441949
const controllerMessenger = new ControllerMessenger<
1945-
SmartTransactionsControllerActions | AllowedActions,
1946-
SmartTransactionsControllerEvents | AllowedEvents
1950+
| SmartTransactionsControllerActions
1951+
| NetworkControllerGetNetworkClientByIdAction
1952+
| NetworkControllerGetStateAction,
1953+
SmartTransactionsControllerEvents | NetworkControllerStateChangeEvent
19471954
>();
19481955
controllerMessenger.registerActionHandler(
19491956
'NetworkController:getNetworkClientById',
@@ -1969,9 +1976,19 @@ async function withController<ReturnValue>(
19691976
}),
19701977
);
19711978

1979+
controllerMessenger.registerActionHandler(
1980+
'NetworkController:getState',
1981+
jest.fn().mockReturnValue({
1982+
selectedNetworkClientId: NetworkType.mainnet,
1983+
}),
1984+
);
1985+
19721986
const messenger = controllerMessenger.getRestricted({
19731987
name: 'SmartTransactionsController',
1974-
allowedActions: ['NetworkController:getNetworkClientById'],
1988+
allowedActions: [
1989+
'NetworkController:getNetworkClientById',
1990+
'NetworkController:getState',
1991+
],
19751992
allowedEvents: ['NetworkController:stateChange'],
19761993
});
19771994

src/SmartTransactionsController.ts

+37-10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import EthQuery from '@metamask/eth-query';
1414
import type {
1515
NetworkClientId,
1616
NetworkControllerGetNetworkClientByIdAction,
17+
NetworkControllerGetStateAction,
1718
NetworkControllerStateChangeEvent,
1819
} from '@metamask/network-controller';
1920
import { StaticIntervalPollingController } from '@metamask/polling-controller';
@@ -138,7 +139,9 @@ export type SmartTransactionsControllerGetStateAction =
138139
export type SmartTransactionsControllerActions =
139140
SmartTransactionsControllerGetStateAction;
140141

141-
export type AllowedActions = NetworkControllerGetNetworkClientByIdAction;
142+
type AllowedActions =
143+
| NetworkControllerGetNetworkClientByIdAction
144+
| NetworkControllerGetStateAction;
142145

143146
export type SmartTransactionsControllerStateChangeEvent =
144147
ControllerStateChangeEvent<
@@ -164,7 +167,7 @@ export type SmartTransactionsControllerEvents =
164167
| SmartTransactionsControllerSmartTransactionEvent
165168
| SmartTransactionsControllerSmartTransactionConfirmationDoneEvent;
166169

167-
export type AllowedEvents = NetworkControllerStateChangeEvent;
170+
type AllowedEvents = NetworkControllerStateChangeEvent;
168171

169172
/**
170173
* The messenger of the {@link SmartTransactionsController}.
@@ -763,6 +766,7 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
763766
status: calculateStatus(stxStatus),
764767
cancellable: isSmartTransactionCancellable(stxStatus),
765768
uuid,
769+
networkClientId,
766770
};
767771
await this.#createOrUpdateSmartTransaction(smartTransaction, {
768772
chainId,
@@ -773,10 +777,14 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
773777
return data;
774778
}
775779

776-
async addNonceToTransaction(
780+
async #addNonceToTransaction(
777781
transaction: UnsignedTransaction,
782+
networkClientId: NetworkClientId,
778783
): Promise<UnsignedTransaction> {
779-
const nonceLock = await this.#getNonceLock(transaction.from);
784+
const nonceLock = await this.#getNonceLock(
785+
transaction.from,
786+
networkClientId,
787+
);
780788
const nonce = nonceLock.nextNonce;
781789
nonceLock.releaseLock();
782790
return {
@@ -802,12 +810,18 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
802810
approvalTx?: UnsignedTransaction,
803811
{ networkClientId }: { networkClientId?: NetworkClientId } = {},
804812
): Promise<Fees> {
805-
const chainId = this.#getChainId({ networkClientId });
813+
const selectedNetworkClientId =
814+
networkClientId ??
815+
this.messagingSystem.call('NetworkController:getState')
816+
.selectedNetworkClientId;
817+
const chainId = this.#getChainId({
818+
networkClientId: selectedNetworkClientId,
819+
});
806820
const transactions = [];
807821
let unsignedTradeTransactionWithNonce;
808822
if (approvalTx) {
809823
const unsignedApprovalTransactionWithNonce =
810-
await this.addNonceToTransaction(approvalTx);
824+
await this.#addNonceToTransaction(approvalTx, selectedNetworkClientId);
811825
transactions.push(unsignedApprovalTransactionWithNonce);
812826
unsignedTradeTransactionWithNonce = {
813827
...tradeTx,
@@ -817,8 +831,9 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
817831
} else if (tradeTx.nonce) {
818832
unsignedTradeTransactionWithNonce = tradeTx;
819833
} else {
820-
unsignedTradeTransactionWithNonce = await this.addNonceToTransaction(
834+
unsignedTradeTransactionWithNonce = await this.#addNonceToTransaction(
821835
tradeTx,
836+
selectedNetworkClientId,
822837
);
823838
}
824839
transactions.push(unsignedTradeTransactionWithNonce);
@@ -875,8 +890,16 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
875890
txParams?: TransactionParams;
876891
networkClientId?: NetworkClientId;
877892
}) {
878-
const chainId = this.#getChainId({ networkClientId });
879-
const ethQuery = this.#getEthQuery({ networkClientId });
893+
const selectedNetworkClientId =
894+
networkClientId ??
895+
this.messagingSystem.call('NetworkController:getState')
896+
.selectedNetworkClientId;
897+
const chainId = this.#getChainId({
898+
networkClientId: selectedNetworkClientId,
899+
});
900+
const ethQuery = this.#getEthQuery({
901+
networkClientId: selectedNetworkClientId,
902+
});
880903
const data = await this.#fetch(
881904
getAPIRequestURL(APIType.SUBMIT_TRANSACTIONS, chainId),
882905
{
@@ -904,7 +927,10 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
904927
let nonceDetails = {};
905928

906929
if (requiresNonce) {
907-
nonceLock = await this.#getNonceLock(txParams.from);
930+
nonceLock = await this.#getNonceLock(
931+
txParams.from,
932+
selectedNetworkClientId,
933+
);
908934
nonce = hexlify(nonceLock.nextNonce);
909935
nonceDetails = nonceLock.nonceDetails;
910936
txParams.nonce ??= nonce;
@@ -928,6 +954,7 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
928954
cancellable: true,
929955
type: transactionMeta?.type ?? 'swap',
930956
transactionId: transactionMeta?.id,
957+
networkClientId: selectedNetworkClientId,
931958
},
932959
{ chainId, ethQuery },
933960
);

src/index.test.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
import { ControllerMessenger } from '@metamask/base-controller';
2+
import {
3+
type NetworkControllerGetNetworkClientByIdAction,
4+
type NetworkControllerGetStateAction,
5+
type NetworkControllerStateChangeEvent,
6+
} from '@metamask/network-controller';
27

38
import DefaultExport, {
49
type SmartTransactionsControllerActions,
510
type SmartTransactionsControllerEvents,
611
} from '.';
7-
import SmartTransactionsController, {
8-
type AllowedActions,
9-
type AllowedEvents,
10-
} from './SmartTransactionsController';
12+
import SmartTransactionsController from './SmartTransactionsController';
1113
import { ClientId } from './types';
1214

1315
describe('default export', () => {
1416
it('exports SmartTransactionsController', () => {
1517
jest.useFakeTimers();
1618
const controllerMessenger = new ControllerMessenger<
17-
SmartTransactionsControllerActions | AllowedActions,
18-
SmartTransactionsControllerEvents | AllowedEvents
19+
| SmartTransactionsControllerActions
20+
| NetworkControllerGetNetworkClientByIdAction
21+
| NetworkControllerGetStateAction,
22+
SmartTransactionsControllerEvents | NetworkControllerStateChangeEvent
1923
>();
2024
const messenger = controllerMessenger.getRestricted({
2125
name: 'SmartTransactionsController',

src/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { NetworkClientId } from '@metamask/network-controller';
12
import type { TransactionMeta } from '@metamask/transaction-controller';
23

34
/** API */
@@ -103,6 +104,7 @@ export type SmartTransaction = {
103104
accountType?: string;
104105
deviceModel?: string;
105106
transactionId?: string; // It's an ID for a regular transaction from the TransactionController.
107+
networkClientId?: NetworkClientId;
106108
};
107109

108110
export type Fee = {

0 commit comments

Comments
 (0)