397 lines
15 KiB
JavaScript
397 lines
15 KiB
JavaScript
import { useTransactionReceipt, useTransaction, useToken } from 'wagmi';
|
|
import { decodeEventLog, decodeFunctionData } from 'viem';
|
|
import { useMemo, useCallback } from "react";
|
|
import { useParams } from "react-router-dom";
|
|
|
|
import { Box, Typography, Link } from "@mui/material";
|
|
import ArrowRightIcon from '@mui/icons-material/ArrowRight';
|
|
|
|
import GhostStyledIcon from "../../components/Icon/GhostIcon";
|
|
import Modal from "../../components/Modal/Modal";
|
|
import Token from "../../components/Token/Token";
|
|
import { PrimaryButton } from "../../components/Button";
|
|
|
|
import { useBondInfo } from "../../hooks/bonds";
|
|
import { getTokenDecimals } from "../../hooks/helpers";
|
|
import { useLocalStorage } from "../../hooks/localstorage";
|
|
import { formatNumber, formatCurrency, shorten } from "../../helpers";
|
|
import { DecimalBigNumber } from "../../helpers/DecimalBigNumber";
|
|
|
|
import { abi as GhostBondDepository } from "../../abi/GhostBondDepository.json";
|
|
import { abi as GhostGatekeeper } from "../../abi/GhostGatekeeper.json";
|
|
import { abi as ERC20Token } from "../../abi/ERC20.json";
|
|
import TokenBox from "./TokenBox";
|
|
|
|
const BondModalClaim = ({ chainId, isOpen, close, config, historyRecord }) => {
|
|
if (historyRecord === null) return null;
|
|
const { network } = useParams();
|
|
|
|
const chainInfo = useMemo(() => {
|
|
const client = config?.getClient();
|
|
|
|
return {
|
|
url: client?.chain?.blockExplorers?.default?.url,
|
|
symbol: client?.chain?.nativeCurrency?.symbol,
|
|
};
|
|
}, [config]);
|
|
|
|
const { data: receipt, isLoading, error } = useTransactionReceipt({
|
|
hash: historyRecord?.txHash,
|
|
query: {
|
|
enabled: !!historyRecord?.txHash && isOpen,
|
|
}
|
|
});
|
|
|
|
const { data: txData } = useTransaction({
|
|
chainId,
|
|
hash: historyRecord.txHash,
|
|
});
|
|
|
|
const functionArgs = useMemo(() => {
|
|
if (!txData) return null;
|
|
const decoded = decodeFunctionData({ abi: GhostBondDepository, data: txData.input });
|
|
if (!decoded) return null;
|
|
|
|
return {
|
|
receiver: decoded.args.at(0),
|
|
isGhst: decoded.args.at(1),
|
|
bondIds: decoded.args.at(2),
|
|
}
|
|
}, [txData]);
|
|
|
|
const allDecodedLogs = useMemo(() => {
|
|
if (!receipt?.logs) return [];
|
|
|
|
return receipt.logs.map((log) => {
|
|
try {
|
|
const decoded = decodeEventLog({
|
|
abi: ERC20Token,
|
|
data: log.data,
|
|
topics: log.topics,
|
|
});
|
|
|
|
return {
|
|
eventName: decoded.eventName,
|
|
args: decoded.args,
|
|
address: log.address
|
|
};
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}).filter(Boolean);
|
|
}, [receipt]);
|
|
|
|
const claimedAmount = useMemo(() => {
|
|
return allDecodedLogs?.reduce((sum, item) => sum + item.args.value, 0n);
|
|
}, [allDecodedLogs]);
|
|
|
|
return (
|
|
<Modal
|
|
maxWidth="420px"
|
|
minHeight="200px"
|
|
open={isOpen}
|
|
headerText={
|
|
<>
|
|
{functionArgs && "Bond Claim"}
|
|
{(!functionArgs) && "Parsing Transaction Hash"}
|
|
</>
|
|
}
|
|
onClose={() => close()}
|
|
>
|
|
{functionArgs && <Box display="flex" flexDirection="column" gap="15px">
|
|
<Box width="100%" gap="20px" display="flex" flexDirection="column" justifyContent="space-between" alignItems="flex-start">
|
|
<Box display="flex" flexDirection="row" justifyContent="space-between" alignItems="center" width="100%" gap="25px">
|
|
<Box
|
|
display="flex"
|
|
alignItems="center"
|
|
justifyContent="center"
|
|
flexShrink={0}
|
|
sx={{ width: "60px", height: "60px" }}
|
|
>
|
|
<Token
|
|
parentSx={{
|
|
display: "flex",
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
"& svg:nth-of-type(2)": {
|
|
transform: "translate(50%, -50%)",
|
|
zIndex: 100,
|
|
}
|
|
}}
|
|
sx={{ transform: "scale(1.65)" }}
|
|
chainTokenName={chainInfo.symbol}
|
|
name={functionArgs.isGhst ? "GHST" : "STNK"}
|
|
/>
|
|
</Box>
|
|
|
|
<Box display="flex" flexDirection="column" justifyContent="space-between" width="100%">
|
|
<Box display="flex" justifyContent="space-between" width="100%">
|
|
<Typography>Receiver</Typography>
|
|
<Link
|
|
href={`${chainInfo.url}/address/${functionArgs.receiver}`}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
>
|
|
<Typography>{shorten(functionArgs.receiver, 10, -8)}</Typography>
|
|
</Link>
|
|
</Box>
|
|
|
|
<Box display="flex" justifyContent="space-between" width="100%">
|
|
<Typography>Claimed</Typography>
|
|
<Typography>
|
|
{formatCurrency(
|
|
new DecimalBigNumber(claimedAmount ?? 0n, getTokenDecimals(functionArgs.isGhst ? "GHST" : "STNK")),
|
|
5,
|
|
functionArgs.isGhst ? "GHST" : "STNK"
|
|
)}
|
|
</Typography>
|
|
</Box>
|
|
|
|
<Box display="flex" justifyContent="space-between" width="100%">
|
|
<Typography>Bond Indexes</Typography>
|
|
<Typography width="160px" textAlign="right">
|
|
{functionArgs.bondIds.map((bond) => formatNumber(bond)).join(", ")}
|
|
</Typography>
|
|
</Box>
|
|
</Box>
|
|
</Box>
|
|
|
|
<PrimaryButton
|
|
fullWidth
|
|
component="a"
|
|
href={`${chainInfo.url}/tx/${historyRecord.txHash}`}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
>
|
|
View Transaction Details
|
|
</PrimaryButton>
|
|
</Box>
|
|
</Box>}
|
|
</Modal>
|
|
)
|
|
}
|
|
|
|
const BondModalBond = ({ chainId, isOpen, close, config, historyRecord }) => {
|
|
if (historyRecord === null) return null;
|
|
const { network } = useParams();
|
|
const { CONSTANTS } = useLocalStorage();
|
|
|
|
const chainInfo = useMemo(() => {
|
|
const client = config?.getClient();
|
|
|
|
return {
|
|
url: client?.chain?.blockExplorers?.default?.url,
|
|
symbol: client?.chain?.nativeCurrency?.symbol,
|
|
};
|
|
}, [config]);
|
|
|
|
const { data: receipt, isLoading, error } = useTransactionReceipt({
|
|
hash: historyRecord?.txHash,
|
|
query: {
|
|
enabled: !!historyRecord?.txHash && isOpen,
|
|
}
|
|
});
|
|
|
|
const { data: txData } = useTransaction({
|
|
chainId,
|
|
hash: historyRecord.txHash,
|
|
});
|
|
|
|
const functionArgs = useMemo(() => {
|
|
if (!txData) return null;
|
|
|
|
try {
|
|
const decoded = decodeFunctionData({
|
|
abi: GhostBondDepository,
|
|
data: txData.input
|
|
});
|
|
|
|
return {
|
|
bondId: decoded.args.at(0),
|
|
amount: decoded.args.at(1),
|
|
maxPrice: decoded.args.at(2),
|
|
receiver: decoded.args.at(3),
|
|
}
|
|
} catch {}
|
|
|
|
try {
|
|
const decoded = decodeFunctionData({
|
|
abi: GhostGatekeeper,
|
|
data: txData.input,
|
|
});
|
|
|
|
return {
|
|
bondId: 0,
|
|
amount: 0n,
|
|
maxPrice: 0n,
|
|
receiver: "0x0000000000000000000000000000000000000000",
|
|
}
|
|
} catch {
|
|
return null;
|
|
}
|
|
}, [txData]);
|
|
|
|
const allDecodedLogs = useMemo(() => {
|
|
if (!receipt?.logs) return [];
|
|
|
|
return receipt.logs.map((log) => {
|
|
try {
|
|
const decoded = decodeEventLog({
|
|
abi: GhostBondDepository,
|
|
data: log.data,
|
|
topics: log.topics,
|
|
});
|
|
|
|
return {
|
|
eventName: decoded.eventName,
|
|
args: decoded.args,
|
|
address: log.address
|
|
};
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}).filter(Boolean);
|
|
}, [receipt]);
|
|
|
|
const { bonds } = useBondInfo(chainId, network, functionArgs ? [functionArgs.bondId] : []);
|
|
|
|
const bondPurchasePrice = useMemo(() => {
|
|
const log = allDecodedLogs.find(l => l.eventName === 'Bond');
|
|
if (!log) return 1n;
|
|
|
|
return log.args.price;
|
|
}, [allDecodedLogs]);
|
|
|
|
const { data: baseToken } = useToken({
|
|
address: bonds?.at(0)?.baseToken.tokenAddress,
|
|
scopeKey: `baseToken-${functionArgs ? functionArgs.bondId : null}-${chainId}`,
|
|
chainId
|
|
});
|
|
|
|
const { data: quoteToken } = useToken({
|
|
address: bonds?.at(0)?.quoteToken.quoteTokenAddress,
|
|
scopeKey: `quoteToken-${functionArgs ? functionArgs.bondId : null}-${chainId}`,
|
|
chainId
|
|
});
|
|
|
|
const estimatedPayout = useMemo(() => {
|
|
const spentAmount = functionArgs ? functionArgs.amount : 0n;
|
|
const bondPrice = bondPurchasePrice ? bondPurchasePrice : 1n;
|
|
const quoteTokenDecimals = quoteToken ? quoteToken.decimals : 18;
|
|
|
|
const mulDivResult = spentAmount * (10n ** 18n) / bondPrice;
|
|
return mulDivResult / (10n ** BigInt(quoteTokenDecimals));
|
|
}, [bondPurchasePrice, functionArgs?.amount, baseToken, quoteToken]);
|
|
|
|
const convertedAmount = useCallback((value) => {
|
|
const decimals = baseToken ? baseToken.decimals : 9;
|
|
const symbol = quoteToken ? quoteToken.symbol : "Unknown";
|
|
|
|
const convertedValue = new DecimalBigNumber(value ? value : 0n, decimals);
|
|
return formatCurrency(convertedValue.toString(), 5, symbol);
|
|
}, [quoteToken, baseToken]);
|
|
|
|
return (
|
|
<Modal
|
|
maxWidth="420px"
|
|
minHeight="200px"
|
|
open={isOpen}
|
|
headerText={
|
|
<>
|
|
{(bonds && functionArgs && bondPurchasePrice) &&
|
|
functionArgs.receiver !== "0x0000000000000000000000000000000000000000"
|
|
? "Bond Purchase" : "Voluntary Verification"}
|
|
{(!bonds || !functionArgs || !bondPurchasePrice) && "Parsing Transaction Hash"}
|
|
</>
|
|
}
|
|
onClose={() => close()}
|
|
>
|
|
{bonds && functionArgs && bondPurchasePrice && <Box display="flex" flexDirection="column" gap="15px">
|
|
<Box width="100%" gap="20px" display="flex" flexDirection="column" justifyContent="space-between" alignItems="flex-start">
|
|
<Box width="100%" display="flex" flexDirection="row" justifyContent="space-around" alignItems="center">
|
|
<TokenBox
|
|
tokenAmount={functionArgs.amount}
|
|
decimals={quoteToken ? quoteToken.decimals : 18}
|
|
tokenSymbol={bonds.at(0) ? bonds.at(0).quoteToken.name : "Unknown"}
|
|
chainTokenSymbol={bonds.at(0) ? bonds.at(0).quoteToken.chainNames.at(0) : chainInfo.symbol}
|
|
/>
|
|
|
|
<GhostStyledIcon sx={{ fontSize: '24px' }} component={ArrowRightIcon} />
|
|
|
|
<TokenBox
|
|
tokenAmount={estimatedPayout}
|
|
decimals={baseToken ? baseToken.decimals : 9}
|
|
tokenSymbol={bonds.at(0) ? bonds.at(0).baseToken.name : "Unknown"}
|
|
chainTokenSymbol={bonds.at(0) ? bonds.at(0).baseToken.chainNames.at(0) : chainInfo.symbol}
|
|
/>
|
|
</Box>
|
|
|
|
<Box display="flex" flexDirection="column" justifyContent="space-between" width="100%">
|
|
<Box display="flex" justifyContent="space-between" width="100%">
|
|
{functionArgs.receiver !== "0x0000000000000000000000000000000000000000" &&
|
|
<>
|
|
<Typography>Receiver</Typography>
|
|
<Link
|
|
href={`${chainInfo.url}/address/${functionArgs.receiver}`}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
>
|
|
<Typography>{shorten(functionArgs.receiver, 12, -10)}</Typography>
|
|
</Link>
|
|
</>
|
|
}
|
|
</Box>
|
|
|
|
<Box display="flex" justifyContent="space-between" width="100%">
|
|
<Typography>Max Price Set</Typography>
|
|
<Typography>{convertedAmount(functionArgs.maxPrice)}</Typography>
|
|
</Box>
|
|
|
|
<Box display="flex" justifyContent="space-between" width="100%">
|
|
<Typography>Effective Price</Typography>
|
|
<Typography>{convertedAmount(bondPurchasePrice)}</Typography>
|
|
</Box>
|
|
</Box>
|
|
|
|
<PrimaryButton
|
|
fullWidth
|
|
component="a"
|
|
href={`${chainInfo.url}/tx/${historyRecord.txHash}`}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
>
|
|
View Transaction Details
|
|
</PrimaryButton>
|
|
</Box>
|
|
</Box>}
|
|
</Modal>
|
|
)
|
|
}
|
|
|
|
const BondModal = ({ chainId, isOpen, close, config, historyRecord }) => {
|
|
const { CONSTANTS } = useLocalStorage();
|
|
|
|
switch (historyRecord.name) {
|
|
case CONSTANTS.bond.bond:
|
|
return <BondModalBond
|
|
chainId={chainId}
|
|
isOpen={isOpen}
|
|
close={close}
|
|
config={config}
|
|
historyRecord={historyRecord}
|
|
/>
|
|
case CONSTANTS.bond.claim:
|
|
return <BondModalClaim
|
|
chainId={chainId}
|
|
isOpen={isOpen}
|
|
close={close}
|
|
config={config}
|
|
historyRecord={historyRecord}
|
|
/>
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export default BondModal;
|