File size: 5,316 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
import { ethers } from "ethers"
import { FLB_TOKEN_ADDRESSES, DONATION_ROUTER_ADDRESSES } from "./constants"
import FLBTokenABI from "@/abi/FLBToken.json"
import DonationRouterABI from "@/abi/DonationRouter.json"

export interface DonationResult {
  hash: string
  transaction: ethers.providers.TransactionResponse
}

export async function donate(signer: ethers.Signer, recipientAddress: string, amount: string): Promise<DonationResult> {
  try {
    const network = await signer.provider?.getNetwork()
    if (!network) throw new Error("No network found")

    let donationRouterAddress: string
    if (network.chainId === 42220) {
      donationRouterAddress = DONATION_ROUTER_ADDRESSES.CELO_MAINNET
    } else if (network.chainId === 44787) {
      donationRouterAddress = DONATION_ROUTER_ADDRESSES.CELO_ALFAJORES
    } else {
      throw new Error("Unsupported network")
    }

    if (donationRouterAddress === "0x0000000000000000000000000000000000000000") {
      // Fallback to direct transfer for testing
      const tx = await signer.sendTransaction({
        to: recipientAddress,
        value: ethers.utils.parseEther(amount),
      })

      return {
        hash: tx.hash,
        transaction: tx,
      }
    }

    const donationRouter = new ethers.Contract(donationRouterAddress, DonationRouterABI, signer)

    const tx = await donationRouter.donate(recipientAddress, {
      value: ethers.utils.parseEther(amount),
    })

    return {
      hash: tx.hash,
      transaction: tx,
    }
  } catch (error) {
    console.error("Donation failed:", error)
    throw error
  }
}

export async function mintFLBTokens(signer: ethers.Signer, amount: string): Promise<DonationResult> {
  try {
    const network = await signer.provider?.getNetwork()
    if (!network) throw new Error("No network found")

    let donationRouterAddress: string
    if (network.chainId === 42220) {
      donationRouterAddress = DONATION_ROUTER_ADDRESSES.CELO_MAINNET
    } else if (network.chainId === 44787) {
      donationRouterAddress = DONATION_ROUTER_ADDRESSES.CELO_ALFAJORES
    } else {
      throw new Error("Unsupported network")
    }

    if (donationRouterAddress === "0x0000000000000000000000000000000000000000") {
      throw new Error("FLB token contract not deployed on this network")
    }

    const donationRouter = new ethers.Contract(donationRouterAddress, DonationRouterABI, signer)

    const tx = await donationRouter.mintFLBToken({
      value: ethers.utils.parseEther(amount),
    })

    return {
      hash: tx.hash,
      transaction: tx,
    }
  } catch (error) {
    console.error("FLB minting failed:", error)
    throw error
  }
}

export async function getFLBBalance(address: string, provider: ethers.providers.Provider): Promise<string> {
  try {
    const network = await provider.getNetwork()

    let tokenAddress: string
    if (network.chainId === 42220) {
      tokenAddress = FLB_TOKEN_ADDRESSES.CELO_MAINNET
    } else if (network.chainId === 44787) {
      tokenAddress = FLB_TOKEN_ADDRESSES.CELO_ALFAJORES
    } else {
      return "0"
    }

    if (tokenAddress === "0x0000000000000000000000000000000000000000") {
      return "0" // Contract not deployed
    }

    const flbToken = new ethers.Contract(tokenAddress, FLBTokenABI, provider)
    const balance = await flbToken.balanceOf(address)

    return ethers.utils.formatEther(balance)
  } catch (error) {
    console.error("Failed to get FLB balance:", error)
    return "0"
  }
}

export async function transferFLBTokens(
  signer: ethers.Signer,
  toAddress: string,
  amount: string,
): Promise<DonationResult> {
  try {
    const network = await signer.provider?.getNetwork()
    if (!network) throw new Error("No network found")

    let tokenAddress: string
    if (network.chainId === 42220) {
      tokenAddress = FLB_TOKEN_ADDRESSES.CELO_MAINNET
    } else if (network.chainId === 44787) {
      tokenAddress = FLB_TOKEN_ADDRESSES.CELO_ALFAJORES
    } else {
      throw new Error("Unsupported network")
    }

    if (tokenAddress === "0x0000000000000000000000000000000000000000") {
      throw new Error("FLB token contract not deployed on this network")
    }

    const flbToken = new ethers.Contract(tokenAddress, FLBTokenABI, signer)
    const amountWei = ethers.utils.parseEther(amount)

    const tx = await flbToken.transfer(toAddress, amountWei)

    return {
      hash: tx.hash,
      transaction: tx,
    }
  } catch (error) {
    console.error("FLB transfer failed:", error)
    throw error
  }
}

// Function to check if address needs identity registration (based on your contract logic)
export async function checkIdentityRegistration(
  address: string,
  provider: ethers.providers.Provider,
): Promise<boolean> {
  try {
    const network = await provider.getNetwork()

    if (network.chainId !== 44787) {
      return false // Only check on Alfajores for now
    }

    const tokenAddress = FLB_TOKEN_ADDRESSES.CELO_ALFAJORES

    if (tokenAddress === "0x0000000000000000000000000000000000000000") {
      return false
    }

    // This would check if the address is registered in your identity system
    // For now, we'll assume all addresses need registration
    return false
  } catch (error) {
    console.error("Failed to check identity registration:", error)
    return false
  }
}