Skip to content

Commit 61dcd20

Browse files
committed
Register working
1 parent 1193fe7 commit 61dcd20

File tree

6 files changed

+82
-53
lines changed

6 files changed

+82
-53
lines changed

contracts/deployments.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"11155111": {
99
"name": "sepolia",
1010
"contracts": {
11-
"Lottery": "0x783444D9F6bB3b9e34393A401aC8E824E016Ed43"
11+
"Lottery": "0x4789F8F8129be6ecc77863AEE18Ba364B3083eE4"
1212
}
1313
},
1414
"31337": {

src/App.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function App() {
2323
const { data: teamCount, refetch: refetchTeamCount } = useGetTeamCount();
2424

2525
const fetchAllTeams = useCallback(async () => {
26-
// --- FIX: Only fetch if connected and teamCount is a valid number ---
26+
// Only fetch if connected and teamCount is a valid number
2727
if (!isConnected || typeof teamCount !== 'bigint' || !chain) {
2828
setTeams([]); // Clear teams if not connected or no data
2929
return;
@@ -68,14 +68,19 @@ function App() {
6868
}
6969
}, [teamCount, chain, isConnected, wagmiConfig]);
7070

71+
// Force refetch function
72+
const forceRefetch = useCallback(() => {
73+
refetchTeamCount();
74+
setRefetchTrigger(prev => prev + 1);
75+
}, [refetchTeamCount]);
7176

72-
// Refetch teams when the dependencies of fetchAllTeams change
77+
// Refetch teams when the dependencies change
7378
useEffect(() => {
7479
fetchAllTeams();
7580
}, [fetchAllTeams]);
7681

7782
// Use the event hook to trigger a refetch
78-
useLotteryEvents(fetchAllTeams);
83+
useLotteryEvents(forceRefetch);
7984

8085
return (
8186
<div className="app-container">

src/components/MakeGuess.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function MakeGuess() {
1414

1515
const handleSubmit = (e: React.FormEvent) => {
1616
e.preventDefault();
17+
handleGuessClick();
1718
};
1819

1920
const handleGuessClick = () => {
@@ -25,9 +26,9 @@ export function MakeGuess() {
2526
}
2627

2728
if (teamAddress && guess) {
28-
makeGuess(teamAddress, parseInt(guess, 10));
29+
makeGuess(teamAddress as `0x${string}`, parseInt(guess, 10));
2930
} else {
30-
setFormError('All fields are required.');
31+
setFormError('All fields are required.');
3132
}
3233
};
3334

@@ -57,7 +58,7 @@ export function MakeGuess() {
5758
required
5859
/>
5960
</div>
60-
<button type="button" onClick={handleGuessClick} disabled={isLoading} className="button">
61+
<button type="submit" disabled={isLoading} className="button">
6162
{isLoading ? 'Submitting...' : 'Make a Guess'}
6263
</button>
6364
</form>

src/components/RegisterTeam.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export function RegisterTeam() {
1515

1616
const handleSubmit = (e: React.FormEvent) => {
1717
e.preventDefault();
18+
handleRegisterClick();
1819
};
1920

2021
const handleRegisterClick = () => {
@@ -26,7 +27,7 @@ export function RegisterTeam() {
2627
}
2728

2829
if (teamName && walletAddress && password) {
29-
registerTeam(walletAddress, teamName, password);
30+
registerTeam(walletAddress as `0x${string}`, teamName, password);
3031
} else {
3132
setFormError('All fields are required.');
3233
}
@@ -35,7 +36,7 @@ export function RegisterTeam() {
3536
return (
3637
<div className="form-card">
3738
<h3>Register Team</h3>
38-
<p className="small-text">(Requires a 2 ETH deposit)</p>
39+
<p className="small-text">(Requires a 0.1 ETH deposit)</p>
3940
<form onSubmit={handleSubmit}>
4041
<div className="form-group">
4142
<label htmlFor="registerNameInput">Team Name</label>
@@ -68,7 +69,7 @@ export function RegisterTeam() {
6869
required
6970
/>
7071
</div>
71-
<button type="button" onClick={handleRegisterClick} disabled={isLoading} className="button">
72+
<button type="submit" disabled={isLoading} className="button">
7273
{isLoading ? 'Registering...' : 'Register Team'}
7374
</button>
7475
</form>

src/hooks/useLottery.ts

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import {
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'
1212
import { 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
*/
2726
export 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+
}

src/wagmi.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ const getContractAddress = (chainId: number): `0x${string}` | undefined => {
2828
return undefined;
2929
};
3030

31+
// Public RPC URLs for Sepolia
32+
const SEPOLIA_RPC_URLS = [
33+
'https://rpc.sepolia.org',
34+
'https://ethereum-sepolia-rpc.publicnode.com',
35+
'https://sepolia.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161', // Public Infura endpoint
36+
'https://rpc2.sepolia.org',
37+
'https://eth-sepolia.g.alchemy.com/v2/demo', // Public Alchemy demo endpoint
38+
];
3139

3240
export const config = createConfig({
3341
chains: [mainnet, sepolia, localhost],
@@ -36,9 +44,12 @@ export const config = createConfig({
3644
],
3745
transports: {
3846
[mainnet.id]: http(),
39-
[sepolia.id]: http(),
40-
// --- FIX: Corrected the localhost RPC URL ---
41-
[localhost.id]: http('[http://127.0.0.1:8545](http://127.0.0.1:8545)'),
47+
[sepolia.id]: http(SEPOLIA_RPC_URLS[0], {
48+
// Add fallback URLs
49+
retryCount: 3,
50+
timeout: 10000,
51+
}),
52+
[localhost.id]: http('http://127.0.0.1:8545'),
4253
},
4354
})
4455

0 commit comments

Comments
 (0)