ghost-dao-interface/src/containers/History/BondModals/BondModalExodus.jsx
2026-11-25 12:14:28 +03:00

229 lines
8.5 KiB
JavaScript

import { useTransactionReceipt, useTransaction, useToken } from 'wagmi';
import { decodeEventLog, decodeFunctionData, toHex } from 'viem';
import { useMemo, useCallback, useState } from "react";
import { useParams } from "react-router-dom";
import { Box, Typography, Link, Skeleton } from "@mui/material";
import CheckIcon from '@mui/icons-material/Check';
import ContentPasteIcon from '@mui/icons-material/ContentPaste';
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 { useTokenSymbol } from "../../../hooks/tokens";
import { getTokenDecimals } from "../../../hooks/helpers";
import { useLocalStorage } from "../../../hooks/localstorage";
import { formatNumber, formatCurrency, shorten } from "../../../helpers";
import { DecimalBigNumber } from "../../../helpers/DecimalBigNumber";
import { abi as GhostStaking } from "../../../abi/GhostStaking.json";
import { abi as GhostGatekeeper } from "../../../abi/GhostGatekeeper.json";
import { abi as ERC20Token } from "../../../abi/ERC20.json";
import TokenBox from "../TokenBox";
const ClickableHex = ({ copyToClipboard, index, value, copiedIndex }) => {
if (!value) return <Skeleton width="185px" />;
return (
<Link
style={{ display: "flex", flexDirection: "row", justifyContent: "center", alignItems: "center" }}
onClick={() => copyToClipboard(value, index)}
>
<Typography>{shorten(value, 12, -10)}</Typography>
<GhostStyledIcon
sx={{ marginLeft: "5px", width: "12px", height: "12px" }}
viewBox="0 0 25 25"
component={copiedIndex === index ? CheckIcon : ContentPasteIcon}
/>
</Link>
)
}
const BondModalExodus = ({ chainId, address, isOpen, close, config, historyRecord }) => {
if (historyRecord === null) return null;
const { network } = useParams();
const { CONSTANTS } = useLocalStorage();
const [copiedIndex, setCopiedIndex] = useState(null);
const copyToClipboard = (text, index) => {
navigator.clipboard.writeText(text).then(() => {
setCopiedIndex(index);
setTimeout(() => setCopiedIndex(null) , 800);
});
};
const chainInfo = useMemo(() => {
const client = config?.getClient();
return {
url: client?.chain?.blockExplorers?.default?.url,
symbol: client?.chain?.nativeCurrency?.symbol,
};
}, [config]);
const { symbol: ftsoSymbol } = useTokenSymbol(chainId, "FTSO");
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: GhostGatekeeper,
data: txData.input,
});
return {
message: decoded?.args.at(0),
nonce: toHex(decoded?.args.at(1)),
signature: toHex(decoded?.args.at(2)),
}
} catch (err) {
return null;
}
}, [txData]);
const allDecodedLogs = useMemo(() => {
if (!receipt?.logs) return [];
return receipt.logs.map((log) => {
try {
const decoded = decodeEventLog({
abi: GhostGatekeeper,
data: log.data,
topics: log.topics,
});
return {
eventName: decoded.eventName,
args: decoded.args,
address: log.address
};
} catch (e) {}
try {
const decoded = decodeEventLog({
abi: GhostStaking,
data: log.data,
topics: log.topics,
});
return {
eventName: decoded.eventName,
args: decoded.args,
address: log.address
};
} catch (e) {
return null;
}
}).filter(Boolean);
}, [receipt]);
const voluntaryVerification = useMemo(() => {
return !!allDecodedLogs.find(l => l.eventName === "VoluntaryVerification");
}, [allDecodedLogs])
const stakedAmount = useMemo(() => {
const log = allDecodedLogs.find(l => l.eventName === 'Staked');
if (!log) return 1n;
return new DecimalBigNumber(log.args.amount, getTokenDecimals("FTSO"));
}, [allDecodedLogs]);
return (
<Modal
maxWidth="420px"
minHeight="200px"
open={isOpen}
headerText={
<>
{(functionArgs && allDecodedLogs) && voluntaryVerification
? "Voluntary Verification" : "Exodus Verification"}
{(!functionArgs || !allDecodedLogs) && "Parsing Transaction Hash"}
</>
}
onClose={() => close()}
>
{<Box display="flex" flexDirection="column" gap="15px">
<Box width="100%" gap="20px" display="flex" flexDirection="column" justifyContent="space-between" alignItems="flex-start">
{!voluntaryVerification && <Box width="100%" display="flex" flexDirection="column" justifyContent="space-around" alignItems="center">
<Box display="flex" justifyContent="space-between" width="100%">
<Typography>Exodus Reward</Typography>
<Typography>{formatCurrency(stakedAmount, 5, ftsoSymbol)}</Typography>
</Box>
<Box display="flex" justifyContent="space-between" width="100%">
<Typography>Receiver</Typography>
<ClickableHex
index={0}
value={address}
copiedIndex={copiedIndex}
copyToClipboard={copyToClipboard}
/>
</Box>
</Box>}
<Box display="flex" flexDirection="column" justifyContent="space-between" width="100%">
<Box display="flex" justifyContent="space-between" width="100%">
<Typography>Message</Typography>
<ClickableHex
index={1}
copiedIndex={copiedIndex}
copyToClipboard={copyToClipboard}
value={functionArgs?.message}
/>
</Box>
<Box display="flex" justifyContent="space-between" width="100%">
<Typography>Nonce</Typography>
<ClickableHex
index={2}
copiedIndex={copiedIndex}
copyToClipboard={copyToClipboard}
value={functionArgs?.nonce}
/>
</Box>
<Box display="flex" justifyContent="space-between" width="100%">
<Typography>Signature</Typography>
<ClickableHex
index={3}
copiedIndex={copiedIndex}
copyToClipboard={copyToClipboard}
value={functionArgs?.signature}
/>
</Box>
</Box>
<PrimaryButton
fullWidth
component="a"
href={`${chainInfo.url}/tx/${historyRecord.txHash}`}
target="_blank"
rel="noopener noreferrer"
>
View Transaction Details
</PrimaryButton>
</Box>
</Box>}
</Modal>
)
}
export default BondModalExodus;