Skip to content

Commit 2bcf315

Browse files
committed
Merge develop, address conflict in migrations file
2 parents 30f88bc + b9173da commit 2bcf315

File tree

10 files changed

+417
-753
lines changed

10 files changed

+417
-753
lines changed

app/scripts/migrations/121.1.test.ts

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { AccountsControllerState } from '@metamask/accounts-controller';
2+
import { createMockInternalAccount } from '../../../test/jest/mocks';
3+
import { migrate, version } from './121.1';
4+
5+
const oldVersion = 121;
6+
7+
const mockInternalAccount = createMockInternalAccount();
8+
const mockAccountsControllerState: AccountsControllerState = {
9+
internalAccounts: {
10+
accounts: {
11+
[mockInternalAccount.id]: mockInternalAccount,
12+
},
13+
selectedAccount: mockInternalAccount.id,
14+
},
15+
};
16+
17+
describe('migration #121.1', () => {
18+
afterEach(() => jest.resetAllMocks());
19+
20+
it('updates the version metadata', async () => {
21+
const oldStorage = {
22+
meta: { version: oldVersion },
23+
data: {
24+
AccountsController: mockAccountsControllerState,
25+
},
26+
};
27+
28+
const newStorage = await migrate(oldStorage);
29+
expect(newStorage.meta).toStrictEqual({ version });
30+
});
31+
32+
it('updates selected account if it is not found in the list of accounts', async () => {
33+
const oldStorage = {
34+
meta: { version: oldVersion },
35+
data: {
36+
AccountsController: {
37+
...mockAccountsControllerState,
38+
internalAccounts: {
39+
...mockAccountsControllerState.internalAccounts,
40+
selectedAccount: 'unknown id',
41+
},
42+
},
43+
},
44+
};
45+
46+
const newStorage = await migrate(oldStorage);
47+
const {
48+
internalAccounts: { selectedAccount },
49+
} = newStorage.data.AccountsController as AccountsControllerState;
50+
expect(selectedAccount).toStrictEqual(mockInternalAccount.id);
51+
expect(newStorage.data.AccountsController).toStrictEqual(
52+
mockAccountsControllerState,
53+
);
54+
});
55+
56+
it('does nothing if the selectedAccount is found in the list of accounts', async () => {
57+
const oldStorage = {
58+
meta: { version: oldVersion },
59+
data: {
60+
AccountsController: mockAccountsControllerState,
61+
},
62+
};
63+
64+
const newStorage = await migrate(oldStorage);
65+
expect(newStorage.data.AccountsController).toStrictEqual(
66+
mockAccountsControllerState,
67+
);
68+
});
69+
});

app/scripts/migrations/121.1.ts

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { AccountsControllerState } from '@metamask/accounts-controller';
2+
import { cloneDeep } from 'lodash';
3+
4+
type VersionedData = {
5+
meta: { version: number };
6+
data: Record<string, unknown>;
7+
};
8+
9+
export const version = 121.1;
10+
11+
/**
12+
* This migration removes depreciated `Txcontroller` key if it is present in state.
13+
*
14+
* @param originalVersionedData - Versioned MetaMask extension state, exactly
15+
* what we persist to dist.
16+
* @param originalVersionedData.meta - State metadata.
17+
* @param originalVersionedData.meta.version - The current state version.
18+
* @param originalVersionedData.data - The persisted MetaMask state, keyed by
19+
* controller.
20+
* @returns Updated versioned MetaMask extension state.
21+
*/
22+
export async function migrate(
23+
originalVersionedData: VersionedData,
24+
): Promise<VersionedData> {
25+
const versionedData = cloneDeep(originalVersionedData);
26+
versionedData.meta.version = version;
27+
transformState(versionedData.data);
28+
return versionedData;
29+
}
30+
31+
function transformState(state: Record<string, unknown>) {
32+
const accountsControllerState = state?.AccountsController as
33+
| AccountsControllerState
34+
| undefined;
35+
36+
if (
37+
accountsControllerState &&
38+
Object.values(accountsControllerState?.internalAccounts.accounts).length >
39+
0 &&
40+
!accountsControllerState?.internalAccounts.accounts[
41+
accountsControllerState?.internalAccounts.selectedAccount
42+
]
43+
) {
44+
accountsControllerState.internalAccounts.selectedAccount = Object.values(
45+
accountsControllerState?.internalAccounts.accounts,
46+
)[0].id;
47+
}
48+
return state;
49+
}

app/scripts/migrations/126.test.ts

+27-46
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,50 @@
1-
import { AccountsControllerState } from '@metamask/accounts-controller';
2-
import { createMockInternalAccount } from '../../../test/jest/mocks';
31
import { migrate, version } from './126';
42

53
const oldVersion = 125;
64

