Skip to content

Commit

Permalink
Merge branch 'develop' into MMI-snapshots-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zone-live authored Feb 22, 2024
2 parents 3e4472d + 275523a commit ac9262d
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 54 deletions.
15 changes: 10 additions & 5 deletions app/scripts/lib/createRPCMethodTrackingMiddleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,16 @@ function getNext(timeout = 500) {
const waitForSeconds = async (seconds) =>
await new Promise((resolve) => setTimeout(resolve, SECOND * seconds));

jest.mock('@metamask/controller-utils', () => ({
detectSIWE: jest.fn().mockImplementation(() => {
return { isSIWEMessage: false };
}),
}));
jest.mock('@metamask/controller-utils', () => {
const actual = jest.requireActual('@metamask/controller-utils');

return {
...actual,
detectSIWE: jest.fn().mockImplementation(() => {
return { isSIWEMessage: false };
}),
};
});

describe('createRPCMethodTrackingMiddleware', () => {
afterEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/lib/middleware/pending.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function createPendingNonceMiddleware({ getPendingNonce }) {
next();
return;
}
res.result = await getPendingNonce(param);
res.result = await getPendingNonce(param, req.networkClientId);
});
}

Expand Down
66 changes: 50 additions & 16 deletions app/scripts/lib/transaction/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,26 @@ describe('Transaction Utils', () => {
).toHaveBeenCalledTimes(1);
expect(
request.transactionController.addTransaction,
).toHaveBeenCalledWith(
TRANSACTION_PARAMS_MOCK,
TRANSACTION_OPTIONS_MOCK,
);
).toHaveBeenCalledWith(TRANSACTION_PARAMS_MOCK, {
...TRANSACTION_OPTIONS_MOCK,
});
});

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 () => {
Expand Down Expand Up @@ -418,10 +434,9 @@ describe('Transaction Utils', () => {
).toHaveBeenCalledTimes(1);
expect(
request.transactionController.addTransaction,
).toHaveBeenCalledWith(
TRANSACTION_PARAMS_MOCK,
TRANSACTION_OPTIONS_MOCK,
);
).toHaveBeenCalledWith(TRANSACTION_PARAMS_MOCK, {
...TRANSACTION_OPTIONS_MOCK,
});

expect(request.ppomController.usePPOM).toHaveBeenCalledTimes(0);
});
Expand Down Expand Up @@ -484,10 +499,9 @@ describe('Transaction Utils', () => {
).toHaveBeenCalledTimes(1);
expect(
request.transactionController.addTransaction,
).toHaveBeenCalledWith(
TRANSACTION_PARAMS_MOCK,
TRANSACTION_OPTIONS_MOCK,
);
).toHaveBeenCalledWith(TRANSACTION_PARAMS_MOCK, {
...TRANSACTION_OPTIONS_MOCK,
});

expect(request.ppomController.usePPOM).toHaveBeenCalledTimes(0);
});
Expand All @@ -504,10 +518,9 @@ describe('Transaction Utils', () => {
).toHaveBeenCalledTimes(1);
expect(
request.transactionController.addTransaction,
).toHaveBeenCalledWith(
TRANSACTION_PARAMS_MOCK,
TRANSACTION_OPTIONS_MOCK,
);
).toHaveBeenCalledWith(TRANSACTION_PARAMS_MOCK, {
...TRANSACTION_OPTIONS_MOCK,
});

