Skip to content

Commit

Permalink
Add migration for updated GasFeeController
Browse files Browse the repository at this point in the history
  • Loading branch information
kylanhurt committed Jun 6, 2024
1 parent 97902b0 commit 9592677
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
98 changes: 98 additions & 0 deletions app/store/migrations/042.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import migration from './042';
import { merge } from 'lodash';
import initialRootState from '../../util/test/initial-root-state';
import { captureException } from '@sentry/react-native';

const oldState = {
engine: {
backgroundState: {
GasFeeController: {
gasFeeEstimates: {},
estimatedGasFeeTimeBounds: {},
gasEstimateType: 'none',
gasFeeEstimatesByChainId: {},
},
},
},
};

const expectedNewState = {
engine: {
backgroundState: {
GasFeeController: {
gasFeeEstimates: {},
estimatedGasFeeTimeBounds: {},
gasEstimateType: 'none',
gasFeeEstimatesByChainId: {},
nonRPCGasFeeApisDisabled: false,
},
},
},
};

jest.mock('@sentry/react-native', () => ({
captureException: jest.fn(),
}));
const mockedCaptureException = jest.mocked(captureException);

describe('Migration #42', () => {
beforeEach(() => {
jest.restoreAllMocks();
jest.resetAllMocks();
});

const invalidStates = [
{
state: merge({}, initialRootState, {
engine: null,
}),
errorMessage:
"FATAL ERROR: Migration 42: Invalid engine state error: 'object'",
scenario: 'engine state is invalid',
},
{
state: merge({}, initialRootState, {
engine: {
backgroundState: null,
},
}),
errorMessage:
"FATAL ERROR: Migration 42: Invalid engine backgroundState error: 'object'",
scenario: 'backgroundState is invalid',
},
{
state: merge({}, initialRootState, {
engine: {
backgroundState: {
GasFeeController: null,
},
},
}),
errorMessage:
"FATAL ERROR: Migration 42: Invalid GasFeeController state error: 'null'",
scenario: 'GasFeeController state is invalid',
},
];

for (const { errorMessage, scenario, state } of invalidStates) {
it(`should capture exception if ${scenario}`, async () => {
const newState = await migration(state);

expect(newState).toStrictEqual(state);
expect(mockedCaptureException).toHaveBeenCalledWith(expect.any(Error));
expect(mockedCaptureException.mock.calls[0][0].message).toBe(
errorMessage,
);
});
}

it('should contain new property nonRPCGasFeeApisDisabled = false in GasFeeController state ', async () => {
const newState = await migration(oldState);
expect(newState).toStrictEqual(expectedNewState);

expect(
// @ts-expect-error: ignore for testing purposes: new state is type unknown
newState.engine.backgroundState.GasFeeController.nonRPCGasFeeApisDisabled,
).toEqual(false);
});
});
32 changes: 32 additions & 0 deletions app/store/migrations/042.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { captureException } from '@sentry/react-native';
import { isObject } from '@metamask/utils';
import { ensureValidState } from './util';

/**
* Migration to update state of GasFeeController
*
* @param state Persisted Redux state
* @returns
*/
export default function migrate(state: unknown) {
if (!ensureValidState(state, 42)) {
return state;
}

const gasFeeControllerState = state.engine.backgroundState.GasFeeController;

if (!isObject(gasFeeControllerState)) {
captureException(
new Error(
`FATAL ERROR: Migration 42: Invalid GasFeeController state error: '${JSON.stringify(
gasFeeControllerState,
)}'`,
),
);
return state;
}

gasFeeControllerState.nonRPCGasFeeApisDisabled = false;

return state;
}
2 changes: 2 additions & 0 deletions app/store/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import migration38 from './038';
import migration39 from './039';
import migration40 from './040';
import migration41 from './041';
import migration42 from './042';

type MigrationFunction = (state: unknown) => unknown;
type AsyncMigrationFunction = (state: unknown) => Promise<unknown>;
Expand Down Expand Up @@ -96,6 +97,7 @@ export const migrationList: MigrationsList = {
39: migration39,
40: migration40,
41: migration41,
42: migration42,
};

// Enable both synchronous and asynchronous migrations
Expand Down

0 comments on commit 9592677

Please sign in to comment.