mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 01:47:00 +01:00
[MMI] replace logic from preferences controller with event emitter (#19781)
* adds the emit and cleans old code * clean up * lint * review update * deep comparison with lodash
This commit is contained in:
parent
de14b1ddb2
commit
9454bddef6
@ -1,6 +1,7 @@
|
||||
import EventEmitter from 'events';
|
||||
import log from 'loglevel';
|
||||
import { captureException } from '@sentry/browser';
|
||||
import { isEqual } from 'lodash';
|
||||
import {
|
||||
PersonalMessageManager,
|
||||
TypedMessageManager,
|
||||
@ -14,11 +15,17 @@ import {
|
||||
REFRESH_TOKEN_CHANGE_EVENT,
|
||||
INTERACTIVE_REPLACEMENT_TOKEN_CHANGE_EVENT,
|
||||
} from '@metamask-institutional/sdk';
|
||||
import {
|
||||
handleMmiPortfolio,
|
||||
setDashboardCookie,
|
||||
} from '@metamask-institutional/portfolio-dashboard';
|
||||
import { toChecksumHexAddress } from '../../../shared/modules/hexstring-utils';
|
||||
import { CHAIN_IDS } from '../../../shared/constants/network';
|
||||
import {
|
||||
BUILD_QUOTE_ROUTE,
|
||||
CONNECT_HARDWARE_ROUTE,
|
||||
} from '../../../ui/helpers/constants/routes';
|
||||
import { previousValueComparator } from '../lib/util';
|
||||
import { getPermissionBackgroundApiMethods } from './permissions';
|
||||
|
||||
export default class MMIController extends EventEmitter {
|
||||
@ -69,6 +76,17 @@ export default class MMIController extends EventEmitter {
|
||||
this.transactionUpdateController.subscribeToEvents();
|
||||
});
|
||||
}
|
||||
|
||||
this.preferencesController.store.subscribe(
|
||||
previousValueComparator(async (prevState, currState) => {
|
||||
const { identities: prevIdentities } = prevState;
|
||||
const { identities: currIdentities } = currState;
|
||||
if (isEqual(prevIdentities, currIdentities)) {
|
||||
return;
|
||||
}
|
||||
await this.prepareMmiPortfolio();
|
||||
}, this.preferencesController.store.getState()),
|
||||
);
|
||||
} // End of constructor
|
||||
|
||||
async persistKeyringsAfterRefreshTokenChange() {
|
||||
@ -542,6 +560,44 @@ export default class MMIController extends EventEmitter {
|
||||
});
|
||||
}
|
||||
|
||||
async handleMmiDashboardData() {
|
||||
await this.appStateController.getUnlockPromise(true);
|
||||
const keyringAccounts = await this.keyringController.getAccounts();
|
||||
const { identities } = this.preferencesController.store.getState();
|
||||
const { metaMetricsId } = this.metaMetricsController.store.getState();
|
||||
const getAccountDetails = (address) =>
|
||||
this.custodyController.getAccountDetails(address);
|
||||
const extensionId = this.extension.runtime.id;
|
||||
const networks = [
|
||||
...this.preferencesController.getRpcMethodPreferences(),
|
||||
{ chainId: CHAIN_IDS.MAINNET },
|
||||
{ chainId: CHAIN_IDS.GOERLI },
|
||||
];
|
||||
|
||||
return handleMmiPortfolio({
|
||||
keyringAccounts,
|
||||
identities,
|
||||
metaMetricsId,
|
||||
networks,
|
||||
getAccountDetails,
|
||||
extensionId,
|
||||
});
|
||||
}
|
||||
|
||||
async prepareMmiPortfolio() {
|
||||
if (!process.env.IN_TEST) {
|
||||
try {
|
||||
const mmiDashboardData = await this.handleMmiDashboardData();
|
||||
const cookieSetUrls =
|
||||
this.mmiConfigurationController.store.mmiConfiguration?.portfolio
|
||||
?.cookieSetUrls;
|
||||
setDashboardCookie(mmiDashboardData, cookieSetUrls);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async setAccountAndNetwork(origin, address, chainId) {
|
||||
await this.appStateController.getUnlockPromise(true);
|
||||
const selectedAddress = this.preferencesController.getSelectedAddress();
|
||||
|
@ -1,8 +1,5 @@
|
||||
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';
|
||||
@ -81,14 +78,6 @@ export default class PreferencesController {
|
||||
this.store.setMaxListeners(13);
|
||||
this.tokenListController = opts.tokenListController;
|
||||
|
||||
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
|
||||
this.handleMmiDashboardData = opts.handleMmiDashboardData;
|
||||
|
||||
if (!process.env.IN_TEST) {
|
||||
this.mmiConfigurationStore = opts.mmiConfigurationStore.getState();
|
||||
}
|
||||
///: END:ONLY_INCLUDE_IN
|
||||
|
||||
this._subscribeToInfuraAvailability();
|
||||
|
||||
global.setPreference = (key, value) => {
|
||||
@ -261,10 +250,6 @@ export default class PreferencesController {
|
||||
return ids;
|
||||
}, {});
|
||||
|
||||
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
|
||||
this.prepareMmiPortfolio();
|
||||
///: END:ONLY_INCLUDE_IN
|
||||
|
||||
this.store.updateState({ identities });
|
||||
}
|
||||
|
||||
@ -290,10 +275,6 @@ export default class PreferencesController {
|
||||
this.setSelectedAddress(selected);
|
||||
}
|
||||
|
||||
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
|
||||
this.prepareMmiPortfolio();
|
||||
///: END:ONLY_INCLUDE_IN
|
||||
|
||||
return address;
|
||||
}
|
||||
|
||||
@ -350,10 +331,6 @@ 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();
|
||||
@ -534,21 +511,6 @@ 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.handleMmiDashboardData();
|
||||
const cookieSetUrls =
|
||||
this.mmiConfigurationStore.mmiConfiguration?.portfolio?.cookieSetUrls;
|
||||
setDashboardCookie(mmiDashboardData, cookieSetUrls);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
///: END:ONLY_INCLUDE_IN
|
||||
|
||||
//
|
||||
// PRIVATE METHODS
|
||||
//
|
||||
|
@ -71,7 +71,6 @@ import {
|
||||
} from '@metamask-institutional/custody-keyring';
|
||||
import { InstitutionalFeaturesController } from '@metamask-institutional/institutional-features';
|
||||
import { CustodyController } from '@metamask-institutional/custody-controller';
|
||||
import { handleMmiPortfolio } from '@metamask-institutional/portfolio-dashboard';
|
||||
import { TransactionUpdateController } from '@metamask-institutional/transaction-update';
|
||||
///: END:ONLY_INCLUDE_IN
|
||||
import { SignatureController } from '@metamask/signature-controller';
|
||||
@ -382,10 +381,6 @@ export default class MetamaskController extends EventEmitter {
|
||||
),
|
||||
tokenListController: this.tokenListController,
|
||||
provider: this.provider,
|
||||
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
|
||||
handleMmiDashboardData: this.handleMmiDashboardData.bind(this),
|
||||
mmiConfigurationStore: this.mmiConfigurationController.store,
|
||||
///: END:ONLY_INCLUDE_IN
|
||||
});
|
||||
|
||||
this.preferencesController.store.subscribe(async ({ currentLocale }) => {
|
||||
@ -3918,7 +3913,8 @@ export default class MetamaskController extends EventEmitter {
|
||||
),
|
||||
handleMmiCheckIfTokenIsPresent:
|
||||
this.mmiController.handleMmiCheckIfTokenIsPresent.bind(this),
|
||||
handleMmiDashboardData: this.handleMmiDashboardData.bind(this),
|
||||
handleMmiDashboardData:
|
||||
this.mmiController.handleMmiDashboardData.bind(this),
|
||||
handleMmiOpenSwaps: this.mmiController.handleMmiOpenSwaps.bind(this),
|
||||
handleMmiSetAccountAndNetwork:
|
||||
this.mmiController.setAccountAndNetwork.bind(this),
|
||||
@ -3977,37 +3973,6 @@ export default class MetamaskController extends EventEmitter {
|
||||
return engine;
|
||||
}
|
||||
|
||||
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
|
||||
/**
|
||||
* This method is needed in preferences controller
|
||||
* so it needs to be here and not in our controller because
|
||||
* preferences controllers is initiated first
|
||||
*/
|
||||
async handleMmiDashboardData() {
|
||||
await this.appStateController.getUnlockPromise(true);
|
||||
const keyringAccounts = await this.keyringController.getAccounts();
|
||||
const { identities } = this.preferencesController.store.getState();
|
||||
const { metaMetricsId } = this.metaMetricsController.store.getState();
|
||||
const getAccountDetails = (address) =>
|
||||
this.custodyController.getAccountDetails(address);
|
||||
const extensionId = this.extension.runtime.id;
|
||||
const networks = [
|
||||
...this.preferencesController.getRpcMethodPreferences(),
|
||||
{ chainId: CHAIN_IDS.MAINNET },
|
||||
{ chainId: CHAIN_IDS.GOERLI },
|
||||
];
|
||||
|
||||
return handleMmiPortfolio({
|
||||
keyringAccounts,
|
||||
identities,
|
||||
metaMetricsId,
|
||||
networks,
|
||||
getAccountDetails,
|
||||
extensionId,
|
||||
});
|
||||
}
|
||||
///: END:ONLY_INCLUDE_IN
|
||||
|
||||
/**
|
||||
* TODO:LegacyProvider: Delete
|
||||
* A method for providing our public config info over a stream.
|
||||
|
Loading…
Reference in New Issue
Block a user