Skip to content

Commit

Permalink
chore: Update @metamask/transaction-controller peer dependency to 42.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptodev-2s committed Dec 12, 2024
1 parent 739da76 commit e375543
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 56 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- **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))

## [15.0.0]
### Changed
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@metamask/gas-fee-controller": "^22.0.0",
"@metamask/json-rpc-engine": "^10.0.1",
"@metamask/network-controller": "^22.0.0",
"@metamask/transaction-controller": "^38.0.0",
"@metamask/transaction-controller": "^42.0.0",
"@types/jest": "^26.0.24",
"@types/lodash": "^4.14.194",
"@types/node": "^18.19.17",
Expand All @@ -75,7 +75,7 @@
},
"peerDependencies": {
"@metamask/network-controller": "^22.0.0",
"@metamask/transaction-controller": "^38.0.0"
"@metamask/transaction-controller": "^42.0.0"
},
"peerDependenciesMeta": {
"@metamask/accounts-controller": {
Expand Down
27 changes: 22 additions & 5 deletions src/SmartTransactionsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import {
ChainId,
} from '@metamask/controller-utils';
import {
type NetworkControllerGetNetworkClientByIdAction,
type NetworkControllerGetStateAction,
type NetworkControllerStateChangeEvent,
NetworkStatus,
RpcEndpointType,
type NetworkState,
Expand All @@ -25,8 +28,6 @@ import SmartTransactionsController, {
getDefaultSmartTransactionsControllerState,
} from './SmartTransactionsController';
import type {
AllowedActions,
AllowedEvents,
SmartTransactionsControllerActions,
SmartTransactionsControllerEvents,
} from './SmartTransactionsController';
Expand Down Expand Up @@ -1237,6 +1238,7 @@ describe('SmartTransactionsController', () => {
txParams: {
from: '0x123',
},
networkClientId: 'selectedNetworkClientId',
},
],
state: {
Expand Down Expand Up @@ -1267,6 +1269,7 @@ describe('SmartTransactionsController', () => {
txParams: {
from: '0x123',
},
networkClientId: 'selectedNetworkClientId',
},
'Smart transaction cancelled',
);
Expand Down Expand Up @@ -1294,6 +1297,7 @@ describe('SmartTransactionsController', () => {
txParams: {
from: '0x123',
},
networkClientId: NetworkType.mainnet,
},
],
},
Expand Down Expand Up @@ -1361,6 +1365,7 @@ describe('SmartTransactionsController', () => {
txParams: {
from: '0x123',
},
networkClientId: NetworkType.mainnet,
},
],
},
Expand Down Expand Up @@ -1942,8 +1947,10 @@ async function withController<ReturnValue>(
const [{ ...rest }, fn] = args.length === 2 ? args : [{}, args[0]];
const { options } = rest;
const controllerMessenger = new ControllerMessenger<
SmartTransactionsControllerActions | AllowedActions,
SmartTransactionsControllerEvents | AllowedEvents
| SmartTransactionsControllerActions
| NetworkControllerGetNetworkClientByIdAction
| NetworkControllerGetStateAction,
SmartTransactionsControllerEvents | NetworkControllerStateChangeEvent
>();
controllerMessenger.registerActionHandler(
'NetworkController:getNetworkClientById',
Expand All @@ -1969,9 +1976,19 @@ async function withController<ReturnValue>(
}),
);

controllerMessenger.registerActionHandler(
'NetworkController:getState',
jest.fn().mockReturnValue({
selectedNetworkClientId: NetworkType.mainnet,
}),
);

const messenger = controllerMessenger.getRestricted({
name: 'SmartTransactionsController',
allowedActions: ['NetworkController:getNetworkClientById'],
allowedActions: [
'NetworkController:getNetworkClientById',
'NetworkController:getState',
],
allowedEvents: ['NetworkController:stateChange'],
});

Expand Down
47 changes: 37 additions & 10 deletions src/SmartTransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import EthQuery from '@metamask/eth-query';
import type {
NetworkClientId,
NetworkControllerGetNetworkClientByIdAction,
NetworkControllerGetStateAction,
NetworkControllerStateChangeEvent,
} from '@metamask/network-controller';
import { StaticIntervalPollingController } from '@metamask/polling-controller';
Expand Down Expand Up @@ -138,7 +139,9 @@ export type SmartTransactionsControllerGetStateAction =
export type SmartTransactionsControllerActions =
SmartTransactionsControllerGetStateAction;

export type AllowedActions = NetworkControllerGetNetworkClientByIdAction;
type AllowedActions =
| NetworkControllerGetNetworkClientByIdAction
| NetworkControllerGetStateAction;

export type SmartTransactionsControllerStateChangeEvent =
ControllerStateChangeEvent<
Expand All @@ -164,7 +167,7 @@ export type SmartTransactionsControllerEvents =
| SmartTransactionsControllerSmartTransactionEvent
| SmartTransactionsControllerSmartTransactionConfirmationDoneEvent;

export type AllowedEvents = NetworkControllerStateChangeEvent;
type AllowedEvents = NetworkControllerStateChangeEvent;

/**
* The messenger of the {@link SmartTransactionsController}.
Expand Down Expand Up @@ -763,6 +766,7 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
status: calculateStatus(stxStatus),
cancellable: isSmartTransactionCancellable(stxStatus),
uuid,
networkClientId,
};
await this.#createOrUpdateSmartTransaction(smartTransaction, {
chainId,
Expand All @@ -773,10 +777,14 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
return data;
}

async addNonceToTransaction(
async #addNonceToTransaction(
transaction: UnsignedTransaction,
networkClientId: NetworkClientId,
): Promise<UnsignedTransaction> {
const nonceLock = await this.#getNonceLock(transaction.from);
const nonceLock = await this.#getNonceLock(
transaction.from,
networkClientId,
);
const nonce = nonceLock.nextNonce;
nonceLock.releaseLock();
return {
Expand All @@ -802,12 +810,18 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
approvalTx?: UnsignedTransaction,
{ networkClientId }: { networkClientId?: NetworkClientId } = {},
): Promise<Fees> {
const chainId = this.#getChainId({ networkClientId });
const selectedNetworkClientId =
networkClientId ??
this.messagingSystem.call('NetworkController:getState')
.selectedNetworkClientId;
const chainId = this.#getChainId({
networkClientId: selectedNetworkClientId,
});
const transactions = [];
let unsignedTradeTransactionWithNonce;
if (approvalTx) {
const unsignedApprovalTransactionWithNonce =
await this.addNonceToTransaction(approvalTx);
await this.#addNonceToTransaction(approvalTx, selectedNetworkClientId);
transactions.push(unsignedApprovalTransactionWithNonce);
unsignedTradeTransactionWithNonce = {
...tradeTx,
Expand All @@ -817,8 +831,9 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
} else if (tradeTx.nonce) {
unsignedTradeTransactionWithNonce = tradeTx;
} else {
unsignedTradeTransactionWithNonce = await this.addNonceToTransaction(
unsignedTradeTransactionWithNonce = await this.#addNonceToTransaction(
tradeTx,
selectedNetworkClientId,
);
}
transactions.push(unsignedTradeTransactionWithNonce);
Expand Down Expand Up @@ -875,8 +890,16 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
txParams?: TransactionParams;
networkClientId?: NetworkClientId;
}) {
const chainId = this.#getChainId({ networkClientId });
const ethQuery = this.#getEthQuery({ networkClientId });
const selectedNetworkClientId =
networkClientId ??
this.messagingSystem.call('NetworkController:getState')
.selectedNetworkClientId;
const chainId = this.#getChainId({
networkClientId: selectedNetworkClientId,
});
const ethQuery = this.#getEthQuery({
networkClientId: selectedNetworkClientId,
});
const data = await this.#fetch(
getAPIRequestURL(APIType.SUBMIT_TRANSACTIONS, chainId),
{
Expand Down Expand Up @@ -904,7 +927,10 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
let nonceDetails = {};

if (requiresNonce) {
nonceLock = await this.#getNonceLock(txParams.from);
nonceLock = await this.#getNonceLock(
txParams.from,
selectedNetworkClientId,
);
nonce = hexlify(nonceLock.nextNonce);
nonceDetails = nonceLock.nonceDetails;
txParams.nonce ??= nonce;
Expand All @@ -928,6 +954,7 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
cancellable: true,
type: transactionMeta?.type ?? 'swap',
transactionId: transactionMeta?.id,
networkClientId: selectedNetworkClientId,
},
{ chainId, ethQuery },
);
Expand Down
16 changes: 10 additions & 6 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { ControllerMessenger } from '@metamask/base-controller';
import {
type NetworkControllerGetNetworkClientByIdAction,
type NetworkControllerGetStateAction,
type NetworkControllerStateChangeEvent,
} from '@metamask/network-controller';

import DefaultExport, {
type SmartTransactionsControllerActions,
type SmartTransactionsControllerEvents,
} from '.';
import SmartTransactionsController, {
type AllowedActions,
type AllowedEvents,
} from './SmartTransactionsController';
import SmartTransactionsController from './SmartTransactionsController';
import { ClientId } from './types';

describe('default export', () => {
it('exports SmartTransactionsController', () => {
jest.useFakeTimers();
const controllerMessenger = new ControllerMessenger<
SmartTransactionsControllerActions | AllowedActions,
SmartTransactionsControllerEvents | AllowedEvents
| SmartTransactionsControllerActions
| NetworkControllerGetNetworkClientByIdAction
| NetworkControllerGetStateAction,
SmartTransactionsControllerEvents | NetworkControllerStateChangeEvent
>();
const messenger = controllerMessenger.getRestricted({
name: 'SmartTransactionsController',
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { NetworkClientId } from '@metamask/network-controller';
import type { TransactionMeta } from '@metamask/transaction-controller';

/** API */
Expand Down Expand Up @@ -103,6 +104,7 @@ export type SmartTransaction = {
accountType?: string;
deviceModel?: string;
transactionId?: string; // It's an ID for a regular transaction from the TransactionController.
networkClientId?: NetworkClientId;
};

export type Fee = {
Expand Down
Loading

0 comments on commit e375543

Please sign in to comment.