Skip to content

Commit

Permalink
refactor: remove global network from transaction controller (#28449)
Browse files Browse the repository at this point in the history
## **Description**

Upgrade `@metamask/transaction-controller` to remove all usages of the
global network.

Specifically:

- Remove deleted constructor options.
- Add `getGlobalChainId` and `getGlobalNetworkClientId` private methods
in `MetamaskController`.
- Remove `TRANSACTION_MULTICHAIN` environment variable.
- Add `networkClientId` to test data.
- Update calls to:
  - `addTransaction`
  - `estimateGasBuffered`
  - `getNonceLock`
  - `getTransactions`
  - `startIncomingTransactionPolling`
  - `updateIncomingTransactions`
  - `wipeTransactions`

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/28449?quickstart=1)

## **Related issues**

Fixes:
[#3499](MetaMask/MetaMask-planning#3499)

## **Manual testing steps**

Full regression of all transaction flows.

## **Screenshots/Recordings**

### **Before**

### **After**

## **Pre-merge author checklist**

- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
  • Loading branch information
matthewwalsh0 authored Nov 29, 2024
1 parent 5e52a7d commit 7d252e9
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 105 deletions.
2 changes: 1 addition & 1 deletion app/scripts/controllers/mmi-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ export class MMIController {
}
}

const txList = this.txStateManager.getTransactions({}, [], false); // Includes all transactions, but we are looping through keyrings. Currently filtering is done in updateCustodianTransactions :-/
const txList = this.txStateManager.getTransactions(); // Includes all transactions, but we are looping through keyrings. Currently filtering is done in updateCustodianTransactions :-/

try {
updateCustodianTransactions({
Expand Down
1 change: 1 addition & 0 deletions app/scripts/lib/transaction/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ describe('Transaction metrics', () => {
type: TransactionType.simpleSend,
origin: ORIGIN_METAMASK,
chainId: mockChainId,
networkClientId: 'testNetworkClientId',
time: 1624408066355,
defaultGasEstimates: {
gas: '0x7b0d',
Expand Down
1 change: 1 addition & 0 deletions app/scripts/lib/transaction/smart-transactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ function withRequest<ReturnValue>(
},
type: TransactionType.simpleSend,
chainId: CHAIN_IDS.MAINNET,
networkClientId: 'testNetworkClientId',
time: 1624408066355,
defaultGasEstimates: {
gas: '0x7b0d',
Expand Down
39 changes: 1 addition & 38 deletions app/scripts/lib/transaction/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const TRANSACTION_PARAMS_MOCK: TransactionParams = {

const TRANSACTION_OPTIONS_MOCK: AddTransactionOptions = {
actionId: 'mockActionId',
networkClientId: 'mockNetworkClientId',
origin: 'mockOrigin',
requireApproval: false,
type: TransactionType.simpleSend,
Expand Down Expand Up @@ -151,23 +152,6 @@ describe('Transaction Utils', () => {
});
});

it('adds transaction with networkClientId if process.env.TRANSACTION_MULTICHAIN is set', async () => {
process.env.TRANSACTION_MULTICHAIN = '1';

await addTransaction(request);

expect(
request.transactionController.addTransaction,
).toHaveBeenCalledTimes(1);
expect(
request.transactionController.addTransaction,
).toHaveBeenCalledWith(TRANSACTION_PARAMS_MOCK, {
...TRANSACTION_OPTIONS_MOCK,
networkClientId: 'mockNetworkClientId',
});
process.env.TRANSACTION_MULTICHAIN = '';
});

it('returns transaction meta', async () => {
const transactionMeta = await addTransaction(request);
expect(transactionMeta).toStrictEqual(TRANSACTION_META_MOCK);
Expand Down Expand Up @@ -541,27 +525,6 @@ describe('Transaction Utils', () => {
});
});

it('adds transaction with networkClientId if process.env.TRANSACTION_MULTICHAIN is set', async () => {
process.env.TRANSACTION_MULTICHAIN = '1';

await addDappTransaction(dappRequest);

expect(
request.transactionController.addTransaction,
).toHaveBeenCalledTimes(1);
expect(
request.transactionController.addTransaction,
).toHaveBeenCalledWith(TRANSACTION_PARAMS_MOCK, {
...TRANSACTION_OPTIONS_MOCK,
networkClientId: 'mockNetworkClientId',
method: DAPP_REQUEST_MOCK.method,
requireApproval: true,
securityAlertResponse: DAPP_REQUEST_MOCK.securityAlertResponse,
type: undefined,
});
process.env.TRANSACTION_MULTICHAIN = '';
});

it('returns transaction hash', async () => {
const transactionHash = await addDappTransaction(dappRequest);
expect(transactionHash).toStrictEqual(TRANSACTION_META_MOCK.hash);
Expand Down
7 changes: 4 additions & 3 deletions app/scripts/lib/transaction/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type BaseAddTransactionRequest = {
};

type FinalAddTransactionRequest = BaseAddTransactionRequest & {
transactionOptions: AddTransactionOptions;
transactionOptions: Partial<AddTransactionOptions>;
};

export type AddTransactionRequest = FinalAddTransactionRequest & {
Expand All @@ -66,7 +66,7 @@ export async function addDappTransaction(
const { id: actionId, method, origin } = dappRequest;
const { securityAlertResponse, traceContext } = dappRequest;

const transactionOptions: AddTransactionOptions = {
const transactionOptions: Partial<AddTransactionOptions> = {
actionId,
method,
origin,
Expand Down Expand Up @@ -143,10 +143,11 @@ async function addTransactionWithController(
transactionParams,
networkClientId,
} = request;

const { result, transactionMeta } =
await transactionController.addTransaction(transactionParams, {
...transactionOptions,
...(process.env.TRANSACTION_MULTICHAIN ? { networkClientId } : {}),
networkClientId,
});

return {
Expand Down
Loading

0 comments on commit 7d252e9

Please sign in to comment.