|
| 1 | +import { useState, useEffect, useCallback } from 'react'; |
| 2 | +import { useBcForgeClient } from './context'; |
| 3 | +import { Keypair } from '@stellar/stellar-sdk'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Hook to fetch basic token information (name, symbol, decimals). |
| 7 | + */ |
| 8 | +export function useBcForgeToken() { |
| 9 | + const client = useBcForgeClient(); |
| 10 | + const [data, setData] = useState<{ name: string; symbol: string; decimals: number } | null>(null); |
| 11 | + const [loading, setLoading] = useState(true); |
| 12 | + const [error, setError] = useState<Error | null>(null); |
| 13 | + |
| 14 | + useEffect(() => { |
| 15 | + async function fetchData() { |
| 16 | + try { |
| 17 | + setLoading(true); |
| 18 | + const [name, symbol, decimals] = await Promise.all([ |
| 19 | + client.getName(), |
| 20 | + client.getSymbol(), |
| 21 | + client.getDecimals(), |
| 22 | + ]); |
| 23 | + setData({ name, symbol, decimals }); |
| 24 | + } catch (err) { |
| 25 | + setError(err instanceof Error ? err : new Error(String(err))); |
| 26 | + } finally { |
| 27 | + setLoading(false); |
| 28 | + } |
| 29 | + } |
| 30 | + fetchData(); |
| 31 | + }, [client]); |
| 32 | + |
| 33 | + return { data, loading, error }; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Hook to fetch the balance of a specific address. |
| 38 | + */ |
| 39 | +export function useBalance(address: string | undefined) { |
| 40 | + const client = useBcForgeClient(); |
| 41 | + const [data, setData] = useState<bigint | null>(null); |
| 42 | + const [loading, setLoading] = useState(false); |
| 43 | + const [error, setError] = useState<Error | null>(null); |
| 44 | + |
| 45 | + const fetchBalance = useCallback(async () => { |
| 46 | + if (!address) return; |
| 47 | + try { |
| 48 | + setLoading(true); |
| 49 | + const balance = await client.getBalance(address); |
| 50 | + setData(balance); |
| 51 | + } catch (err) { |
| 52 | + setError(err instanceof Error ? err : new Error(String(err))); |
| 53 | + } finally { |
| 54 | + setLoading(false); |
| 55 | + } |
| 56 | + }, [client, address]); |
| 57 | + |
| 58 | + useEffect(() => { |
| 59 | + fetchBalance(); |
| 60 | + }, [fetchBalance]); |
| 61 | + |
| 62 | + return { data, loading, error, refetch: fetchBalance }; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Hook to perform mint operations. |
| 67 | + */ |
| 68 | +export function useMint() { |
| 69 | + const client = useBcForgeClient(); |
| 70 | + const [loading, setLoading] = useState(false); |
| 71 | + const [error, setError] = useState<Error | null>(null); |
| 72 | + |
| 73 | + const mint = useCallback(async (to: string, amount: bigint, source: Keypair) => { |
| 74 | + try { |
| 75 | + setLoading(true); |
| 76 | + setError(null); |
| 77 | + const result = await client.mint(to, amount, source); |
| 78 | + return result; |
| 79 | + } catch (err) { |
| 80 | + const error = err instanceof Error ? err : new Error(String(err)); |
| 81 | + setError(error); |
| 82 | + throw error; |
| 83 | + } finally { |
| 84 | + setLoading(false); |
| 85 | + } |
| 86 | + }, [client]); |
| 87 | + |
| 88 | + return { mint, loading, error }; |
| 89 | +} |
0 commit comments