Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multichain debug #28402

Merged
5 changes: 3 additions & 2 deletions test/e2e/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ async function withFixtures(options, testSuite) {
contractRegistry = ganacheSeeder.getContractRegistry();
}

await fixtureServer.start();
fixtureServer.loadJsonState(fixtures, contractRegistry);

if (ganacheOptions?.concurrent) {
ganacheOptions.concurrent.forEach(async (ganacheSettings) => {
const { port, chainId, ganacheOptions2 } = ganacheSettings;
Expand All @@ -138,8 +141,6 @@ async function withFixtures(options, testSuite) {
await initBundler(bundlerServer, ganacheServer, usePaymaster);
}

await fixtureServer.start();
fixtureServer.loadJsonState(fixtures, contractRegistry);
await phishingPageServer.start();
if (dapp) {
if (dappOptions?.numberOfDapps) {
Expand Down
146 changes: 146 additions & 0 deletions test/e2e/mock-balance-polling/mock-balance-polling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import { MockttpServer } from 'mockttp';

const CONTRACT_ADDRESS = {
BalanceCheckerEthereumMainnet: '0xb1f8e55c7f64d203c1400b9d8555d050f94adf39',
BalanceCheckerLineaMainnet: '0xf62e6a41561b3650a69bb03199c735e3e3328c0d',
};

const infuraUrl: string =
'https://mainnet.infura.io/v3/00000000000000000000000000000000';
const infuraLineaMainnetUrl: string =
'https://linea-mainnet.infura.io/v3/00000000000000000000000000000000';
const infuraLineaSepoliaUrl: string =
'https://linea-sepolia.infura.io/v3/00000000000000000000000000000000';
const infuraSepoliaUrl: string =
'https://sepolia.infura.io/v3/00000000000000000000000000000000';

/**
* Mocks multi network balance polling requests.
*
* @param mockServer - The mock server instance to set up the mocks on.
* @returns A promise that resolves when all mocks have been set up.
*/
export async function mockMultiNetworkBalancePolling(
mockServer: MockttpServer,
): Promise<void> {
await mockServer
.forPost(infuraUrl)
.withJsonBodyIncluding({ method: 'eth_getBalance' })
.thenCallback(() => ({
statusCode: 200,
json: {
jsonrpc: '2.0',
id: '1111111111111111',
result: '0x1158E460913D00000',
},
}));

await mockServer
.forPost(infuraSepoliaUrl)
.withJsonBodyIncluding({ method: 'eth_getBalance' })
.thenCallback(() => ({
statusCode: 200,
json: {
jsonrpc: '2.0',
id: '6183194981233610',
result: '0x1158E460913D00000',
},
}));

await mockServer
.forPost(infuraLineaMainnetUrl)
.withJsonBodyIncluding({ method: 'eth_getBalance' })
.thenCallback(() => ({
statusCode: 200,
json: {
jsonrpc: '2.0',
id: '6183194981233610',
result: '0x1158E460913D00000',
},
}));

await mockServer
.forPost(infuraLineaSepoliaUrl)
.withJsonBodyIncluding({ method: 'eth_getBalance' })
.thenCallback(() => ({
statusCode: 200,
json: {
jsonrpc: '2.0',
id: '6183194981233610',
result: '0x1158E460913D00000',
},
}));

await mockServer
.forPost(infuraUrl)
.withJsonBodyIncluding({ method: 'net_version' })
.thenCallback(() => ({
statusCode: 200,
json: {
jsonrpc: '2.0',
id: '6183194981233610',
result: '0x1',
},
}));

await mockServer
.forPost(infuraLineaMainnetUrl)
.withJsonBodyIncluding({ method: 'eth_call' })
.thenCallback(() => ({
statusCode: 200,
json: {
jsonrpc: '2.0',
id: '1111111111111111',
result:
'0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000001158E460913D000000000000000000000000000000000000000000000000000000001699651aa88200000000000000000000000000000000000000000000000000001beca58919dc0000000000000000000000000000000000000000000000000000974189179054f0000000000000000000000000000000000000000000000000001d9ae54845818000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000110d9316ec0000000000000000000000000000000000000000000000000000000000000000000',
},
}));

await mockServer
.forPost(infuraUrl)
.withJsonBodyIncluding({
method: 'eth_call',
params: [
{
to: CONTRACT_ADDRESS.BalanceCheckerEthereumMainnet,
},
],
})
.thenCallback(() => ({
statusCode: 200,
json: {
jsonrpc: '2.0',
id: '6183194981233610',
result:
'0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000001158E460913D000000000000000000000000000000000000000000000000000000001699651aa88200000000000000000000000000000000000000000000000000001beca58919dc0000000000000000000000000000000000000000000000000000974189179054f0000000000000000000000000000000000000000000000000001d9ae54845818000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000110d9316ec0000000000000000000000000000000000000000000000000000000000000000000',
},
}));

await mockServer
.forGet(
'https://accounts.api.cx.metamask.io/v2/accounts/0x5cfe73b6021e818b776b421b1c4db2474086a7e1/balances',
)
.withQuery({
networks: 1,
})
.thenCallback(() => ({
statusCode: 200,
json: {
count: 0,
balances: [
{
object: 'token',
address: '0x0000000000000000000000000000000000000000',
symbol: 'ETH',
name: 'Ether',
type: 'native',
timestamp: '2015-07-30T03:26:13.000Z',
decimals: 18,
chainId: 1,
balance: '20',
},
],
unprocessedNetworks: [],
},
}));
}
2 changes: 2 additions & 0 deletions test/e2e/mock-e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const blacklistedHosts = [
'goerli.infura.io',
'mainnet.infura.io',
'sepolia.infura.io',
'linea-mainnet.infura.io',
'linea-sepolia.infura.io',
];
const {
mockEmptyStalelistAndHotlist,
Expand Down
1 change: 0 additions & 1 deletion test/e2e/page-objects/flows/login.flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const loginWithoutBalanceValidation = async (
await driver.navigate();
const loginPage = new LoginPage(driver);
await loginPage.check_pageIsLoaded();
await driver.delay(10000);
await loginPage.loginToHomepage(password);
};

Expand Down
5 changes: 4 additions & 1 deletion test/e2e/tests/ppom/ppom-blockaid-alert-simple-send.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const {
logInWithBalanceValidation,
WINDOW_TITLES,
} = require('../../helpers');
const {
mockMultiNetworkBalancePolling,
} = require('../../mock-balance-polling/mock-balance-polling');
const { SECURITY_ALERTS_PROD_API_BASE_URL } = require('./constants');
const { mockServerJsonRpc } = require('./mocks/mock-server-json-rpc');

Expand All @@ -32,13 +35,13 @@ const SEND_REQUEST_BASE_MOCK = {
};

async function mockInfura(mockServer) {
await mockMultiNetworkBalancePolling(mockServer);
await mockServerJsonRpc(mockServer, [
['eth_blockNumber'],
['eth_call'],
['eth_estimateGas'],
['eth_feeHistory'],
['eth_gasPrice'],
['eth_getBalance'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed, as we are mocking the balances using the mockMultiNetworkBalancePolling

['eth_getBlockByNumber'],
['eth_getCode'],
['eth_getTransactionCount'],
Expand Down
20 changes: 9 additions & 11 deletions test/e2e/tests/smart-transactions/mock-requests-for-swap-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { MockttpServer } from 'mockttp';
import { mockEthDaiTrade } from '../swaps/shared';
import { mockMultiNetworkBalancePolling } from '../../mock-balance-polling/mock-balance-polling';
import { mockServerJsonRpc } from '../ppom/mocks/mock-server-json-rpc';

const STX_UUID = '0d506aaa-5e38-4cab-ad09-2039cb7a0f33';

Expand Down Expand Up @@ -288,18 +290,14 @@ const GET_TRANSACTION_BY_HASH_RESPONSE = {
};

export const mockSwapRequests = async (mockServer: MockttpServer) => {
await mockEthDaiTrade(mockServer);
await mockMultiNetworkBalancePolling(mockServer);

await mockServer
.forJsonRpcRequest({
method: 'eth_getBalance',
params: ['0x5cfe73b6021e818b776b421b1c4db2474086a7e1'],
})
.thenJson(200, {
id: 3806592044086814,
jsonrpc: '2.0',
result: '0x1bc16d674ec80000', // 2 ETH
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed, as we are mocking the balances using the mockMultiNetworkBalancePolling

await mockServerJsonRpc(mockServer, [
['eth_blockNumber'],
['eth_getBlockByNumber'],
['eth_chainId', { result: `0x1` }],
]);
await mockEthDaiTrade(mockServer);

await mockServer
.forPost('https://transaction.api.cx.metamask.io/networks/1/getFees')
Expand Down
4 changes: 1 addition & 3 deletions test/e2e/tests/smart-transactions/smart-transactions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
import FixtureBuilder from '../../fixture-builder';
import { unlockWallet, withFixtures } from '../../helpers';
import { Driver } from '../../webdriver/driver';
import { CHAIN_IDS } from '../../../../shared/constants/network';
import { mockSwapRequests } from './mock-requests-for-swap-test';

export async function withFixturesForSmartTransactions(
Expand All @@ -20,10 +19,9 @@ export async function withFixturesForSmartTransactions(
},
test: (args: { driver: Driver }) => Promise<void>,
) {
const inputChainId = CHAIN_IDS.MAINNET;
await withFixtures(
{
fixtures: new FixtureBuilder({ inputChainId })
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not needed and creates a miss-match with networks, causing to send network requests to localhost
we are using the fitxutre withEthereumMainnet, so we shouldn't change anything from the localhost default fixture

https://github.com/MetaMask/metamask-extension/blob/develop/test/e2e/default-fixture.js#L174

(I think we should also remove this from the default fixture to avoid future network miss-matches, but we can leave this out of scope from this PR)

fixtures: new FixtureBuilder()
.withPermissionControllerConnectedToTestDapp()
.withPreferencesControllerSmartTransactionsOptedIn()
.withNetworkControllerOnMainnet()
Expand Down
Loading