diff --git a/src/components/GameModal.tsx b/src/components/GameModal.tsx index f45394e..692eb28 100644 --- a/src/components/GameModal.tsx +++ b/src/components/GameModal.tsx @@ -11,6 +11,7 @@ import { Game, games } from "../data/games"; // Assuming you have a games data f import { PriceSelection } from "./PriceSelection"; import { GameSelection } from "./GameSelection"; import { CLUSTER_URL } from "@/data/shared"; +import { createBet } from "@/shared/solana_helpers"; // Change to Mainnet when deploying @@ -23,32 +24,10 @@ export default function GameModal({ isOpen, onClose }: GameModalProps) { const { wallets } = useSolanaWallets(); const { authenticated } = usePrivy(); - const [program, setProgram] = useState>(); - const [solanaWallet, setSolanaWallet] = useState(); - const [selectedGame, setSelectedGame] = useState(null); const [selectedPrice, setSelectedPrice] = useState(null); - useEffect(() => { - if (wallets.length > 0) { - const solWallet = wallets[0]; - setSolanaWallet(solWallet); - const connection = new Connection(CLUSTER_URL, "confirmed"); - const wallet = { - publicKey: new PublicKey(solWallet.address), - signTransaction: solWallet.signTransaction, - signAllTransactions: solWallet.signAllTransactions, - }; - - const provider = new AnchorProvider(connection, wallet, { - preflightCommitment: "processed", - }); - - const programInstance = new Program(idl, provider); - setProgram(programInstance); - } - }, [wallets]); const handleCreateGame = async () => { @@ -57,7 +36,7 @@ export default function GameModal({ isOpen, onClose }: GameModalProps) { return; } - const wallet = solanaWallet; // Get connected Privy wallet + const wallet = wallets[0]; // Get connected Privy wallet if (!wallet) { toast.error("Please connect your wallet."); return; @@ -67,53 +46,9 @@ export default function GameModal({ isOpen, onClose }: GameModalProps) { toast.error("Please select a game and a price."); return; } - if (!program) { - toast.error("Solana program not initialized."); - return; - } - try { - const nonce = 1; - const [bet_list_pda] = await PublicKey.findProgramAddress( - [Buffer.from("bets_list")], - program.programId - ); - const connection = new Connection(CLUSTER_URL, "confirmed"); - - console.log(`bets list : ${bet_list_pda}`); - - // Create transaction - const tx = await program.methods - .createBet(new BN(selectedPrice * 1000000000), selectedGame.id, new BN(nonce)) - .accounts({ - betsList: bet_list_pda, - }) - .transaction(); // Get transaction object instead of RPC call - - tx.feePayer = new PublicKey(wallet.address); - tx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash; - - // Sign transaction with Privy - const signedTx = await wallet.signTransaction(tx); - - // Send transaction// Replace with correct RPC endpoint - const txId = await connection.sendRawTransaction(signedTx.serialize()); - - toast.success(`Bet created successfully! TX: ${txId}`); - console.log(`Transaction ID: ${txId}`); - onClose(); - } catch (error:unknown) { - - - const errorMessage = String(error); // Converts error to string safely - - if (errorMessage.includes("already in use")) { - toast.error("You can't make 2 bets for the same game."); - } else { - toast.error("Failed to create bet."); - console.error(error); - } - } + await createBet(wallets[0],selectedPrice,selectedGame); + onClose(); }; if (!isOpen) return null; diff --git a/src/shared/solana_helpers.ts b/src/shared/solana_helpers.ts index 2ed648b..79822ab 100644 --- a/src/shared/solana_helpers.ts +++ b/src/shared/solana_helpers.ts @@ -1,10 +1,12 @@ import { CLUSTER_URL } from "@/data/shared"; import { Bets } from "@/idl/bets"; -import { AnchorProvider, Program } from "@coral-xyz/anchor"; +import { AnchorProvider, BN, Program } from "@coral-xyz/anchor"; import { ConnectedSolanaWallet } from "@privy-io/react-auth"; import { Connection, Keypair, LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js"; import idl from "../idl/bets_idl.json"; import { Bet } from "@/types/Bet"; +import { toast } from "sonner"; +import { Game } from "@/data/games"; export const fetchOpenBets = async (wallets: ConnectedSolanaWallet): Promise => { @@ -108,3 +110,63 @@ export const fetchOpenBets = async (wallets: ConnectedSolanaWallet): Promise(idl, provider); + const [bet_list_pda] = await PublicKey.findProgramAddress( + [Buffer.from("bets_list")], + program.programId + ); + try { + const nonce = 1; + const [bet_list_pda] = await PublicKey.findProgramAddress( + [Buffer.from("bets_list")], + program.programId + ); + const connection = new Connection(CLUSTER_URL, "confirmed"); + + console.log(`bets list : ${bet_list_pda}`); + + // Create transaction + const tx = await program.methods + .createBet(new BN(selectedPrice * 1000000000), selectedGame.id, new BN(nonce)) + .accounts({ + betsList: bet_list_pda, + }) + .transaction(); // Get transaction object instead of RPC call + + tx.feePayer = new PublicKey(wallets.address); + tx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash; + + // Sign transaction with Privy + const signedTx = await wallet.signTransaction(tx); + + // Send transaction// Replace with correct RPC endpoint + const txId = await connection.sendRawTransaction(signedTx.serialize()); + + toast.success(`Bet created successfully! TX: ${txId}`); + console.log(`Transaction ID: ${txId}`); + } catch (error:unknown) { + + + const errorMessage = String(error); // Converts error to string safely + + if (errorMessage.includes("already in use")) { + toast.error("You can't make 2 bets for the same game."); + } else { + toast.error("Failed to create bet."); + console.error(error); + } + } +} \ No newline at end of file