From 2b5c112a02095a28eae69b3a0eff43fb8d12d477 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Regadas?= Date: Tue, 1 Aug 2023 11:17:36 +0100 Subject: [PATCH] [MMI] swap button redirect when account is Custody (#20234) * wip * adds test * add value to constants * clean up --- shared/constants/swaps.ts | 2 + .../app/wallet-overview/eth-overview.js | 12 +++++- .../app/wallet-overview/eth-overview.test.js | 40 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/shared/constants/swaps.ts b/shared/constants/swaps.ts index 093ef470b..1a7d9f605 100644 --- a/shared/constants/swaps.ts +++ b/shared/constants/swaps.ts @@ -133,6 +133,8 @@ export const WETH_ARBITRUM_CONTRACT_ADDRESS = const SWAPS_TESTNET_CHAIN_ID = '0x539'; +export const MMI_SWAPS_URL = 'https://metamask-institutional.io/swap'; + export const SWAPS_API_V2_BASE_URL = 'https://swap.metaswap.codefi.network'; export const SWAPS_DEV_API_V2_BASE_URL = 'https://swap.dev-api.cx.metamask.io'; export const GAS_API_BASE_URL = 'https://gas-api.metaswap.codefi.network'; diff --git a/ui/components/app/wallet-overview/eth-overview.js b/ui/components/app/wallet-overview/eth-overview.js index e0aa2aa27..21127d10e 100644 --- a/ui/components/app/wallet-overview/eth-overview.js +++ b/ui/components/app/wallet-overview/eth-overview.js @@ -15,7 +15,7 @@ import { getMmiPortfolioUrl, } from '../../../selectors/institutional/selectors'; ///: END:ONLY_INCLUDE_IN - +import { MMI_SWAPS_URL } from '../../../../shared/constants/swaps'; import { I18nContext } from '../../../contexts/i18n'; import { SEND_ROUTE, @@ -25,6 +25,7 @@ import Tooltip from '../../ui/tooltip'; import UserPreferencedCurrencyDisplay from '../user-preferenced-currency-display'; import { PRIMARY, SECONDARY } from '../../../helpers/constants/common'; import { + getAccountType, isBalanceCached, getShouldShowFiat, getCurrentKeyring, @@ -77,6 +78,7 @@ const EthOverview = ({ className, showAddress }) => { const isSwapsChain = useSelector(getIsSwapsChain); const defaultSwapsToken = useSelector(getSwapsDefaultToken); const chainId = useSelector(getCurrentChainId); + const accountType = useSelector(getAccountType); ///: BEGIN:ONLY_INCLUDE_IN(build-mmi) const mmiPortfolioEnabled = useSelector(getMmiPortfolioEnabled); @@ -254,6 +256,14 @@ const EthOverview = ({ className, showAddress }) => { /> } onClick={() => { + if (accountType === 'custody') { + global.platform.openTab({ + url: MMI_SWAPS_URL, + }); + + return; + } + if (isSwapsChain) { trackEvent({ event: MetaMetricsEventName.NavSwapButtonClicked, diff --git a/ui/components/app/wallet-overview/eth-overview.test.js b/ui/components/app/wallet-overview/eth-overview.test.js index 7a1a884eb..32e73fbee 100644 --- a/ui/components/app/wallet-overview/eth-overview.test.js +++ b/ui/components/app/wallet-overview/eth-overview.test.js @@ -72,6 +72,7 @@ describe('EthOverview', () => { const ETH_OVERVIEW_BUY = 'eth-overview-buy'; const ETH_OVERVIEW_BRIDGE = 'eth-overview-bridge'; const ETH_OVERVIEW_PORTFOLIO = 'eth-overview-portfolio'; + const ETH_OVERVIEW_SWAP = 'token-overview-button-swap'; const ETH_OVERVIEW_PRIMARY_CURRENCY = 'eth-overview__primary-currency'; const ETH_OVERVIEW_SECONDARY_CURRENCY = 'eth-overview__secondary-currency'; @@ -191,6 +192,45 @@ describe('EthOverview', () => { ); }); + it('should open the MMI PD Swaps URI when clicking on Swap button with a Custody account', async () => { + const mockedStoreWithCustodyKeyring = { + metamask: { + ...mockStore.metamask, + keyrings: [ + { + type: 'Custody', + accounts: ['0x1'], + }, + ], + }, + }; + + const mockedStore = configureMockStore([thunk])( + mockedStoreWithCustodyKeyring, + ); + + const { queryByTestId } = renderWithProvider( + , + mockedStore, + ); + + const swapButton = queryByTestId(ETH_OVERVIEW_SWAP); + + expect(swapButton).toBeInTheDocument(); + expect(swapButton).not.toBeDisabled(); + + fireEvent.click(swapButton); + expect(openTabSpy).toHaveBeenCalledTimes(1); + + await waitFor(() => + expect(openTabSpy).toHaveBeenCalledWith({ + url: expect.stringContaining( + 'https://metamask-institutional.io/swap', + ), + }), + ); + }); + it('should have the Bridge button disabled if chain id is not part of supported chains', () => { const mockedFantomStore = { ...mockStore,