import useSWR from 'swr';
import { useMemo } from "react";
import {
Box,
Button,
Divider,
IconButton,
Paper,
SvgIcon,
Typography,
useMediaQuery,
useTheme,
} from "@mui/material";
import { styled } from '@mui/material/styles';
import CloseIcon from '@mui/icons-material/Close';
import LoopIcon from '@mui/icons-material/Loop';
import ReactGA from "react-ga4";
import { ReactElement, useState, useEffect } from "react";
import { useNavigate, useLocation, createSearchParams } from "react-router-dom";
import GhostStyledIcon from "../../Icon/GhostIcon";
import { Tokens, NativeToken, useWallet } from "./Token";
import { PrimaryButton, SecondaryButton } from "../../Button";
import ArrowUpIcon from "../../../assets/icons/arrow-up.svg?react";
import { formatCurrency, shorten } from "../../../helpers";
import { DecimalBigNumber } from "../../../helpers/DecimalBigNumber";
import { RESERVE_ADDRESSES, FTSO_ADDRESSES } from "../../../constants/addresses";
import { useUnstableProvider } from "../../../hooks/ghost"
import { useAccount, useDisconnect, useSwitchChain } from "wagmi";
const DisconnectButton = ({ onClose }) => {
const { disconnect } = useDisconnect();
return (
{
disconnect();
onClose();
}}
variant="contained"
fullWidth
>
Disconnect
);
};
const CloseButton = styled(IconButton)(theme => ({
root: {
...theme.overrides?.MuiButton?.containedSecondary,
width: "30px",
height: "30px",
},
}));
const WalletTotalValue = ({ chainName, address, tokens, currency, setCurrency }) => {
const ghstPrice = tokens?.ghst ? tokens.ghst.price : tokens?.price;
const tokensObject = tokens?.ghst ? tokens : { tokens };
const [topHovered, setTopHovered] = useState(false);
const walletTotalValueUSD = Object.values(tokensObject).reduce(
(totalValue, token) => totalValue.add(token.balance.mul(token.price)),
new DecimalBigNumber(0n, 0),
);
const walletValue = {
USD: walletTotalValueUSD ? walletTotalValueUSD : new DecimalBigNumber(0n, 0),
GHST: walletTotalValueUSD && ghstPrice.gt(new DecimalBigNumber(0, 0)) ?
walletTotalValueUSD.div(ghstPrice)
:
new DecimalBigNumber(0n, 0),
};
return (
setCurrency(currency === "USD" ? "GHST" : "USD")}
onMouseEnter={() => setTopHovered(true)}
onMouseLeave={() => setTopHovered(false)}
>
{chainName?.toUpperCase()}
{shorten(address)}
{tokens?.ghst &&
}
{formatCurrency(walletValue[currency], currency === "USD" ? 2 : 5, currency === "USD" ? "USD" : "👻" )}
);
};
function InitialWalletView({ isWalletOpen, address, chainId, onClose }) {
const theme = useTheme();
const navigate = useNavigate();
const location = useLocation();
const tokens = useWallet(chainId, address);
const { client, casperApi, account, isExtensionMissing } = useUnstableProvider();
const { chains } = useSwitchChain();
const [currency, setCurrency] = useState("USD");
const [accountIndex, setAccountIndex] = useState(0);
const chainName = useMemo(() => {
const chain = chains.find(chain => chain.id === chainId)
if (!chain) return null
return chain.name.toLowerCase()
}, [chains, chainId]);
const { data: chainProperties } = useSWR(
client ? ['chainProperties', client] : null,
() => client.getChainSpecData(),
{
revalidateOnFocus: false
}
);
const { data: accountBalanceRaw } = useSWR(
client ? ['accountBalance', client] : null,
() => casperApi.query.System.Account.getValue(account),
{
refreshInterval: 12000,
revalidateOnFocus: true
}
);
const accountBalance = useMemo(() => {
const decimals = chainProperties ? chainProperties.properties.tokenDecimals : 18;
if (!accountBalanceRaw) return new DecimalBigNumber(0n, decimals);
const balance = accountBalanceRaw.data.free
+ accountBalanceRaw.data.frozen
+ accountBalanceRaw.data.reserved;
return new DecimalBigNumber(balance, decimals);
}, [accountBalanceRaw, chainProperties]);
const nativeGhst = useMemo(() => {
return {
...tokens.ghst,
balance: accountBalance,
}
}, [tokens, accountBalance])
const onBtnClick = (dexName, from, to) => {
navigate({
pathname: `${chains.find(chain => chain.id === chainId).name.toLowerCase()}/dex/` + dexName,
search: createSearchParams({
pool: true,
from: from,
to: to,
}).toString()
})
onClose();
}
useEffect(() => {
if (isWalletOpen && chainName) {
ReactGA.send({ hitType: "pageview", page: `/${chainName.toLowerCase()}/sidebar` });
}
}, [isWalletOpen, chainName])
useEffect(() => {
if (isWalletOpen) {
Object.values(tokens ?? {}).forEach(token => token.refetch());
}
}, [isWalletOpen, tokens])
return (
{!isExtensionMissing
? <>
>
:
GHOST Connect is not detected!
Download GHOST Connect for bounties, validator stats, and (10, 10) {"Stake\u00B2"} rewards.
window.open(GHOST_CONNECT, '_blank', 'noopener,noreferrer')}
>
Get GHOST Connect
}
);
}
export default InitialWalletView;