-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(migration): enable token auto-detection when basic functionality…
… is on (#26406) <!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> Adds a migration that enables token auto-detection if the basic functionality toggle is on. It also removes an unused property `showTokenAutodetectModalOnUpgrade` from the app metadata controller. It is versioned 125.1 because it's planned to be cherry picked to release 12.3, and that's the subsequent migration version in that branch. [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/26406?quickstart=1) - Install an older build like tag `v12.1.0` - In settings, disable token auto detection but keep basic functionality on - Upgrade to this build, reload extension - should see `Running migration 125.1...` in console - Verify token autodetection is enabled in settings - Download state from advanced settings - Verify state does not contain `showTokenAutodetectModalOnUpgrade` <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> <!-- [screenshots/recordings] --> <!-- [screenshots/recordings] --> - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. --------- Co-authored-by: sahar-fehri <[email protected]>
- Loading branch information
1 parent
fc9b12a
commit 615cc15
Showing
6 changed files
with
171 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { migrate, version } from './125.1'; | ||
|
||
const oldVersion = 125; | ||
|
||
describe(`migration #${version}`, () => { | ||
afterEach(() => jest.resetAllMocks()); | ||
|
||
it('updates the version metadata', async () => { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: {}, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
expect(newStorage.meta).toStrictEqual({ version }); | ||
}); | ||
|
||
it('Gracefully handles empty/undefined PreferencesController', async () => { | ||
for (const PreferencesController of [{}, undefined, null, 1, '', []]) { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: { PreferencesController }, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
expect(newStorage.data.TxController).toStrictEqual(undefined); | ||
} | ||
}); | ||
|
||
it('Enables token autodetection when basic functionality is on', async () => { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: { | ||
PreferencesController: { | ||
useExternalServices: true, | ||
}, | ||
}, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
expect(newStorage.data).toEqual({ | ||
PreferencesController: { | ||
useExternalServices: true, | ||
useTokenDetection: true, | ||
}, | ||
}); | ||
}); | ||
|
||
it('Does not enable token autodetection when basic functionality is off', async () => { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: { | ||
PreferencesController: { | ||
useExternalServices: false, | ||
}, | ||
}, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
expect(newStorage.data).toEqual({ | ||
PreferencesController: { | ||
useExternalServices: false, | ||
}, | ||
}); | ||
}); | ||
|
||
it('Removes showTokenAutodetectModalOnUpgrade from the app metadata controller', async () => { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: { | ||
AppMetadataController: { | ||
previousMigrationVersion: oldVersion, | ||
currentMigrationVersion: version, | ||
showTokenAutodetectModalOnUpgrade: null, | ||
}, | ||
}, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
expect(newStorage.data).toEqual({ | ||
AppMetadataController: { | ||
previousMigrationVersion: oldVersion, | ||
currentMigrationVersion: version, | ||
}, | ||
}); | ||
}); | ||
|
||
it('Does nothing if showTokenAutodetectModalOnUpgrade is not in the app metadata controller', async () => { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: { | ||
AppMetadataController: { | ||
previousMigrationVersion: oldVersion, | ||
currentMigrationVersion: version, | ||
}, | ||
}, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
expect(newStorage.data).toEqual({ | ||
AppMetadataController: { | ||
previousMigrationVersion: oldVersion, | ||
currentMigrationVersion: version, | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { hasProperty, isObject } from '@metamask/utils'; | ||
import { cloneDeep } from 'lodash'; | ||
|
||
type VersionedData = { | ||
meta: { version: number }; | ||
data: Record<string, unknown>; | ||
}; | ||
|
||
export const version = 125.1; | ||
|
||
/** | ||
* This migration enables token auto-detection if the basic functionality toggle is on. | ||
* | ||
* It also removes an unused property `showTokenAutodetectModalOnUpgrade` from the app metadata controller. | ||
* | ||
* @param originalVersionedData - Versioned MetaMask extension state, exactly | ||
* what we persist to dist. | ||
* @param originalVersionedData.meta - State metadata. | ||
* @param originalVersionedData.meta.version - The current state version. | ||
* @param originalVersionedData.data - The persisted MetaMask state, keyed by | ||
* controller. | ||
* @returns Updated versioned MetaMask extension state. | ||
*/ | ||
export async function migrate( | ||
originalVersionedData: VersionedData, | ||
): Promise<VersionedData> { | ||
const versionedData = cloneDeep(originalVersionedData); | ||
versionedData.meta.version = version; | ||
transformState(versionedData.data); | ||
return versionedData; | ||
} | ||
|
||
function transformState(state: Record<string, unknown>) { | ||
if ( | ||
hasProperty(state, 'PreferencesController') && | ||
isObject(state.PreferencesController) && | ||
state.PreferencesController.useExternalServices === true | ||
) { | ||
state.PreferencesController.useTokenDetection = true; | ||
} | ||
|
||
if ( | ||
hasProperty(state, 'AppMetadataController') && | ||
isObject(state.AppMetadataController) | ||
) { | ||
delete state.AppMetadataController.showTokenAutodetectModalOnUpgrade; | ||
} | ||
|
||
return state; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters