1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +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:
António Regadas 2023-06-28 15:45:22 +01:00 committed by GitHub
parent de14b1ddb2
commit 9454bddef6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 75 deletions

View File

@ -1,6 +1,7 @@
import EventEmitter from 'events'; import EventEmitter from 'events';
import log from 'loglevel'; import log from 'loglevel';
import { captureException } from '@sentry/browser'; import { captureException } from '@sentry/browser';
import { isEqual } from 'lodash';
import { import {
PersonalMessageManager, PersonalMessageManager,
TypedMessageManager, TypedMessageManager,
@ -14,11 +15,17 @@ import {
REFRESH_TOKEN_CHANGE_EVENT, REFRESH_TOKEN_CHANGE_EVENT,
INTERACTIVE_REPLACEMENT_TOKEN_CHANGE_EVENT, INTERACTIVE_REPLACEMENT_TOKEN_CHANGE_EVENT,
} from '@metamask-institutional/sdk'; } from '@metamask-institutional/sdk';
import {
handleMmiPortfolio,
setDashboardCookie,
} from '@metamask-institutional/portfolio-dashboard';
import { toChecksumHexAddress } from '../../../shared/modules/hexstring-utils'; import { toChecksumHexAddress } from '../../../shared/modules/hexstring-utils';
import { CHAIN_IDS } from '../../../shared/constants/network';
import { import {
BUILD_QUOTE_ROUTE, BUILD_QUOTE_ROUTE,
CONNECT_HARDWARE_ROUTE, CONNECT_HARDWARE_ROUTE,
} from '../../../ui/helpers/constants/routes'; } from '../../../ui/helpers/constants/routes';
import { previousValueComparator } from '../lib/util';
import { getPermissionBackgroundApiMethods } from './permissions'; import { getPermissionBackgroundApiMethods } from './permissions';
export default class MMIController extends EventEmitter { export default class MMIController extends EventEmitter {
@ -69,6 +76,17 @@ export default class MMIController extends EventEmitter {
this.transactionUpdateController.subscribeToEvents(); 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 } // End of constructor
async persistKeyringsAfterRefreshTokenChange() { 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) { async setAccountAndNetwork(origin, address, chainId) {
await this.appStateController.getUnlockPromise(true); await this.appStateController.getUnlockPromise(true);
const selectedAddress = this.preferencesController.getSelectedAddress(); const selectedAddress = this.preferencesController.getSelectedAddress();

View File

@ -1,8 +1,5 @@
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';
@ -81,14 +78,6 @@ export default class PreferencesController {
this.store.setMaxListeners(13); this.store.setMaxListeners(13);
this.tokenListController = opts.tokenListController; 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(); this._subscribeToInfuraAvailability();
global.setPreference = (key, value) => { global.setPreference = (key, value) => {
@ -261,10 +250,6 @@ 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 });
} }
@ -290,10 +275,6 @@ export default class PreferencesController {
this.setSelectedAddress(selected); this.setSelectedAddress(selected);
} }
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
this.prepareMmiPortfolio();
///: END:ONLY_INCLUDE_IN
return address; return address;
} }
@ -350,10 +331,6 @@ 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();
@ -534,21 +511,6 @@ 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.handleMmiDashboardData();
const cookieSetUrls =
this.mmiConfigurationStore.mmiConfiguration?.portfolio?.cookieSetUrls;
setDashboardCookie(mmiDashboardData, cookieSetUrls);
} catch (error) {
console.error(error);
}
}
}
///: END:ONLY_INCLUDE_IN
// //
// PRIVATE METHODS // PRIVATE METHODS
// //

View File

@ -71,7 +71,6 @@ import {
} from '@metamask-institutional/custody-keyring'; } from '@metamask-institutional/custody-keyring';
import { InstitutionalFeaturesController } from '@metamask-institutional/institutional-features'; import { InstitutionalFeaturesController } from '@metamask-institutional/institutional-features';
import { CustodyController } from '@metamask-institutional/custody-controller'; import { CustodyController } from '@metamask-institutional/custody-controller';
import { handleMmiPortfolio } from '@metamask-institutional/portfolio-dashboard';
import { TransactionUpdateController } from '@metamask-institutional/transaction-update'; import { TransactionUpdateController } from '@metamask-institutional/transaction-update';
///: END:ONLY_INCLUDE_IN ///: END:ONLY_INCLUDE_IN
import { SignatureController } from '@metamask/signature-controller'; import { SignatureController } from '@metamask/signature-controller';
@ -382,10 +381,6 @@ export default class MetamaskController extends EventEmitter {
), ),
tokenListController: this.tokenListController, tokenListController: this.tokenListController,
provider: this.provider, 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 }) => { this.preferencesController.store.subscribe(async ({ currentLocale }) => {
@ -3918,7 +3913,8 @@ export default class MetamaskController extends EventEmitter {
), ),
handleMmiCheckIfTokenIsPresent: handleMmiCheckIfTokenIsPresent:
this.mmiController.handleMmiCheckIfTokenIsPresent.bind(this), this.mmiController.handleMmiCheckIfTokenIsPresent.bind(this),
handleMmiDashboardData: this.handleMmiDashboardData.bind(this), handleMmiDashboardData:
this.mmiController.handleMmiDashboardData.bind(this),
handleMmiOpenSwaps: this.mmiController.handleMmiOpenSwaps.bind(this), handleMmiOpenSwaps: this.mmiController.handleMmiOpenSwaps.bind(this),
handleMmiSetAccountAndNetwork: handleMmiSetAccountAndNetwork:
this.mmiController.setAccountAndNetwork.bind(this), this.mmiController.setAccountAndNetwork.bind(this),
@ -3977,37 +3973,6 @@ export default class MetamaskController extends EventEmitter {
return engine; 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 * TODO:LegacyProvider: Delete
* A method for providing our public config info over a stream. * A method for providing our public config info over a stream.