|
| 1 | +import type { ReactElement } from 'react' |
| 2 | +import { useEffect, useState } from 'react' |
| 3 | +import { AddressTable, TableAddresses } from '@/components/AddressTable' |
| 4 | + |
| 5 | +const CONFIG_URL = 'https://raw.githubusercontent.com/ethereum-optimism/superchain-registry/main/superchain/configs/configs.json'; |
| 6 | + |
| 7 | +export function SuperchainContractTable({ |
| 8 | + chain, |
| 9 | + explorer, |
| 10 | +}: { |
| 11 | + chain: string, |
| 12 | + explorer: string, |
| 13 | +}): ReactElement { |
| 14 | + const [config, setConfig] = useState<Record<string, any> | null>(null) |
| 15 | + const [loading, setLoading] = useState(true) |
| 16 | + const [error, setError] = useState<string | null>(null) |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + async function fetchAddresses() { |
| 20 | + try { |
| 21 | + const response = await fetch(CONFIG_URL) |
| 22 | + if (!response.ok) { |
| 23 | + throw new Error('Failed to fetch config') |
| 24 | + } |
| 25 | + const data = await response.json() |
| 26 | + setConfig(data) |
| 27 | + } catch (err) { |
| 28 | + setError(err.message) |
| 29 | + console.error('Error fetching config:', err) |
| 30 | + } finally { |
| 31 | + setLoading(false) |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + fetchAddresses() |
| 36 | + }, []) |
| 37 | + |
| 38 | + if (loading) { |
| 39 | + return <div>Loading...</div> |
| 40 | + } |
| 41 | + |
| 42 | + if (error) { |
| 43 | + return <div>Error: {error}</div> |
| 44 | + } |
| 45 | + |
| 46 | + // Find the superchain config for the given chain. |
| 47 | + const superchain = config?.superchains.find( |
| 48 | + (sc: any) => sc.config.L1.ChainID.toString() === chain |
| 49 | + ) |
| 50 | + |
| 51 | + // Create a TableAddresses object with the ProtocolVersions and SuperchainConfig addresses. |
| 52 | + const addresses: TableAddresses | undefined = superchain && { |
| 53 | + ProtocolVersions: superchain.config.ProtocolVersionsAddr, |
| 54 | + SuperchainConfig: superchain.config.SuperchainConfigAddr, |
| 55 | + }; |
| 56 | + |
| 57 | + return ( |
| 58 | + <AddressTable |
| 59 | + chain={chain} |
| 60 | + explorer={explorer} |
| 61 | + legacy={false} |
| 62 | + addresses={addresses} |
| 63 | + /> |
| 64 | + ) |
| 65 | +} |
0 commit comments