From 4e93bcc22e5e5948d10e520716c88351f6edd6f8 Mon Sep 17 00:00:00 2001 From: Eugene Chybisov Date: Wed, 9 Feb 2022 18:01:54 +0200 Subject: [PATCH] feat: add connect wallet button --- packages/widget/src/components/SwapButton.tsx | 32 +++++++- .../widget/src/components/WalletHeader.tsx | 78 ++++++++++++++++--- packages/widget/src/hooks/connectorHooks.ts | 34 ++++++++ packages/widget/src/pages/SwapPage.tsx | 20 +++-- .../widget/src/services/connectors/eip1193.ts | 28 +++++++ .../widget/src/services/connectors/empty.ts | 4 + .../src/services/connectors/metaMask.ts | 6 ++ .../widget/src/services/connectors/network.ts | 8 ++ .../widget/src/services/connectors/url.ts | 8 ++ .../src/services/connectors/walletConnect.ts | 17 ++++ .../src/services/connectors/walletLink.ts | 11 +++ packages/widget/src/theme.ts | 3 + 12 files changed, 226 insertions(+), 23 deletions(-) create mode 100644 packages/widget/src/hooks/connectorHooks.ts create mode 100644 packages/widget/src/services/connectors/eip1193.ts create mode 100644 packages/widget/src/services/connectors/empty.ts create mode 100644 packages/widget/src/services/connectors/metaMask.ts create mode 100644 packages/widget/src/services/connectors/network.ts create mode 100644 packages/widget/src/services/connectors/url.ts create mode 100644 packages/widget/src/services/connectors/walletConnect.ts create mode 100644 packages/widget/src/services/connectors/walletLink.ts diff --git a/packages/widget/src/components/SwapButton.tsx b/packages/widget/src/components/SwapButton.tsx index 8b08ca0b9..e5e4a3613 100644 --- a/packages/widget/src/components/SwapButton.tsx +++ b/packages/widget/src/components/SwapButton.tsx @@ -1,8 +1,36 @@ -import { Button } from '@mui/material'; +import { LoadingButton } from '@mui/lab'; import { styled } from '@mui/material/styles'; +import { useTranslation } from 'react-i18next'; +import { + usePriorityAccount, + usePriorityConnector, + usePriorityIsActivating, + usePriorityIsActive, +} from '../hooks/connectorHooks'; -export const SwapButton = styled(Button)({ +export const Button = styled(LoadingButton)({ textTransform: 'none', borderRadius: 0, padding: '12px 16px', }); + +export const SwapButton = () => { + const { t } = useTranslation(); + const connector = usePriorityConnector(); + const isActive = usePriorityIsActive(); + const isActivating = usePriorityIsActivating(); + const account = usePriorityAccount(); + + return ( + + ); +}; diff --git a/packages/widget/src/components/WalletHeader.tsx b/packages/widget/src/components/WalletHeader.tsx index 29abf73fa..e28b38a30 100644 --- a/packages/widget/src/components/WalletHeader.tsx +++ b/packages/widget/src/components/WalletHeader.tsx @@ -1,21 +1,79 @@ -import { Typography } from '@mui/material'; +import { Box, Menu, MenuItem, Typography } from '@mui/material'; +import { styled } from '@mui/material/styles'; +import { useState } from 'react'; import { useTranslation } from 'react-i18next'; +import { + usePriorityAccount, + usePriorityConnector, +} from '../hooks/connectorHooks'; import { Header } from './Header'; +export const WalletTypography = styled(Typography)(({ theme }) => ({ + transition: theme.transitions.create(['color']), + '&:hover': { + color: theme.palette.grey[200], + cursor: 'pointer', + }, +})); + export const WalletHeader: React.FC = () => { const { t } = useTranslation(); + const [menuAnchor, setMenuAnchor] = useState(null); + const open = Boolean(menuAnchor); + const connector = usePriorityConnector(); + const account = usePriorityAccount(); + const walletAddress = account + ? `${account.substring(0, 7)}...${account.substring(account.length - 7)}` + : null; + + const handleClick = (event: React.MouseEvent) => { + setMenuAnchor(event.currentTarget); + }; + + const handleClose = () => { + setMenuAnchor(null); + }; + + const handleDisconnect = () => { + connector.deactivate(); + handleClose(); + }; + return (
- + + {walletAddress + ? t(`header.walletConnected`, { walletAddress }) + : t(`header.walletNotConnected`)} + + + - {t(`header.walletConnected`, { walletAddress: '0000000000' })} - + + {t(`header.disconnect`)} + +
); }; diff --git a/packages/widget/src/hooks/connectorHooks.ts b/packages/widget/src/hooks/connectorHooks.ts new file mode 100644 index 000000000..da1af33ec --- /dev/null +++ b/packages/widget/src/hooks/connectorHooks.ts @@ -0,0 +1,34 @@ +import { getPriorityConnector } from '@web3-react/core'; +import { + hooks as metaMaskHooks, + metaMask, +} from '../services/connectors/metaMask'; + +export const { + useSelectedChainId, + useSelectedAccounts, + useSelectedIsActivating, + useSelectedError, + useSelectedAccount, + useSelectedIsActive, + useSelectedProvider, + useSelectedENSNames, + useSelectedENSName, + useSelectedWeb3React, + usePriorityConnector, + usePriorityChainId, + usePriorityAccounts, + usePriorityIsActivating, + usePriorityError, + usePriorityAccount, + usePriorityIsActive, + usePriorityProvider, + usePriorityENSNames, + usePriorityENSName, + usePriorityWeb3React, +} = getPriorityConnector( + [metaMask, metaMaskHooks], + // [walletConnect, walletConnectHooks], + // [walletLink, walletLinkHooks], + // [network, networkHooks], +); diff --git a/packages/widget/src/pages/SwapPage.tsx b/packages/widget/src/pages/SwapPage.tsx index 7547deedc..611634758 100644 --- a/packages/widget/src/pages/SwapPage.tsx +++ b/packages/widget/src/pages/SwapPage.tsx @@ -65,7 +65,7 @@ export const SwapPage: React.FC = () => { - {t(`swap.form.from`)} + {t(`swap.from`)} { color="text.secondary" sx={{ alignSelf: 'end' }} > - {t(`swap.form.to`)} + {t(`swap.to`)} @@ -165,7 +165,7 @@ export const SwapPage: React.FC = () => { color="text.primary" sx={{ alignSelf: 'end' }} > - {t(`swap.form.sendToRecipient`)} + {t(`swap.sendToRecipient`)} @@ -187,7 +187,7 @@ export const SwapPage: React.FC = () => { color="text.primary" sx={{ alignSelf: 'end' }} > - {t(`swap.form.routePriority.title`)} + {t(`swap.routePriority.title`)} @@ -200,7 +200,7 @@ export const SwapPage: React.FC = () => { MenuProps={{ elevation: 2 }} > - {t(`swap.form.routePriority.recommended`)} + {t(`swap.routePriority.recommended`)} @@ -234,7 +234,7 @@ export const SwapPage: React.FC = () => { color="text.secondary" sx={{ alignSelf: 'end' }} > - {t(`swap.form.gas`)} + {t(`swap.gas`)} { color="text.primary" sx={{ alignSelf: 'end' }} > - {t(`swap.form.price`, { value: 20 })} + {t(`swap.price`, { value: 20 })} { color="text.secondary" sx={{ alignSelf: 'end' }} > - {t(`swap.form.waitingTime`)} + {t(`swap.waitingTime`)} { - - {t(`swap.button`)} - + ); diff --git a/packages/widget/src/services/connectors/eip1193.ts b/packages/widget/src/services/connectors/eip1193.ts new file mode 100644 index 000000000..b38a930a5 --- /dev/null +++ b/packages/widget/src/services/connectors/eip1193.ts @@ -0,0 +1,28 @@ +import { Eip1193Bridge } from '@ethersproject/experimental'; +import { JsonRpcProvider } from '@ethersproject/providers'; +import { initializeConnector } from '@web3-react/core'; +import { EIP1193 } from '@web3-react/eip1193'; +import { URLS } from '../chains'; + +class Eip1193BridgeWithoutAccounts extends Eip1193Bridge { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + request(request: { method: string; params?: any[] }): Promise { + if ( + request.method === 'eth_requestAccounts' || + request.method === 'eth_accounts' + ) + return Promise.resolve([]); + return super.request(request); + } +} + +const ethersProvider = new JsonRpcProvider(URLS[1][0], 1); +const eip1193Provider = new Eip1193BridgeWithoutAccounts( + ethersProvider.getSigner(), + ethersProvider, +); + +export const [eip1193, hooks] = initializeConnector( + (actions) => new EIP1193(actions, eip1193Provider), + [1], +); diff --git a/packages/widget/src/services/connectors/empty.ts b/packages/widget/src/services/connectors/empty.ts new file mode 100644 index 000000000..f46c32ce5 --- /dev/null +++ b/packages/widget/src/services/connectors/empty.ts @@ -0,0 +1,4 @@ +import { initializeConnector } from '@web3-react/core'; +import { Empty, EMPTY } from '@web3-react/empty'; + +export const [empty, hooks] = initializeConnector(() => EMPTY); diff --git a/packages/widget/src/services/connectors/metaMask.ts b/packages/widget/src/services/connectors/metaMask.ts new file mode 100644 index 000000000..ba65149e3 --- /dev/null +++ b/packages/widget/src/services/connectors/metaMask.ts @@ -0,0 +1,6 @@ +import { initializeConnector } from '@web3-react/core'; +import { MetaMask } from '@web3-react/metamask'; + +export const [metaMask, hooks] = initializeConnector( + (actions) => new MetaMask(actions), +); diff --git a/packages/widget/src/services/connectors/network.ts b/packages/widget/src/services/connectors/network.ts new file mode 100644 index 000000000..a080bac82 --- /dev/null +++ b/packages/widget/src/services/connectors/network.ts @@ -0,0 +1,8 @@ +import { initializeConnector } from '@web3-react/core'; +import { Network } from '@web3-react/network'; +import { URLS } from '../chains'; + +export const [network, hooks] = initializeConnector( + (actions) => new Network(actions, URLS), + Object.keys(URLS).map((chainId) => Number(chainId)), +); diff --git a/packages/widget/src/services/connectors/url.ts b/packages/widget/src/services/connectors/url.ts new file mode 100644 index 000000000..f22c6860b --- /dev/null +++ b/packages/widget/src/services/connectors/url.ts @@ -0,0 +1,8 @@ +import { initializeConnector } from '@web3-react/core'; +import { Url } from '@web3-react/url'; +import { URLS } from '../chains'; + +export const [url, hooks] = initializeConnector( + (actions) => new Url(actions, URLS[1][0]), + [1], +); diff --git a/packages/widget/src/services/connectors/walletConnect.ts b/packages/widget/src/services/connectors/walletConnect.ts new file mode 100644 index 000000000..d3b9cfc33 --- /dev/null +++ b/packages/widget/src/services/connectors/walletConnect.ts @@ -0,0 +1,17 @@ +import { initializeConnector } from '@web3-react/core'; +import { WalletConnect } from '@web3-react/walletconnect'; +import { URLS } from '../chains'; + +export const [walletConnect, hooks] = initializeConnector( + (actions) => + new WalletConnect(actions, { + rpc: Object.keys(URLS).reduce<{ [chainId: number]: string }>( + (accumulator, chainId) => { + accumulator[Number(chainId)] = URLS[Number(chainId)][0]; + return accumulator; + }, + {}, + ), + }), + Object.keys(URLS).map((chainId) => Number(chainId)), +); diff --git a/packages/widget/src/services/connectors/walletLink.ts b/packages/widget/src/services/connectors/walletLink.ts new file mode 100644 index 000000000..839ad89db --- /dev/null +++ b/packages/widget/src/services/connectors/walletLink.ts @@ -0,0 +1,11 @@ +import { initializeConnector } from '@web3-react/core'; +import { WalletLink } from '@web3-react/walletlink'; +import { URLS } from '../chains'; + +export const [walletLink, hooks] = initializeConnector( + (actions) => + new WalletLink(actions, { + url: URLS[1][0], + appName: 'lifi-widget', + }), +); diff --git a/packages/widget/src/theme.ts b/packages/widget/src/theme.ts index 0656dd5f4..c3c6c1554 100644 --- a/packages/widget/src/theme.ts +++ b/packages/widget/src/theme.ts @@ -26,6 +26,9 @@ export const theme = createTheme({ primary: '#000', secondary: '#52575b', }, + success: { + main: '#0AA65B', + }, grey: { 100: '#F4F5F6', 200: '#EFF1F2',