1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00

[MMI] Added code fences to the following controllers: app-state, metametrics, preferences (#17894)

* Added code fences to the following controllers: app-state, metametrics, preferences

* Fixed prettier

* Updated code to be align with new changes in MMI

* Changing code fences

* Remove uneeded files

* Fixed tests

* Fixed code fence

* Changing logic to use async/await

* Removed accountAddress

* Reverted code from develop
This commit is contained in:
Albert Olivé 2023-05-23 16:16:23 +02:00 committed by GitHub
parent a9429c5c0d
commit ae3021c697
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 118 additions and 1 deletions

View File

@ -374,6 +374,25 @@ export default class AppStateController extends EventEmitter {
this.store.updateState({ usedNetworks }); this.store.updateState({ usedNetworks });
} }
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
/**
* Set the interactive replacement token with a url and the old refresh token
*
* @param {object} opts
* @param opts.url
* @param opts.oldRefreshToken
* @returns {void}
*/
showInteractiveReplacementTokenBanner({ url, oldRefreshToken }) {
this.store.updateState({
interactiveReplacementToken: {
url,
oldRefreshToken,
},
});
}
///: END:ONLY_INCLUDE_IN
/** /**
* A setter for the currentPopupId which indicates the id of popup window that's currently active * A setter for the currentPopupId which indicates the id of popup window that's currently active
* *

View File

@ -625,10 +625,21 @@ export default class MetaMetricsController {
* @returns {MetaMetricsContext} * @returns {MetaMetricsContext}
*/ */
_buildContext(referrer, page = METAMETRICS_BACKGROUND_PAGE_OBJECT) { _buildContext(referrer, page = METAMETRICS_BACKGROUND_PAGE_OBJECT) {
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
const mmiProps = {};
if (this.extension?.runtime?.id) {
mmiProps.extensionId = this.extension.runtime.id;
}
///: END:ONLY_INCLUDE_IN
return { return {
app: { app: {
name: 'MetaMask Extension', name: 'MetaMask Extension',
version: this.version, version: this.version,
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
...mmiProps,
///: END:ONLY_INCLUDE_IN
}, },
userAgent: window.navigator.userAgent, userAgent: window.navigator.userAgent,
page, page,
@ -658,6 +669,15 @@ export default class MetaMetricsController {
referrer, referrer,
environmentType = ENVIRONMENT_TYPE_BACKGROUND, environmentType = ENVIRONMENT_TYPE_BACKGROUND,
} = rawPayload; } = rawPayload;
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
const mmiProps = {};
if (this.extension?.runtime?.id) {
mmiProps.extensionId = this.extension.runtime.id;
}
///: END:ONLY_INCLUDE_IN
return { return {
event, event,
messageId: buildUniqueMessageId(rawPayload), messageId: buildUniqueMessageId(rawPayload),
@ -676,6 +696,9 @@ export default class MetaMetricsController {
locale: this.locale, locale: this.locale,
chain_id: properties?.chain_id ?? this.chainId, chain_id: properties?.chain_id ?? this.chainId,
environment_type: environmentType, environment_type: environmentType,
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
...mmiProps,
///: END:ONLY_INCLUDE_IN
}, },
context: this._buildContext(referrer, page), context: this._buildContext(referrer, page),
}; };
@ -689,6 +712,13 @@ export default class MetaMetricsController {
* @returns {MetaMetricsTraits | null} traits that have changed since last update * @returns {MetaMetricsTraits | null} traits that have changed since last update
*/ */
_buildUserTraitsObject(metamaskState) { _buildUserTraitsObject(metamaskState) {
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
const mmiAccountAddress =
metamaskState.custodyAccountDetails &&
Object.keys(metamaskState.custodyAccountDetails).length
? Object.keys(metamaskState.custodyAccountDetails)[0]
: null;
///: END:ONLY_INCLUDE_IN
const { traits, previousUserTraits } = this.store.getState(); const { traits, previousUserTraits } = this.store.getState();
/** @type {MetaMetricsTraits} */ /** @type {MetaMetricsTraits} */
const currentTraits = { const currentTraits = {
@ -728,6 +758,11 @@ export default class MetaMetricsController {
[MetaMetricsUserTrait.DesktopEnabled]: [MetaMetricsUserTrait.DesktopEnabled]:
metamaskState.desktopEnabled || false, metamaskState.desktopEnabled || false,
///: END:ONLY_INCLUDE_IN ///: END:ONLY_INCLUDE_IN
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
[MetaMetricsUserTrait.MmiExtensionId]: this.extension?.runtime?.id,
[MetaMetricsUserTrait.MmiAccountAddress]: mmiAccountAddress,
[MetaMetricsUserTrait.MmiIsCustodian]: Boolean(mmiAccountAddress),
///: END:ONLY_INCLUDE_IN
[MetaMetricsUserTrait.SecurityProviders]: [MetaMetricsUserTrait.SecurityProviders]:
metamaskState.transactionSecurityCheckEnabled ? ['opensea'] : [], metamaskState.transactionSecurityCheckEnabled ? ['opensea'] : [],
}; };

View File

@ -23,6 +23,14 @@ const FAKE_CHAIN_ID = '0x1338';
const LOCALE = 'en_US'; const LOCALE = 'en_US';
const TEST_META_METRICS_ID = '0xabc'; const TEST_META_METRICS_ID = '0xabc';
const DUMMY_ACTION_ID = 'DUMMY_ACTION_ID'; const DUMMY_ACTION_ID = 'DUMMY_ACTION_ID';
const MOCK_EXTENSION_ID = 'testid';
const MOCK_EXTENSION = {
runtime: {
id: MOCK_EXTENSION_ID,
setUninstallURL: () => undefined,
},
};
const MOCK_TRAITS = { const MOCK_TRAITS = {
test_boolean: true, test_boolean: true,
@ -39,7 +47,11 @@ const MOCK_INVALID_TRAITS = {
}; };
const DEFAULT_TEST_CONTEXT = { const DEFAULT_TEST_CONTEXT = {
app: { name: 'MetaMask Extension', version: VERSION }, app: {
name: 'MetaMask Extension',
version: VERSION,
extensionId: MOCK_EXTENSION_ID,
},
page: METAMETRICS_BACKGROUND_PAGE_OBJECT, page: METAMETRICS_BACKGROUND_PAGE_OBJECT,
referrer: undefined, referrer: undefined,
userAgent: window.navigator.userAgent, userAgent: window.navigator.userAgent,
@ -56,6 +68,7 @@ const DEFAULT_EVENT_PROPERTIES = {
revenue: undefined, revenue: undefined,
value: undefined, value: undefined,
currency: undefined, currency: undefined,
extensionId: MOCK_EXTENSION_ID,
...DEFAULT_SHARED_PROPERTIES, ...DEFAULT_SHARED_PROPERTIES,
}; };
@ -149,6 +162,7 @@ function getMetaMetricsController({
}, },
events: {}, events: {},
}, },
extension: MOCK_EXTENSION,
}); });
} }
describe('MetaMetricsController', function () { describe('MetaMetricsController', function () {
@ -973,6 +987,11 @@ describe('MetaMetricsController', function () {
[MetaMetricsUserTrait.TokenDetectionEnabled]: true, [MetaMetricsUserTrait.TokenDetectionEnabled]: true,
[MetaMetricsUserTrait.DesktopEnabled]: false, [MetaMetricsUserTrait.DesktopEnabled]: false,
[MetaMetricsUserTrait.SecurityProviders]: [], [MetaMetricsUserTrait.SecurityProviders]: [],
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
[MetaMetricsUserTrait.MmiExtensionId]: 'testid',
[MetaMetricsUserTrait.MmiAccountAddress]: null,
[MetaMetricsUserTrait.MmiIsCustodian]: false,
///: END:ONLY_INCLUDE_IN
}); });
}); });

