Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { renderHook } from '@testing-library/react-hooks';
import { useShouldRenderGasSponsoredBanner } from './index';
import { useIsNetworkGasSponsored } from '../useIsNetworkGasSponsored';
import { useIsHardwareWalletForBridge } from '../useIsHardwareWalletForBridge';
import { useSelector } from 'react-redux';
import {
selectSourceToken,
selectDestToken,
} from '../../../../../core/redux/slices/bridge';

jest.mock('../useIsNetworkGasSponsored');
jest.mock('../useIsHardwareWalletForBridge');
jest.mock('react-redux', () => ({
useSelector: jest.fn(),
}));
Expand All @@ -16,6 +18,10 @@ const mockUseIsNetworkGasSponsored =
useIsNetworkGasSponsored as jest.MockedFunction<
typeof useIsNetworkGasSponsored
>;
const mockUseIsHardwareWalletForBridge =
useIsHardwareWalletForBridge as jest.MockedFunction<
typeof useIsHardwareWalletForBridge
>;
const mockUseSelector = useSelector as jest.MockedFunction<typeof useSelector>;

const SOURCE_CHAIN_ID = '0x1';
Expand Down Expand Up @@ -51,6 +57,7 @@ describe('useShouldRenderGasSponsoredBanner', () => {
jest.clearAllMocks();
mockTokens({});
mockUseIsNetworkGasSponsored.mockReturnValue(false);
mockUseIsHardwareWalletForBridge.mockReturnValue(false);
});

