diff --git a/ui/pages/bridge/index.test.tsx b/ui/pages/bridge/index.test.tsx index 7878126c47e1..e5f225108b88 100644 --- a/ui/pages/bridge/index.test.tsx +++ b/ui/pages/bridge/index.test.tsx @@ -90,6 +90,6 @@ describe('Bridge', () => { expect(getByText('Bridge')).toBeInTheDocument(); expect(container).toMatchSnapshot(); - expect(mockResetBridgeState).toHaveBeenCalledTimes(2); + expect(mockResetBridgeState).toHaveBeenCalledTimes(1); }); }); diff --git a/ui/pages/bridge/index.tsx b/ui/pages/bridge/index.tsx index fa815a0de2b9..15c3a0365cc9 100644 --- a/ui/pages/bridge/index.tsx +++ b/ui/pages/bridge/index.tsx @@ -67,8 +67,6 @@ const CrossChainSwap = () => { useEffect(() => { // Reset controller and inputs before unloading the page - resetControllerAndInputStates(); - window.addEventListener('beforeunload', resetControllerAndInputStates); return () => { diff --git a/ui/pages/bridge/prepare/bridge-input-group.tsx b/ui/pages/bridge/prepare/bridge-input-group.tsx index ac545fb834dd..6c75c703e22b 100644 --- a/ui/pages/bridge/prepare/bridge-input-group.tsx +++ b/ui/pages/bridge/prepare/bridge-input-group.tsx @@ -15,7 +15,11 @@ import { TabName } from '../../../components/multichain/asset-picker-amount/asse import { useI18nContext } from '../../../hooks/useI18nContext'; import { getLocale } from '../../../selectors'; import { getCurrentCurrency } from '../../../ducks/metamask/metamask'; -import { formatCurrencyAmount, formatTokenAmount } from '../utils/quote'; +import { + formatCurrencyAmount, + formatTokenAmount, + isNativeAddress, +} from '../utils/quote'; import { Column, Row } from '../layout'; import { Display, @@ -234,7 +238,7 @@ export const BridgeInputGroup = ({ : undefined} {onMaxButtonClick && token && - token.type !== AssetType.native && + !isNativeAddress(token.address) && balanceAmount && ( { }, [rotateSwitchTokens]); useEffect(() => { - // Reset controller and inputs on load - dispatch(resetBridgeState()); + if (activeQuote) { + // Get input data from active quote + const { srcAsset, destAsset, destChainId } = activeQuote.quote; + const quoteSrcToken = fromTokens[srcAsset.address.toLowerCase()]; + const quoteDestChainId = decimalToPrefixedHex(destChainId); + const quoteDestToken = toTokens[destAsset.address.toLowerCase()]; + + if (quoteSrcToken && quoteDestToken && quoteDestChainId) { + // Set inputs to values from active quote + dispatch(setFromTokenInputValue(null)); + dispatch( + setFromToken({ ...quoteSrcToken, image: quoteSrcToken.iconUrl }), + ); + dispatch(setToChainId(quoteDestChainId)); + dispatch( + setToToken({ ...quoteDestToken, image: quoteDestToken.iconUrl }), + ); + } + } else { + // Reset controller and inputs on load + dispatch(resetBridgeState()); + } }, []); // Scroll to bottom of the page when banners are shown diff --git a/ui/pages/home/home.component.js b/ui/pages/home/home.component.js index df4ebaca0776..3daed446261a 100644 --- a/ui/pages/home/home.component.js +++ b/ui/pages/home/home.component.js @@ -58,6 +58,7 @@ import { CONNECTED_ACCOUNTS_ROUTE, AWAITING_SWAP_ROUTE, PREPARE_SWAP_ROUTE, + CROSS_CHAIN_SWAP_ROUTE, ///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta,build-flask) ONBOARDING_SECURE_YOUR_WALLET_ROUTE, ///: END:ONLY_INCLUDE_IF @@ -159,6 +160,7 @@ export default class Home extends PureComponent { onTabClick: PropTypes.func.isRequired, haveSwapsQuotes: PropTypes.bool.isRequired, showAwaitingSwapScreen: PropTypes.bool.isRequired, + haveBridgeQuotes: PropTypes.bool.isRequired, setDataCollectionForMarketing: PropTypes.func.isRequired, dataCollectionForMarketing: PropTypes.bool, swapsFetchParams: PropTypes.object, @@ -220,6 +222,7 @@ export default class Home extends PureComponent { const { closeNotificationPopup, haveSwapsQuotes, + haveBridgeQuotes, isNotification, pendingApprovals, showAwaitingSwapScreen, @@ -235,7 +238,10 @@ export default class Home extends PureComponent { pendingApprovals.length || (!isNotification && !stayOnHomePage && - (showAwaitingSwapScreen || haveSwapsQuotes || swapsFetchParams)) + (showAwaitingSwapScreen || + haveSwapsQuotes || + swapsFetchParams || + haveBridgeQuotes)) ) { this.state.redirecting = true; } @@ -289,6 +295,7 @@ export default class Home extends PureComponent { history, isNotification, haveSwapsQuotes, + haveBridgeQuotes, showAwaitingSwapScreen, swapsFetchParams, location, @@ -306,6 +313,8 @@ export default class Home extends PureComponent { history.push(AWAITING_SWAP_ROUTE); } else if (canRedirect && (haveSwapsQuotes || swapsFetchParams)) { history.push(PREPARE_SWAP_ROUTE); + } else if (canRedirect && haveBridgeQuotes) { + history.push(CROSS_CHAIN_SWAP_ROUTE + PREPARE_SWAP_ROUTE); } else if (pendingApprovals.length) { navigateToConfirmation( pendingApprovals[0].id, diff --git a/ui/pages/home/home.container.js b/ui/pages/home/home.container.js index cae73b180f4d..9d29c91f7a8f 100644 --- a/ui/pages/home/home.container.js +++ b/ui/pages/home/home.container.js @@ -96,6 +96,7 @@ const mapStateToProps = (state) => { connectedStatusPopoverHasBeenShown, defaultHomeActiveTabName, swapsState, + bridgeState, dataCollectionForMarketing, participateInMetaMetrics, firstTimeFlowType, @@ -161,6 +162,7 @@ const mapStateToProps = (state) => { haveSwapsQuotes: Boolean(Object.values(swapsState.quotes || {}).length), swapsFetchParams: swapsState.fetchParams, showAwaitingSwapScreen: swapsState.routeState === 'awaiting', + haveBridgeQuotes: Boolean(Object.values(bridgeState?.quotes || {}).length), isMainnet: getIsMainnet(state), originOfCurrentTab, shouldShowWeb3ShimUsageNotification,