Skip to content

Commit

Permalink
Merge branch 'develop' into fix/fix-reading-undefined-from-market-data
Browse files Browse the repository at this point in the history
  • Loading branch information
sahar-fehri authored Oct 3, 2024
2 parents 04196d0 + 537b3fe commit 2d4de6e
Show file tree
Hide file tree
Showing 20 changed files with 543 additions and 50 deletions.
7 changes: 7 additions & 0 deletions app/scripts/controllers/app-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default class AppStateController extends EventEmitter {
trezorModel: null,
currentPopupId: undefined,
onboardingDate: null,
lastViewedUserSurvey: null,
newPrivacyPolicyToastClickedOrClosed: null,
newPrivacyPolicyToastShownDate: null,
// This key is only used for checking if the user had set advancedGasFee
Expand Down Expand Up @@ -198,6 +199,12 @@ export default class AppStateController extends EventEmitter {
});
}

setLastViewedUserSurvey(id) {
this.store.updateState({
lastViewedUserSurvey: id,
});
}

setNewPrivacyPolicyToastClickedOrClosed() {
this.store.updateState({
newPrivacyPolicyToastClickedOrClosed: true,
Expand Down
2 changes: 2 additions & 0 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3528,6 +3528,8 @@ export default class MetamaskController extends EventEmitter {
),
setOnboardingDate:
appStateController.setOnboardingDate.bind(appStateController),
setLastViewedUserSurvey:
appStateController.setLastViewedUserSurvey.bind(appStateController),
setNewPrivacyPolicyToastClickedOrClosed:
appStateController.setNewPrivacyPolicyToastClickedOrClosed.bind(
appStateController,
Expand Down
45 changes: 33 additions & 12 deletions app/scripts/offscreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ import {
} from '../../shared/constants/offscreen-communication';
import { getSocketBackgroundToMocha } from '../../test/e2e/background-socket/socket-background-to-mocha';

/**
* Returns whether the offscreen document already exists or not.
*
* See https://developer.chrome.com/docs/extensions/reference/api/offscreen#before_chrome_116_check_if_an_offscreen_document_is_open
*
* @returns True if the offscreen document already is has been opened, otherwise false.
*/
async function hasOffscreenDocument() {
const { chrome, clients } = globalThis;
// getContexts is only available in Chrome 116+
if ('getContexts' in chrome.runtime) {
const contexts = await chrome.runtime.getContexts({
contextTypes: ['OFFSCREEN_DOCUMENT'],
});
return contexts.length > 0;
}
const matchedClients = await clients.matchAll();
const url = chrome.runtime.getURL('offscreen.html');
return matchedClients.some((client) => client.url === url);
}

/**
* Creates an offscreen document that can be used to load additional scripts
* and iframes that can communicate with the extension through the chrome
Expand Down Expand Up @@ -41,6 +62,14 @@ export async function createOffscreen() {
});

try {
const offscreenExists = await hasOffscreenDocument();

// In certain cases the offscreen document may already exist during boot, if it does, we close it and recreate it.
if (offscreenExists) {
console.debug('Found existing offscreen document, closing.');
await chrome.offscreen.closeDocument();
}

await chrome.offscreen.createDocument({
url: './offscreen.html',
reasons: ['IFRAME_SCRIPTING'],
Expand All @@ -51,18 +80,10 @@ export async function createOffscreen() {
if (offscreenDocumentLoadedListener) {
chrome.runtime.onMessage.removeListener(offscreenDocumentLoadedListener);
}
if (
error?.message?.startsWith(
'Only a single offscreen document may be created',
)
) {
console.debug('Offscreen document already exists; skipping creation');
} else {
// Report unrecongized errors without halting wallet initialization
// Failures to create the offscreen document does not compromise wallet data integrity or
// core functionality, it's just needed for specific features.
captureException(error);
}
// Report unrecongized errors without halting wallet initialization
// Failures to create the offscreen document does not compromise wallet data integrity or
// core functionality, it's just needed for specific features.
captureException(error);
return;
}

Expand Down
1 change: 1 addition & 0 deletions builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ features:
# env object supports both declarations (- FOO), and definitions (- FOO: BAR).
# Variables that were declared have to be defined somewhere in the load chain before usage
env:
- ACCOUNTS_USE_DEV_APIS: false
- BRIDGE_USE_DEV_APIS: false
- SWAPS_USE_DEV_APIS: false
- PORTFOLIO_URL: https://portfolio.metamask.io
Expand Down
21 changes: 10 additions & 11 deletions privacy-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
[
"accounts.api.cx.metamask.io",
"acl.execution.metamask.io",
"api.blockchair.com",
"api.lens.dev",
"api.segment.io",
"api.web3modal.com",
"app.ens.domains",
"arbitrum-mainnet.infura.io",
"authentication.api.cx.metamask.io",
"bafkreifvhjdf6ve4jfv6qytqtux5nd4nwnelioeiqx5x2ez5yrgrzk7ypi.ipfs.dweb.link",
"bafybeidxfmwycgzcp4v2togflpqh2gnibuexjy4m4qqwxp7nh3jx5zlh4y.ipfs.dweb.link",
"bridge.api.cx.metamask.io",
"cdn.segment.com",
"cdn.segment.io",
"cdnjs.cloudflare.com",
"chainid.network",
"client-side-detection.api.cx.metamask.io",
"configuration.dev.metamask-institutional.io",
"configuration.metamask-institutional.io",
"connect.trezor.io",
Expand All @@ -24,18 +27,20 @@
"gas.api.cx.metamask.io",
"github.com",
"goerli.infura.io",
"lattice.gridplus.io",
"localhost:8000",
"localhost:8545",
"mainnet.infura.io",
"metamask.eth",
"metamask.github.io",
"min-api.cryptocompare.com",
"nft.api.cx.metamask.io",
"oidc.api.cx.metamask.io",
"on-ramp-content.api.cx.metamask.io",
"on-ramp-content.uat-api.cx.metamask.io",
"phishing-detection.api.cx.metamask.io",
"portfolio.metamask.io",
"price.api.cx.metamask.io",
"on-ramp-content.api.cx.metamask.io",
"on-ramp-content.uat-api.cx.metamask.io",
"proxy.api.cx.metamask.io",
"raw.githubusercontent.com",
"registry.npmjs.org",
Expand All @@ -50,14 +55,8 @@
"token.api.cx.metamask.io",
"tokens.api.cx.metamask.io",
"tx-sentinel-ethereum-mainnet.api.cx.metamask.io",
"unresponsive-rpc.url",
"www.4byte.directory",
"lattice.gridplus.io",
"unresponsive-rpc.test",
"authentication.api.cx.metamask.io",
"oidc.api.cx.metamask.io",
"price.api.cx.metamask.io",
"token.api.cx.metamask.io",
"client-side-detection.api.cx.metamask.io",
"user-storage.api.cx.metamask.io"
"unresponsive-rpc.url",
"user-storage.api.cx.metamask.io",
"www.4byte.directory"
]
6 changes: 6 additions & 0 deletions shared/constants/accounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const ACCOUNTS_DEV_API_BASE_URL =
'https://accounts.dev-api.cx.metamask.io';
export const ACCOUNTS_PROD_API_BASE_URL = 'https://accounts.api.cx.metamask.io';
export const ACCOUNTS_API_BASE_URL = process.env.ACCOUNTS_USE_DEV_APIS
? ACCOUNTS_DEV_API_BASE_URL
: ACCOUNTS_PROD_API_BASE_URL;
2 changes: 2 additions & 0 deletions shared/constants/metametrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ export enum MetaMetricsEventName {
SrpCopiedToClipboard = 'Copies SRP to clipboard',
SrpToConfirmBackup = 'SRP Backup Confirm Displayed',
StakingEntryPointClicked = 'Stake Button Clicked',
SurveyToast = 'Survey Toast',
SupportLinkClicked = 'Support Link Clicked',
TermsOfUseShown = 'Terms of Use Shown',
TermsOfUseAccepted = 'Terms of Use Accepted',
Expand Down Expand Up @@ -779,6 +780,7 @@ export enum MetaMetricsEventCategory {
Retention = 'Retention',
Send = 'Send',
Settings = 'Settings',
Feedback = 'Feedback',
Snaps = 'Snaps',
Swaps = 'Swaps',
Tokens = 'Tokens',
Expand Down
50 changes: 50 additions & 0 deletions test/e2e/mock-e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const {
BRIDGE_DEV_API_BASE_URL,
BRIDGE_PROD_API_BASE_URL,
} = require('../../shared/constants/bridge');
const {
ACCOUNTS_DEV_API_BASE_URL,
ACCOUNTS_PROD_API_BASE_URL,
} = require('../../shared/constants/accounts');
const {
GAS_API_BASE_URL,
SWAPS_API_V2_BASE_URL,
Expand Down Expand Up @@ -313,6 +317,52 @@ async function setupMocking(
}),
);

[
`${ACCOUNTS_DEV_API_BASE_URL}/v1/users/fake-metrics-id/surveys`,
`${ACCOUNTS_DEV_API_BASE_URL}/v1/users/fake-metrics-fd20/surveys`,
`${ACCOUNTS_DEV_API_BASE_URL}/v1/users/test-metrics-id/surveys`,
`${ACCOUNTS_DEV_API_BASE_URL}/v1/users/invalid-metrics-id/surveys`,
`${ACCOUNTS_PROD_API_BASE_URL}/v1/users/fake-metrics-id/surveys`,
`${ACCOUNTS_PROD_API_BASE_URL}/v1/users/fake-metrics-fd20/surveys`,
`${ACCOUNTS_PROD_API_BASE_URL}/v1/users/test-metrics-id/surveys`,
`${ACCOUNTS_PROD_API_BASE_URL}/v1/users/invalid-metrics-id/surveys`,
].forEach(
async (url) =>
await server.forGet(url).thenCallback(() => {
return {
statusCode: 200,
json: {
userId: '0x123',
surveys: {},
},
};
}),
);

let surveyCallCount = 0;
[
`${ACCOUNTS_DEV_API_BASE_URL}/v1/users/fake-metrics-id-power-user/surveys`,
`${ACCOUNTS_PROD_API_BASE_URL}/v1/users/fake-metrics-id-power-user/surveys`,
].forEach(
async (url) =>
await server.forGet(url).thenCallback(() => {
const surveyId = surveyCallCount > 2 ? 2 : surveyCallCount;
surveyCallCount += 1;
return {
statusCode: 200,
json: {
userId: '0x123',
surveys: {
url: 'https://example.com',
description: `Test survey ${surveyId}`,
cta: 'Take survey',
id: surveyId,
},
},
};
}),
);

await server
.forGet(`https://token.api.cx.metamask.io/tokens/${chainId}`)
.thenCallback(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"showAccountBanner": true,
"trezorModel": null,
"onboardingDate": null,
"lastViewedUserSurvey": null,
"newPrivacyPolicyToastClickedOrClosed": "boolean",
"newPrivacyPolicyToastShownDate": "number",
"hadAdvancedGasFeesSetPriorToMigration92_3": false,
Expand All @@ -62,16 +63,8 @@
"bridgeState": {
"bridgeFeatureFlags": {
"extensionSupport": "boolean",
"srcNetworkAllowlist": {
"0": "string",
"1": "string",
"2": "string"
},
"destNetworkAllowlist": {
"0": "string",
"1": "string",
"2": "string"
}
"srcNetworkAllowlist": { "0": "string", "1": "string", "2": "string" },
"destNetworkAllowlist": { "0": "string", "1": "string", "2": "string" }
}
}
},
Expand Down Expand Up @@ -180,6 +173,7 @@
"permissionActivityLog": "object"
},
"PreferencesController": {
"selectedAddress": "string",
"useBlockie": false,
"useNonceField": false,
"usePhishDetect": true,
Expand Down Expand Up @@ -229,8 +223,7 @@
"useExternalNameSources": "boolean",
"useTransactionSimulations": true,
"enableMV3TimestampSave": true,
"useExternalServices": "boolean",
"selectedAddress": "string"
"useExternalServices": "boolean"
},
"QueuedRequestController": { "queuedRequestCount": 0 },
"SelectedNetworkController": { "domains": "object" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"internalAccounts": { "accounts": "object", "selectedAccount": "string" },
"transactions": "object",
"networkConfigurations": "object",
"networkConfigurationsByChainId": "object",
"addressBook": "object",
"confirmationExchangeRates": {},
"pendingTokens": "object",
Expand Down Expand Up @@ -71,6 +70,7 @@
"showAccountBanner": true,
"trezorModel": null,
"onboardingDate": null,
"lastViewedUserSurvey": null,
"newPrivacyPolicyToastClickedOrClosed": "boolean",
"newPrivacyPolicyToastShownDate": "number",
"hadAdvancedGasFeesSetPriorToMigration92_3": false,
Expand All @@ -96,7 +96,9 @@
"status": "available"
}
},
"networkConfigurationsByChainId": "object",
"keyrings": "object",
"selectedAddress": "string",
"useNonceField": false,
"usePhishDetect": true,
"dismissSeedBackUpReminder": true,
Expand Down Expand Up @@ -128,16 +130,15 @@
"useTransactionSimulations": true,
"enableMV3TimestampSave": true,
"useExternalServices": "boolean",
"selectedAddress": "string",
"metaMetricsId": "fake-metrics-id",
"marketingCampaignCookieId": null,
"metaMetricsDataDeletionId": null,
"metaMetricsDataDeletionTimestamp": 0,
"eventsBeforeMetricsOptIn": "object",
"traits": "object",
"previousUserTraits": "object",
"fragments": "object",
"segmentApiCalls": "object",
"metaMetricsDataDeletionId": null,
"metaMetricsDataDeletionTimestamp": 0,
"currentCurrency": "usd",
"alertEnabledness": { "unconnectedAccount": true, "web3ShimUsage": true },
"unconnectedAccountAlertShownOrigins": "object",
Expand Down Expand Up @@ -249,16 +250,8 @@
"bridgeState": {
"bridgeFeatureFlags": {
"extensionSupport": "boolean",
"srcNetworkAllowlist": {
"0": "string",
"1": "string",
"2": "string"
},
"destNetworkAllowlist": {
"0": "string",
"1": "string",
"2": "string"
}
"srcNetworkAllowlist": { "0": "string", "1": "string", "2": "string" },
"destNetworkAllowlist": { "0": "string", "1": "string", "2": "string" }
}
},
"ensEntries": "object",
Expand Down
Loading

0 comments on commit 2d4de6e

Please sign in to comment.