"use client"
import { useState } from "react"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { Badge } from "@/components/ui/badge"
import { Coins, Send, RefreshCw, ExternalLink, AlertCircle } from "lucide-react"
import { useWallet } from "@/providers/wallet-provider"
import { transferFLBTokens } from "@/lib/contract-service"
import { toast } from "@/hooks/use-toast"
import { FLB_TOKEN_ADDRESSES } from "@/lib/constants"
export function FLBTokenDashboard() {
const { connected, address, balance, chainId, signer, updateBalance } = useWallet()
const [transferTo, setTransferTo] = useState("")
const [transferAmount, setTransferAmount] = useState("")
const [transferring, setTransferring] = useState(false)
const [refreshing, setRefreshing] = useState(false)
const isOnAlfajores = chainId === 44787
const contractAddress = isOnAlfajores ? FLB_TOKEN_ADDRESSES.CELO_ALFAJORES : null
const handleTransfer = async () => {
if (!signer || !transferTo || !transferAmount) {
toast({
title: "Invalid Input",
description: "Please enter a valid address and amount",
variant: "destructive",
})
return
}
if (Number.parseFloat(transferAmount) > Number.parseFloat(balance.flb)) {
toast({
title: "Insufficient Balance",
description: "You don't have enough FLB tokens",
variant: "destructive",
})
return
}
setTransferring(true)
try {
const result = await transferFLBTokens(signer, transferTo, transferAmount)
toast({
title: "Transfer Initiated",
description: `Transaction hash: ${result.hash.slice(0, 10)}...`,
})
// Wait for transaction confirmation
await result.transaction.wait()
toast({
title: "Transfer Successful",
description: `Sent ${transferAmount} FLB to ${transferTo.slice(0, 10)}...`,
})
// Reset form and update balance
setTransferTo("")
setTransferAmount("")
await updateBalance()
} catch (error: any) {
console.error("Transfer failed:", error)
toast({
title: "Transfer Failed",
description: error.message || "Failed to transfer FLB tokens",
variant: "destructive",
})
} finally {
setTransferring(false)
}
}
const handleRefreshBalance = async () => {
setRefreshing(true)
try {
await updateBalance()
toast({
title: "Balance Updated",
description: "Your token balance has been refreshed",
})
} catch (error) {
toast({
title: "Refresh Failed",
description: "Failed to update balance",
variant: "destructive",
})
} finally {
setRefreshing(false)
}
}
const openContractOnExplorer = () => {
if (contractAddress) {
window.open(`https://alfajores.celoscan.io/address/${contractAddress}`, "_blank")
}
}
if (!connected) {
return (
{contractAddress.slice(0, 10)}...{contractAddress.slice(-8)}