File size: 6,022 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import * as ethers from "ethers"
import { config, fetchContractConfig } from "./config"
import FLBTokenABI from "@/abi/FLBToken.json"
import HealthActorsRegistryABI from "@/abi/HealthActorsRegistry.json"
import DonationRouterABI from "@/abi/DonationRouter.json"

// Types
export type HealthActor = {
  name: string
  location: string
  credentials: string
  isVerified: boolean
}

// Cache for contract addresses
let contractConfig = { ...config }

// Initialize contract config
export const initializeContractConfig = async () => {
  if (typeof window !== "undefined") {
    contractConfig = await fetchContractConfig()
  }
  return contractConfig
}

// Get provider
export const getProvider = () => {
  if (typeof window !== "undefined" && window.ethereum) {
    return new ethers.providers.Web3Provider(window.ethereum)
  }
  return null
}

// Get signer
export const getSigner = () => {
  const provider = getProvider()
  if (!provider) return null
  try {
    return provider.getSigner()
  } catch (error) {
    console.error("Error getting signer:", error)
    return null
  }
}

// Get FLB Token contract
export const getFLBTokenContract = async (withSigner = false) => {
  const provider = getProvider()
  if (!provider) return null

  // Ensure we have the latest contract config
  await initializeContractConfig()

  if (!contractConfig.tokenContract) return null

  try {
    const contract = new ethers.Contract(contractConfig.tokenContract, FLBTokenABI, provider)

    if (withSigner) {
      const signer = getSigner()
      if (!signer) return null
      return contract.connect(signer)
    }

    return contract
  } catch (error) {
    console.error("Error getting FLB token contract:", error)
    return null
  }
}

// Get Health Actors Registry contract
export const getHealthRegistryContract = async (withSigner = false) => {
  const provider = getProvider()
  if (!provider) return null

  // Ensure we have the latest contract config
  await initializeContractConfig()

  if (!contractConfig.healthRegistry) return null

  try {
    const contract = new ethers.Contract(contractConfig.healthRegistry, HealthActorsRegistryABI, provider)

    if (withSigner) {
      const signer = getSigner()
      if (!signer) return null
      return contract.connect(signer)
    }

    return contract
  } catch (error) {
    console.error("Error getting health registry contract:", error)
    return null
  }
}

// Get Donation Router contract
export const getDonationRouterContract = async (withSigner = false) => {
  const provider = getProvider()
  if (!provider) return null

  // Ensure we have the latest contract config
  await initializeContractConfig()

  if (!contractConfig.donationRouter) return null

  try {
    const contract = new ethers.Contract(contractConfig.donationRouter, DonationRouterABI, provider)

    if (withSigner) {
      const signer = getSigner()
      if (!signer) return null
      return contract.connect(signer)
    }

    return contract
  } catch (error) {
    console.error("Error getting donation router contract:", error)
    return null
  }
}

// Check if an address is a verified health actor
export const isVerifiedHealthActor = async (address: string): Promise<boolean> => {
  try {
    const contract = await getHealthRegistryContract()
    if (!contract) throw new Error("Contract not available")

    return await contract.isVerifiedHealthActor(address)
  } catch (error) {
    console.error("Error checking health actor verification:", error)
    return false
  }
}

// Get health actor information
export const getHealthActorInfo = async (address: string): Promise<HealthActor | null> => {
  try {
    const contract = await getHealthRegistryContract()
    if (!contract) throw new Error("Contract not available")

    const info = await contract.getHealthActorInfo(address)
    return {
      name: info.name,
      location: info.location,
      credentials: info.credentials,
      isVerified: info.isVerified,
    }
  } catch (error) {
    console.error("Error getting health actor info:", error)
    return null
  }
}

// Register as a health actor
export const registerHealthActor = async (name: string, location: string, credentials: string): Promise<boolean> => {
  try {
    const contract = await getHealthRegistryContract(true)
    if (!contract) throw new Error("Contract not available")

    const tx = await contract.registerHealthActor(name, location, credentials)
    await tx.wait()
    return true
  } catch (error) {
    console.error("Error registering health actor:", error)
    return false
  }
}

// Get FLB token balance
export const getFLBBalance = async (address: string): Promise<string> => {
  try {
    const contract = await getFLBTokenContract()
    if (!contract) throw new Error("Contract not available")

    const balance = await contract.balanceOf(address)
    return ethers.formatUnits(balance, 18)
  } catch (error) {
    console.error("Error getting FLB balance:", error)
    return "0"
  }
}

// Donate to a health actor
export const donateToHealthActor = async (recipientAddress: string, amount: string): Promise<boolean> => {
  try {
    const contract = await getDonationRouterContract(true)
    if (!contract) throw new Error("Donation Router contract not available")

    const amountWei = ethers.parseEther(amount)
    const tx = await contract.donate(recipientAddress, { value: amountWei })
    await tx.wait()
    return true
  } catch (error) {
    console.error("Error donating to health actor:", error)
    return false
  }
}

// Mint FLB tokens
export const mintFLBTokens = async (amount: string): Promise<boolean> => {
  try {
    const contract = await getDonationRouterContract(true)
    if (!contract) throw new Error("Donation Router contract not available")

    const amountWei = ethers.parseEther(amount)
    const tx = await contract.mintFLBToken({ value: amountWei })
    await tx.wait()
    return true
  } catch (error) {
    console.error("Error minting FLB tokens:", error)
    return false
  }
}