diff --git a/src/shared/connected-wallet.tsx b/src/shared/connected-wallet.tsx index ba92e96..7727cc4 100644 --- a/src/shared/connected-wallet.tsx +++ b/src/shared/connected-wallet.tsx @@ -1,22 +1,48 @@ -import React from "react"; +import React, { useEffect, useRef } from "react"; import { ThirdwebSDK } from "@3rdweb/sdk"; -import { Text, Button, Flex, Icon, Tooltip, useClipboard, useToast } from "@chakra-ui/react"; -import { IoWalletOutline } from "react-icons/io5"; -import { useAccount } from "wagmi"; +import { + Text, + Button, + Flex, + Icon, + Tooltip, + useClipboard, + useToast, + Modal, + ModalCloseButton, + ModalContent, + ModalOverlay, + useDisclosure, + ModalBody, + ButtonGroup, + IconButton, + ModalHeader, + Heading, + Stack, +} from "@chakra-ui/react"; +import { IoCopy, IoWalletOutline } from "react-icons/io5"; +import { useAccount, useBalance, useNetwork, useProvider } from "wagmi"; import { RiMoneyDollarCircleLine } from "react-icons/ri"; import { isAddressZero, useTokenModule } from "./tokenHooks"; -import { useEffect } from "react"; import { useQuery } from "react-query"; +import { ethers } from "ethers"; +import { ChainIDToNativeSymbol } from "./commonRPCUrls"; interface IConnectedWallet { sdk? : ThirdwebSDK; tokenAddress?: string; - showBalance?: boolean; } -export const ConnectedWallet: React.FC = ({ sdk, tokenAddress, showBalance }) => { +function shortenAddress(str: string) { + return `${str.substring(0, 6)}...${str.substring(str.length - 4)}`; +} + +export const ConnectedWallet: React.FC = ({ sdk, tokenAddress }) => { const toast = useToast(); + const baseProvider = useProvider(); + const { isOpen, onOpen, onClose } = useDisclosure({ defaultIsOpen: false }); const [{ data }, disconnect] = useAccount(); + const [{ data: network }] = useNetwork(); const { onCopy } = useClipboard(data?.address || ""); const tokenModule = useTokenModule(sdk, tokenAddress); @@ -30,6 +56,16 @@ export const ConnectedWallet: React.FC = ({ sdk, tokenAddress, isAddressZero(tokenAddress) || tokenAddress.toLowerCase() === otherAddressZero.toLowerCase() ) { + /* + const balance = await baseProvider.getBalance(data?.address); + + return { + value: balance, + displayValue: ethers.utils.formatEther(balance).slice(0, 6), + symbol: ChainIDToNativeSymbol[network?.chain?.id || 0], + }; + */ + return null; } @@ -38,7 +74,25 @@ export const ConnectedWallet: React.FC = ({ sdk, tokenAddress, { enabled: !!data?.address && !!tokenModule, } - ) + ); + + const switchWallet = async () => { + const provider = data?.connector?.getProvider(); + if (!provider?.isMetaMask || !provider.request) return; + + await provider.request({ + method: "wallet_requestPermissions", + params: [{ eth_accounts: {} }], + }); + + onClose(); + } + + + const disconnectWallet = () => { + disconnect(); + onClose(); + } const copyAddress = () => { onCopy(); @@ -50,42 +104,106 @@ export const ConnectedWallet: React.FC = ({ sdk, tokenAddress, }) } - if (!data?.address) { - return null; - } - return ( - - - - {balance && showBalance && ( - - - - {balance?.displayValue} {balance?.symbol} - - + {data?.address && ( + <> + + {balance && ( + + + + {balance?.displayValue} {balance?.symbol} + + + )} + )} - + + + + + + + + Account Details + + + + + + Connected Wallet + + } + /> + + {data?.connector?.getProvider()?.isMetaMask && ( + + )} + + + + + {balance && ( + + Balance + + + + + {balance?.displayValue} {balance?.symbol} + + + + + )} + + + + ) } \ No newline at end of file diff --git a/src/widgets/marketplace.tsx b/src/widgets/marketplace.tsx index ac7aae7..c445ff9 100644 --- a/src/widgets/marketplace.tsx +++ b/src/widgets/marketplace.tsx @@ -92,7 +92,7 @@ const Header: React.FC = (props) => { align="center" justify="flex-end" > - + ) }