"use client"; import { useEffect, useState } from "react"; import { Address, AddressInput, Balance, EtherInput } from "@scaffold-ui/components"; import { Address as AddressType, createWalletClient, http, parseEther } from "viem"; import { hardhat } from "viem/chains"; import { useAccount } from "wagmi"; import { BanknotesIcon } from "@heroicons/react/24/outline"; import { useTargetNetwork, useTransactor } from "~~/hooks/scaffold-eth"; import { notification } from "~~/utils/scaffold-eth"; // Account index to use from generated hardhat accounts. const FAUCET_ACCOUNT_INDEX = 0; const localWalletClient = createWalletClient({ chain: hardhat, transport: http(), }); /** * Faucet modal which lets you send ETH to any address. */ export const Faucet = () => { const [loading, setLoading] = useState(false); const [inputAddress, setInputAddress] = useState(); const [faucetAddress, setFaucetAddress] = useState(); const [sendValue, setSendValue] = useState(""); const { targetNetwork } = useTargetNetwork(); const { chain: ConnectedChain } = useAccount(); const faucetTxn = useTransactor(localWalletClient); useEffect(() => { const getFaucetAddress = async () => { try { const accounts = await localWalletClient.getAddresses(); setFaucetAddress(accounts[FAUCET_ACCOUNT_INDEX]); } catch (error) { notification.error( <>

Cannot connect to local provider

- Did you forget to run yarn chain ?

- Or you can change targetNetwork in{" "} scaffold.config.ts

, ); console.error("⚡️ ~ file: Faucet.tsx:getFaucetAddress ~ error", error); } }; getFaucetAddress(); }, []); const sendETH = async () => { if (!faucetAddress || !inputAddress) { return; } try { setLoading(true); await faucetTxn({ to: inputAddress, value: parseEther(sendValue as `${number}`), account: faucetAddress, }); setLoading(false); setInputAddress(undefined); setSendValue(""); } catch (error) { console.error("⚡️ ~ file: Faucet.tsx:sendETH ~ error", error); setLoading(false); } }; // Render only on local chain if (ConnectedChain?.id !== hardhat.id) { return null; } return (
); };