diff --git a/src/app/dashboard/pool-market/[id]/components/poolPrediction.tsx b/src/app/dashboard/pool-market/[id]/components/poolPrediction.tsx index fa60d02..31481ce 100644 --- a/src/app/dashboard/pool-market/[id]/components/poolPrediction.tsx +++ b/src/app/dashboard/pool-market/[id]/components/poolPrediction.tsx @@ -5,6 +5,7 @@ import Image from 'next/image'; import React, { useState } from 'react'; import { FaSpinner } from 'react-icons/fa'; import CancelPool from './CancelPool'; +import useVotePool from '@/app/hooks/useVotePool'; interface Prediction { name: string; @@ -18,12 +19,28 @@ interface Prediction { address?: string; hasParticipatedAlready?: boolean; isParticipationLoading?: boolean; + poolStatus?: string; + onVoteSuccess?: () => void; } -export default function PoolPrediction({ predictions, name, creator, poolId, isConnected, address, hasParticipatedAlready, isParticipationLoading }: Prediction) { +export default function PoolPrediction({ predictions, name, creator, poolId, isConnected, address, hasParticipatedAlready, isParticipationLoading, poolStatus, onVoteSuccess }: Prediction) { const [stake, setStake] = useState('0'); const [selectedOption, setSelectedOption] = useState('Option 1'); const [selectedOdds, setSelectedOdds] = useState('1.17'); + + const { voteStatus, voteOnPool } = useVotePool(poolId); + + const handleVote = async () => { + if (!stake || parseFloat(stake) <= 0) { + return; + } + + const result = await voteOnPool(selectedOption, stake, poolStatus); + + if (result && onVoteSuccess) { + onVoteSuccess(); + } + }; return (
@@ -33,11 +50,10 @@ export default function PoolPrediction({ predictions, name, creator, poolId, isC
{predictions.map((prediction, index) => ( {+selectedOdds * +stake} strk
- { isParticipationLoading - ?
+ {isParticipationLoading + ?
- : hasParticipatedAlready - ? + : hasParticipatedAlready + ?
- : isConnected ? + : isConnected ?
-
:
diff --git a/src/app/dashboard/pool-market/[id]/page.tsx b/src/app/dashboard/pool-market/[id]/page.tsx index d47c066..8edad9b 100644 --- a/src/app/dashboard/pool-market/[id]/page.tsx +++ b/src/app/dashboard/pool-market/[id]/page.tsx @@ -202,16 +202,16 @@ export default function Market() { {readIsLoading ? Array.from({ length: 3 }).map((_, i) => ( - - )) + + )) : poolDetails && ( - - )} + + )} {/* {similarPools.map((pool, index) => ( { + // Refresh pool data after successful vote + window.location.reload(); + }} predictions={[ { options: poolDetails?.option1 || "Option 1", diff --git a/src/app/hooks/useVotePool.ts b/src/app/hooks/useVotePool.ts new file mode 100644 index 0000000..28d5f5a --- /dev/null +++ b/src/app/hooks/useVotePool.ts @@ -0,0 +1,74 @@ +import { ActionType } from "@/lib/types"; +import { myProvider } from "@/lib/utils"; +import { PREDIFI_CONTRACT_ADDRESS } from "@/static"; +import { useAccount } from "@starknet-react/core"; +import { useState } from "react"; +import toast from "react-hot-toast"; +import { cairo, CallData } from "starknet"; + +export default function useVotePool(poolId: string) { + const { account } = useAccount(); + + const [voteStatus, setVoteStatus] = useState("idle"); + + const voteOnPool = async (option: string, amount: string, poolStatus?: string) => { + if (!account) { + toast.error("Account not connected!"); + return; + } + + if (!amount || parseFloat(amount) <= 0) { + toast.error("Please enter a valid vote amount!"); + return; + } + + if (!option) { + toast.error("Please select an option!"); + return; + } + + // Check if pool is active + if (poolStatus && poolStatus !== "Active") { + toast.error("Cannot vote on inactive pool!"); + return; + } + + + try { + setVoteStatus("pending"); + + const result = await account.execute({ + contractAddress: PREDIFI_CONTRACT_ADDRESS, + entrypoint: "vote", + calldata: CallData.compile({ + pool_id: cairo.uint256(poolId), + option: cairo.felt(option), + amount: cairo.uint256(amount), + }), + }); + + const status = await myProvider.waitForTransaction( + result.transaction_hash + ); + + if (status.isSuccess()) { + toast.success(`Success! Voted ${amount} STRK on ${option}.`); + setVoteStatus("success"); + return true; + } + return false; + } catch (err) { + console.log(err); + setVoteStatus("error"); + toast.error("Error voting on pool. Please try again."); + return false; + } finally { + setVoteStatus("idle"); + } + }; + + return { + voteStatus, + voteOnPool, + }; +} \ No newline at end of file diff --git a/src/constants/functionNames.ts b/src/constants/functionNames.ts index c995e7b..7a3235a 100644 --- a/src/constants/functionNames.ts +++ b/src/constants/functionNames.ts @@ -4,4 +4,5 @@ export const GET_LOCKED_POOLS = "get_locked_pools"; export const GET_ACTIVE_POOLS = "get_active_pools"; export const GET_CLOSED_POOLS = "get_closed_pools" export const MANUALLY_UPDATE_POOL_STATE = "manually_update_pool_state" +export const GET_POOL_VOTE = "get_pool_vote"