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: enable STX by default with migration and notification #12857

Draft
wants to merge 32 commits into
base: main
Choose a base branch
from

Conversation

httpJunkie
Copy link

@httpJunkie httpJunkie commented Jan 8, 2025

Description

This PR enables Smart Transactions (STX) in MetaMask Mobile by default through migration number (TBD) for users who have either opted out or haven't interacted with the STX toggle, provided they have no recorded STX activity.

How it works (if user does not have STX enabled or prior STX Transaction history:

  • Upon Migration STX is enabled in Settings:
  • A banner alert will displays on the following transaction confirmations
    • Send Confirmation flow
    • Swaps confirmation flow
    • Contract deployment & interactions (deploy, minting, etc.)

In the case a user migrates from a previous version of the Mobile app and the migration runs and sets STX toggle "ON" in Settings > Advanced > Smart Transactions, they will receive an Alert on transaction confirmation screens until dismissed, or by clicking on the "Higher success rates" link within the alert. If they click on the link in the banner alert they will get sent to: What is 'Smart Transactions'? for more information. When returning to the confirmation they just navigated from the banner alert should not show and should never show again.

Edge Cases:

If a user is new and setting up a wallet for the first time, they will not receive the Banner Alert. If a user imports a new wallet during a fresh install of the extension on a new browser or recovers a wallet, it's possible they may not see the alert if STX was on in a previous install. The STX Banner Alert is dismissed and will not show again if a user is in the state to get shown the banner and toggles STX off independently even if they do not physically dismiss the STX Banner Alert.

Migration Logic:

  1. If smartTransactionsOptInStatus is null (new/never interacted)
  • Sets status to true
  • Enables notification flag
  1. If status is false (previously opted out):
  • With no Ethereum Mainnet STX activity: Sets to true with notification
  • With existing Mainnet STX activity: Preserves user preference
  1. If status is true: No changes needed

UI Components:

TBD, still exploring the Mobile UI for implementation solution.

Running Unit Tests

Migration Test:

yarn jest "./app/store/migrations/096.test.ts" --no-cache

Hook Test:

yarn jest "./app/components/hooks/useSmartTransactionsEnabled/useSmartTransactionsEnabled.test.ts" --no-cache

Component Test:

yarn jest "./app/components/Views/confirmations/components/SmartTransactionsEnabledBanner/SmartTransactionsEnabledBanner.test.ts" --no-cache

Manual testing steps

Test Migration (using a wallet/account with no STX Transactions)
Start with an older repo version:

Terminal #1

git checkout tags/v7.32.0
yarn setup && yarn watch

Terminal #1

yarn start:ios
  1. Import or setup a wallet without STX transactions, launch the wallet (do not enable STX if prompted), check that toggle is OFF in: Settings > Advanced > Smart Transactions

  2. Switch to feature branch and run app:

Terminal #1

git checkout tags/v7.32.0
yarn setup && yarn watch

Terminal #1

yarn start:ios
  1. Test that Alert only shows during confirmation screens for transactions and contract interactions, but not for signing
  2. Create a Send transaction to your own wallet for 0.0001 ETH
  3. Ensure that Smart Transactions Banner Alert IS showing
  4. Start a Swaps transaction on Ethereum Mainnet
    7 Ensure that Smart Transactions Banner Alert IS showing
  5. Try several Signs (ETH Sign, Personal Sign, Sign Typed Data, etc..) and ensure the STX Banner Alert does not show on those confirmations screens.
  6. Ensure that link in alert "Higher success rates" link (inspect) goes to: What is 'Smart Transactions'?
  7. Dismiss the Smart Transactions Banner Alert
  8. Create a Send transaction to your own wallet for 0.0001 ETH
  9. Ensure that Smart Transactions Banner Alert IS NOT showing

Screenshots/Recordings

N/A yet...

Before

After

Pre-merge author checklist

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

- Enable STX for new users or those that have not enabled in settings
- Preserves settings for users who explicitly opted out and have existing STX transactions
- Adds a migration flag to track this change
…ror messages using team's established pattern.
- Mock Sentry error handling ensuring we can track when errors are captured
- Setup `beforeEach()` restore/reset for all mocks
-  Define test cases for error handling
- Each case has: invalid input state, expected error message, and description
- Invalid States Test: how migration handles malformed data
- Runs each invalid state test case
- Verifies: Invalid state returns unchanged, Error is captured by Sentry, Error message matches expected format

Also, Setup first test to ensure STX is enabled and migration flag is set when undefined opt-in status
- Null status test, similar to undefined opt-in status test
- Tests migration with null status
- Verifies that STX is enabled and migration flag is set
@httpJunkie httpJunkie added team-confirmations Push issues to confirmations team team-transactions Transactions team labels Jan 8, 2025
@httpJunkie httpJunkie self-assigned this Jan 8, 2025
@httpJunkie httpJunkie requested a review from a team as a code owner January 8, 2025 06:09
@httpJunkie httpJunkie marked this pull request as draft January 8, 2025 06:09
Copy link
Contributor

github-actions bot commented Jan 8, 2025

CLA Signature Action:

Thank you for your submission, we really appreciate it. We ask that you read and sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just by adding a comment to this pull request with this exact sentence:

I have read the CLA Document and I hereby sign the CLA

By commenting with the above message you are agreeing to the terms of the CLA. Your account will be recorded as agreeing to our CLA so you don't need to sign it again for future contributions to this repository.

0 out of 1 committers have signed the CLA.
@httpJunkie

@metamaskbot metamaskbot added the INVALID-PR-TEMPLATE PR's body doesn't match template label Jan 8, 2025
- create opt-out test
- tests that users explicit opt-out is respected when STX transactions exist
- verifies migration flag is set but preference is unchanged
- create opt-out test
- tests that users explicit opt-out is respected when STX transactions exist
- verifies migration flag is set but preference is unchanged
- tests that opt-out is overridden when user has no transaction history
- verifies migration flag is set and preference is changed
- added test for initializing preferences
- added test for when STX is already enabled
- use `merge` with `initialRootState` consistently across all tests
const invalidStates = [
{
state: null,
errorMessage: "FATAL ERROR: Migration 65: Invalid state error: 'null'",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you can save 65 into a constant and use that one. E.g.

const migrationVersion = 65;

- uses redux selectors to access the state
- checks flags we need (opt-in status and migration applied)
- Returns (isEnabled, isMigrationApplied) for our banner to use
- Provides proper TypeScript types
- check default states
- ensure proper state reading
- handle partial states
- test conditional rendering
- check text content
- ensure close button functionality works
- ensure link functionality works
- useSmartTransactionsEnabled enhanced to include dismiss functionality
- add a dispatch action to update the smartTransactionsOptInStatus
- remove external onClose prop from SmartTransactionsEnabledBanner in favor of using the hook's dismiss functionality
- update component and hook tests to account for new functionality

These changes require more changes in the next commit to add the smartTransactionsBannerDismissed field to our PreferencesController state as well to add a reducer case for SET_SMART_TRANSACTIONS_BANNER_DISMISSED and update any types/interfaces in our codebase that reference PreferencesController.
…nitialize it. Update the migration and test to account for the new change
@@ -74,3 +74,10 @@ export function setTokenSortConfig(tokenSortConfig) {
tokenSortConfig,
};
}

export function setSmartTransactionsBannerDismissed(bannerDismissed) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: to make it clearer, I would call it setSmartTransactionsMigrationBannerDismissed


export function setSmartTransactionsBannerDismissed(bannerDismissed) {
return {
type: 'SET_SMART_TRANSACTIONS_BANNER_DISMISSED',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SET_SMART_TRANSACTIONS_MIGRATION_BANNER_DISMISSED

import { SmartTransactionsEnabledBannerProps } from './SmartTransactionsEnabledBanner.types';
import useSmartTransactionsEnabled from '../../../../hooks/useSmartTransactionsEnabled/useSmartTransactionsEnabled';

const SMART_TRANSACTIONS_LEARN_MORE = AppConstants.URLS.SMART_TXS;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this constant is only used in 1 place, you can just delete it and use it there directly. I would also rename it a little:

Linking.openURL(AppConstants.URLS.SMART_TRANSACTIONS);


const SET_SMART_TRANSACTIONS_BANNER_DISMISSED = 'SET_SMART_TRANSACTIONS_BANNER_DISMISSED';

const useSmartTransactionsEnabled = () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useSmartTransactionsEnabled naming is very similar to existing selectSmartTransactionsEnabled, which can lead to confusion.

If you see the selectShouldUseSmartTransaction selector, it checks 2 things:

  • isSmartTransactionsEnabled, which indicates if the STX feature is even available, no matter what's in Settings
  • smartTransactionsOptInStatus, which indicates if it's on or off in Settings
    Both have to be true in order for the mobile app to use smart transactions.

I see that in the hooks folder not many files use the state: RootState code. Either we should rename this hook since there is another function with a very similar name and different meaning or we can create individual selectors for isMigrationApplied, isBannerDismissed, etc. and remove this hook

@@ -0,0 +1,12 @@
import { StyleSheet, ViewStyle } from 'react-native';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we call this and other files SmartTransactionsMigrationBanner?

…nsure that styles are applied to match the mockup design and add the Banner Alter to the SendFlow Confirmation page.
…k is viewed. Update useSmartTransactionsEnabled hook to set feature flags for smartTransactionsBannerDismissed. Remove action from app/actions/settings. Test is still failing (TBD)
- update the Engine mock to be more explicit
- mock both the default export and the named exports of the Engine module
… to ensure:

- mock is clean at start
- mock was called exactly once
- arguments are correct
- Make types in PreferencesState more accurate knowing the values will exist after migration
- Prevent unnecessary null checks and TS hints and error checking
- Memoize shouldShowBanner calculation to prevent unnecessary recalculations when other state changes, follows basic practices for derived state only recalculating when dependencies changes
- Move smartTransactionsBannerDismissed from PreferencesController root to featureFlags object
- Add type safety improvements for feature flags initialization
- Add SmartTransactionsController state type guard
- Update migration test to check for banner state in featureFlags instead of root
- covers token transfer approval
- covers dapp and contract interaction like deploy a contract and mint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
INVALID-PR-TEMPLATE PR's body doesn't match template team-confirmations Push issues to confirmations team team-transactions Transactions team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants