Skip to content

Commit

Permalink
feat: migrate to native primary currency
Browse files Browse the repository at this point in the history
  • Loading branch information
salimtb committed Mar 1, 2024
1 parent 330f415 commit 6bcf50d
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 35 deletions.
34 changes: 0 additions & 34 deletions app/components/Views/Settings/GeneralSettings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
setUseBlockieIcon,
setHideZeroBalanceTokens,
} from '../../../../actions/settings';
import PickComponent from '../../PickComponent';
import { toDataUrl } from '../../../../util/blockies.js';
import Jazzicon from 'react-native-jazzicon';
import { ThemeContext, mockTheme } from '../../../../util/theme';
Expand Down Expand Up @@ -150,10 +149,6 @@ class Settings extends PureComponent {
* Active search engine
*/
searchEngine: PropTypes.string,
/**
* Active primary currency
*/
primaryCurrency: PropTypes.string,
/**
* Show a BlockieIcon instead of JazzIcon
*/
Expand Down Expand Up @@ -284,7 +279,6 @@ class Settings extends PureComponent {
render() {
const {
currentCurrency,
primaryCurrency,
useBlockieIcon,
setUseBlockieIcon,
selectedAddress,
Expand Down Expand Up @@ -319,34 +313,6 @@ class Settings extends PureComponent {
</View>
</View>
</View>
<View style={styles.setting}>
<Text variant={TextVariant.BodyLGMedium}>
{strings('app_settings.primary_currency_title')}
</Text>
<Text
variant={TextVariant.BodyMD}
color={TextColor.Alternative}
style={styles.desc}
>
{strings('app_settings.primary_currency_desc')}
</Text>
{this.primaryCurrencyOptions && (
<View style={styles.accessory}>
<PickComponent
pick={this.selectPrimaryCurrency}
textFirst={strings(
'app_settings.primary_currency_text_first',
)}
valueFirst={'ETH'}
textSecond={strings(
'app_settings.primary_currency_text_second',
)}
valueSecond={'Fiat'}
selectedValue={primaryCurrency}
/>
</View>
)}
</View>
<View style={styles.setting}>
<Text variant={TextVariant.BodyLGMedium}>
{strings('app_settings.current_language')}
Expand Down
68 changes: 68 additions & 0 deletions app/store/migrations/032.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import migrate from './032';
import { captureException } from '@sentry/react-native';

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

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

it('should change the default primary currency state to Fiat', () => {
const oldState = {
settings: {
primaryCurrency: 'Fiat',
},
};

const newState = migrate(oldState);
expect(newState).toStrictEqual({
settings: {
primaryCurrency: 'ETH',
},
});
});

it('should throw error if primaryCurrency property is not defined', () => {
const oldState = {
settings: {},
};

const newState = migrate(oldState);
expect(newState).toStrictEqual(oldState);
expect(mockedCaptureException).toHaveBeenCalledWith(expect.any(Error));
expect(mockedCaptureException.mock.calls[0][0].message).toBe(
`Migration 30: Invalid state settings parameter: '${JSON.stringify(
oldState.settings,
)}'`,
);
});

it('should throw error if settings property is not defined', () => {
const oldState = {
settings: undefined,
};

const newState = migrate(oldState);
expect(newState).toStrictEqual(oldState);
expect(mockedCaptureException).toHaveBeenCalledWith(expect.any(Error));
expect(mockedCaptureException.mock.calls[0][0].message).toBe(
`Migration 30: Invalid settings state: '${typeof oldState.settings}'`,
);
});

it('should throw error if state is not defined', () => {
const oldState = undefined;

const newState = migrate(oldState);
expect(newState).toStrictEqual(oldState);
expect(mockedCaptureException).toHaveBeenCalledWith(expect.any(Error));
expect(mockedCaptureException.mock.calls[0][0].message).toBe(
`Migration 30: Invalid state: '${typeof oldState}'`,
);
});
});
49 changes: 49 additions & 0 deletions app/store/migrations/032.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { isObject, hasProperty } from '@metamask/utils';
import { captureException } from '@sentry/react-native';

enum PrimaryCurrency {
ETH = 'ETH',
Fiat = 'Fiat',
}

/**
* Migrate back to set primaryCurrency as 'ETH' by default
*
* @param {unknown} state - Redux state
* @returns
*/
export default function migrate(state: unknown) {
if (!isObject(state)) {
captureException(
new Error(`Migration 30: Invalid state: '${typeof state}'`),
);
return state;
}

if (!isObject(state.settings)) {
captureException(
new Error(
`Migration 30: Invalid settings state: '${typeof state.settings}'`,
),
);
return state;
}

if (!hasProperty(state.settings, 'primaryCurrency')) {
captureException(
new Error(
`Migration 30: Invalid state settings parameter: '${JSON.stringify(
state.settings,
)}'`,
),
);
return state;
}

if (!state.settings.primaryCurrency) {
return state;
}

state.settings.primaryCurrency = PrimaryCurrency.ETH;
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 @@ -32,6 +32,7 @@ import migration28 from './028';
import migration29 from './029';
import migration30 from './030';
import migration31 from './031';
import migration32 from './032';

// We do not keep track of the old state
// We create this type for better readability
Expand Down Expand Up @@ -70,6 +71,7 @@ export const migrations: MigrationManifest = {
29: migration29 as unknown as (state: OldState) => PersistedState,
30: migration30 as unknown as (state: OldState) => PersistedState,
31: migration31 as unknown as (state: OldState) => PersistedState,
32: migration32 as unknown as (state: OldState) => PersistedState,
};

// The latest (i.e. highest) version number.
Expand Down
2 changes: 1 addition & 1 deletion ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
Base64: cecfb41a004124895a7bcee567a89bae5a89d49b
boost: 57d2868c099736d80fcd648bf211b4431e51a558
boost: 7dcd2de282d72e344012f7d6564d024930a6a440
Branch: 4ac024cb3c29b0ef628048694db3c4cfa679beb0
BVLinearGradient: e3aad03778a456d77928f594a649e96995f1c872
CocoaAsyncSocket: 065fd1e645c7abab64f7a6a2007a48038fdc6a99
Expand Down

0 comments on commit 6bcf50d

Please sign in to comment.