Skip to content

Commit

Permalink
fix: add useSafeChainsListValidation && useMultiRpcMigration
Browse files Browse the repository at this point in the history
  • Loading branch information
salimtb committed Sep 25, 2024
1 parent e52002e commit 9ec7355
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 14 deletions.
24 changes: 19 additions & 5 deletions packages/preferences-controller/src/PreferencesController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ describe('PreferencesController', () => {
selectedAddress: '',
useTokenDetection: true,
useNftDetection: false,
openSeaEnabled: false,
displayNftMedia: false,
securityAlertsEnabled: false,
isMultiAccountBalancesEnabled: true,
showTestNetworks: false,
isIpfsGatewayEnabled: true,
useTransactionSimulations: true,
useSafeChainsListValidation: true,
useMultiRpcMigration: true,
showIncomingTransactions: Object.values(
ETHERSCAN_SUPPORTED_CHAIN_IDS,
).reduce((acc, curr) => {
Expand Down Expand Up @@ -355,19 +357,31 @@ describe('PreferencesController', () => {

it('should set useNftDetection', () => {
const controller = setupPreferencesController();
controller.setOpenSeaEnabled(true);
controller.setDisplayNftMedia(true);
controller.setUseNftDetection(true);
expect(controller.state.useNftDetection).toBe(true);
});

it('should throw an error when useNftDetection is set and openSeaEnabled is false', () => {
it('should throw an error when useNftDetection is set and displayNftMedia is false', () => {
const controller = setupPreferencesController();
controller.setOpenSeaEnabled(false);
controller.setDisplayNftMedia(false);
expect(() => controller.setUseNftDetection(true)).toThrow(
'useNftDetection cannot be enabled if openSeaEnabled is false',
'useNftDetection cannot be enabled if displayNftMedia is false',
);
});

it('should set useSafeChainsListValidation', () => {
const controller = setupPreferencesController();
controller.setUseSafeChainsListValidation(true);
expect(controller.state.useSafeChainsListValidation).toBe(true);
});

it('should set useMultiRpcMigration', () => {
const controller = setupPreferencesController();
controller.setUseMultiRpcMigration(true);
expect(controller.state.useMultiRpcMigration).toBe(true);
});

it('should set featureFlags', () => {
const controller = setupPreferencesController();
controller.setFeatureFlag('Feature A', true);
Expand Down
58 changes: 49 additions & 9 deletions packages/preferences-controller/src/PreferencesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export type PreferencesState = {
/**
* Controls whether the OpenSea API is used
*/
openSeaEnabled: boolean;
displayNftMedia: boolean;
/**
* Controls whether "security alerts" are enabled
*/
Expand Down Expand Up @@ -110,6 +110,14 @@ export type PreferencesState = {
* Controls whether transaction simulations are enabled
*/
useTransactionSimulations: boolean;
/**
* Controls whether Multi rpc modal is displayed or not
*/
useMultiRpcMigration: boolean;
/**
* Controls whether chain validation is enabled or not
*/
useSafeChainsListValidation: boolean;
};

const metadata = {
Expand All @@ -119,7 +127,7 @@ const metadata = {
isIpfsGatewayEnabled: { persist: true, anonymous: true },
isMultiAccountBalancesEnabled: { persist: true, anonymous: true },
lostIdentities: { persist: true, anonymous: false },
openSeaEnabled: { persist: true, anonymous: true },
displayNftMedia: { persist: true, anonymous: true },
securityAlertsEnabled: { persist: true, anonymous: true },
selectedAddress: { persist: true, anonymous: false },
showTestNetworks: { persist: true, anonymous: true },
Expand All @@ -128,6 +136,8 @@ const metadata = {
useTokenDetection: { persist: true, anonymous: true },
smartTransactionsOptInStatus: { persist: true, anonymous: false },
useTransactionSimulations: { persist: true, anonymous: true },
useMultiRpcMigration: { persist: true, anonymous: true },
useSafeChainsListValidation: { persist: true, anonymous: true },
};

const name = 'PreferencesController';
Expand Down Expand Up @@ -169,7 +179,7 @@ export function getDefaultPreferencesState() {
isIpfsGatewayEnabled: true,
isMultiAccountBalancesEnabled: true,
lostIdentities: {},
openSeaEnabled: false,
displayNftMedia: false,
securityAlertsEnabled: false,
selectedAddress: '',
showIncomingTransactions: {
Expand Down Expand Up @@ -197,6 +207,8 @@ export function getDefaultPreferencesState() {
showTestNetworks: false,
useNftDetection: false,
useTokenDetection: true,
useMultiRpcMigration: true,
useSafeChainsListValidation: true,
smartTransactionsOptInStatus: false,
useTransactionSimulations: true,
};
Expand Down Expand Up @@ -395,9 +407,9 @@ export class PreferencesController extends BaseController<
* @param useNftDetection - Boolean indicating user preference on NFT detection.
*/
setUseNftDetection(useNftDetection: boolean) {
if (useNftDetection && !this.state.openSeaEnabled) {
if (useNftDetection && !this.state.displayNftMedia) {
throw new Error(
'useNftDetection cannot be enabled if openSeaEnabled is false',
'useNftDetection cannot be enabled if displayNftMedia is false',
);
}
this.update((state) => {
Expand All @@ -408,12 +420,12 @@ export class PreferencesController extends BaseController<
/**
* Toggle the opensea enabled setting.
*
* @param openSeaEnabled - Boolean indicating user preference on using OpenSea's API.
* @param displayNftMedia - Boolean indicating user preference on using OpenSea's API.
*/
setOpenSeaEnabled(openSeaEnabled: boolean) {
setDisplayNftMedia(displayNftMedia: boolean) {
this.update((state) => {
state.openSeaEnabled = openSeaEnabled;
if (!openSeaEnabled) {
state.displayNftMedia = displayNftMedia;
if (!displayNftMedia) {
state.useNftDetection = false;
}
});
Expand Down Expand Up @@ -483,6 +495,34 @@ export class PreferencesController extends BaseController<
}
}

/**
* Toggle the use safe chains list validation.
*
* @param useSafeChainsListValidation - Boolean indicating user preference on using chainid.network third part to check safe networks.
*/
setUseSafeChainsListValidation(useSafeChainsListValidation: boolean) {
this.update((state) => {
state.useSafeChainsListValidation = useSafeChainsListValidation;
if (!useSafeChainsListValidation) {
state.useSafeChainsListValidation = false;
}
});
}

/**
* Toggle multi rpc migration modal.
*
* @param useMultiRpcMigration - Boolean indicating if the multi rpc modal will be displayed or not.
*/
setUseMultiRpcMigration(useMultiRpcMigration: boolean) {
this.update((state) => {
state.useMultiRpcMigration = useMultiRpcMigration;
if (!useMultiRpcMigration) {
state.useMultiRpcMigration = false;
}
});
}

/**
* A setter for the user to opt into smart transactions
*
Expand Down

0 comments on commit 9ec7355

Please sign in to comment.