diff --git a/src/components/YourGames.tsx b/src/components/YourGames.tsx index e05ca68..296437f 100644 --- a/src/components/YourGames.tsx +++ b/src/components/YourGames.tsx @@ -4,8 +4,9 @@ import Image from "next/image"; import { games } from "../data/games"; import { FiTrash } from "react-icons/fi"; import { useSolanaWallets } from "@privy-io/react-auth"; -import { fetchOpenBets } from "@/shared/solana_helpers"; -import {Bet} from "../types/Bet"; +import { fetchOpenBets, closeBet } from "@/shared/solana_helpers"; // Import close function +import { Bet } from "../types/Bet"; + export default function YourGames() { const { wallets, ready } = useSolanaWallets(); const [openBets, setOpenBets] = useState([]); @@ -13,13 +14,14 @@ export default function YourGames() { const [loading, setLoading] = useState(true); // Function to fetch open bets - const updateBets= async ()=>{ - const bets:Bet[] = await fetchOpenBets(wallets[0]); + const updateBets = async () => { + const bets: Bet[] = await fetchOpenBets(wallets[0]); setMyBets(bets.filter((bet) => bet.owner === wallets[0].address)); setOpenBets(bets); setLoading(false); console.log(`Got ${bets.length} bets`); - } + }; + // Auto-refresh every 10 seconds useEffect(() => { if (!ready) return; @@ -29,17 +31,36 @@ export default function YourGames() { return () => clearInterval(interval); // Cleanup on unmount }, [ready]); + // Function to handle bet closing + const handleCloseBet = async (bet: Bet) => { + const confirmed = window.confirm( + `Are you sure you want to close this bet? You will receive your refund back to your wallet.` + ); + if (!confirmed) return; + + try { + console.log(`Closing bet ${bet.id}`); + await closeBet(wallets[0], bet.id); + alert("Bet closed successfully!"); + updateBets(); // Refresh bets after closing + } catch (error) { + console.error("Error closing bet:", error); + alert("Failed to close the bet. Please try again."); + } + }; + return (

Your Games

{loading ? (

Loading your games...

- ) : - myBets.length === 0 ? <> :( + ) : myBets.length === 0 ? ( + <> + ) : (
{myBets.map((bet) => { - console.log(`Finding game for the id ${bet.id}`) + console.log(`Finding game for the id ${bet.id}`); const game = games.find((g) => g.id === bet.id); // Match game if (!game) return null; // Skip unmatched bets @@ -70,7 +91,7 @@ export default function YourGames() { {/* Close Button (Trash Icon) */} diff --git a/src/shared/solana_helpers.ts b/src/shared/solana_helpers.ts index 3ee96f7..2ed648b 100644 --- a/src/shared/solana_helpers.ts +++ b/src/shared/solana_helpers.ts @@ -2,7 +2,7 @@ import { CLUSTER_URL } from "@/data/shared"; import { Bets } from "@/idl/bets"; import { AnchorProvider, Program } from "@coral-xyz/anchor"; import { ConnectedSolanaWallet } from "@privy-io/react-auth"; -import { Connection, LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js"; +import { Connection, Keypair, LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js"; import idl from "../idl/bets_idl.json"; import { Bet } from "@/types/Bet"; @@ -51,3 +51,60 @@ export const fetchOpenBets = async (wallets: ConnectedSolanaWallet): Promise(idl, provider); + const [bet_list_pda] = await PublicKey.findProgramAddress( + [Buffer.from("bets_list")], + program.programId + ); + + // Fetch the bet list + const betList = await program.account.betsList.fetch(bet_list_pda); + + let chosenBet: PublicKey | null = null; + + for (const bet of betList.bets) { + const betAcc = await program.account.betVault.fetch(bet); + if (betAcc.owner.toBase58() === wallets.address && betAcc.gameId.toString() === betId) { + chosenBet = bet; + break; + } + } + + if (!chosenBet) { + console.error("Bet not found or not owned by the user"); + return; + } + + const winner = new PublicKey(wallets.address); + + // Execute the closeBet transaction + const tx = await program.methods + .closeBet(winner) + .accounts({ + betVault: chosenBet, + betsList: bet_list_pda, + winner: winner, + }) + .rpc(); + + console.log(`Transaction: ${tx}`); + await connection.confirmTransaction(tx); + } catch (error) { + console.error("Error closing bet:", error); + } +}