toast link
This commit is contained in:
parent
609fdcad20
commit
f0aeadbc0c
|
|
@ -8,6 +8,7 @@ import { PriceSelection } from "./PriceSelection";
|
|||
import { GameSelection } from "./GameSelection";
|
||||
import { createBet } from "@/shared/solana_helpers";
|
||||
import { Game } from "@/types/Game";
|
||||
import { EXPLORER_TX_TEMPLATE } from "@/data/shared";
|
||||
|
||||
interface GameModalProps {
|
||||
isOpen: boolean;
|
||||
|
|
@ -41,7 +42,15 @@ export default function GameModal({ isOpen, onClose }: GameModalProps) {
|
|||
|
||||
setIsProcessing(true);
|
||||
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();
|
||||
} catch (error) {
|
||||
console.error("Error creating bet:", error);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import Image from "next/image";
|
||||
import { Game } from "../data/games";
|
||||
import { Game } from "../types/Game";
|
||||
|
||||
interface GameSelectionProps {
|
||||
games: Game[];
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ import { FiTrash } from "react-icons/fi";
|
|||
import { useSolanaWallets } from "@privy-io/react-auth";
|
||||
import { fetchOpenBets, closeBet } from "@/shared/solana_helpers";
|
||||
import { Bet } from "../types/Bet";
|
||||
import { toast } from "sonner";
|
||||
import { EXPLORER_TX_TEMPLATE } from "@/data/shared";
|
||||
|
||||
export default function YourGames() {
|
||||
const { wallets, ready } = useSolanaWallets();
|
||||
|
|
@ -35,11 +37,19 @@ export default function YourGames() {
|
|||
if (!selectedBet) return;
|
||||
setIsProcessing(true);
|
||||
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();
|
||||
} catch (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 {
|
||||
setIsProcessing(false);
|
||||
setSelectedBet(null);
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
const connection = new Connection(CLUSTER_URL);
|
||||
const wallet = {
|
||||
|
|
@ -89,30 +89,38 @@ export const fetchOpenBets = async (wallets: ConnectedSolanaWallet): Promise<Bet
|
|||
|
||||
if (!chosenBet) {
|
||||
console.error("Bet not found or not owned by the user");
|
||||
return;
|
||||
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();
|
||||
|
||||
.closeBet(winner)
|
||||
.accounts({
|
||||
betVault: chosenBet,
|
||||
betsList: bet_list_pda,
|
||||
winner: winner,
|
||||
})
|
||||
.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}`);
|
||||
await connection.confirmTransaction(tx);
|
||||
return txId;
|
||||
} catch (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 wallet = {
|
||||
publicKey: new PublicKey(wallets.address),
|
||||
|
|
@ -155,8 +163,8 @@ export async function createBet(wallets:ConnectedSolanaWallet,selectedPrice:numb
|
|||
// 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}`);
|
||||
return txId;
|
||||
} catch (error:unknown) {
|
||||
|
||||
|
||||
|
|
@ -169,4 +177,6 @@ export async function createBet(wallets:ConnectedSolanaWallet,selectedPrice:numb
|
|||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user