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: Implement redesigned staking confirmation entry point #13361

Merged
merged 31 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
4ffc18d
Initial commit
OGPoyraz Feb 5, 2025
eb30eca
Merge branch 'main' into ogp/4078
OGPoyraz Feb 5, 2025
da4eb23
Update
OGPoyraz Feb 5, 2025
bfdf0df
Update
OGPoyraz Feb 5, 2025
574eccb
Merge branch 'main' into ogp/4078
OGPoyraz Feb 5, 2025
d91c948
Merge branch 'main' into ogp/4078
OGPoyraz Feb 6, 2025
b57115b
Fix suggestions
OGPoyraz Feb 6, 2025
c4d7683
Merge branch 'main' into ogp/4078
OGPoyraz Feb 6, 2025
e4b52cf
Update
OGPoyraz Feb 6, 2025
ff9283d
Fix unit tests
OGPoyraz Feb 6, 2025
b94c23e
Fix unit tests
OGPoyraz Feb 6, 2025
83a4325
Fix tests
OGPoyraz Feb 6, 2025
b8a3894
Fix unit tests
OGPoyraz Feb 6, 2025
78a2f9e
Support environment variable for featureflags
OGPoyraz Feb 6, 2025
f49bd0f
Fix lint
OGPoyraz Feb 6, 2025
f8d4829
Fix tests
OGPoyraz Feb 6, 2025
ca575f4
Adjust flat confirmations
OGPoyraz Feb 6, 2025
0b19c44
import sorts
OGPoyraz Feb 6, 2025
8144bd6
Remove unused code
OGPoyraz Feb 6, 2025
e914848
Fix import order
OGPoyraz Feb 6, 2025
a229262
Rename useTransactionMetadataRequest
OGPoyraz Feb 6, 2025
93768cc
Adjust test name
OGPoyraz Feb 6, 2025
4e2844e
Fix suggestions
OGPoyraz Feb 7, 2025
5e89bbb
Add missing snapshot
OGPoyraz Feb 7, 2025
0da244d
Merge branch 'main' into ogp/4078
OGPoyraz Feb 7, 2025
4d4d6c9
Add missing test
OGPoyraz Feb 7, 2025
baf1019
Update test
OGPoyraz Feb 7, 2025
5252b8b
Update app/components/Approvals/TransactionApproval/TransactionApprov…
OGPoyraz Feb 7, 2025
c5c342e
Update app/components/Views/confirmations/hooks/useConfirmationRedesi…
OGPoyraz Feb 7, 2025
f6f8e71
Update TransactionApproval tests
OGPoyraz Feb 7, 2025
afec503
Merge branch 'main' into ogp/4078
OGPoyraz Feb 7, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ import {
TransactionApproval,
TransactionModalType,
} from './TransactionApproval';

import { useConfirmationRedesignEnabled } from '../../Views/confirmations/hooks/useConfirmationRedesignEnabled';
import renderWithProvider from '../../../util/test/renderWithProvider';

jest.mock(
'../../Views/confirmations/hooks/useConfirmationRedesignEnabled',
() => ({
useConfirmationRedesignEnabled: jest.fn(),
}),
);
jest.mock('../../Views/confirmations/hooks/useApprovalRequest');

