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
Expand Up @@ -9,6 +9,8 @@ import {
getDisplayAmount,
} from '.';
import { BridgeToken } from '../../types';
import { CHAIN_IDS } from '@metamask/transaction-controller';
import { POLYGON_NATIVE_TOKEN } from '../../constants/assets';

jest.mock('../../hooks/useLatestBalance', () => ({
useLatestBalance: jest.fn(),
Expand Down Expand Up @@ -242,6 +244,139 @@ describe('TokenInputArea', () => {
// Assert
expect(getByText('Max')).toBeTruthy();
});

it('treats Polygon native token as native asset after zero address conversion', () => {
const polygonNativeToken: BridgeToken = {
address: POLYGON_NATIVE_TOKEN, // 0x0000...001010
symbol: 'POL',
decimals: 18,
chainId: CHAIN_IDS.POLYGON as `0x${string}`,
};
const tokenBalance = '10';

const stateWithoutGasless = {
...initialState,
engine: {
...initialState.engine,
backgroundState: {
...initialState.engine.backgroundState,
RemoteFeatureFlagController: {
remoteFeatureFlags: {
...initialState.engine.backgroundState.RemoteFeatureFlagController
.remoteFeatureFlags,
bridgeConfigV2: {
...initialState.engine.backgroundState
.RemoteFeatureFlagController.remoteFeatureFlags
.bridgeConfigV2,
chains: {
...initialState.engine.backgroundState
.RemoteFeatureFlagController.remoteFeatureFlags
.bridgeConfigV2.chains,
'eip155:137': {
isActiveSrc: true,
isActiveDest: true,
isGaslessSwapEnabled: false,
},
},
},
},
},
},
},
};

const { queryByText } = renderScreen(
() => (
<TokenInputArea
testID="token-input"
tokenType={TokenInputAreaType.Source}
token={polygonNativeToken}
tokenBalance={tokenBalance}
onMaxPress={mockOnMaxPress}
/>
),
{
name: 'TokenInputArea',
},
{ state: stateWithoutGasless },
);

// After conversion to zero address, Polygon native token is treated as native
// Native tokens hide Max button when gasless swaps are disabled
expect(queryByText('Max')).toBeNull();
});

it('displays max button for Polygon native token when gasless swaps are enabled', () => {
const polygonNativeToken: BridgeToken = {
address: POLYGON_NATIVE_TOKEN, // 0x0000...001010
symbol: 'POL',
decimals: 18,
chainId: CHAIN_IDS.POLYGON as `0x${string}`,
};
const destToken: BridgeToken = {
address: '0x2791bca1f2de4661ed88a30c99a7a9449aa84174', // USDC
symbol: 'USDC',
decimals: 6,
chainId: CHAIN_IDS.POLYGON as `0x${string}`,
};
const tokenBalance = '10';

const stateWithGaslessSwap = {
...initialState,
engine: {
...initialState.engine,
backgroundState: {
...initialState.engine.backgroundState,
RemoteFeatureFlagController: {
remoteFeatureFlags: {
...initialState.engine.backgroundState.RemoteFeatureFlagController
.remoteFeatureFlags,
bridgeConfigV2: {
...initialState.engine.backgroundState
.RemoteFeatureFlagController.remoteFeatureFlags
.bridgeConfigV2,
chains: {
...initialState.engine.backgroundState
.RemoteFeatureFlagController.remoteFeatureFlags
.bridgeConfigV2.chains,
'eip155:137': {
isActiveSrc: true,
isActiveDest: true,
isGaslessSwapEnabled: true,
},
},
},
},
},
},
},
bridge: {
...initialState.bridge,
sourceToken: polygonNativeToken,
destToken,
},
};

const { getByText } = renderScreen(
() => (
<TokenInputArea
testID="token-input"
tokenType={TokenInputAreaType.Source}
token={polygonNativeToken}
tokenBalance={tokenBalance}
onMaxPress={mockOnMaxPress}
/>
),
{
name: 'TokenInputArea',
},
{ state: stateWithGaslessSwap },
);

// After conversion to zero address, Polygon native token is treated as native
// Native tokens show Max button when gasless swaps are enabled
expect(getByText('Max')).toBeTruthy();
});
});

describe('calculateFontSize', () => {
Expand Down
17 changes: 13 additions & 4 deletions app/components/UI/Bridge/components/TokenInputArea/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ import { renderShortAddress } from '../../../../../util/address';
import { FlexDirection } from '../../../Box/box.types';
import { isNativeAddress } from '@metamask/bridge-controller';
import { Theme } from '../../../../../util/theme/models';
import { CHAIN_IDS } from '@metamask/transaction-controller';
import { POLYGON_NATIVE_TOKEN } from '../../constants/assets';
import { zeroAddress } from 'ethereumjs-util';

const MAX_DECIMALS = 5;
export const MAX_INPUT_LENGTH = 36;
Expand Down Expand Up @@ -301,11 +304,17 @@ export const TokenInputArea = forwardRef<
}`
: undefined;

const isNativeAsset = isNativeAddress(token?.address);
// Polygon native token address can be 0x0000000000000000000000000000000000001010
// so we need to use the zero address for the token address
const tokenAddress =
token?.chainId === CHAIN_IDS.POLYGON &&
token?.address === POLYGON_NATIVE_TOKEN
? zeroAddress()
: token?.address;

const isNativeAsset = isNativeAddress(tokenAddress);
const formattedAddress =
token?.address && !isNativeAsset
? formatAddress(token?.address)
: undefined;
tokenAddress && !isNativeAsset ? formatAddress(tokenAddress) : undefined;

const subtitle =
tokenType === TokenInputAreaType.Source
Expand Down
Loading