Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(migration): enable token auto-detection when basic functionality is on #26406

Merged
merged 24 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d1a1289
revert token and nft modals
bergeron Aug 14, 2024
e5859bd
messages.json, lint
bergeron Aug 14, 2024
d31cf81
messages.json
bergeron Aug 14, 2024
35e7b8d
fix test
bergeron Aug 14, 2024
b1681b7
yarn verify-locales:fix
bergeron Aug 14, 2024
61cc7c1
migration
bergeron Aug 14, 2024
0c56579
add migration to index
bergeron Aug 14, 2024
7b4d2cb
fix e2e test
bergeron Aug 14, 2024
3f487ba
fix e2e test
bergeron Aug 14, 2024
4fff5a8
Merge branch 'develop' into brian/migrate-token-detection-enable
bergeron Aug 19, 2024
d611f43
rename 123.1
bergeron Aug 19, 2024
0d199c2
remove `showTokenAutodetectModalOnUpgrade`
bergeron Aug 19, 2024
b3702c6
add test for `showTokenAutodetectModalOnUpgrade`
bergeron Aug 19, 2024
0fa09c9
Merge branch 'develop' into brian/migrate-token-detection-enable
bergeron Aug 19, 2024
7570a56
Merge branch 'develop' into brian/migrate-token-detection-enable
bergeron Aug 20, 2024
d0ef069
Merge branch 'develop' into brian/migrate-token-detection-enable
bergeron Aug 26, 2024
ef855fc
Merge branch 'develop' into brian/migrate-token-detection-enable
bergeron Aug 27, 2024
3f24df8
Update and rename 123.1.ts to 125.1.ts
bergeron Sep 11, 2024
6f6664d
Update and rename 123.1.test.ts to 125.1.test.ts
bergeron Sep 11, 2024
44d5c91
Merge branch 'develop' into brian/migrate-token-detection-enable
bergeron Sep 11, 2024
2ef5df8
Update index.js
bergeron Sep 11, 2024
cd353e4
.
bergeron Sep 12, 2024
d4a5206
.
bergeron Sep 12, 2024
f4bdf42
Merge branch 'develop' into brian/migrate-token-detection-enable
sahar-fehri Sep 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions app/scripts/migrations/125.1.test.ts
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,
},
});
});
});
50 changes: 50 additions & 0 deletions app/scripts/migrations/125.1.ts
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;
}
1 change: 1 addition & 0 deletions app/scripts/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ const migrations = [
require('./123'),
require('./124'),
require('./125'),
require('./125.1'),
require('./126'),
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
"dismissSeedBackUpReminder": true,
"useMultiAccountBalanceChecker": true,
"useSafeChainsListValidation": "boolean",
"useTokenDetection": false,
"useTokenDetection": true,
"useNftDetection": false,
"use4ByteResolution": true,
"useCurrencyRateCheck": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"dismissSeedBackUpReminder": true,
"useMultiAccountBalanceChecker": true,
"useSafeChainsListValidation": true,
"useTokenDetection": false,
"useTokenDetection": true,
"useNftDetection": false,
"useCurrencyRateCheck": true,
"useRequestQueue": true,
Expand Down
13 changes: 11 additions & 2 deletions test/e2e/tests/multichain/asset-picker-send.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ describe('AssetPickerSendFlow @no-mmi', function () {
async ({ driver }: { driver: Driver }) => {
await unlockWallet(driver);

// Disable token auto detection
await driver.openNewURL(
`${driver.extensionUrl}/home.html#settings/security`,
);
await driver.clickElement(
'[data-testid="autoDetectTokens"] .toggle-button',
);
await driver.navigate();

// Open the send flow
openActionMenuAndStartSendFlow(driver);

Expand Down Expand Up @@ -72,13 +81,13 @@ describe('AssetPickerSendFlow @no-mmi', function () {

assert.equal(tokenListSecondaryValue, '$250,000.00');

// Search for BNB
// Search for CHZ
const searchInputField = await driver.waitForSelector(
'[data-testid="asset-picker-modal-search-input"]',
);
await searchInputField.sendKeys('CHZ');

// check that BNB is disabled
// check that CHZ is disabled
const [, tkn] = await driver.findElements(
'[data-testid="multichain-token-list-button"]',
);
Expand Down