44import {
55 useAccount ,
66 useBalance ,
7- useContractRead ,
8- useContractWrite ,
9- useWaitForTransactionReceipt , // <-- CORRECTED: Changed from useWaitForTransaction
7+ useReadContract ,
8+ useWriteContract ,
9+ useWaitForTransactionReceipt ,
1010 useWatchContractEvent ,
1111} from 'wagmi'
1212import { lotteryContractConfig } from '../wagmi'
@@ -20,20 +20,17 @@ export type Team = {
2020 score : number ;
2121} ;
2222
23-
2423/**
2524 * Fetches the total number of registered teams.
2625 */
2726export function useGetTeamCount ( ) {
2827 const { chain } = useAccount ( ) ;
2928 const contractAddress = chain ? lotteryContractConfig . address ( chain . id ) : undefined ;
3029
31- return useContractRead ( {
30+ return useReadContract ( {
3231 ...lotteryContractConfig ,
3332 address : contractAddress ,
3433 functionName : 'getTeamCount' ,
35- // Watch for changes to refetch automatically
36- watch : true ,
3734 } ) ;
3835}
3936
@@ -45,12 +42,14 @@ export function useGetTeamDetails(teamIndex: number) {
4542 const { chain } = useAccount ( ) ;
4643 const contractAddress = chain ? lotteryContractConfig . address ( chain . id ) : undefined ;
4744
48- return useContractRead ( {
45+ return useReadContract ( {
4946 ...lotteryContractConfig ,
5047 address : contractAddress ,
5148 functionName : 'getTeamDetails' ,
5249 args : [ BigInt ( teamIndex ) ] ,
53- enabled : teamIndex !== undefined , // Only run if teamIndex is valid
50+ query : {
51+ enabled : teamIndex !== undefined ,
52+ }
5453 } ) ;
5554}
5655
@@ -62,34 +61,43 @@ export function useRegisterTeam() {
6261 const { chain } = useAccount ( ) ;
6362 const contractAddress = chain ? lotteryContractConfig . address ( chain . id ) : undefined ;
6463
65- const { data, isLoading, isSuccess, write, error } = useContractWrite ( {
66- ...lotteryContractConfig ,
67- address : contractAddress ,
68- functionName : 'registerTeam' ,
69- } ) ;
64+ const { data : hash , isPending, isSuccess, writeContract, error } = useWriteContract ( ) ;
7065
7166 const {
7267 data : receipt ,
73- isLoading : isPending ,
74- isSuccess : txIsSuccess ,
75- } = useWaitForTransactionReceipt ( { hash : data ?. hash } ) ; // <-- CORRECTED HOOK
68+ isLoading : isConfirming ,
69+ isSuccess : isConfirmed ,
70+ } = useWaitForTransactionReceipt ( {
71+ hash,
72+ } ) ;
7673
7774 // Exposing a function to be called from the component
78- const registerTeam = ( walletAddress : `0x${string } `, teamName : string , password : string ) => {
79- if ( write ) {
80- write ( {
75+ const registerTeam = async ( walletAddress : `0x${string } `, teamName : string , password : string ) => {
76+ if ( ! contractAddress ) {
77+ console . error ( 'No contract address found for chain:' , chain ?. id ) ;
78+ return ;
79+ }
80+
81+ try {
82+ writeContract ( {
83+ ...lotteryContractConfig ,
84+ address : contractAddress ,
85+ functionName : 'registerTeam' ,
8186 args : [ walletAddress , teamName , password ] ,
82- value : parseEther ( '2 ' ) , // The contract requires a 2 ETH deposit
87+ value : parseEther ( '0.1 ' ) , // The contract requires a 0.1 ETH deposit
8388 } ) ;
89+ } catch ( err ) {
90+ console . error ( 'Error calling writeContract:' , err ) ;
8491 }
8592 } ;
8693
8794 return {
8895 registerTeam,
89- isLoading : isLoading || isPending ,
90- isSuccess : isSuccess && txIsSuccess ,
96+ isLoading : isPending || isConfirming ,
97+ isSuccess : isSuccess && isConfirmed ,
9198 error,
9299 receipt,
100+ hash,
93101 } ;
94102}
95103
@@ -100,30 +108,34 @@ export function useMakeGuess() {
100108 const { chain } = useAccount ( ) ;
101109 const contractAddress = chain ? lotteryContractConfig . address ( chain . id ) : undefined ;
102110
103- const { data, isLoading, isSuccess, write, error } = useContractWrite ( {
104- ...lotteryContractConfig ,
105- address : contractAddress ,
106- functionName : 'makeAGuess' ,
107- } ) ;
111+ const { data : hash , isPending, isSuccess, writeContract, error } = useWriteContract ( ) ;
108112
109113 const {
110114 data : receipt ,
111- isLoading : isPending ,
112- isSuccess : txIsSuccess ,
113- } = useWaitForTransactionReceipt ( { hash : data ?. hash } ) ; // <-- CORRECTED HOOK
115+ isLoading : isConfirming ,
116+ isSuccess : isConfirmed ,
117+ } = useWaitForTransactionReceipt ( {
118+ hash,
119+ } ) ;
114120
115121 const makeGuess = ( teamAddress : `0x${string } `, guess : number ) => {
116- if ( write ) {
117- write ( {
118- args : [ teamAddress , BigInt ( guess ) ] ,
119- } ) ;
122+ if ( ! contractAddress ) {
123+ console . error ( 'No contract address found' ) ;
124+ return ;
120125 }
126+
127+ writeContract ( {
128+ ...lotteryContractConfig ,
129+ address : contractAddress ,
130+ functionName : 'makeAGuess' ,
131+ args : [ teamAddress , BigInt ( guess ) ] ,
132+ } ) ;
121133 } ;
122134
123135 return {
124136 makeGuess,
125- isLoading : isLoading || isPending ,
126- isSuccess : isSuccess && txIsSuccess ,
137+ isLoading : isPending || isConfirming ,
138+ isSuccess : isSuccess && isConfirmed ,
127139 error,
128140 receipt,
129141 } ;
@@ -145,7 +157,6 @@ export function useGetLotteryBalance(refetchTrigger: any) {
145157 refetch ( ) ;
146158 } , [ refetchTrigger , refetch ] ) ;
147159
148-
149160 return data ;
150161}
151162
@@ -169,10 +180,10 @@ export function useLotteryEvents(refetch: () => void) {
169180 useWatchContractEvent ( {
170181 ...lotteryContractConfig ,
171182 address : contractAddress ,
172- eventName : 'LogGuessMade' , // Ensure this event name matches your contract
183+ eventName : 'LogGuessMade' ,
173184 onLogs : ( logs ) => {
174185 console . log ( 'Guess Made Event:' , logs ) ;
175186 refetch ( ) ;
176187 } ,
177188 } ) ;
178- }
189+ }
0 commit comments