264 lines
9.7 KiB
TypeScript
264 lines
9.7 KiB
TypeScript
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 (
|
|
<PrimaryButton
|
|
onClick={() => {
|
|
disconnect();
|
|
onClose();
|
|
}}
|
|
variant="contained"
|
|
fullWidth
|
|
>
|
|
Disconnect
|
|
</PrimaryButton>
|
|
);
|
|
};
|
|
|
|
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 (
|
|
<Box
|
|
style={{ cursor: "pointer" }}
|
|
onClick={() => setCurrency(currency === "USD" ? "GHST" : "USD")}
|
|
onMouseEnter={() => setTopHovered(true)}
|
|
onMouseLeave={() => setTopHovered(false)}
|
|
>
|
|
<Box width="100%" display="flex" flexDirection="row" justifyContent="space-between" alignItems="center">
|
|
<Box>
|
|
<Typography style={{ lineHeight: 1.1, fontWeight: 600, fontSize: "0.975rem" }} color="textSecondary">
|
|
{chainName?.toUpperCase()}
|
|
</Typography>
|
|
<Typography style={{ fontSize: ".85rem", lineHeight: 1.1 }} variant="h3">
|
|
{shorten(address)}
|
|
</Typography>
|
|
</Box>
|
|
{tokens?.ghst && <Box pt="1px">
|
|
<GhostStyledIcon
|
|
viewBox="0 0 24 24"
|
|
component={LoopIcon}
|
|
style={{
|
|
transition: "transform .3s ease-in-out",
|
|
transitionTimingFunction: " cubic-bezier(.4,0,.2,1)",
|
|
transform: topHovered ? "rotateZ(-180deg)" : "rotateZ(0deg)",
|
|
}}
|
|
/>
|
|
</Box>}
|
|
</Box>
|
|
|
|
<Typography style={{ fontWeight: 700 }} variant="h3">
|
|
{formatCurrency(walletValue[currency], currency === "USD" ? 2 : 5, currency === "USD" ? "USD" : "👻" )}
|
|
</Typography>
|
|
</Box>
|
|
);
|
|
};
|
|
|
|
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 (
|
|
<Paper>
|
|
<Box sx={{ padding: theme.spacing(0, 3), display: "flex", flexDirection: "column", minHeight: "100vh" }}>
|
|
<Box sx={{ display: "flex", justifyContent: "space-between", padding: theme.spacing(3, 0, 1) }}>
|
|
<WalletTotalValue
|
|
currency={currency}
|
|
setCurrency={setCurrency}
|
|
chainName={chainName}
|
|
address={address}
|
|
tokens={tokens}
|
|
/>
|
|
<CloseButton size="small" onClick={onClose} aria-label="close wallet">
|
|
<GhostStyledIcon component={CloseIcon} />
|
|
</CloseButton>
|
|
</Box>
|
|
|
|
<Box sx={{ margin: theme.spacing(2, -3) }}>
|
|
<Divider />
|
|
</Box>
|
|
|
|
<Box sx={{ display: "flex", flexDirection: "column" }} >
|
|
<Tokens address={address} tokens={tokens} onClose={onClose} />
|
|
</Box>
|
|
|
|
<Box sx={{ margin: theme.spacing(2, -3, 3) }}>
|
|
<Divider />
|
|
</Box>
|
|
|
|
{!isExtensionMissing
|
|
? <>
|
|
<Box sx={{ display: "flex", flexDirection: "column" }} style={{ gap: theme.spacing(2, 1) }}>
|
|
<WalletTotalValue
|
|
currency={currency}
|
|
setCurrency={setCurrency}
|
|
chainName={chainProperties?.name ?? ""}
|
|
address={account?.address ?? "...wait"}
|
|
tokens={nativeGhst}
|
|
/>
|
|
<NativeToken {...nativeGhst} />
|
|
</Box>
|
|
<Box sx={{ margin: theme.spacing(2, -3, 3) }}>
|
|
<Divider />
|
|
</Box>
|
|
</>
|
|
: <Box
|
|
sx={{ borderRadius: "15px", background: theme.colors.paper.background }}
|
|
width="100%"
|
|
height="100%"
|
|
padding="20px 30px"
|
|
display="flex"
|
|
flexDirection="column"
|
|
justifyContent="space-around"
|
|
alignItems="center"
|
|
gap="10px"
|
|
>
|
|
<Typography sx={{ textAlign: "center" }} variant="subtitle1">GHOST Connect is not detected!</Typography>
|
|
<Typography sx={{ textAlign: "center" }} variant="body2">Download GHOST Connect for bounties, validator stats, and (10, 10) {"Stake\u00B2"} rewards.</Typography>
|
|
<PrimaryButton
|
|
fullWidth
|
|
onClick={() => window.open(GHOST_CONNECT, '_blank', 'noopener,noreferrer')}
|
|
>
|
|
Get GHOST Connect
|
|
</PrimaryButton>
|
|
</Box>
|
|
}
|
|
|
|
|
|
|
|
<Box sx={{ width: "100%", marginTop: "auto", marginX: "auto", padding: theme.spacing(2, 0) }}>
|
|
<DisconnectButton onClose={onClose} />
|
|
</Box>
|
|
</Box>
|
|
</Paper>
|
|
);
|
|
}
|
|
|
|
export default InitialWalletView;
|