|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useAccount, useSwitchChain, createConfig, http } from 'wagmi'; |
| 4 | +import { useState } from 'react'; |
| 5 | +import { AirdropABI } from '../utils/abis/AirdropABI'; |
| 6 | +import { base } from 'viem/chains'; |
| 7 | +import { parseEther } from 'viem'; |
| 8 | +import { encodeFunctionData } from 'viem'; |
| 9 | +import { useSendCalls } from 'wagmi/experimental'; |
| 10 | +import { useRouter } from 'next/navigation'; |
| 11 | +import { |
| 12 | + GAME_CONTRACT_ADDRESS, |
| 13 | + PAYMASTER_URL, |
| 14 | + PLAY_FEE_ETH, |
| 15 | +} from '../utils/constants'; |
| 16 | + |
| 17 | +interface PlayButtonProps { |
| 18 | + onSuccess: () => void; |
| 19 | + finalScore?: number; |
| 20 | +} |
| 21 | + |
| 22 | +export const config = createConfig({ |
| 23 | + chains: [base], |
| 24 | + transports: { |
| 25 | + [base.id]: http(PAYMASTER_URL), |
| 26 | + }, |
| 27 | +}); |
| 28 | + |
| 29 | +export function PlayButton({ onSuccess, finalScore }: PlayButtonProps) { |
| 30 | + const router = useRouter(); |
| 31 | + const account = useAccount(); |
| 32 | + const [isLoading, setIsLoading] = useState(false); |
| 33 | + const [error, setError] = useState<string | null>(null); |
| 34 | + const [success, setSuccess] = useState(false); |
| 35 | + const { switchChain } = useSwitchChain(); |
| 36 | + const { sendCalls } = useSendCalls({ |
| 37 | + mutation: { |
| 38 | + onSuccess: () => { |
| 39 | + setSuccess(true); |
| 40 | + onSuccess(); |
| 41 | + router.push('/category-select'); |
| 42 | + }, |
| 43 | + onError: (error) => { |
| 44 | + console.error('Error starting game:', error); |
| 45 | + setError('Failed to start game. Please try again.'); |
| 46 | + }, |
| 47 | + }, |
| 48 | + }); |
| 49 | + |
| 50 | + // const capabilities = useCapabilities({ config }); |
| 51 | + |
| 52 | + // console.log(capabilities); |
| 53 | + |
| 54 | + const handlePlay = async () => { |
| 55 | + if (!account.isConnected) { |
| 56 | + setError('Please connect your wallet first'); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + if (!account.chainId) { |
| 61 | + setError('Please connect to Base network'); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + if (account.chainId !== base.id) { |
| 66 | + switchChain({ chainId: base.id }); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + setIsLoading(true); |
| 71 | + setError(null); |
| 72 | + setSuccess(false); |
| 73 | + |
| 74 | + console.log('PAYMASTER_URL', PAYMASTER_URL); |
| 75 | + try { |
| 76 | + const data = encodeFunctionData({ |
| 77 | + abi: AirdropABI, |
| 78 | + functionName: 'startGame', |
| 79 | + args: [true], |
| 80 | + }); |
| 81 | + |
| 82 | + const calls = [ |
| 83 | + { |
| 84 | + to: GAME_CONTRACT_ADDRESS, |
| 85 | + data, |
| 86 | + value: parseEther(PLAY_FEE_ETH), |
| 87 | + }, |
| 88 | + ]; |
| 89 | + |
| 90 | + sendCalls({ |
| 91 | + calls, |
| 92 | + capabilities: { |
| 93 | + paymasterService: { |
| 94 | + url: 'https://api.developer.coinbase.com/rpc/v1/base/2Ohgm8ABsCDs0AUApVoi5ivZGe2t7eHb', |
| 95 | + }, |
| 96 | + }, |
| 97 | + }); |
| 98 | + } catch (err) { |
| 99 | + console.error('Error starting game:', err); |
| 100 | + setError('Failed to start game. Please try again.'); |
| 101 | + } finally { |
| 102 | + setIsLoading(false); |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + return ( |
| 107 | + <div className='w-full'> |
| 108 | + {error && ( |
| 109 | + <div className='mb-4'> |
| 110 | + <p className='text-red-500'>{error}</p> |
| 111 | + </div> |
| 112 | + )} |
| 113 | + {success && ( |
| 114 | + <div className='mb-4'> |
| 115 | + <p className='text-green-500'>Game started successfully! 🎮</p> |
| 116 | + </div> |
| 117 | + )} |
| 118 | + <button |
| 119 | + onClick={handlePlay} |
| 120 | + disabled={isLoading || !account.isConnected} |
| 121 | + className='w-full px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 disabled:bg-gray-400 disabled:cursor-not-allowed' |
| 122 | + > |
| 123 | + {isLoading ? ( |
| 124 | + <div className='flex items-center justify-center'> |
| 125 | + <div className='animate-spin rounded-full h-5 w-5 border-b-2 border-white mr-2'></div> |
| 126 | + Starting Game... |
| 127 | + </div> |
| 128 | + ) : !account.isConnected ? ( |
| 129 | + 'Connect Wallet' |
| 130 | + ) : finalScore !== undefined ? ( |
| 131 | + 'Play Again' |
| 132 | + ) : ( |
| 133 | + 'Start Game' |
| 134 | + )} |
| 135 | + </button> |
| 136 | + </div> |
| 137 | + ); |
| 138 | +} |
0 commit comments