import { useState, type FormEvent, useEffect } from 'react' import { useStore } from '@nanostores/react' import { useNetwork, useEnsAddress, useEnsName } from 'wagmi' import type { SendTransactionArgs, WriteContractPreparedArgs } from 'wagmi/actions' import { $selectedToken, $isInitSend } from '@features/Web3/stores' import siteConfig from '@config/blog.config' import { prepareTransaction, sendTransaction } from './actions' import styles from './Send.module.css' import { SendTable } from './SendTable' import { Loader } from '@components/Loader' export function Send({ amount }: { amount: string }) { const { ens } = siteConfig.author.ether const { chain } = useNetwork() const selectedToken = useStore($selectedToken) // Always resolve to address from ENS name and vice versa // so nobody has to trust my config values. const { data: to } = useEnsAddress({ name: ens, chainId: 1 }) const { data: ensResolved } = useEnsName({ address: to as `0x${string}` | undefined, chainId: 1 }) const [txConfig, setTxConfig] = useState< SendTransactionArgs | WriteContractPreparedArgs >() const [txHash, setTxHash] = useState() const [isLoading, setIsLoading] = useState(false) useEffect(() => { async function init() { if (!selectedToken || !amount || !to || !chain?.id) return const config = await prepareTransaction( selectedToken, amount, to, chain.id ) setTxConfig(config) } init() }, [selectedToken || amount || to || chain?.id]) async function handleSend(event: FormEvent) { event?.preventDefault() try { setIsLoading(true) const result = await sendTransaction(selectedToken, txConfig) setTxHash(result?.hash) setIsLoading(false) } catch (error: unknown) { console.error((error as Error).message) setIsLoading(false) } } console.log(txHash) return ( <>
{/*
You are sending
*/}
) }