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

fix: Update STX Banner Alert, include chainSupportsSmartTransactions #29911

Merged
merged 18 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
16 changes: 8 additions & 8 deletions app/scripts/migrations/135.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('migration #135', () => {
expect(newStorage.meta).toStrictEqual({ version: 135 });
});

it('should set stx opt-in to true and migration flag when stx opt-in status is null', async () => {
it('should set stx opt-in to true and mark as migration-enabled when opt-in status is null', async () => {
const oldStorage: VersionedData = {
meta: { version: prevVersion },
data: {
Expand All @@ -41,7 +41,7 @@ describe('migration #135', () => {
).toBe(true);
});

it('should set stx opt-in to true and migration flag when stx opt-in status is undefined', async () => {
it('should set stx opt-in to true and mark as migration-enabled when opt-in status is undefined', async () => {
const oldStorage: VersionedData = {
meta: { version: prevVersion },
data: {
Expand All @@ -60,7 +60,7 @@ describe('migration #135', () => {
).toBe(true);
});

it('should set stx opt-in to true and migration flag when stx opt-in is false and no existing mainnet smart transactions', async () => {
it('should set stx opt-in to true and mark as migration-enabled when opt-in is false and no existing mainnet transactions', async () => {
const oldStorage: VersionedData = {
meta: { version: prevVersion },
data: {
Expand Down Expand Up @@ -91,7 +91,7 @@ describe('migration #135', () => {
).toBe(true);
});

it('should not change stx opt-in when stx opt-in is false but has existing smart transactions, but should set migration flag', async () => {
it('should preserve disabled stx state when user has transaction history', async () => {
const oldStorage: VersionedData = {
meta: { version: prevVersion },
data: {
Expand All @@ -118,10 +118,10 @@ describe('migration #135', () => {
expect(
newStorage.data.PreferencesController?.preferences
?.smartTransactionsMigrationApplied,
).toBe(true);
).toBeUndefined();
});

it('should not change stx opt-in when stx opt-in is already true, but should set migration flag', async () => {
it('should preserve existing stx enabled state', async () => {
const oldStorage: VersionedData = {
meta: { version: prevVersion },
data: {
Expand All @@ -141,7 +141,7 @@ describe('migration #135', () => {
expect(
newStorage.data.PreferencesController?.preferences
?.smartTransactionsMigrationApplied,
).toBe(true);
).toBeUndefined();
});

it('should initialize preferences object if it does not exist', async () => {
Expand All @@ -161,7 +161,7 @@ describe('migration #135', () => {
expect(
newStorage.data.PreferencesController?.preferences
?.smartTransactionsMigrationApplied,
).toBe(true);
).toBeUndefined();
});

it('should capture exception if PreferencesController state is invalid', async () => {
Expand Down
11 changes: 6 additions & 5 deletions app/scripts/migrations/135.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ function transformState(state: VersionedData['data']) {
smartTransactionsOptInStatus: true,
smartTransactionsMigrationApplied: true,
};
} else {
state.PreferencesController.preferences = {
...state.PreferencesController.preferences,
smartTransactionsMigrationApplied: true,
};
// // Removing this block, as we want to set migration flag only when user has opted in
dan437 marked this conversation as resolved.
Show resolved Hide resolved
// } else {
// state.PreferencesController.preferences = {
// ...state.PreferencesController.preferences,
// smartTransactionsMigrationApplied: true,
// };
}

return state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { renderWithProvider } from '../../../../../test/jest/rendering';
import configureStore from '../../../../store/store';
import { AlertTypes } from '../../../../../shared/constants/alerts';
import { setAlertEnabledness } from '../../../../store/actions';
import { mockNetworkState } from '../../../../../test/stub/networks';
import { CHAIN_IDS } from '../../../../../shared/constants/network';
import { SmartTransactionsBannerAlert } from './smart-transactions-banner-alert';

type TestConfirmContextValue = {
Expand Down Expand Up @@ -56,10 +58,41 @@ describe('SmartTransactionsBannerAlert', () => {
smartTransactionsOptInStatus: true,
smartTransactionsMigrationApplied: true,
},
featureFlags: {
smartTransactionsEnabled: true,
},
smartTransactionsFeatureFlags: {
enabled: true,
},
swapsState: {
swapsFeatureFlags: {
ethereum: {
extensionActive: true,
mobileActive: false,
smartTransactions: {
expectedDeadline: 45,
maxDeadline: 150,
extensionReturnTxHashAsap: false,
},
},
smartTransactions: {
extensionActive: true,
mobileActive: false,
},
},
},
smartTransactionsState: {
liveness: true,
},
...mockNetworkState({
id: 'network-configuration-id-1',
chainId: CHAIN_IDS.MAINNET,
rpcUrl: 'https://mainnet.infura.io/v3/',
}),
},
};

it('renders banner when alert is enabled, STX is opted in, and migration is applied', () => {
it('renders banner when all conditions are met', () => {
const store = configureStore(mockState);
renderWithProvider(<SmartTransactionsBannerAlert />, store);

Expand All @@ -79,14 +112,12 @@ describe('SmartTransactionsBannerAlert', () => {

it('does not render when alert is disabled', () => {
const disabledState = {
...mockState,
metamask: {
...mockState.metamask,
alertEnabledness: {
[AlertTypes.smartTransactionsMigration]: false,
},
preferences: {
smartTransactionsOptInStatus: true,
smartTransactionsMigrationApplied: true,
},
},
};
const store = configureStore(disabledState);
Expand All @@ -99,12 +130,11 @@ describe('SmartTransactionsBannerAlert', () => {

it('does not render when migration has not been applied', () => {
const noMigrationState = {
...mockState,
metamask: {
alertEnabledness: {
[AlertTypes.smartTransactionsMigration]: true,
},
...mockState.metamask,
preferences: {
smartTransactionsOptInStatus: true,
...mockState.metamask.preferences,
smartTransactionsMigrationApplied: false,
},
},
Expand All @@ -117,6 +147,67 @@ describe('SmartTransactionsBannerAlert', () => {
).not.toBeInTheDocument();
});

it('does not render when chain does not support smart transactions', () => {
const unsupportedChainState = {
...mockState,
metamask: {
...mockState.metamask,
...mockNetworkState({
id: 'network-configuration-id-2',
chainId: CHAIN_IDS.POLYGON,
rpcUrl: 'https://polygon-rpc.com',
}),
},
};
const store = configureStore(unsupportedChainState);
renderWithProvider(<SmartTransactionsBannerAlert />, store);

expect(
screen.queryByTestId('smart-transactions-banner-alert'),
).not.toBeInTheDocument();
});

it('does not render when smart transactions preference is disabled', () => {
const disabledPreferenceState = {
...mockState,
metamask: {
...mockState.metamask,
preferences: {
...mockState.metamask.preferences,
smartTransactionsOptInStatus: false, // Add this
},
featureFlags: {
smartTransactionsEnabled: false,
},
smartTransactionsFeatureFlags: {
enabled: false,
},
swapsState: {
swapsFeatureFlags: {
ethereum: {
extensionActive: false,
smartTransactions: {
expectedDeadline: 45,
maxDeadline: 150,
extensionReturnTxHashAsap: false,
},
},
smartTransactions: {
extensionActive: false,
mobileActive: false,
},
},
},
},
};
const store = configureStore(disabledPreferenceState);
renderWithProvider(<SmartTransactionsBannerAlert />, store);

expect(
screen.queryByTestId('smart-transactions-banner-alert'),
).not.toBeInTheDocument();
});

it('dismisses banner when close button or link is clicked', () => {
const store = configureStore(mockState);

Expand Down Expand Up @@ -224,7 +315,6 @@ describe('SmartTransactionsBannerAlert', () => {

it('handles being outside of ConfirmContext correctly', () => {
const store = configureStore(mockState);

renderWithProvider(<SmartTransactionsBannerAlert />, store);

expect(
Expand All @@ -235,12 +325,10 @@ describe('SmartTransactionsBannerAlert', () => {
it('automatically dismisses banner when Smart Transactions is manually disabled', () => {
const store = configureStore({
metamask: {
alertEnabledness: {
[AlertTypes.smartTransactionsMigration]: true,
},
...mockState.metamask,
preferences: {
...mockState.metamask.preferences,
smartTransactionsOptInStatus: false,
smartTransactionsMigrationApplied: true,
},
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import {
getSmartTransactionsOptInStatusInternal,
getSmartTransactionsMigrationAppliedInternal,
} from '../../../../../shared/modules/selectors/smart-transactions';
import {
getCurrentChainSupportsSmartTransactions,
getSmartTransactionsPreferenceEnabled,
} from '../../../../../shared/modules/selectors';

type MarginType = 'default' | 'none' | 'noTop' | 'onlyTop';

Expand Down Expand Up @@ -55,6 +59,14 @@ export const SmartTransactionsBannerAlert: React.FC<SmartTransactionsBannerAlert
getSmartTransactionsMigrationAppliedInternal,
);

const chainSupportsSmartTransactions = useSelector(
getCurrentChainSupportsSmartTransactions,
);

const smartTransactionsPreferenceEnabled = useSelector(
getSmartTransactionsPreferenceEnabled,
);

const dismissAlert = useCallback(() => {
setAlertEnabledness(AlertTypes.smartTransactionsMigration, false);
}, []);
Expand All @@ -68,7 +80,9 @@ export const SmartTransactionsBannerAlert: React.FC<SmartTransactionsBannerAlert
const alertConditions =
alertEnabled &&
smartTransactionsOptIn &&
smartTransactionsMigrationApplied;
smartTransactionsMigrationApplied &&
chainSupportsSmartTransactions &&
smartTransactionsPreferenceEnabled;

const shouldRender =
currentConfirmation === null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ describe('TransactionAlerts', () => {
});

describe('Smart Transactions Migration Alert', () => {
it('shows when alert is enabled, opted in, and migration applied', () => {
it('shows when stx was enabled by migration', () => {
const { getByTestId } = render({
componentProps: {
txData: {
Expand All @@ -585,15 +585,35 @@ describe('Smart Transactions Migration Alert', () => {
networkConfigurationsByChainId: {
[CHAIN_ID_MOCK]: {
chainId: CHAIN_ID_MOCK,
rpcEndpoints: [
{
rpcUrl: 'https://mainnet.infura.io/v3/',
networkClientId: '1',
blockExplorerUrl: 'https://etherscan.io',
},
],
defaultBlockExplorerUrlIndex: 0,
},
},
selectedNetworkClientId: '1',
alertEnabledness: {
[AlertTypes.smartTransactionsMigration]: true,
},
preferences: {
smartTransactionsOptInStatus: true,
smartTransactionsMigrationApplied: true,
},
featureFlags: {
smartTransactionsEnabled: true,
},
swapsState: {
swapsFeatureFlags: {
smartTransactions: {
extensionActive: true,
mobileActive: false,
},
},
},
},
},
});
Expand Down
Loading