Skip to content

Commit

Permalink
Merge branch 'main' into circles-metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmurdoch authored Feb 7, 2025
2 parents eae1546 + 335b3bb commit a0c0b0e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NetworkConfiguration } from '@metamask/network-controller';
import { migrate, version } from './142.1';
import { migrate, version } from './134.1';

describe(`Migration ${version}`, () => {
let originalSentry: typeof global.sentry;
Expand All @@ -21,14 +21,14 @@ describe(`Migration ${version}`, () => {
sentryCaptureExceptionMock.mockClear();
});

it('updates the meta version to 142.1 regardless of state content', async () => {
it('updates the meta version to 134.1 regardless of state content', async () => {
const dummyState = {
meta: { version: 0 },
data: {},
};

const migrated = await migrate(dummyState);
expect(migrated.meta.version).toBe(142.1);
expect(migrated.meta.version).toBe(134.1);
});

it('returns original state if AccountsController is missing', async () => {
Expand All @@ -41,9 +41,8 @@ describe(`Migration ${version}`, () => {
};
const result = await migrate(originalState);

expect(sentryCaptureExceptionMock).toHaveBeenCalled();
expect(result.data).toEqual(originalState.data);
expect(result.meta.version).toBe(142.1);
expect(result.meta.version).toBe(134.1);
});

it('returns original state if AccountsController is not an object', async () => {
Expand Down Expand Up @@ -242,7 +241,6 @@ describe(`Migration ${version}`, () => {
};

const result = await migrate(originalState);
expect(sentryCaptureExceptionMock).toHaveBeenCalled();
expect(result.data).toEqual(originalState.data);
});

Expand Down Expand Up @@ -313,6 +311,43 @@ describe(`Migration ${version}`, () => {
>;
expect(tokensControllerState.tokens).toEqual(originalTokens);

expect(result.meta.version).toBe(142.1);
expect(result.meta.version).toBe(134.1);
});

it('returns original state and logs error if tokens is not empty but allTokensForChain is not an object', async () => {
const originalState = {
meta: { version: 0 },
data: {
AccountsController: {
internalAccounts: { selectedAccount: '0x123' },
},
NetworkController: {
selectedNetworkClientId: 'mainnet',
networkConfigurationsByChainId: {
'0x1': {
rpcEndpoints: [{ networkClientId: 'mainnet' }],
},
},
},
TokensController: {
tokens: [{ address: '0xtokenA' }], // Non-empty tokens array
allTokens: {
'0x1': null, // allTokensForChain is not an object
},
},
},
};

const result = await migrate(originalState);

expect(sentryCaptureExceptionMock).toHaveBeenCalledWith(
expect.objectContaining({
message: expect.stringContaining(
`Migration ${version}: tokens is not an empty array, but allTokensForChain is not an object.`,
),
}),
);

expect(result.data).toEqual(originalState.data);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type VersionedData = {
data: Record<string, unknown>;
};

export const version = 142.1;
export const version = 134.1;

/**
* This migration attempts to reset `TokensController.tokens` to the list of tokens
Expand Down Expand Up @@ -168,17 +168,27 @@ function transformState(
return state;
}

const { tokens } = tokensControllerState;
const { allTokens } = tokensControllerState;
const allTokensForChain = allTokens[currentChainId];
if (!isObject(allTokensForChain)) {

if (
Array.isArray(tokens) &&
tokens.length > 0 &&
!isObject(allTokensForChain)
) {
global.sentry?.captureException?.(
new Error(
`Migration ${version}: allTokens["${currentChainId}"] is missing or not an object.`,
`Migration ${version}: tokens is not an empty array, but allTokensForChain is not an object.`,
),
);
return state;
}

if (!isObject(allTokensForChain)) {
return state;
}

const accountTokens = allTokensForChain[selectedAccount];
if (!Array.isArray(accountTokens)) {
global.sentry?.captureException?.(
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ const migrations = [
require('./133.1'),
require('./133.2'),
require('./134'),
require('./134.1'),
require('./135'),
require('./136'),
require('./137'),
Expand All @@ -166,7 +167,6 @@ const migrations = [
require('./140'),
require('./141'),
require('./142'),
require('./142.1'),
require('./143'),
];

Expand Down

0 comments on commit a0c0b0e

Please sign in to comment.