expect(request.ppomController.usePPOM).toHaveBeenCalledTimes(0);
});
Expand All @@ -533,6 +546,27 @@ 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
16 changes: 10 additions & 6 deletions app/scripts/lib/transaction/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,17 @@ async function addTransactionOrUserOperation(
async function addTransactionWithController(
request: FinalAddTransactionRequest,
) {
const { transactionController, transactionOptions, transactionParams } =
request;
const {
transactionController,
transactionOptions,
transactionParams,
networkClientId,
} = request;
const { result, transactionMeta } =
await transactionController.addTransaction(
transactionParams,
transactionOptions,
);
await transactionController.addTransaction(transactionParams, {
...transactionOptions,
...(process.env.TRANSACTION_MULTICHAIN ? { networkClientId } : {}),
});

return {
transactionMeta,
Expand Down
28 changes: 23 additions & 5 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,10 @@ export default class MetamaskController extends EventEmitter {
getGasFeeEstimates: this.gasFeeController.fetchGasFeeEstimates.bind(
this.gasFeeController,
),
getNetworkClientRegistry:
this.networkController.getNetworkClientRegistry.bind(
this.networkController,
),
getNetworkState: () => this.networkController.state,
getPermittedAccounts: this.getPermittedAccounts.bind(this),
getSavedGasFees: () =>
Expand All @@ -1478,6 +1482,7 @@ export default class MetamaskController extends EventEmitter {
],
getSelectedAddress: () =>
this.accountsController.getSelectedAccount().address,
isMultichainEnabled: process.env.TRANSACTION_MULTICHAIN,
incomingTransactions: {
includeTokenTransfers: false,
isEnabled: () =>
Expand All @@ -1493,7 +1498,12 @@ export default class MetamaskController extends EventEmitter {
},
messenger: this.controllerMessenger.getRestricted({
name: 'TransactionController',
allowedActions: [`${this.approvalController.name}:addRequest`],
allowedActions: [
`${this.approvalController.name}:addRequest`,
'NetworkController:findNetworkClientIdByChainId',
'NetworkController:getNetworkClientById',
],
allowedEvents: [`NetworkController:stateChange`],
}),
onNetworkStateChange: (listener) => {
networkControllerMessenger.subscribe(
Expand Down Expand Up @@ -4241,7 +4251,9 @@ export default class MetamaskController extends EventEmitter {
}) {
return {
dappRequest,
networkClientId: this.networkController.state.selectedNetworkClientId,
networkClientId:
dappRequest?.networkClientId ??
this.networkController.state.selectedNetworkClientId,
selectedAccount: this.accountsController.getSelectedAccount(),
transactionController: this.txController,
transactionOptions,
Expand Down Expand Up @@ -5308,11 +5320,13 @@ export default class MetamaskController extends EventEmitter {
* Returns the nonce that will be associated with a transaction once approved
*
* @param {string} address - The hex string address for the transaction
* @param networkClientId - The optional networkClientId to get the nonce lock with
* @returns {Promise<number>}
*/
async getPendingNonce(address) {
async getPendingNonce(address, networkClientId) {
const { nonceDetails, releaseLock } = await this.txController.getNonceLock(
address,
process.env.TRANSACTION_MULTICHAIN ? networkClientId : undefined,
);

const pendingNonce = nonceDetails.params.highestSuggested;
Expand All @@ -5325,10 +5339,14 @@ export default class MetamaskController extends EventEmitter {
* Returns the next nonce according to the nonce-tracker
*
* @param {string} address - The hex string address for the transaction
* @param networkClientId - The optional networkClientId to get the nonce lock with
* @returns {Promise<number>}
*/
async getNextNonce(address) {
const nonceLock = await this.txController.getNonceLock(address);
async getNextNonce(address, networkClientId) {
const nonceLock = await this.txController.getNonceLock(
address,
process.env.TRANSACTION_MULTICHAIN ? networkClientId : undefined,
);
nonceLock.releaseLock();
return nonceLock.nextNonce;
}
Expand Down
2 changes: 2 additions & 0 deletions builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ env:
- BLOCKAID_PUBLIC_KEY
# Determines if feature flagged Multichain UI should be used
- MULTICHAIN: ''
# Determines if feature flagged Multichain Transactions should be used
- TRANSACTION_MULTICHAIN: ''

###
# Meta variables
Expand Down
3 changes: 2 additions & 1 deletion lavamoat/browserify/beta/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"TextEncoder": true
},
"packages": {
"@metamask/assets-controllers>multiformats": true
"@ensdomains/content-hash>cids>multibase": true
}
},
"@ensdomains/content-hash>js-base64": {
Expand Down Expand Up @@ -1981,6 +1981,7 @@
"@metamask/eth-query": true,
"@metamask/gas-fee-controller": true,
"@metamask/metamask-eth-abis": true,
"@metamask/network-controller": true,
"@metamask/providers>@metamask/rpc-errors": true,
"@metamask/transaction-controller>nonce-tracker": true,
"@metamask/utils": true,
Expand Down
3 changes: 2 additions & 1 deletion lavamoat/browserify/desktop/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"TextEncoder": true
},
"packages": {
"@metamask/assets-controllers>multiformats": true
"@ensdomains/content-hash>cids>multibase": true
}
},
"@ensdomains/content-hash>js-base64": {
Expand Down Expand Up @@ -2234,6 +2234,7 @@
"@metamask/eth-query": true,
"@metamask/gas-fee-controller": true,
"@metamask/metamask-eth-abis": true,
"@metamask/network-controller": true,
"@metamask/providers>@metamask/rpc-errors": true,
"@metamask/transaction-controller>nonce-tracker": true,
"@metamask/utils": true,
Expand Down
3 changes: 2 additions & 1 deletion lavamoat/browserify/flask/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"TextEncoder": true
},
"packages": {
"@metamask/assets-controllers>multiformats": true
"@ensdomains/content-hash>cids>multibase": true
}
},
"@ensdomains/content-hash>js-base64": {
Expand Down Expand Up @@ -2268,6 +2268,7 @@
"@metamask/eth-query": true,
"@metamask/gas-fee-controller": true,
"@metamask/metamask-eth-abis": true,
"@metamask/network-controller": true,
"@metamask/providers>@metamask/rpc-errors": true,
"@metamask/transaction-controller>nonce-tracker": true,
"@metamask/utils": true,
Expand Down
3 changes: 2 additions & 1 deletion lavamoat/browserify/main/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"TextEncoder": true
},
"packages": {
"@metamask/assets-controllers>multiformats": true
"@ensdomains/content-hash>cids>multibase": true
}
},
"@ensdomains/content-hash>js-base64": {
Expand Down Expand Up @@ -2191,6 +2191,7 @@
"@metamask/eth-query": true,
"@metamask/gas-fee-controller": true,
"@metamask/metamask-eth-abis": true,
"@metamask/network-controller": true,
"@metamask/providers>@metamask/rpc-errors": true,
"@metamask/transaction-controller>nonce-tracker": true,
"@metamask/utils": true,
Expand Down
3 changes: 2 additions & 1 deletion lavamoat/browserify/mmi/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"TextEncoder": true
},
"packages": {
"@metamask/assets-controllers>multiformats": true
"@ensdomains/content-hash>cids>multibase": true
}
},
"@ensdomains/content-hash>js-base64": {
Expand Down Expand Up @@ -2289,6 +2289,7 @@
"@metamask/eth-query": true,
"@metamask/gas-fee-controller": true,
"@metamask/metamask-eth-abis": true,
"@metamask/network-controller": true,
"@metamask/providers>@metamask/rpc-errors": true,
"@metamask/transaction-controller>nonce-tracker": true,
"@metamask/utils": true,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@
"dependencies": {
"@babel/runtime": "^7.23.2",
"@blockaid/ppom_release": "^1.4.1",
"@ensdomains/content-hash": "^2.5.6",
"@ensdomains/content-hash": "^2.5.7",
"@ethereumjs/common": "^3.1.1",
"@ethereumjs/tx": "^4.1.1",
"@ethersproject/abi": "^5.6.4",
Expand Down Expand Up @@ -298,7 +298,7 @@
"@metamask/snaps-rpc-methods": "^6.0.0",
"@metamask/snaps-sdk": "^2.1.0",
"@metamask/snaps-utils": "^6.1.0",
"@metamask/transaction-controller": "^22.0.0",
"@metamask/transaction-controller": "^23.0.0",
"@metamask/user-operation-controller": "^1.0.0",
"@metamask/utils": "^8.2.1",
"@ngraveio/bc-ur": "^1.1.6",
Expand Down
3 changes: 3 additions & 0 deletions ui/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
///: END:ONLY_INCLUDE_IF
getInternalAccountByAddress,
getSelectedInternalAccount,
getSelectedNetworkClientId,
} from '../selectors';
import {
computeEstimatedGasLimit,
Expand Down Expand Up @@ -4087,10 +4088,12 @@ export function getNextNonce(): ThunkAction<
> {
return async (dispatch, getState) => {
const { address } = getSelectedInternalAccount(getState());
const networkClientId = getSelectedNetworkClientId(getState());
let nextNonce;
try {
nextNonce = await submitRequestToBackground<string>('getNextNonce', [
address,
networkClientId,
]);
} catch (error) {
dispatch(displayWarning(error));
Expand Down
Loading

0 comments on commit ac9262d

Please sign in to comment.