toast link

This commit is contained in:
Sewmina 2025-04-03 12:05:52 +05:30
parent 609fdcad20
commit f0aeadbc0c
5 changed files with 49 additions and 18 deletions

View File

@ -8,6 +8,7 @@ import { PriceSelection } from "./PriceSelection";
import { GameSelection } from "./GameSelection"; import { GameSelection } from "./GameSelection";
import { createBet } from "@/shared/solana_helpers"; import { createBet } from "@/shared/solana_helpers";
import { Game } from "@/types/Game"; import { Game } from "@/types/Game";
import { EXPLORER_TX_TEMPLATE } from "@/data/shared";
interface GameModalProps { interface GameModalProps {
isOpen: boolean; isOpen: boolean;
@ -41,7 +42,15 @@ export default function GameModal({ isOpen, onClose }: GameModalProps) {
setIsProcessing(true); setIsProcessing(true);
try { try {
await createBet(wallet, selectedPrice, selectedGame); const tx = await createBet(wallet, selectedPrice, selectedGame);
const url = EXPLORER_TX_TEMPLATE.replace("{address}", tx);
toast.success(`Bet created successfully!`, {
action: {
label: "View TX",
onClick: () => window.open(url, "_blank"),
},
});
onClose(); onClose();
} catch (error) { } catch (error) {
console.error("Error creating bet:", error); console.error("Error creating bet:", error);

View File

@ -1,5 +1,5 @@
import Image from "next/image"; import Image from "next/image";
import { Game } from "../data/games"; import { Game } from "../types/Game";
interface GameSelectionProps { interface GameSelectionProps {
games: Game[]; games: Game[];

View File

@ -6,6 +6,8 @@ import { FiTrash } from "react-icons/fi";
import { useSolanaWallets } from "@privy-io/react-auth"; import { useSolanaWallets } from "@privy-io/react-auth";
import { fetchOpenBets, closeBet } from "@/shared/solana_helpers"; import { fetchOpenBets, closeBet } from "@/shared/solana_helpers";
import { Bet } from "../types/Bet"; import { Bet } from "../types/Bet";
import { toast } from "sonner";
import { EXPLORER_TX_TEMPLATE } from "@/data/shared";
export default function YourGames() { export default function YourGames() {
const { wallets, ready } = useSolanaWallets(); const { wallets, ready } = useSolanaWallets();
@ -35,11 +37,19 @@ export default function YourGames() {
if (!selectedBet) return; if (!selectedBet) return;
setIsProcessing(true); setIsProcessing(true);
try { try {
await closeBet(wallets[0], selectedBet.id); const tx = await closeBet(wallets[0], selectedBet.id);
const url = EXPLORER_TX_TEMPLATE.replace("{address}", tx);
toast.success(`Closed the bet successfully!`, {
action: {
label: "View TX",
onClick: () => window.open(url, "_blank"),
},
});
updateBets(); updateBets();
} catch (error) { } catch (error) {
console.error("Error closing bet:", error); console.error("Error closing bet:", error);
alert("Failed to close the bet. Please try again."); toast.message("Failed to close the bet. Please try again.");
} finally { } finally {
setIsProcessing(false); setIsProcessing(false);
setSelectedBet(null); setSelectedBet(null);

View File

@ -1 +1,3 @@
export const CLUSTER_URL = "https://api.devnet.solana.com"; export const CLUSTER_URL = "https://api.devnet.solana.com";
export const EXPLORER_ADDRESS_TEMPLATE = "https://explorer.solana.com/address/{address}?cluster=devnet";
export const EXPLORER_TX_TEMPLATE = "https://explorer.solana.com/tx/{address}?cluster=devnet";

View File

@ -56,7 +56,7 @@ export const fetchOpenBets = async (wallets: ConnectedSolanaWallet): Promise<Bet
export async function closeBet(wallets: ConnectedSolanaWallet, betId: string) { export async function closeBet(wallets: ConnectedSolanaWallet, betId: string): Promise<string> {
try { try {
const connection = new Connection(CLUSTER_URL); const connection = new Connection(CLUSTER_URL);
const wallet = { const wallet = {
@ -89,30 +89,38 @@ export const fetchOpenBets = async (wallets: ConnectedSolanaWallet): Promise<Bet
if (!chosenBet) { if (!chosenBet) {
console.error("Bet not found or not owned by the user"); console.error("Bet not found or not owned by the user");
return; return "";
} }
const winner = new PublicKey(wallets.address); const winner = new PublicKey(wallets.address);
// Execute the closeBet transaction // Execute the closeBet transaction
const tx = await program.methods const tx = await program.methods
.closeBet(winner) .closeBet(winner)
.accounts({ .accounts({
betVault: chosenBet, betVault: chosenBet,
betsList: bet_list_pda, betsList: bet_list_pda,
winner: winner, winner: winner,
}) })
.rpc(); .transaction();
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());
console.log(`Transaction: ${tx}`); console.log(`Transaction: ${tx}`);
await connection.confirmTransaction(tx); return txId;
} catch (error) { } catch (error) {
console.error("Error closing bet:", error); console.error("Error closing bet:", error);
} }
return "";
} }
export async function createBet(wallets:ConnectedSolanaWallet,selectedPrice:number,selectedGame:Game){ export async function createBet(wallets:ConnectedSolanaWallet,selectedPrice:number,selectedGame:Game):Promise<string>{
const connection = new Connection(CLUSTER_URL); const connection = new Connection(CLUSTER_URL);
const wallet = { const wallet = {
publicKey: new PublicKey(wallets.address), publicKey: new PublicKey(wallets.address),
@ -155,8 +163,8 @@ export async function createBet(wallets:ConnectedSolanaWallet,selectedPrice:numb
// Send transaction// Replace with correct RPC endpoint // Send transaction// Replace with correct RPC endpoint
const txId = await connection.sendRawTransaction(signedTx.serialize()); const txId = await connection.sendRawTransaction(signedTx.serialize());
toast.success(`Bet created successfully! TX: ${txId}`);
console.log(`Transaction ID: ${txId}`); console.log(`Transaction ID: ${txId}`);
return txId;
} catch (error:unknown) { } catch (error:unknown) {
@ -169,4 +177,6 @@ export async function createBet(wallets:ConnectedSolanaWallet,selectedPrice:numb
console.error(error); console.error(error);
} }
} }
return "";
} }