jest.mock('../../UI/QRHardware/withQRHardwareAwareness', () =>
Expand All @@ -30,6 +38,13 @@ const mockApprovalRequest = (approvalRequest?: ApprovalRequest<any>) => {
describe('TransactionApproval', () => {
beforeEach(() => {
jest.resetAllMocks();
(
useConfirmationRedesignEnabled as jest.MockedFn<
typeof useConfirmationRedesignEnabled
>
).mockReturnValue({
isRedesignedEnabled: false,
});
});

it('renders approval component if transaction type is dapp', () => {
Expand Down Expand Up @@ -79,9 +94,9 @@ describe('TransactionApproval', () => {
it('returns null if no approval request', () => {
mockApprovalRequest(undefined);

const wrapper = shallow(<TransactionApproval />);
const { toJSON } = renderWithProvider(<TransactionApproval />, {});

expect(wrapper).toMatchSnapshot();
expect(toJSON()).toMatchInlineSnapshot(`null`);
});

it('returns null if incorrect approval request type', () => {
Expand All @@ -91,9 +106,9 @@ describe('TransactionApproval', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);

const wrapper = shallow(<TransactionApproval />);
const { toJSON } = renderWithProvider(<TransactionApproval />, {});

expect(wrapper).toMatchSnapshot();
expect(toJSON()).toMatchInlineSnapshot(`null`);
});

it('returns null if incorrect transaction type', () => {
Expand All @@ -103,8 +118,34 @@ describe('TransactionApproval', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);

const wrapper = shallow(<TransactionApproval transactionType="invalid" />);
const { toJSON } = renderWithProvider(
<TransactionApproval transactionType="invalid" />,
{},
);

expect(wrapper).toMatchSnapshot();
expect(toJSON()).toMatchInlineSnapshot(`null`);
});

it('returns null if redesign is enabled', () => {
mockApprovalRequest({
type: ApprovalTypes.TRANSACTION,
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);

(
useConfirmationRedesignEnabled as jest.MockedFn<
typeof useConfirmationRedesignEnabled
>
).mockReturnValue({
isRedesignedEnabled: true,
});

const { toJSON } = renderWithProvider(
<TransactionApproval transactionType={TransactionModalType.Dapp} />,
{},
);

expect(toJSON()).toMatchInlineSnapshot(`null`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Approve from '../../Views/confirmations/ApproveView/Approve';
import QRSigningModal from '../../UI/QRHardware/QRSigningModal';
import withQRHardwareAwareness from '../../UI/QRHardware/withQRHardwareAwareness';
import { IQRState } from '../../UI/QRHardware/types';
import { useConfirmationRedesignEnabled } from '../../Views/confirmations/hooks/useConfirmationRedesignEnabled';

export enum TransactionModalType {
Transaction = 'transaction',
Expand All @@ -24,6 +25,7 @@ export interface TransactionApprovalProps {

const TransactionApprovalInternal = (props: TransactionApprovalProps) => {
const { approvalRequest } = useApprovalRequest();
const { isRedesignedEnabled } = useConfirmationRedesignEnabled();
const [modalVisible, setModalVisible] = useState(false);
const { onComplete: propsOnComplete } = props;

Expand All @@ -32,7 +34,10 @@ const TransactionApprovalInternal = (props: TransactionApprovalProps) => {
propsOnComplete();
}, [propsOnComplete]);

if (approvalRequest?.type !== ApprovalTypes.TRANSACTION && !modalVisible) {
if (
(approvalRequest?.type !== ApprovalTypes.TRANSACTION && !modalVisible) ||
isRedesignedEnabled
) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,3 @@ exports[`TransactionApproval renders approve component if transaction type is tr
modalVisible={true}
/>
`;

exports[`TransactionApproval returns null if incorrect approval request type 1`] = `""`;

exports[`TransactionApproval returns null if incorrect transaction type 1`] = `""`;

exports[`TransactionApproval returns null if no approval request 1`] = `""`;
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useNavigation } from '@react-navigation/native';
import React, { useCallback, useEffect } from 'react';
import { View } from 'react-native';
import { useSelector } from 'react-redux';
import { strings } from '../../../../../../locales/i18n';
import Button, {
ButtonSize,
Expand All @@ -20,14 +21,25 @@ import useStakingInputHandlers from '../../hooks/useStakingInput';
import InputDisplay from '../../components/InputDisplay';
import { MetaMetricsEvents, useMetrics } from '../../../../hooks/useMetrics';
import { withMetaMetrics } from '../../utils/metaMetrics/withMetaMetrics';
import usePoolStakedDeposit from '../../hooks/usePoolStakedDeposit';
import { formatEther } from 'ethers/lib/utils';
import { EVENT_PROVIDERS, EVENT_LOCATIONS } from '../../constants/events';
import { selectConfirmationRedesignFlags } from '../../../../../selectors/featureFlagController';
import { selectSelectedInternalAccount } from '../../../../../selectors/accountsController';

const StakeInputView = () => {
const title = strings('stake.stake_eth');
const navigation = useNavigation();
const { styles, theme } = useStyles(styleSheet, {});
const { trackEvent, createEventBuilder } = useMetrics();
const { attemptDepositTransaction } = usePoolStakedDeposit();
const confirmationRedesignFlags = useSelector(
selectConfirmationRedesignFlags,
);
const isStakingDepositRedesignedEnabled =
confirmationRedesignFlags?.staking_transactions;
NicolasMassart marked this conversation as resolved.
Show resolved Hide resolved
const activeAccount = useSelector(selectSelectedInternalAccount);


const {
isEth,
Expand Down Expand Up @@ -62,7 +74,15 @@ const StakeInputView = () => {
});
};

const handleStakePress = useCallback(() => {
const handleStakePress = useCallback(async () => {
if (isStakingDepositRedesignedEnabled) {
await attemptDepositTransaction(
amountWei.toString(),
activeAccount?.address as string,
);
return;
}
Matt561 marked this conversation as resolved.
Show resolved Hide resolved
NicolasMassart marked this conversation as resolved.
Show resolved Hide resolved

if (isHighGasCostImpact()) {
trackEvent(
createEventBuilder(
Expand Down Expand Up @@ -126,6 +146,9 @@ const StakeInputView = () => {
amountEth,
estimatedGasFeeWei,
getDepositTxGasPercentage,
isStakingDepositRedesignedEnabled,
activeAccount,
attemptDepositTransaction,
]);

const handleMaxButtonPress = () => {
Expand Down
21 changes: 18 additions & 3 deletions app/components/Views/confirmations/Confirm/Confirm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
personalSignatureConfirmationState,
securityAlertResponse,
typedSignV1ConfirmationState,
stakingDepositConfirmationState,
} from '../../../../util/test/confirm-data-helpers';
import Confirm from './index';

Expand Down Expand Up @@ -33,7 +34,21 @@ jest.mock('react-native-gzip', () => ({
}));

describe('Confirm', () => {
it('should render correct information for personal sign', async () => {
it('renders flat confirmation', async () => {
const { getByTestId } = renderWithProvider(<Confirm />, {
state: stakingDepositConfirmationState,
});
expect(getByTestId('flat-confirmation-container')).toBeDefined();
});

it('renders modal confirmation', async () => {
const { getByTestId } = renderWithProvider(<Confirm />, {
state: typedSignV1ConfirmationState,
});
expect(getByTestId('modal-confirmation-container')).toBeDefined();
});

it('renders correct information for personal sign', async () => {
const { getAllByRole, getByText } = renderWithProvider(<Confirm />, {
state: personalSignatureConfirmationState,
});
Expand All @@ -48,7 +63,7 @@ describe('Confirm', () => {
expect(getAllByRole('button')).toHaveLength(2);
});

it('should render correct information for typed sign v1', async () => {
it('renders correct information for typed sign v1', async () => {
const { getAllByRole, getAllByText, getByText, queryByText } =
renderWithProvider(<Confirm />, {
state: typedSignV1ConfirmationState,
Expand All @@ -62,7 +77,7 @@ describe('Confirm', () => {
expect(queryByText('This is a deceptive request')).toBeNull();
});

it('should render blockaid banner if confirmation has blockaid error response', async () => {
it('renders blockaid banner if confirmation has blockaid error response', async () => {
const { getByText } = renderWithProvider(<Confirm />, {
state: {
...typedSignV1ConfirmationState,
Expand Down
24 changes: 14 additions & 10 deletions app/components/Views/confirmations/Confirm/Confirm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,26 @@ import { SafeAreaView } from 'react-native-safe-area-context';
import { TransactionType } from '@metamask/transaction-controller';

import { useStyles } from '../../../../component-library/hooks';
import AccountNetworkInfo from '../components/Confirm/AccountNetworkInfo';
import BottomModal from '../components/UI/BottomModal';
import Footer from '../components/Confirm/Footer';
import Info from '../components/Confirm/Info';
import SignatureBlockaidBanner from '../components/Confirm/SignatureBlockaidBanner';
import Title from '../components/Confirm/Title';
import useApprovalRequest from '../hooks/useApprovalRequest';
import { useConfirmationRedesignEnabled } from '../hooks/useConfirmationRedesignEnabled';

import { useTransactionMetadataRequest } from '../hooks/useTransactionMetadataRequest';
import styleSheet from './Confirm.styles';

// todo: if possible derive way to dynamically check if confirmation should be rendered flat
// todo: unit test coverage to be added once we have flat confirmations in place
const FLAT_CONFIRMATIONS: TransactionType[] = [
// To be filled with flat confirmations
const FLAT_TRANSACTION_CONFIRMATIONS: TransactionType[] = [
TransactionType.stakingDeposit,
];

const ConfirmWrapped = () => (
<>
<ScrollView>
<Title />
<SignatureBlockaidBanner />
<AccountNetworkInfo />
<Info />
</ScrollView>
<Footer />
Expand All @@ -35,27 +32,34 @@ const ConfirmWrapped = () => (

const Confirm = () => {
const { approvalRequest } = useApprovalRequest();
const transactionMetadata = useTransactionMetadataRequest();
const { isRedesignedEnabled } = useConfirmationRedesignEnabled();
const { styles } = useStyles(styleSheet, {});

if (!isRedesignedEnabled) {
return null;
}

jpuri marked this conversation as resolved.
Show resolved Hide resolved
const isFlatConfirmation = FLAT_CONFIRMATIONS.includes(
approvalRequest?.type as TransactionType,
const isFlatConfirmation = FLAT_TRANSACTION_CONFIRMATIONS.includes(
transactionMetadata?.type as TransactionType,
);

if (isFlatConfirmation) {
return (
<SafeAreaView style={styles.mainContainer}>
<SafeAreaView
style={styles.mainContainer}
testID="flat-confirmation-container"
>
<ConfirmWrapped />
</SafeAreaView>
);
}

return (
<BottomModal canCloseOnBackdropClick={false}>
<BottomModal
canCloseOnBackdropClick={false}
testID="modal-confirmation-container"
>
<View style={styles.container} testID={approvalRequest?.type}>
<ConfirmWrapped />
</View>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ import renderWithProvider from '../../../../../../util/test/renderWithProvider';
import { personalSignatureConfirmationState } from '../../../../../../util/test/confirm-data-helpers';
import Info from './Info';

jest.mock('../../../../../../core/Engine', () => ({
getTotalFiatAccountBalance: () => ({ tokenFiat: 10 }),
context: {
KeyringController: {
state: {
keyrings: [],
},
getOrAddQRKeyring: jest.fn(),
},
},
controllerMessenger: {
subscribe: jest.fn(),
},
}));

describe('Info', () => {
it('should render correctly for personal sign', async () => {
const { getByText } = renderWithProvider(<Info />, {
Expand Down
NicolasMassart marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
import { TransactionType } from '@metamask/transaction-controller';
import { ApprovalType } from '@metamask/controller-utils';
import React from 'react';

import useApprovalRequest from '../../../hooks/useApprovalRequest';
import { useTransactionMetadataRequest } from '../../../hooks/useTransactionMetadataRequest';
import PersonalSign from './PersonalSign';
import TypedSignV1 from './TypedSignV1';
import TypedSignV3V4 from './TypedSignV3V4';
import StakingDeposit from './StakingDeposit';

interface ConfirmationInfoComponentRequest {
signatureRequestVersion?: string;
transactionType?: TransactionType;
}

const ConfirmationInfoComponentMap = {
[TransactionType.personalSign]: () => PersonalSign,
[TransactionType.signTypedData]: (approvalRequestVersion: string) => {
if (approvalRequestVersion === 'V1') return TypedSignV1;
[TransactionType.signTypedData]: ({
signatureRequestVersion,
}: ConfirmationInfoComponentRequest) => {
if (signatureRequestVersion === 'V1') return TypedSignV1;
return TypedSignV3V4;
},
[ApprovalType.Transaction]: ({
transactionType,
}: ConfirmationInfoComponentRequest) => {
if (transactionType === TransactionType.stakingDeposit)
return StakingDeposit;
return null;
},
};

const Info = () => {
const { approvalRequest } = useApprovalRequest();
const transactionMetadata = useTransactionMetadataRequest();

if (!approvalRequest?.type) {
return null;
Expand All @@ -24,11 +42,12 @@ const Info = () => {
const { requestData } = approvalRequest ?? {
requestData: {},
};
const approvalRequestVersion = requestData?.version;
const signatureRequestVersion = requestData?.version;
const transactionType = transactionMetadata?.type;

const InfoComponent: React.FC = ConfirmationInfoComponentMap[
const InfoComponent = ConfirmationInfoComponentMap[
approvalRequest?.type as keyof typeof ConfirmationInfoComponentMap
](approvalRequestVersion);
]({ signatureRequestVersion, transactionType }) as React.FC;

return <InfoComponent />;
};
Expand Down
Loading
Loading