Refactor game creation
This commit is contained in:
parent
95aa843a6e
commit
7ff553fda2
|
|
@ -11,6 +11,7 @@ import { Game, games } from "../data/games"; // Assuming you have a games data f
|
||||||
import { PriceSelection } from "./PriceSelection";
|
import { PriceSelection } from "./PriceSelection";
|
||||||
import { GameSelection } from "./GameSelection";
|
import { GameSelection } from "./GameSelection";
|
||||||
import { CLUSTER_URL } from "@/data/shared";
|
import { CLUSTER_URL } from "@/data/shared";
|
||||||
|
import { createBet } from "@/shared/solana_helpers";
|
||||||
|
|
||||||
// Change to Mainnet when deploying
|
// Change to Mainnet when deploying
|
||||||
|
|
||||||
|
|
@ -23,32 +24,10 @@ export default function GameModal({ isOpen, onClose }: GameModalProps) {
|
||||||
const { wallets } = useSolanaWallets();
|
const { wallets } = useSolanaWallets();
|
||||||
const { authenticated } = usePrivy();
|
const { authenticated } = usePrivy();
|
||||||
|
|
||||||
const [program, setProgram] = useState<Program<Bets>>();
|
|
||||||
const [solanaWallet, setSolanaWallet] = useState<ConnectedSolanaWallet>();
|
|
||||||
|
|
||||||
const [selectedGame, setSelectedGame] = useState<Game | null>(null);
|
const [selectedGame, setSelectedGame] = useState<Game | null>(null);
|
||||||
const [selectedPrice, setSelectedPrice] = useState<number | null>(null);
|
const [selectedPrice, setSelectedPrice] = useState<number | null>(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<Bets>(idl, provider);
|
|
||||||
setProgram(programInstance);
|
|
||||||
}
|
|
||||||
}, [wallets]);
|
|
||||||
|
|
||||||
const handleCreateGame = async () => {
|
const handleCreateGame = async () => {
|
||||||
|
|
||||||
|
|
@ -57,7 +36,7 @@ export default function GameModal({ isOpen, onClose }: GameModalProps) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const wallet = solanaWallet; // Get connected Privy wallet
|
const wallet = wallets[0]; // Get connected Privy wallet
|
||||||
if (!wallet) {
|
if (!wallet) {
|
||||||
toast.error("Please connect your wallet.");
|
toast.error("Please connect your wallet.");
|
||||||
return;
|
return;
|
||||||
|
|
@ -67,53 +46,9 @@ export default function GameModal({ isOpen, onClose }: GameModalProps) {
|
||||||
toast.error("Please select a game and a price.");
|
toast.error("Please select a game and a price.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!program) {
|
|
||||||
toast.error("Solana program not initialized.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
await createBet(wallets[0],selectedPrice,selectedGame);
|
||||||
const nonce = 1;
|
onClose();
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!isOpen) return null;
|
if (!isOpen) return null;
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,12 @@
|
||||||
import { CLUSTER_URL } from "@/data/shared";
|
import { CLUSTER_URL } from "@/data/shared";
|
||||||
import { Bets } from "@/idl/bets";
|
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 { ConnectedSolanaWallet } from "@privy-io/react-auth";
|
||||||
import { Connection, Keypair, 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 idl from "../idl/bets_idl.json";
|
||||||
import { Bet } from "@/types/Bet";
|
import { Bet } from "@/types/Bet";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
import { Game } from "@/data/games";
|
||||||
|
|
||||||
|
|
||||||
export const fetchOpenBets = async (wallets: ConnectedSolanaWallet): Promise<Bet[]> => {
|
export const fetchOpenBets = async (wallets: ConnectedSolanaWallet): Promise<Bet[]> => {
|
||||||
|
|
@ -108,3 +110,63 @@ export const fetchOpenBets = async (wallets: ConnectedSolanaWallet): Promise<Bet
|
||||||
console.error("Error closing bet:", error);
|
console.error("Error closing bet:", error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export async function createBet(wallets:ConnectedSolanaWallet,selectedPrice:number,selectedGame:Game){
|
||||||
|
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
|
||||||
|
);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user