ghost-dao-interface/src/containers/Bond/BondModal.jsx
Uncle Fatso 4de736ea08
make the bond purchase functionality work again
Signed-off-by: Uncle Fatso <uncle.fatso@ghostchain.io>
2026-11-09 19:07:47 +03:00

206 lines
8.5 KiB
JavaScript

import { Box, Container, Skeleton, Typography, useMediaQuery } from "@mui/material";
import { useEffect, useState, useMemo, useCallback } from "react";
import { useLocation, useParams, useNavigate } from "react-router-dom";
import { useAccount, useSwitchChain } from "wagmi";
import { isAddress } from "viem";
import ReactGA from "react-ga4";
import Countdown from "../../components/Countdown/Countdown";
import Paper from "../../components/Paper/Paper";
import PageTitle from "../../components/PageTitle/PageTitle";
import Metric from "../../components/Metric/Metric";
import MetricCollection from "../../components/Metric/MetricCollection";
import TokenStack from "../../components/TokenStack/TokenStack";
import BondPrice from "./components/BondPrice";
import BondDiscount from "./components/BondDiscount";
import BondInputArea from "./components/BondInputArea";
import BondSettingsModal from "./components/BondSettingsModal";
import NotFound from "../NotFound/NotFound";
import { DecimalBigNumber } from "../../helpers/DecimalBigNumber";
import { isNetworkLegacy } from "../../constants";
import { formatCurrency } from "../../helpers";
import { useLocalStorage } from "../../hooks/localstorage";
import { useLiveBonds } from "../../hooks/bonds";
import { useFtsoPrice } from "../../hooks/prices";
const SLIPPAGE_PREFIX = "bond-slippage";
const DECIMALS_PREFIX = "bond-decimals";
const RECIPIENT_PREFIX = "bond-recipient";
const BondModalContainer = ({ chainId, address, config, connect }) => {
const { id, network } = useParams();
const { liveBonds } = useLiveBonds(chainId, network);
const bond = liveBonds.find(bond => bond.id === Number(id));
if (!bond) return <NotFound />;
return <BondModal config={config} chainId={chainId} bond={bond} address={address} connect={connect} />;
};
export const BondModal = ({ bond, chainId, address, config, connect }) => {
const navigate = useNavigate();
const { network } = useParams();
const { pathname } = useLocation();
const isSmallScreen = useMediaQuery("(max-width: 650px)");
const isVerySmallScreen = useMediaQuery("(max-width: 379px)");
const [isSettingsOpen, setSettingsOpen] = useState(false);
const { useStorageValue, setStorageValue } = useLocalStorage();
const { data: slippage } = useStorageValue(chainId, address, SLIPPAGE_PREFIX, "10");
const { data: formatDecimals } = useStorageValue(chainId, address, DECIMALS_PREFIX, "5");
const { data: recipientAddress } = useStorageValue(chainId, address, RECIPIENT_PREFIX, address);
useEffect(() => {
ReactGA.send({ hitType: "pageview", page: pathname });
}, [pathname])
const setRecipientAddress = useCallback((value) => {
if (isAddress(value)) {
setStorageValue(chainId, address, RECIPIENT_PREFIX, value);
}
}, [chainId, address]);
const setSlippage = useCallback((value) => {
const maybeValue = parseFloat(value);
if (!maybeValue || parseFloat(value) <= 100) {
setStorageValue(chainId, address, SLIPPAGE_PREFIX, value);
}
}, [chainId, address]);
const setFormatDecimals = useCallback((value) => {
if (Number(value) <= 17) {
setStorageValue(chainId, address, DECIMALS_PREFIX, value);
}
}, [chainId, address]);
useEffect(() => {
const handleKeyDown = (event) => {
if (event.key === "Escape") isSettingsOpen ? setSettingsOpen(false) : navigate(`/${network}/bonds`);
};
window.addEventListener("keydown", handleKeyDown);
return () => window.removeEventListener("keydown", handleKeyDown);
}, [network, navigate, isSettingsOpen]);
const chainSymbol = useMemo(() => {
const chainSymbol = config?.getClient()?.chain?.nativeCurrency?.symbol;
if (chainSymbol) return chainSymbol;
return "WTF";
}, [config]);
const preparedQuoteToken = useMemo(() => {
if (isNetworkLegacy(chainId)) {
return {
address: bond.quoteToken.quoteTokenAddress,
chainNames: bond.quoteToken.chainNames,
icons: bond.quoteToken.icons,
name: bond.quoteToken.name,
}
}
return { address: undefined, icons: [chainSymbol], name: chainSymbol };
}, [bond, chainSymbol, chainId])
return (
<Box>
<PageTitle
subtitle="We need this one just for alignment"
name={
<Box display="flex" flexDirection="row" alignItems="center">
<TokenStack
chainTokenNames={preparedQuoteToken.chainNames}
tokens={preparedQuoteToken.icons}
sx={{ fontSize: "27px" }}
/>
<Box display="flex" flexDirection="column" ml={1} justifyContent="center" alignItems="center">
<Typography variant="h4" fontWeight={500}>
{preparedQuoteToken.name} Bond
</Typography>
</Box>
</Box>
}
/>
<Box display="flex" flexDirection="column" alignItems="center">
<BondSettingsModal
open={isSettingsOpen}
handleClose={() => setSettingsOpen(false)}
slippage={slippage}
recipientAddress={recipientAddress}
formatDecimals={formatDecimals}
onRecipientAddressChange={event => setRecipientAddress(event.currentTarget.value)}
onSlippageChange={event => setSlippage(event.currentTarget.value)}
onDecimalsChange={event => setFormatDecimals(event.currentTarget.value)}
/>
<Container
style={{
marginTop: "15px",
paddingLeft: isSmallScreen || isVerySmallScreen ? "0" : "3.3rem",
paddingRight: isSmallScreen || isVerySmallScreen ? "0" : "3.3rem",
}}
>
<Paper
fullWidth
enableBackground
headerContent={
<Box display="flex" alignItems="center" flexDirection="row" gap="5px">
<Typography variant="h6">
Bond Expires&nbsp;
<Countdown otherwise="[EXPIRED]" targetTime={bond.duration * 1000} />
</Typography>
</Box>
}
>
<MetricCollection>
<Metric
label={`Bond Price`}
tooltip="Some description about what the bond price is"
metric={bond.isSoldOut ? ( "--" ) : ( <BondPrice price={bond.price.inUsd} /> )}
/>
<Metric
label="Market Price"
metric={<TokenPrice priceInUsd={bond.price.marketPriceInUsd} />}
/>
<Metric
label="Discount"
tooltip="Some description about what the discount is"
metric={<BondDiscount discount={bond.discount} textOnly />}
/>
</MetricCollection>
</Paper>
</Container>
<Box width="100%" display="flex" justifyContent="center" alignItems="center">
<BondInputArea
chainId={chainId}
bond={bond}
config={config}
connect={connect}
address={address}
slippage={slippage}
recipientAddress={recipientAddress}
preparedQuoteToken={preparedQuoteToken}
handleSettingsOpen={() => setSettingsOpen(true)}
formatDecimals={formatDecimals}
/>
</Box>
</Box>
</Box>
);
};
const TokenPrice = ({ priceInUsd }) => {
return priceInUsd ? (
<>{formatCurrency(priceInUsd, 2)}</>
) : (
<Skeleton width={60} />
);
};
export default BondModalContainer;