diff --git a/crypto_clash_frontend/app/clash/Reveal.tsx b/crypto_clash_frontend/app/clash/Reveal.tsx index 45f6aab..23bdc73 100644 --- a/crypto_clash_frontend/app/clash/Reveal.tsx +++ b/crypto_clash_frontend/app/clash/Reveal.tsx @@ -1,43 +1,105 @@ import { useState } from "react"; +import Web3 from "web3"; interface RevealProps { - revealMove: string; - setRevealMove: (v: string) => void; - handleReveal: () => void; - loading: boolean; account: string; contract: any; - bothRevealed: string; - handleBothRevealed: () => void; - playerARevealed: string; - handlePlayerARevealed: () => void; - playerBRevealed: string; - handlePlayerBRevealed: () => void; + config: Config | null; + web3: Web3 | null; + setStatus: (status: string) => void; } export default function Reveal({ - revealMove, - setRevealMove, - handleReveal, - loading, account, contract, - bothRevealed, - handleBothRevealed, - playerARevealed, - handlePlayerARevealed, - playerBRevealed, - handlePlayerBRevealed, + config, + web3, + setStatus, }: Readonly) { + const [loading, setLoading] = useState(false); + const [revealMove, setRevealMove] = useState(""); + const [bothRevealed, setBothRevealed] = useState(""); + const [playerARevealed, setPlayerARevealed] = useState(""); + const [playerBRevealed, setPlayerBRevealed] = useState(""); const [revealTimeLeft, setRevealTimeLeft] = useState(""); + // Reveal phase read-only handlers + const handleBothRevealed = async () => { + if (!contract) return; + setLoading(true); + try { + const res = await contract.methods.bothRevealed().call(); + setBothRevealed(res ? "true" : "false"); + } catch (err: any) { + setStatus("Failed to fetch bothRevealed: " + err.message); + } finally { + setLoading(false); + } + }; + + const handlePlayerARevealed = async () => { + if (!contract) return; + setLoading(true); + try { + const res = await contract.methods.playerARevealed().call(); + setPlayerARevealed(res ? "true" : "false"); + } catch (err: any) { + setStatus("Failed to fetch playerARevealed: " + err.message); + } finally { + setLoading(false); + } + }; + + const handlePlayerBRevealed = async () => { + if (!contract) return; + setLoading(true); + try { + const res = await contract.methods.playerBRevealed().call(); + setPlayerBRevealed(res ? "true" : "false"); + } catch (err: any) { + setStatus("Failed to fetch playerBRevealed: " + err.message); + } finally { + setLoading(false); + } + }; + const handleRevealTimeLeft = async () => { if (!contract) return; + setLoading(true); try { const res = await contract.methods.revealTimeLeft().call(); setRevealTimeLeft(res.toString()); } catch (err: any) { - console.error("Failed to fetch revealTimeLeft: " + err.message); + setStatus("Failed to fetch revealTimeLeft: " + err.message); + } finally { + setLoading(false); + } + }; + + const handleReveal = async () => { + if (!contract || !web3 || !account) return; + setLoading(true); + setStatus(""); + try { + const tx = contract.methods.reveal(revealMove); + const gas = await tx.estimateGas({ from: account }); + const result = await (globalThis as any).ethereum.request({ + method: "eth_sendTransaction", + params: [ + { + from: account, + to: config?.GAME_CONTRACT_ADDRESS, + data: tx.encodeABI(), + gas: web3.utils.toHex(gas), + chainId: web3.utils.toHex(await web3.eth.net.getId()), + }, + ], + }); + setStatus("Reveal tx sent: " + result); + } catch (err: any) { + setStatus("Reveal failed: " + err.message); + } finally { + setLoading(false); } }; return ( diff --git a/crypto_clash_frontend/app/clash/page.tsx b/crypto_clash_frontend/app/clash/page.tsx index 60db0f6..9ea6f28 100644 --- a/crypto_clash_frontend/app/clash/page.tsx +++ b/crypto_clash_frontend/app/clash/page.tsx @@ -19,7 +19,6 @@ export default function Clash() { const [registerGameId, setRegisterGameId] = useState("0"); const [registerBet, setRegisterBet] = useState(""); - const [revealMove, setRevealMove] = useState(""); // State for read-only contract calls const [betMin, setBetMin] = useState(""); const [activeGameIds, setActiveGameIds] = useState(""); @@ -31,46 +30,6 @@ export default function Clash() { const [pastGame, setPastGame] = useState(null); const [pastGamesCount, setPastGamesCount] = useState(""); const [whoAmI, setWhoAmI] = useState(""); - const [bothRevealed, setBothRevealed] = useState(""); - const [playerARevealed, setPlayerARevealed] = useState(""); - const [playerBRevealed, setPlayerBRevealed] = useState(""); - // Reveal phase read-only handlers - const handleBothRevealed = async () => { - if (!contract) return; - setLoading(true); - try { - const res = await contract.methods.bothRevealed().call(); - setBothRevealed(res ? "true" : "false"); - } catch (err: any) { - setStatus("Failed to fetch bothRevealed: " + err.message); - } finally { - setLoading(false); - } - }; - const handlePlayerARevealed = async () => { - if (!contract) return; - setLoading(true); - try { - const res = await contract.methods.playerARevealed().call(); - setPlayerARevealed(res ? "true" : "false"); - } catch (err: any) { - setStatus("Failed to fetch playerARevealed: " + err.message); - } finally { - setLoading(false); - } - }; - const handlePlayerBRevealed = async () => { - if (!contract) return; - setLoading(true); - try { - const res = await contract.methods.playerBRevealed().call(); - setPlayerBRevealed(res ? "true" : "false"); - } catch (err: any) { - setStatus("Failed to fetch playerBRevealed: " + err.message); - } finally { - setLoading(false); - } - }; // Read-only contract function handlers const handleGetBetMin = async () => { if (!contract || !web3) return; @@ -245,33 +204,6 @@ export default function Clash() { } }; - const handleReveal = async () => { - if (!contract || !web3 || !account) return; - setLoading(true); - setStatus(""); - try { - const tx = contract.methods.reveal(revealMove); - const gas = await tx.estimateGas({ from: account }); - const result = await (globalThis as any).ethereum.request({ - method: "eth_sendTransaction", - params: [ - { - from: account, - to: config?.GAME_CONTRACT_ADDRESS, - data: tx.encodeABI(), - gas: web3.utils.toHex(gas), - chainId: web3.utils.toHex(await web3.eth.net.getId()), - }, - ], - }); - setStatus("Reveal tx sent: " + result); - } catch (err: any) { - setStatus("Reveal failed: " + err.message); - } finally { - setLoading(false); - } - }; - return (
@@ -372,18 +304,11 @@ export default function Clash() { )} {phase === "reveal" && ( )}