Skip to content

feat: mms-1876 swap pre populated #30929

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

Merged
merged 9 commits into from
Mar 13, 2025
108 changes: 108 additions & 0 deletions shared/constants/swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,111 @@ export enum Slippage {
default = 2,
high = 3,
}

const ETH_USDC_TOKEN_OBJECT = {
symbol: CURRENCY_SYMBOLS.USDC,
name: 'USD Coin',
address: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
decimals: 6,
iconUrl:
'https://static.cx.metamask.io/api/v1/tokenIcons/1/0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.png',
};

const BSC_USDT_TOKEN_OBJECT = {
symbol: CURRENCY_SYMBOLS.USDT,
name: 'Tether USD',
address: '0x55d398326f99059ff775485246999027b3197955',
decimals: 18,
iconUrl:
'https://static.cx.metamask.io/api/v1/tokenIcons/56/0x55d398326f99059ff775485246999027b3197955.png',
};

const POLYGON_USDT_TOKEN_OBJECT = {
symbol: CURRENCY_SYMBOLS.USDT,
name: 'Tether USD',
address: '0xc2132d05d31c914a87c6611c10748aeb04b58e8f',
decimals: 6,
iconUrl:
'https://static.cx.metamask.io/api/v1/tokenIcons/137/0xc2132d05d31c914a87c6611c10748aeb04b58e8f.png',
};

const ARBITRUM_USDC_TOKEN_OBJECT = {
symbol: CURRENCY_SYMBOLS.USDC,
name: 'USD Coin',
address: '0xaf88d065e77c8cc2239327c5edb3a432268e5831',
decimals: 6,
iconUrl:
'https://static.cx.metamask.io/api/v1/tokenIcons/42161/0xaf88d065e77c8cc2239327c5edb3a432268e5831.png',
};

const AVALANCHE_USDC_TOKEN_OBJECT = {
symbol: CURRENCY_SYMBOLS.USDC,
name: 'USD Coin',
address: '0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
decimals: 6,
iconUrl:
'https://static.cx.metamask.io/api/v1/tokenIcons/43114/0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e.png',
};

const OPTIMISM_WETH_TOKEN_OBJECT = {
symbol: CURRENCY_SYMBOLS.WETH,
name: 'Wrapped Ether',
address: '0x4200000000000000000000000000000000000006',
decimals: 18,
iconUrl:
'https://static.cx.metamask.io/api/v1/tokenIcons/10/0x4200000000000000000000000000000000000006.png',
};

const BASE_USDC_TOKEN_OBJECT = {
symbol: CURRENCY_SYMBOLS.USDC,
name: 'USD Coin',
address: '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
decimals: 6,
iconUrl:
'https://static.cx.metamask.io/api/v1/tokenIcons/8453/0x833589fcd6edb6e08f4c7c32d4f71b54bda02913.png',
};

const LINEA_USDC_TOKEN_OBJECT = {
symbol: CURRENCY_SYMBOLS.USDC,
name: 'USD Coin',
address: '0x176211869ca2b568f2a7d4ee941e073a821ee1ff',
decimals: 6,
iconUrl:
'https://static.cx.metamask.io/api/v1/tokenIcons/59144/0x176211869ca2b568f2a7d4ee941e073a821ee1ff.png',
};

const ZKSYNC_USDT_TOKEN_OBJECT = {
symbol: CURRENCY_SYMBOLS.USDT,
name: 'USD Coin',
address: '0x493257fd37edb34451f62edf8d2a0c418852ba4c',
decimals: 6,
iconUrl:
'https://static.cx.metamask.io/api/v1/tokenIcons/324/0x493257fd37edb34451f62edf8d2a0c418852ba4c.png',
};

///: BEGIN:ONLY_INCLUDE_IF(solana-swaps)
const SOLANA_USDC_TOKEN_OBJECT = {
address:
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/token:EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
symbol: 'USDC',
name: 'USD Coin',
decimals: 6,
iconUrl:
'https://static.cx.metamask.io/api/v2/tokenIcons/assets/solana/5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/token/EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v.png',
};
///: END:ONLY_INCLUDE_IF

export const DEFAULT_TO_TOKEN_BY_NETWORK = {
[CHAIN_IDS.MAINNET]: ETH_USDC_TOKEN_OBJECT,
[CHAIN_IDS.BSC]: BSC_USDT_TOKEN_OBJECT,
[CHAIN_IDS.POLYGON]: POLYGON_USDT_TOKEN_OBJECT,
[CHAIN_IDS.ARBITRUM]: ARBITRUM_USDC_TOKEN_OBJECT,
[CHAIN_IDS.AVALANCHE]: AVALANCHE_USDC_TOKEN_OBJECT,
[CHAIN_IDS.OPTIMISM]: OPTIMISM_WETH_TOKEN_OBJECT,
[CHAIN_IDS.BASE]: BASE_USDC_TOKEN_OBJECT,
[CHAIN_IDS.LINEA_MAINNET]: LINEA_USDC_TOKEN_OBJECT,
[CHAIN_IDS.ZKSYNC_ERA]: ZKSYNC_USDT_TOKEN_OBJECT,
///: BEGIN:ONLY_INCLUDE_IF(solana-swaps)
[MultichainNetworks.SOLANA]: SOLANA_USDC_TOKEN_OBJECT,
///: END:ONLY_INCLUDE_IF
};
10 changes: 10 additions & 0 deletions ui/pages/swaps/prepare-swap-page/prepare-swap-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ import {
QUOTES_EXPIRED_ERROR,
MAX_ALLOWED_SLIPPAGE,
SWAPS_QUOTE_MAX_RETURN_DIFFERENCE_PERCENTAGE,
DEFAULT_TO_TOKEN_BY_NETWORK,
} from '../../../../shared/constants/swaps';
import {
CHAINID_DEFAULT_BLOCK_EXPLORER_URL_MAP,
Expand Down Expand Up @@ -754,6 +755,15 @@ export default function PrepareSwapPage({
}
}, [showQuotesLoadingAnimation]);

// Set the default destination token for the swap
useEffect(() => {
if (!selectedToToken?.address && DEFAULT_TO_TOKEN_BY_NETWORK[chainId]) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a condition here to skip pre-populating the toToken if the default token === fromToken?

Repro steps:

  • From homepage, go to asset details page for the default token (i.e, USDC on ethereum)
  • Click Swap from there
  • See that the Swap page has USDC -> USDC filled in, which is an invalid route

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updating it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @micaelae , I refactored the PR, to use a hook, not only we need to take that edge case, but always return one of the main from and to pair.

const defaultToken = DEFAULT_TO_TOKEN_BY_NETWORK[chainId];
defaultToken && dispatch(setSwapToToken(defaultToken));
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [tokensToSearchSwapTo, chainId]);

const onOpenImportTokenModalClick = (item) => {
setTokenForImport(item);
setIsImportTokenModalOpen(true);
Expand Down
Loading