7-
const mockInternalAccount = createMockInternalAccount();
8-
const mockAccountsControllerState: AccountsControllerState = {
9-
internalAccounts: {
10-
accounts: {
11-
[mockInternalAccount.id]: mockInternalAccount,
12-
},
13-
selectedAccount: mockInternalAccount.id,
14-
},
15-
};
16-
17-
describe('migration #126', () => {
18-
afterEach(() => jest.resetAllMocks());
19-
5+
describe(`migration #${version}`, () => {
206
it('updates the version metadata', async () => {
217
const oldStorage = {
228
meta: { version: oldVersion },
23-
data: {
24-
AccountsController: mockAccountsControllerState,
25-
},
9+
data: {},
2610
};
2711

2812
const newStorage = await migrate(oldStorage);
13+
2914
expect(newStorage.meta).toStrictEqual({ version });
3015
});
3116

32-
it('updates selected account if it is not found in the list of accounts', async () => {
33-
const oldStorage = {
34-
meta: { version: oldVersion },
35-
data: {
36-
AccountsController: {
37-
...mockAccountsControllerState,
38-
internalAccounts: {
39-
...mockAccountsControllerState.internalAccounts,
40-
selectedAccount: 'unknown id',
41-
},
42-
},
17+
it('Does nothing if `providerConfig` is not in the network controller state', async () => {
18+
const oldState = {
19+
NetworkController: {
20+
selectedNetworkClientId: 'mainnet',
4321
},
4422
};
4523

46-
const newStorage = await migrate(oldStorage);
47-
const {
48-
internalAccounts: { selectedAccount },
49-
} = newStorage.data.AccountsController as AccountsControllerState;
50-
expect(selectedAccount).toStrictEqual(mockInternalAccount.id);
51-
expect(newStorage.data.AccountsController).toStrictEqual(
52-
mockAccountsControllerState,
53-
);
24+
const transformedState = await migrate({
25+
meta: { version: oldVersion },
26+
data: oldState,
27+
});
28+
29+
expect(transformedState.data).toStrictEqual(oldState);
5430
});
5531

56-
it('does nothing if the selectedAccount is found in the list of accounts', async () => {
57-
const oldStorage = {
58-
meta: { version: oldVersion },
59-
data: {
60-
AccountsController: mockAccountsControllerState,
32+
it('Removes providerConfig from the network controller state', async () => {
33+
const oldState = {
34+
NetworkController: {
35+
selectedNetworkClientId: 'mainnet',
36+
providerConfig: {
37+
chainId: '0x1',
38+
ticker: 'ETH',
39+
} as object | undefined,
6140
},
6241
};
42+
const transformedState = await migrate({
43+
meta: { version: oldVersion },
44+
data: oldState,
45+
});
6346

64-
const newStorage = await migrate(oldStorage);
65-
expect(newStorage.data.AccountsController).toStrictEqual(
66-
mockAccountsControllerState,
67-
);
47+
delete oldState.NetworkController.providerConfig;
48+
expect(transformedState.data).toStrictEqual(oldState);
6849
});
6950
});

app/scripts/migrations/126.ts

+10-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AccountsControllerState } from '@metamask/accounts-controller';
1+
import { hasProperty, isObject } from '@metamask/utils';
22
import { cloneDeep } from 'lodash';
33

44
type VersionedData = {
@@ -9,14 +9,12 @@ type VersionedData = {
99
export const version = 126;
1010

1111
/**
12-
* This migration removes depreciated `Txcontroller` key if it is present in state.
12+
* This migration removes `providerConfig` from the network controller state.
1313
*
14-
* @param originalVersionedData - Versioned MetaMask extension state, exactly
15-
* what we persist to dist.
14+
* @param originalVersionedData - Versioned MetaMask extension state, exactly what we persist to dist.
1615
* @param originalVersionedData.meta - State metadata.
1716
* @param originalVersionedData.meta.version - The current state version.
18-
* @param originalVersionedData.data - The persisted MetaMask state, keyed by
19-
* controller.
17+
* @param originalVersionedData.data - The persisted MetaMask state, keyed by controller.
2018
* @returns Updated versioned MetaMask extension state.
2119
*/
2220
export async function migrate(
@@ -28,22 +26,14 @@ export async function migrate(
2826
return versionedData;
2927
}
3028

31-
function transformState(state: Record<string, unknown>) {
32-
const accountsControllerState = state?.AccountsController as
33-
| AccountsControllerState
34-
| undefined;
35-
29+
function transformState(
30+
state: Record<string, unknown>,
31+
): Record<string, unknown> {
3632
if (
37-
accountsControllerState &&
38-
Object.values(accountsControllerState?.internalAccounts.accounts).length >
39-
0 &&
40-
!accountsControllerState?.internalAccounts.accounts[
41-
accountsControllerState?.internalAccounts.selectedAccount
42-
]
33+
hasProperty(state, 'NetworkController') &&
34+
isObject(state.NetworkController)
4335
) {
44-
accountsControllerState.internalAccounts.selectedAccount = Object.values(
45-
accountsControllerState?.internalAccounts.accounts,
46-
)[0].id;
36+
delete state.NetworkController.providerConfig;
4737
}
4838
return state;
4939
}

app/scripts/migrations/127.test.ts

-50
This file was deleted.

app/scripts/migrations/127.ts

-39
This file was deleted.

app/scripts/migrations/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ const migrations = [
138138
require('./120.5'),
139139
require('./120.6'),
140140
require('./121'),
141+
require('./121.1'),
141142
require('./121.2'),
142143
require('./122'),
143144
require('./123'),
144145
require('./124'),
145146
require('./125'),
146147
require('./126'),
147-
require('./127'),
148148
];
149149

150150
export default migrations;

0 commit comments

Comments
 (0)