diff --git a/frontend/src/components/web3/greeter-contract-interactions.tsx b/frontend/src/components/web3/greeter-contract-interactions.tsx deleted file mode 100644 index 4e7e05a..0000000 --- a/frontend/src/components/web3/greeter-contract-interactions.tsx +++ /dev/null @@ -1,144 +0,0 @@ -'use client' - -import { FC, useEffect, useState } from 'react' - -import { ContractIds } from '@/deployments/deployments' -import { zodResolver } from '@hookform/resolvers/zod' -import GreeterContract from '@inkathon/contracts/typed-contracts/contracts/greeter' -import { - contractQuery, - decodeOutput, - useInkathon, - useRegisteredContract, - useRegisteredTypedContract, -} from '@scio-labs/use-inkathon' -import { SubmitHandler, useForm } from 'react-hook-form' -import toast from 'react-hot-toast' -import * as z from 'zod' - -import { Button } from '@/components/ui/button' -import { Card, CardContent } from '@/components/ui/card' -import { Form, FormControl, FormItem, FormLabel } from '@/components/ui/form' -import { Input } from '@/components/ui/input' -import { contractTxWithToast } from '@/utils/contract-tx-with-toast' - -const formSchema = z.object({ - newMessage: z.string().min(1).max(90), -}) - -export const GreeterContractInteractions: FC = () => { - const { api, activeAccount, activeSigner } = useInkathon() - const { contract, address: contractAddress } = useRegisteredContract(ContractIds.Greeter) - const { typedContract } = useRegisteredTypedContract(ContractIds.Greeter, GreeterContract) - const [greeterMessage, setGreeterMessage] = useState() - const [fetchIsLoading, setFetchIsLoading] = useState() - const form = useForm>({ - resolver: zodResolver(formSchema), - }) - - const { register, reset, handleSubmit } = form - - // Fetch Greeting - const fetchGreeting = async () => { - if (!contract || !typedContract || !api) return - - setFetchIsLoading(true) - try { - const result = await contractQuery(api, '', contract, 'greet') - const { output, isError, decodedOutput } = decodeOutput(result, contract, 'greet') - if (isError) throw new Error(decodedOutput) - setGreeterMessage(output) - - // Alternatively: Fetch it with typed contract instance - const typedResult = await typedContract.query.greet() - console.log('Result from typed contract: ', typedResult.value) - } catch (e) { - console.error(e) - toast.error('Error while fetching greeting. Try again…') - setGreeterMessage(undefined) - } finally { - setFetchIsLoading(false) - } - } - useEffect(() => { - fetchGreeting() - }, [typedContract]) - - // Update Greeting - const updateGreeting: SubmitHandler> = async ({ newMessage }) => { - if (!activeAccount || !contract || !activeSigner || !api) { - toast.error('Wallet not connected. Try again…') - return - } - - try { - await contractTxWithToast(api, activeAccount.address, contract, 'setMessage', {}, [ - newMessage, - ]) - reset() - } catch (e) { - console.error(e) - } finally { - fetchGreeting() - } - } - - if (!api) return null - - return ( - <> -
-

Greeter Smart Contract

- -
- {/* Fetched Greeting */} - - - - Fetched Greeting - - - - - - - - {/* Update Greeting */} - - - - - Update Greeting - -
- - -
-
-
- -
-
- - - {/* Contract Address */} -

- {contract ? contractAddress : 'Loading…'} -

-
- - ) -}