"use client"; import { useEffect, useState } from "react"; import SwapLayout from "./Layout"; import useAmountValidation from "./hooks/useAmountValidation"; import useCrossChainFee from "./hooks/useCrossChainFee"; import useDestinationAddress from "./hooks/useDestinationAddress"; import useDestinationAmount from "./hooks/useDestinationAmount"; import useSendTransaction from "./hooks/useSendTransaction"; import { computeSendType, sendTypeDetails } from "./hooks/computeSendType"; import useSwapErrors from "./hooks/useSwapErrors"; import useTokenSelection from "./hooks/useTokenSelection"; import { formatAddress } from "./lib/utils"; interface SwapProps { contract: string; client: any; switchChain: (params: { chainId: number }) => void; address: `0x${string}` | undefined; chain: number; track?: any; balances?: any; } export const Swap: React.FC = ({ contract, track, balances: balancesProp, client, switchChain, address, chain, }) => { const bitcoinAddress = ""; // temporary const [sourceAmount, setSourceAmount] = useState(""); const [isRightChain, setIsRightChain] = useState(true); const [sendButtonText, setSendButtonText] = useState("Send tokens"); const [balances, setBalances] = useState(balancesProp || []); const [balancesLoading, setBalancesLoading] = useState(true); useEffect(() => { const fetchBalances = async () => { setBalancesLoading(true); try { const result = await client.getBalances({ evmAddress: address }); setBalances(result); } catch (error) { console.error("Error fetching local balances:", error); } finally { setBalancesLoading(false); } }; if (balancesProp) { setBalances(balancesProp); setBalancesLoading(false); } else { fetchBalances(); } }, []); const { setSourceToken, sourceTokenSelected, sourceBalances, setDestinationToken, destinationTokenSelected, destinationBalances, } = useTokenSelection(balances, bitcoinAddress); const { crossChainFee } = useCrossChainFee( sourceTokenSelected, destinationTokenSelected, client ); const { isAmountGTFee, isAmountLTBalance } = useAmountValidation( sourceTokenSelected, destinationTokenSelected, sourceAmount, crossChainFee ); const { destinationAmount, destinationAmountIsLoading } = useDestinationAmount( sourceTokenSelected, destinationTokenSelected, sourceAmount, crossChainFee, balances, client ); const { priorityErrors } = useSwapErrors( sourceTokenSelected, destinationTokenSelected, sourceAmount, isAmountGTFee, isAmountLTBalance, destinationAmountIsLoading ); const { addressSelected, isAddressSelectedValid, canChangeAddress, customAddress, setCustomAddress, isCustomAddressValid, saveCustomAddress, } = useDestinationAddress(address, destinationTokenSelected, bitcoinAddress); const { handleSend, isSending } = useSendTransaction( sourceTokenSelected, destinationTokenSelected, sourceAmount, addressSelected, setSourceAmount, contract, bitcoinAddress, client, address, track ); const sendType = computeSendType( sourceTokenSelected, destinationTokenSelected ); const sendDisabled = !sendType || !isAmountGTFee || !isAmountLTBalance || isSending || !isAddressSelectedValid || destinationAmountIsLoading || !destinationAmount || balancesLoading; useEffect(() => { if (isSending) { setSendButtonText("Sending..."); } else if (sendDisabled && priorityErrors.length > 0) { setSendButtonText(priorityErrors[0].message); } else { setSendButtonText("Send Tokens"); } }, [isSending, sendDisabled, priorityErrors, destinationAmountIsLoading]); useEffect(() => { if (sourceTokenSelected?.chain_name === "btc_testnet") { setIsRightChain(true); } else if (chain && sourceTokenSelected) { setIsRightChain( chain.toString() === sourceTokenSelected.chain_id.toString() ); } }, [chain, sourceTokenSelected]); const handleSwitchNetwork = () => { const chain_id = sourceTokenSelected?.chain_id; if (chain_id) { console.log(chain_id); switchChain({ chainId: chain_id }); } }; return (
); };