Skip to content

Commit

Permalink
[MMI] Added code fences to the following controllers: app-state, meta…
Browse files Browse the repository at this point in the history
…metrics, 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
  • Loading branch information
albertolive authored May 23, 2023
1 parent a9429c5 commit ae3021c
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 1 deletion.
19 changes: 19 additions & 0 deletions app/scripts/controllers/app-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,25 @@ export default class AppStateController extends EventEmitter {
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
*
Expand Down
35 changes: 35 additions & 0 deletions app/scripts/controllers/metametrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,10 +625,21 @@ export default class MetaMetricsController {
* @returns {MetaMetricsContext}
*/
_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 {
app: {
name: 'MetaMask Extension',
version: this.version,
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
...mmiProps,
///: END:ONLY_INCLUDE_IN
},
userAgent: window.navigator.userAgent,
page,
Expand Down Expand Up @@ -658,6 +669,15 @@ export default class MetaMetricsController {
referrer,
environmentType = ENVIRONMENT_TYPE_BACKGROUND,
} = 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 {
event,
messageId: buildUniqueMessageId(rawPayload),
Expand All @@ -676,6 +696,9 @@ export default class MetaMetricsController {
locale: this.locale,
chain_id: properties?.chain_id ?? this.chainId,
environment_type: environmentType,
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
...mmiProps,
///: END:ONLY_INCLUDE_IN
},
context: this._buildContext(referrer, page),
};
Expand All @@ -689,6 +712,13 @@ export default class MetaMetricsController {
* @returns {MetaMetricsTraits | null} traits that have changed since last update
*/
_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();
/** @type {MetaMetricsTraits} */
const currentTraits = {
Expand Down Expand Up @@ -728,6 +758,11 @@ export default class MetaMetricsController {
[MetaMetricsUserTrait.DesktopEnabled]:
metamaskState.desktopEnabled || false,
///: 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]:
metamaskState.transactionSecurityCheckEnabled ? ['opensea'] : [],
};
Expand Down
21 changes: 20 additions & 1 deletion app/scripts/controllers/metametrics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ const FAKE_CHAIN_ID = '0x1338';
const LOCALE = 'en_US';
const TEST_META_METRICS_ID = '0xabc';
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 = {
test_boolean: true,
Expand All @@ -39,7 +47,11 @@ const MOCK_INVALID_TRAITS = {
};

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,
referrer: undefined,
userAgent: window.navigator.userAgent,
Expand All @@ -56,6 +68,7 @@ const DEFAULT_EVENT_PROPERTIES = {
revenue: undefined,
value: undefined,
currency: undefined,
extensionId: MOCK_EXTENSION_ID,
...DEFAULT_SHARED_PROPERTIES,
};

Expand Down Expand Up @@ -149,6 +162,7 @@ function getMetaMetricsController({
},
events: {},
},
extension: MOCK_EXTENSION,
});
}
describe('MetaMetricsController', function () {
Expand Down Expand Up @@ -973,6 +987,11 @@ describe('MetaMetricsController', function () {
[MetaMetricsUserTrait.TokenDetectionEnabled]: true,
[MetaMetricsUserTrait.DesktopEnabled]: false,
[MetaMetricsUserTrait.SecurityProviders]: [],
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
[MetaMetricsUserTrait.MmiExtensionId]: 'testid',
[MetaMetricsUserTrait.MmiAccountAddress]: null,
[MetaMetricsUserTrait.MmiIsCustodian]: false,
///: END:ONLY_INCLUDE_IN
});
});

Expand Down
44 changes: 44 additions & 0 deletions app/scripts/controllers/preferences.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ObservableStore } from '@metamask/obs-store';
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 { LedgerTransportTypes } from '../../../shared/constants/hardware-wallets';
import { ThemeType } from '../../../shared/constants/preferences';
Expand Down Expand Up @@ -69,12 +72,25 @@ export default class PreferencesController {
...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._onInfuraIsUnblocked = opts.onInfuraIsUnblocked;
this.store = new ObservableStore(initState);
this.store.setMaxListeners(13);
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();

global.setPreference = (key, value) => {
Expand Down Expand Up @@ -245,6 +261,10 @@ export default class PreferencesController {
return ids;
}, {});

///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
this.prepareMmiPortfolio();
///: END:ONLY_INCLUDE_IN

this.store.updateState({ identities });
}

Expand All @@ -269,6 +289,11 @@ export default class PreferencesController {
const [selected] = Object.keys(identities);
this.setSelectedAddress(selected);
}

///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
this.prepareMmiPortfolio();
///: END:ONLY_INCLUDE_IN

return address;
}

Expand Down Expand Up @@ -325,6 +350,10 @@ export default class PreferencesController {
this.store.updateState({ identities, lostIdentities });
this.addAddresses(addresses);

///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
this.prepareMmiPortfolio();
///: END:ONLY_INCLUDE_IN

// If the selected account is no longer valid,
// select an arbitrary other account:
let selected = this.getSelectedAddress();
Expand Down Expand Up @@ -505,6 +534,21 @@ export default class PreferencesController {
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
//
Expand Down

0 comments on commit ae3021c

Please sign in to comment.