diff --git a/AI_React/index.html b/AI_React/index.html index 2d9439d..e5968f1 100644 --- a/AI_React/index.html +++ b/AI_React/index.html @@ -2,6 +2,8 @@ + + ChainChat diff --git a/AI_React/public/logo.ico b/AI_React/public/logo.ico new file mode 100644 index 0000000..0f77df0 Binary files /dev/null and b/AI_React/public/logo.ico differ diff --git a/AI_React/src/App1.tsx b/AI_React/src/App1.tsx deleted file mode 100644 index 5ff30b1..0000000 --- a/AI_React/src/App1.tsx +++ /dev/null @@ -1,219 +0,0 @@ -import { useMemo, useState } from 'react' -import { - useAccount, - useConnect, - useDisconnect, - usePublicClient, - useReadContract, - useWriteContract, -} from 'wagmi' -import { sepolia } from 'wagmi/chains' -import { injected } from 'wagmi/connectors' -import abi from './abi/StakePass.json' -import axios from 'axios' -import { parseEther } from 'viem' -import './index.css' - -const CONTRACT = import.meta.env.VITE_CONTRACT as `0x${string}` -const BACKEND = import.meta.env.VITE_SERVER as string -const FIXED_ETH = '0.01' // 固定 0.01 ETH - -export default function App() { - /** 钱包 */ - const { connect, connectors, status: connectStatus } = useConnect() - const { disconnect } = useDisconnect() - const { address, chainId, isConnected } = useAccount() - const publicClient = usePublicClient() - - /** 链上是否 Active(≥ 0.01 ETH) */ - const { data: active, refetch: refetchActive } = useReadContract({ - abi, - address: CONTRACT, - functionName: 'isActive', - args: [address ?? '0x0000000000000000000000000000000000000000'], - query: { - enabled: !!address, - // 轻量轮询,防止极少数情况下事件漏触发 - refetchInterval: 4000, - }, - }) - - /** 写合约 */ - const { writeContractAsync, isPending } = useWriteContract() - - /** UI 状态 */ - const [prompt, setPrompt] = useState('') - const [reply, setReply] = useState('') - const [loadingChat, setLoadingChat] = useState(false) - const [statusInfo, setStatusInfo] = useState(null) - - /** 连接钱包(Injected + 指定 Sepolia) */ - const onConnect = () => { - const ic = connectors.find((c) => c.id === injected().id) || connectors[0] - connect({ connector: ic, chainId: sepolia.id }) - } - - /** 固定 0.01 ETH 质押:发送 -> 等待确认 -> 刷新 active + 用量 */ - async function doStakeFixed() { - const hash = await writeContractAsync({ - abi, - address: CONTRACT, - functionName: 'stake', - value: parseEther(FIXED_ETH), - }) - await publicClient!.waitForTransactionReceipt({ hash }) - await refetchActive() - await refreshStatus() - } - - /** 固定 0.01 ETH 赎回(无参):发送 -> 等待确认 -> 刷新 active + 用量 */ - async function doUnstakeFixed() { - const hash = await writeContractAsync({ - abi, - address: CONTRACT, - functionName: 'unstake', - }) - await publicClient!.waitForTransactionReceipt({ hash }) - await refetchActive() - await refreshStatus() - } - - /** 拉取后端 /access/status */ - async function refreshStatus() { - if (!address) return - try { - const r = await axios.get(`${BACKEND}/access/status`, { params: { address } }) - setStatusInfo(r.data) - } catch (e) { - // 静默失败即可,避免打断主流程 - // console.error(e) - } - } - - /** 调用后端 /chat */ - async function chat() { - if (!address) return - setLoadingChat(true) - setReply('') - try { - const r = await axios.post( - `${BACKEND}/chat`, - { - model: 'deepseek-chat', - messages: [{ role: 'user', content: prompt }], - max_tokens: 512, - }, - { headers: { 'x-address': address } }, - ) - setReply(r?.data?.data?.choices?.[0]?.message?.content || '(no content)') - } catch (e: any) { - setReply(`调用失败:${e?.response?.data?.msg || e.message}`) - } finally { - setLoadingChat(false) - refreshStatus() - } - } - - const chainOk = useMemo(() => !isConnected || chainId === sepolia.id, [isConnected, chainId]) - - return ( -
- {/* 顶栏 */} -
-
-

ChainChat

- Sepolia -
- -
- {isConnected ? ( - <> -
👛 {address}
- - - ) : ( - - )} -
-
- - {/* 状态 & 固定 0.01 操作 */} -
-
-
- 网络: - - {chainOk ? 'Sepolia ✅' : `Wrong network (${chainId}) ❌`} - -
-
- 状态: - - {active ? 'Active' : 'Inactive'} - -
-
- -
- -
- - - - - - - {statusInfo - ? `今日已用字符:${statusInfo.used ?? 0} / 上限 ${statusInfo.daily_limit ?? 0}` - : '(点击刷新以查看当日用量)'} - -
-
- - {/* Chat */} -
-
- 只有在 Active 状态下才可发送消息 -
-