describe('returns true when quoteGasSponsored is true', () => {
Expand Down Expand Up @@ -146,6 +153,43 @@ describe('useShouldRenderGasSponsoredBanner', () => {
});

describe('returns false', () => {
it('returns false when quote is sponsored but source account is a hardware wallet', () => {
// Arrange
mockUseIsHardwareWalletForBridge.mockReturnValue(true);

// Act
const { result } = renderHook(() =>
useShouldRenderGasSponsoredBanner({
quoteGasSponsored: true,
hasInsufficientBalance: false,
}),
);

// Assert
expect(result.current).toBe(false);
});

it('returns false when insufficient balance and network is sponsored but source account is a hardware wallet', () => {
// Arrange
mockTokens({
sourceChainId: SOURCE_CHAIN_ID,
destChainId: SAME_CHAIN_DEST_CHAIN_ID,
});
mockUseIsNetworkGasSponsored.mockReturnValue(true);
mockUseIsHardwareWalletForBridge.mockReturnValue(true);

// Act
const { result } = renderHook(() =>
useShouldRenderGasSponsoredBanner({
quoteGasSponsored: false,
hasInsufficientBalance: true,
}),
);

// Assert
expect(result.current).toBe(false);
});

it('returns false when quoteGasSponsored is false and balance is sufficient', () => {
// Arrange
mockUseIsNetworkGasSponsored.mockReturnValue(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
selectSourceToken,
selectDestToken,
} from '../../../../../core/redux/slices/bridge';
import { useIsHardwareWalletForBridge } from '../useIsHardwareWalletForBridge';

interface Props {
quoteGasSponsored?: boolean;
Expand All @@ -16,6 +17,7 @@ export const useShouldRenderGasSponsoredBanner = ({
}: Props) => {
const sourceToken = useSelector(selectSourceToken);
const destToken = useSelector(selectDestToken);
const isHardwareWallet = useIsHardwareWalletForBridge();
const isNetworkGasSponsored = useIsNetworkGasSponsored(sourceToken?.chainId);

// Sponsorship only applies to same-chain (swap) flows; cross-chain bridges
Expand All @@ -27,8 +29,9 @@ export const useShouldRenderGasSponsoredBanner = ({
);

const shouldShowGasSponsored =
quoteGasSponsored ||
(hasInsufficientBalance && isNetworkGasSponsored && isSameChain);
!isHardwareWallet &&
(quoteGasSponsored ||
(hasInsufficientBalance && isNetworkGasSponsored && isSameChain));

return shouldShowGasSponsored;
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ import { transferTransactionStateMock } from '../../__mocks__/transfer-transacti
import { useTransactionMetadataRequest } from '../transactions/useTransactionMetadataRequest';
import { useIsGaslessSupported } from './useIsGaslessSupported';
import { useGaslessSupportedSmartTransactions } from './useGaslessSupportedSmartTransactions';
import { isHardwareAccount } from '../../../../../util/address';

jest.mock('../../../../../util/transactions/sentinel-api');
jest.mock('../../../../../util/transaction-controller');
jest.mock('../../../../../util/transactions/transaction-relay');
jest.mock('../transactions/useTransactionMetadataRequest');
jest.mock('./useGaslessSupportedSmartTransactions');
jest.mock('../../../../../util/address', () => ({
...jest.requireActual('../../../../../util/address'),
isHardwareAccount: jest.fn(),
}));

const SMART_TRANSACTIONS_ENABLED_STATE = {
swaps: {
Expand Down Expand Up @@ -54,6 +59,7 @@ describe('useIsGaslessSupported', () => {
const useGaslessSupportedSmartTransactionsMock = jest.mocked(
useGaslessSupportedSmartTransactions,
);
const isHardwareAccountMock = jest.mocked(isHardwareAccount);

beforeEach(() => {
mockUseTransactionMetadataRequest.mockReturnValue({
Expand All @@ -66,6 +72,7 @@ describe('useIsGaslessSupported', () => {
isSupported: false,
pending: false,
});
isHardwareAccountMock.mockReturnValue(false);
});

describe('Gasless Smart Transactions', () => {
Expand Down Expand Up @@ -94,6 +101,32 @@ describe('useIsGaslessSupported', () => {
);
});

it('returns isSupported false when smart transactions are enabled but sender is a hardware wallet', async () => {
const stateWithSmartTransactionEnabled = merge(
{},
transferConfirmationState,
SMART_TRANSACTIONS_ENABLED_STATE,
);
useGaslessSupportedSmartTransactionsMock.mockReturnValue({
isSmartTransaction: true,
isSupported: true,
pending: false,
});
isHardwareAccountMock.mockReturnValue(true);

const { result } = renderHookWithProvider(() => useIsGaslessSupported(), {
state: stateWithSmartTransactionEnabled,
});

await waitFor(() =>
expect(result.current).toEqual({
isSupported: false,
isSmartTransaction: true,
pending: false,
}),
);
});

it('returns isSupported and isSmartTransaction as false when smart transactions are disabled', async () => {
const { result } = renderHookWithProvider(() => useIsGaslessSupported(), {
state: transferTransactionStateMock,
Expand Down Expand Up @@ -153,6 +186,24 @@ describe('useIsGaslessSupported', () => {
});
});

it('returns isSupported false when the sender is a hardware wallet even if relay is supported', async () => {
isRelaySupportedMock.mockResolvedValue(true);
isHardwareAccountMock.mockReturnValue(true);

const state = merge({}, transferTransactionStateMock);
const { result } = renderHookWithProvider(() => useIsGaslessSupported(), {
state,
});

await waitFor(() => {
expect(result.current).toEqual({
isSupported: false,
isSmartTransaction: false,
pending: false,
});
});
});

it('returns isSupported false and isSmartTransaction: false when relay not supported', async () => {
isRelaySupportedMock.mockResolvedValue(false);

Expand Down
4 changes: 2 additions & 2 deletions tests/api-mocking/mock-responses/simulations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const RECIPIENT_ADDRESS_MOCK = '0x0c54fccd2e384b4bb6f2e405bf5cbc15a017aafb';
const SENTINEL_URL = 'https://tx-sentinel-ethereum-mainnet.api.cx.metamask.io';
const LOCALHOST_SENTINEL_URL =
device.getPlatform() === 'android'
? 'https://tx-sentinel-127.0.0.1.api.cx.metamask.io/'
: 'https://tx-sentinel-localhost.api.cx.metamask.io/';
? 'https://tx-sentinel-127.0.0.1.api.cx.metamask.io'
: 'https://tx-sentinel-localhost.api.cx.metamask.io';

export const SEND_ETH_TRANSACTION_MOCK = {
data: '0x',
Expand Down
4 changes: 2 additions & 2 deletions tests/api-mocking/mock-responses/transaction-relay-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const TRANSACTION_HASH =
const SENTINEL_URL = 'https://tx-sentinel-ethereum-mainnet.api.cx.metamask.io';
const LOCALHOST_SENTINEL_URL =
device.getPlatform() === 'android'
? 'https://tx-sentinel-127.0.0.1.api.cx.metamask.io/'
: 'https://tx-sentinel-localhost.api.cx.metamask.io/';
? 'https://tx-sentinel-127.0.0.1.api.cx.metamask.io'
: 'https://tx-sentinel-localhost.api.cx.metamask.io';

export const SEND_ETH_TRANSACTION_MOCK = {
data: '0x',
Expand Down
22 changes: 22 additions & 0 deletions tests/api-mocking/mock-responses/tx-sentinel-networks-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ export const TX_SENTINEL_NETWORKS_MAP = {
hidden: false,
sendBundle: false,
},
/**
* Local Anvil / fixture chain (0x539). Required for EIP-7702 relay + gasless
* confirmation E2E (`gas-fee-tokens-eip-7702-sponsored.spec.ts`): `isRelaySupported`
* reads Sentinel `/networks` via `getSentinelNetworkFlags`.
*/
'1337': {
name: 'Localhost',
group: 'ethereum',
chainID: 1337,
nativeCurrency: {
name: 'ETH',
symbol: 'ETH',
decimals: 18,
},
network: 'localhost',
explorer: 'http://localhost:8545/explorer',
confirmations: true,
smartTransactions: true,
relayTransactions: true,
hidden: false,
sendBundle: false,
},
'137': {
name: 'Polygon Mainnet',
group: 'polygon',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ import { RelayStatus } from '../../../../app/util/transactions/transaction-relay
const TRANSACTION_UUID_MOCK = '1234-5678';
const SENDER_ADDRESS_MOCK = '0x76cf1cdd1fcc252442b50d6e97207228aa4aefc3';
const RECIPIENT_ADDRESS_MOCK = '0x0c54fccd2e384b4bb6f2e405bf5cbc15a017aafb';
const SENTINEL_URL = 'https://tx-sentinel-localhost.api.cx.metamask.io';
/** Match {@link gas-fee-tokens-eip-7702.spec.ts} and {@link transaction-relay-mocks} for proxy URL matching. */
const LOCALHOST_SENTINEL_URL =
device.getPlatform() === 'android'
? 'https://tx-sentinel-127.0.0.1.api.cx.metamask.io'
: 'https://tx-sentinel-localhost.api.cx.metamask.io';

const SEND_ETH_TRANSACTION_MOCK = {
data: '0x',
Expand All @@ -51,10 +55,39 @@ const SIMULATION_ENABLED_NETWORKS_WITH_RELAY = {
1337: {
...SIMULATION_ENABLED_NETWORKS_MOCK.response[1337],
relayTransactions: true,
sendBundle: true,
},
1: {
network: 'ethereum-mainnet',
confirmations: true,
relayTransactions: true,
sendBundle: true,
},
},
};

const SIMULATION_SPONSORED_REQUEST_BODY = {
jsonrpc: '2.0',
method: 'infura_simulateTransactions',
params: [
{
transactions: [SEND_ETH_TRANSACTION_MOCK],
suggestFees: {
withFeeTransfer: true,
withTransfer: true,
with7702: true,
},
},
],
};

const SIMULATION_SPONSORED_IGNORE_FIELDS = [
'params.0.blockOverrides',
'id',
'params.0.transactions',
'params.0.suggestFees',
];

const SIMULATION_RESPONSE = {
jsonrpc: '2.0',
result: {
Expand Down Expand Up @@ -98,24 +131,22 @@ const setupCommonMocks = async (mockServer: Mockttp) => {
1000,
);

// Mock infura_simulateTransactions
await setupMockRequest(mockServer, {
requestMethod: 'GET',
url: `${LOCALHOST_SENTINEL_URL}/network`,
response: SIMULATION_ENABLED_NETWORKS_WITH_RELAY.response,
responseCode: 200,
});

// Mock infura_simulateTransactions (align body + ignoreFields with gas-fee-tokens-eip-7702.spec.ts)
await setupMockPostRequest(
mockServer,
SENTINEL_URL,
{
jsonrpc: '2.0',
method: 'infura_simulateTransactions',
params: [
{
transactions: [SEND_ETH_TRANSACTION_MOCK],
suggestFees: { withFeeTransfer: true, withTransfer: true },
},
],
},
LOCALHOST_SENTINEL_URL,
SIMULATION_SPONSORED_REQUEST_BODY,
SIMULATION_RESPONSE,
{
statusCode: 200,
ignoreFields: ['id', 'params'],
ignoreFields: SIMULATION_SPONSORED_IGNORE_FIELDS,
priority: 1000,
},
);
Expand Down Expand Up @@ -184,8 +215,17 @@ const performSendTransaction = async () => {
await SendView.pressContinueButton();
await SendView.inputRecipientAddress(RECIPIENT_ADDRESS_MOCK);
await SendView.pressReviewButton();
await Assertions.expectElementToBeVisible(RowComponents.GasFeesDetails, {
description: 'gas fees row is present on review screen',
timeout: 30000,
});
await Assertions.expectElementToBeVisible(
RowComponents.NetworkFeePaidByMetaMask,
{
description:
'network fee shows MetaMask-sponsored gas after relay + simulation settle',
timeout: 60000,
},
);
await Utilities.waitForElementToBeVisible(FooterActions.confirmButton);
await Utilities.waitForElementToStopMoving(FooterActions.confirmButton, {
Expand Down Expand Up @@ -216,7 +256,7 @@ describe(
// Mock eth_sendRelayTransaction
await setupMockPostRequest(
mockServer,
SENTINEL_URL,
LOCALHOST_SENTINEL_URL,
{
jsonrpc: '2.0',
method: 'eth_sendRelayTransaction',
Expand All @@ -232,7 +272,7 @@ describe(
// Status check mock
await setupMockRequest(mockServer, {
requestMethod: 'GET',
url: `${SENTINEL_URL}/smart-transactions/${TRANSACTION_UUID_MOCK}`,
url: `${LOCALHOST_SENTINEL_URL}/smart-transactions/${TRANSACTION_UUID_MOCK}`,
response: {
transactions: [
{
Expand Down
Loading