|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | + |
| 3 | +import { useChainId } from "wagmi"; |
| 4 | + |
| 5 | +import { DisputeKits } from "consts/index"; |
| 6 | + |
| 7 | +interface UseDisputeKitAddressesParams { |
| 8 | + disputeKitAddress?: string; |
| 9 | +} |
| 10 | + |
| 11 | +interface UseDisputeKitAddressesAllReturn { |
| 12 | + availableDisputeKits: Record<string, DisputeKits>; |
| 13 | + isLoading: boolean; |
| 14 | + error: string | null; |
| 15 | +} |
| 16 | + |
| 17 | +const DISPUTE_KIT_CONFIG = { |
| 18 | + [DisputeKits.Classic]: "disputeKitClassicAddress", |
| 19 | + [DisputeKits.Shutter]: "disputeKitShutterAddress", |
| 20 | + [DisputeKits.Gated]: "disputeKitGatedAddress", |
| 21 | + [DisputeKits.GatedShutter]: "disputeKitGatedShutterAddress", |
| 22 | +} as const; |
| 23 | + |
| 24 | +/** |
| 25 | + * Hook to get dispute kit name based on address |
| 26 | + * @param disputeKitAddress - Optional specific dispute kit address to identify |
| 27 | + * @returns The human-readable name of the dispute kit and loading state |
| 28 | + */ |
| 29 | +export const useDisputeKitAddresses = ({ disputeKitAddress }: UseDisputeKitAddressesParams = {}) => { |
| 30 | + const chainId = useChainId(); |
| 31 | + const [disputeKitName, setDisputeKitName] = useState<DisputeKits | undefined>(undefined); |
| 32 | + const [isLoading, setIsLoading] = useState(true); |
| 33 | + const [error, setError] = useState<string | null>(null); |
| 34 | + |
| 35 | + useEffect(() => { |
| 36 | + const loadDisputeKitName = async () => { |
| 37 | + try { |
| 38 | + setIsLoading(true); |
| 39 | + setError(null); |
| 40 | + |
| 41 | + // If no dispute kit address is provided, we can't determine the type |
| 42 | + if (!disputeKitAddress) { |
| 43 | + setDisputeKitName(undefined); |
| 44 | + setIsLoading(false); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + // If no chainId, we can't look up from generated contracts |
| 49 | + if (!chainId) { |
| 50 | + setDisputeKitName(undefined); |
| 51 | + setIsLoading(false); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + // Dynamic import to handle cases where generated contracts might not be available |
| 56 | + try { |
| 57 | + const generatedContracts = await import("hooks/contracts/generated"); |
| 58 | + |
| 59 | + // Check each dispute kit to see if the address matches |
| 60 | + for (const [humanName, contractKey] of Object.entries(DISPUTE_KIT_CONFIG)) { |
| 61 | + const addressMapping = generatedContracts[contractKey as keyof typeof generatedContracts]; |
| 62 | + |
| 63 | + if (addressMapping && typeof addressMapping === "object" && chainId in addressMapping) { |
| 64 | + const contractAddress = addressMapping[chainId as keyof typeof addressMapping] as string; |
| 65 | + if ( |
| 66 | + contractAddress && |
| 67 | + typeof contractAddress === "string" && |
| 68 | + contractAddress.toLowerCase() === disputeKitAddress.toLowerCase() |
| 69 | + ) { |
| 70 | + setDisputeKitName(humanName as DisputeKits); |
| 71 | + return; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // If no address matches, return undefined |
| 77 | + setDisputeKitName(undefined); |
| 78 | + } catch { |
| 79 | + // If we can't import generated contracts, return undefined |
| 80 | + setDisputeKitName(undefined); |
| 81 | + } |
| 82 | + } catch (err) { |
| 83 | + console.error("Failed to determine dispute kit name:", err); |
| 84 | + setError("Failed to determine dispute kit type"); |
| 85 | + setDisputeKitName(undefined); |
| 86 | + } finally { |
| 87 | + setIsLoading(false); |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + loadDisputeKitName(); |
| 92 | + }, [chainId, disputeKitAddress]); |
| 93 | + |
| 94 | + return { |
| 95 | + disputeKitName, |
| 96 | + isLoading, |
| 97 | + error, |
| 98 | + }; |
| 99 | +}; |
| 100 | + |
| 101 | +/** |
| 102 | + * Hook to get all dispute kit addresses for the current chain |
| 103 | + * @returns All dispute kit addresses, loading state, and error state |
| 104 | + */ |
| 105 | +export const useDisputeKitAddressesAll = (): UseDisputeKitAddressesAllReturn => { |
| 106 | + const chainId = useChainId(); |
| 107 | + const [availableDisputeKits, setAvailableDisputeKits] = useState<Record<string, DisputeKits>>({}); |
| 108 | + const [isLoading, setIsLoading] = useState(true); |
| 109 | + const [error, setError] = useState<string | null>(null); |
| 110 | + |
| 111 | + useEffect(() => { |
| 112 | + const loadAllDisputeKitAddresses = async () => { |
| 113 | + try { |
| 114 | + setIsLoading(true); |
| 115 | + setError(null); |
| 116 | + |
| 117 | + // If no chainId, we can't look up from generated contracts |
| 118 | + if (!chainId) { |
| 119 | + setAvailableDisputeKits({}); |
| 120 | + setIsLoading(false); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + // Dynamic import to handle cases where generated contracts might not be available |
| 125 | + try { |
| 126 | + const generatedContracts = await import("hooks/contracts/generated"); |
| 127 | + const newAvailableDisputeKits: Record<string, DisputeKits> = {}; |
| 128 | + |
| 129 | + // Iterate through all dispute kits and get their addresses |
| 130 | + for (const [humanName, contractKey] of Object.entries(DISPUTE_KIT_CONFIG)) { |
| 131 | + const addressMapping = generatedContracts[contractKey as keyof typeof generatedContracts]; |
| 132 | + |
| 133 | + if (addressMapping && typeof addressMapping === "object" && chainId in addressMapping) { |
| 134 | + const contractAddress = addressMapping[chainId as keyof typeof addressMapping] as string; |
| 135 | + if (contractAddress && typeof contractAddress === "string") { |
| 136 | + newAvailableDisputeKits[contractAddress.toLowerCase()] = humanName as DisputeKits; |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + setAvailableDisputeKits(newAvailableDisputeKits); |
| 142 | + } catch { |
| 143 | + // If we can't import generated contracts, return empty object |
| 144 | + setAvailableDisputeKits({}); |
| 145 | + } |
| 146 | + } catch (err) { |
| 147 | + console.error("Failed to load dispute kit addresses:", err); |
| 148 | + setError("Failed to load dispute kit addresses"); |
| 149 | + setAvailableDisputeKits({}); |
| 150 | + } finally { |
| 151 | + setIsLoading(false); |
| 152 | + } |
| 153 | + }; |
| 154 | + |
| 155 | + loadAllDisputeKitAddresses(); |
| 156 | + }, [chainId]); |
| 157 | + |
| 158 | + return { |
| 159 | + availableDisputeKits, |
| 160 | + isLoading, |
| 161 | + error, |
| 162 | + }; |
| 163 | +}; |
0 commit comments