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: migrate to native primary currency #8720

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const TransactionReviewEIP1559Update = ({
renderableTotalMinNative,
renderableTotalMinConversion,
renderableTotalMaxNative,
renderableTotalMaxConversion,
renderableGasFeeMaxConversion,
timeEstimateColor,
timeEstimate,
Expand Down Expand Up @@ -298,10 +299,8 @@ const TransactionReviewEIP1559Update = ({
grey={timeEstimateColor !== 'orange'}
orange={timeEstimateColor === 'orange'}
>
{switchNativeCurrencyDisplayOptions(
renderableGasFeeMaxNative,
renderableGasFeeMaxConversion,
)}
{renderableGasFeeMaxNative} {'\n'}
{renderableGasFeeMaxConversion}
</Text>
</Text>
</FadeAnimationView>
Expand Down Expand Up @@ -390,10 +389,8 @@ const TransactionReviewEIP1559Update = ({
{strings('transaction_review_eip1559.max_amount')}:
</Text>{' '}
<Text small noMargin>
{switchNativeCurrencyDisplayOptions(
renderableTotalMaxNative,
renderableGasFeeMaxConversion,
)}
{renderableTotalMaxNative} {'\n'}
{renderableTotalMaxConversion}
</Text>
</Text>
</FadeAnimationView>
Expand Down
66 changes: 66 additions & 0 deletions app/store/migrations/035.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import migrate, { PrimaryCurrency } from './035';
import { captureException } from '@sentry/react-native';

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

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

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

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

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

const newState = await migrate(oldState);
expect(newState).toStrictEqual(oldState);
expect(mockedCaptureException).toHaveBeenCalledWith(expect.any(Error));
expect(mockedCaptureException.mock.calls[0][0].message).toBe(
`Migration 35: state.settings.primaryCurrency does not exist: '{}'`,
);
});

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

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

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

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

export enum PrimaryCurrency {
ETH = 'ETH',
}

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

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

if (!hasProperty(state.settings, 'primaryCurrency')) {
captureException(
new Error(
`Migration 35: state.settings.primaryCurrency does not exist: '${JSON.stringify(
state.settings,
)}'`,
),
);
}

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 @@ -35,6 +35,7 @@ import migration31 from './031';
import migration32 from './032';
import migration33 from './033';
import migration34 from './034';
import migration35 from './035';

// We do not keep track of the old state
// We create this type for better readability
Expand Down Expand Up @@ -78,6 +79,7 @@ export const migrations: MigrationManifest = {
32: migration32 as unknown as AsyncMigration,
33: migration33 as unknown as AsyncMigration,
34: migration34 as unknown as AsyncMigration,
35: migration35 as unknown as AsyncMigration,
};

// The latest (i.e. highest) version number.
Expand Down
Loading