// "use client"; import type { Agent } from "@/types/agent"; import VRMDemo from "./vrm-demo"; import { PriceChart } from "./price-chart"; import { TokenData } from "./token-data"; import { AgentDescription } from "./agent-description"; import { SocialMediaButtons } from "./social-media-buttons"; import { AgentTags } from "./agent-tags"; import { AgentTiers } from "./agent-tiers"; import { Button } from "./ui/button"; import { MessageSquare, ArrowRightLeft } from "lucide-react"; import { Integrations } from "./integrations"; import { useEffect, useState } from "react"; import { useTokens } from "@/hooks/use-token"; import { AlertTriangle } from "lucide-react"; import { ConnectButton } from "@rainbow-me/rainbowkit"; import { useReserveToken } from "@/hooks/use-reserve-token"; import { useAccount, useReadContract } from "wagmi"; import { ERC721_ABI } from "@/utils/abi/erc721"; import { formatUnits } from "ethers"; import { ERC20_ABI } from "@/utils/abi/erc20"; import { AgentVrmDiagnosis } from "./agent-diagnosis"; import { AgentDemo } from "./agent-demo"; interface AgentDetailsProps { agent: Agent; } const AMICA_URL = process.env.NEXT_PUBLIC_AMICA_URL as string; const delay = (ms: number) => new Promise(res => setTimeout(res, ms)); export function AgentDetails({ agent }: AgentDetailsProps) { const [diagnosisPassed, setDiagnosisPassed] = useState(false); const [reserveAmount, setReserveAmount] = useState(""); const [talentShow, setTalentShow] = useState(false); const [talentRunning, setTalentRunning] = useState(false); const { isConnected, address } = useAccount(); const { stats, priceHistory, tokenAddress, loading, error } = useTokens(Number(agent.id)); const { write: reserveTokens, isLoading: reserving, isSuccess: reserveSuccess, error: reserveError } = useReserveToken(); const { data, isLoading: loadingAius, refetch: refetchAiusData } = useReadContract({ address: process.env.NEXT_PUBLIC_CONTRACT_ADDRESS! as `0x${string}`, abi: ERC721_ABI, functionName: "getAiusAndOwed", args: [BigInt(agent.id), address], query: { enabled: isConnected && !!address }, }); const [aius, owed] = (data as [bigint, bigint]) || [BigInt(0), BigInt(0)]; const { data: aiusAmount, refetch: refetchAiusAmount } = useReadContract({ address: process.env.NEXT_PUBLIC_AIUS_CONTRACT_ADDRESS! as `0x${string}`, abi: ERC20_ABI, functionName: "balanceOf", args: [address], query: { enabled: isConnected && !!address }, }); const formattedAius = aius ? formatUnits(aius, 18) : "0.0"; const formattedOwed = owed ? formatUnits(owed, 18) : "0.0"; const isPairNotCreated = error == "Pair not created"; // refetch aius and reserve amount after reserve, reload page if pair created useEffect(() => { if (reserveSuccess) { const refetchAndCheck = async () => { const { data: aiusAndOwedData } = await refetchAiusData(); await refetchAiusAmount(); setReserveAmount(""); const [newAius, newOwed] = (aiusAndOwedData as [bigint, bigint]) || [BigInt(0), BigInt(0)]; const formattedAius = parseFloat(formatUnits(newAius, 18)); if (formattedAius >= 100) { await delay(5000); window.location.reload(); // Refresh the page } }; refetchAndCheck(); } }, [refetchAiusData, reserveSuccess, refetchAiusAmount]); if (loading) { return (
); } if (error && !isPairNotCreated) { return (
Error loading agents: {typeof error === "object" && error !== null && "message" in error ? (error as Error).message : String(error)}
); } return (

{agent.name}

{agent.token} | {agent.tier?.name} (Level {agent.tier?.level})

{isPairNotCreated && (
Pair not created yet.
)} {!isPairNotCreated && ()}
{!isPairNotCreated && ()}
{isPairNotCreated ? (
{isConnected && ( <>
AIUS Deposited on token: {formattedAius ? formattedAius.toString() : loadingAius ? "Loading..." : "0"}
Your Own AINFT: {formattedOwed ? formattedOwed.toString() : loadingAius ? "Loading..." : "0"}
{ const inputValue = parseFloat(e.target.value); const maxLimit = 100 - Number(formattedAius); if (!isNaN(inputValue) && inputValue <= maxLimit) { setReserveAmount(e.target.value); } else if (e.target.value === "") { setReserveAmount(""); } }} placeholder={`${100 - Number(formattedAius)} AIUS before added liquidity`} className="w-full pl-4 pr-16 py-3 text-sm border rounded-xl shadow-sm font-roboto-mono focus:outline-none focus:ring-2 focus:ring-yellow-400" /> AIUS
)} {reserveError && !reserveError?.message?.toLowerCase().includes("user rejected") && (
{reserveError.message}
)}
) : ( )}
{agent.tags.length > 1 && }
); }