File size: 4,477 Bytes
78d0e31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"use client"

import { useEffect, useState } from "react"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { fetchContractConfig } from "@/lib/config"
import { getFLBTokenContract, getHealthRegistryContract, getDonationRouterContract } from "@/lib/contract-utils"

type ContractStatus = {
  name: string
  address: string
  connected: boolean
  error?: string
}

export function ContractStatus() {
  const [contracts, setContracts] = useState<ContractStatus[]>([
    { name: "FLB Token", address: "", connected: false },
    { name: "Health Registry", address: "", connected: false },
    { name: "Donation Router", address: "", connected: false },
  ])
  const [isLoading, setIsLoading] = useState(true)
  const [chainId, setChainId] = useState("56")

  useEffect(() => {
    const checkContracts = async () => {
      try {
        // Fetch contract addresses from server
        const config = await fetchContractConfig()
        setChainId(config.chainId)

        // Check FLB Token
        const flbContract = await getFLBTokenContract()
        let flbConnected = false
        let flbError = ""

        try {
          if (flbContract) {
            const name = await flbContract.name()
            flbConnected = !!name
          }
        } catch (error: any) {
          flbError = error.message
        }

        // Check Health Registry
        const registryContract = await getHealthRegistryContract()
        let registryConnected = false
        let registryError = ""

        try {
          if (registryContract) {
            // Just try to call a view function to see if it's connected
            await registryContract.isVerifiedHealthActor("0x0000000000000000000000000000000000000000")
            registryConnected = true
          }
        } catch (error: any) {
          registryError = error.message
        }

        // Check Donation Router
        const routerContract = await getDonationRouterContract()
        let routerConnected = false
        let routerError = ""

        try {
          if (routerContract) {
            // Just try to access a public variable
            await routerContract.flbToken()
            routerConnected = true
          }
        } catch (error: any) {
          routerError = error.message
        }

        setContracts([
          {
            name: "FLB Token",
            address: config.tokenContract,
            connected: flbConnected,
            error: flbError,
          },
          {
            name: "Health Registry",
            address: config.healthRegistry,
            connected: registryConnected,
            error: registryError,
          },
          {
            name: "Donation Router",
            address: config.donationRouter,
            connected: routerConnected,
            error: routerError,
          },
        ])
      } catch (error) {
        console.error("Error checking contracts:", error)
      } finally {
        setIsLoading(false)
      }
    }

    checkContracts()
  }, [])

  return (
    <Card className="flame-card">
      <CardHeader>
        <CardTitle className="flame-text">Contract Status</CardTitle>
      </CardHeader>
      <CardContent>
        {isLoading ? (
          <p className="text-center">Checking contract connectivity...</p>
        ) : (
          <div className="space-y-4">
            {contracts.map((contract) => (
              <div key={contract.name} className="flex flex-col">
                <div className="flex items-center justify-between">
                  <span className="font-medium">{contract.name}</span>
                  <span
                    className={`px-2 py-1 rounded text-xs ${
                      contract.connected ? "bg-green-900 text-green-300" : "bg-red-900 text-red-300"
                    }`}
                  >
                    {contract.connected ? "Connected" : "Not Connected"}
                  </span>
                </div>
                <p className="text-xs text-gray-400 truncate">{contract.address}</p>
                {!contract.connected && contract.error && (
                  <p className="text-xs text-red-400 mt-1">{contract.error.substring(0, 100)}...</p>
                )}
              </div>
            ))}
            <div className="mt-4 text-xs text-gray-400">
              <p>Chain ID: {chainId} (BNB Smart Chain)</p>
            </div>
          </div>
        )}
      </CardContent>
    </Card>
  )
}