close game partially
This commit is contained in:
parent
6a1580afa7
commit
95aa843a6e
|
|
@ -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 { 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<Bet[]>([]);
|
||||
|
|
@ -19,7 +20,8 @@ export default function YourGames() {
|
|||
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 (
|
||||
<section className="py-16 px-6">
|
||||
<h2 className="text-3xl font-bold text-white mb-6">Your Games</h2>
|
||||
|
||||
{loading ? (
|
||||
<p className="text-gray-400">Loading your games...</p>
|
||||
) :
|
||||
myBets.length === 0 ? <></> :(
|
||||
) : myBets.length === 0 ? (
|
||||
<></>
|
||||
) : (
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
|
||||
{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) */}
|
||||
<button
|
||||
className="absolute bottom-3 right-3 bg-red-600 text-white p-2 rounded-full transition duration-300 hover:bg-red-800"
|
||||
onClick={() => console.log(`Removing bet ${bet.id}`)}
|
||||
onClick={() => handleCloseBet(bet)}
|
||||
>
|
||||
<FiTrash size={16} />
|
||||
</button>
|
||||
|
|
|
|||
|
|
@ -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<Bet
|
|||
}
|
||||
return [];
|
||||
};
|
||||
|
||||
|
||||
|
||||
export async function closeBet(wallets: ConnectedSolanaWallet, betId: string) {
|
||||
try {
|
||||
const connection = new Connection(CLUSTER_URL);
|
||||
const wallet = {
|
||||
publicKey: new PublicKey(wallets.address),
|
||||
signTransaction: wallets.signTransaction,
|
||||
signAllTransactions: wallets.signAllTransactions,
|
||||
};
|
||||
const provider = new AnchorProvider(connection, wallet, {
|
||||
preflightCommitment: "confirmed",
|
||||
});
|
||||
|
||||
const program = new Program<Bets>(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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user