View File

@ -1,5 +1,8 @@
import { ObservableStore } from '@metamask/obs-store'; import { ObservableStore } from '@metamask/obs-store';
import { normalize as normalizeAddress } from 'eth-sig-util'; import { normalize as normalizeAddress } from 'eth-sig-util';
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
import { setDashboardCookie } from '@metamask-institutional/portfolio-dashboard';
///: END:ONLY_INCLUDE_IN
import { IPFS_DEFAULT_GATEWAY_URL } from '../../../shared/constants/network'; import { IPFS_DEFAULT_GATEWAY_URL } from '../../../shared/constants/network';
import { LedgerTransportTypes } from '../../../shared/constants/hardware-wallets'; import { LedgerTransportTypes } from '../../../shared/constants/hardware-wallets';
import { ThemeType } from '../../../shared/constants/preferences'; import { ThemeType } from '../../../shared/constants/preferences';
@ -69,12 +72,25 @@ export default class PreferencesController {
...opts.initState, ...opts.initState,
}; };
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
initState.useTokenDetection = Boolean(process.env.TOKEN_DETECTION_V2);
///: END:ONLY_INCLUDE_IN
this.network = opts.network;
this._onInfuraIsBlocked = opts.onInfuraIsBlocked; this._onInfuraIsBlocked = opts.onInfuraIsBlocked;
this._onInfuraIsUnblocked = opts.onInfuraIsUnblocked; this._onInfuraIsUnblocked = opts.onInfuraIsUnblocked;
this.store = new ObservableStore(initState); this.store = new ObservableStore(initState);
this.store.setMaxListeners(13); this.store.setMaxListeners(13);
this.tokenListController = opts.tokenListController; this.tokenListController = opts.tokenListController;
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
this.handleMmiPortfolio = opts.handleMmiPortfolio;
if (!process.env.IN_TEST) {
this.mmiConfigurationStore = opts.mmiConfigurationStore.getState();
}
///: END:ONLY_INCLUDE_IN
this._subscribeToInfuraAvailability(); this._subscribeToInfuraAvailability();
global.setPreference = (key, value) => { global.setPreference = (key, value) => {
@ -245,6 +261,10 @@ export default class PreferencesController {
return ids; return ids;
}, {}); }, {});
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
this.prepareMmiPortfolio();
///: END:ONLY_INCLUDE_IN
this.store.updateState({ identities }); this.store.updateState({ identities });
} }
@ -269,6 +289,11 @@ export default class PreferencesController {
const [selected] = Object.keys(identities); const [selected] = Object.keys(identities);
this.setSelectedAddress(selected); this.setSelectedAddress(selected);
} }
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
this.prepareMmiPortfolio();
///: END:ONLY_INCLUDE_IN
return address; return address;
} }
@ -325,6 +350,10 @@ export default class PreferencesController {
this.store.updateState({ identities, lostIdentities }); this.store.updateState({ identities, lostIdentities });
this.addAddresses(addresses); this.addAddresses(addresses);
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
this.prepareMmiPortfolio();
///: END:ONLY_INCLUDE_IN
// If the selected account is no longer valid, // If the selected account is no longer valid,
// select an arbitrary other account: // select an arbitrary other account:
let selected = this.getSelectedAddress(); let selected = this.getSelectedAddress();
@ -505,6 +534,21 @@ export default class PreferencesController {
return this.store.getState().disabledRpcMethodPreferences; return this.store.getState().disabledRpcMethodPreferences;
} }
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
async prepareMmiPortfolio() {
if (!process.env.IN_TEST) {
try {
const mmiDashboardData = await this.handleMmiPortfolio();
const cookieSetUrls =
this.mmiConfigurationStore.mmiConfiguration?.portfolio?.cookieSetUrls;
setDashboardCookie(mmiDashboardData, cookieSetUrls);
} catch (error) {
console.error(error);
}
}
}
///: END:ONLY_INCLUDE_IN
// //
// PRIVATE METHODS // PRIVATE METHODS
// //