11import type { ReactElement } from 'react' ;
22import { useEffect , useState } from 'react' ;
3- import { AddressTable , TableAddresses } from '@/components/AddressTable' ;
4- import toml from 'toml' ;
3+ import { AddressTable , type TableAddresses } from './AddressTable' ;
54
6- const CONFIG_URL = 'https://raw.githubusercontent.com/ethereum-optimism/superchain-registry/main/superchain/configs/mainnet/superchain.toml' ;
5+ function getConfigUrl ( chain : string ) : string {
6+ const isTestnet = chain === '11155111' ;
7+ const network = isTestnet ? 'sepolia' : 'mainnet' ;
8+ return `https://raw.githubusercontent.com/ethereum-optimism/superchain-registry/main/superchain/configs/${ network } /superchain.toml` ;
9+ }
10+
11+ function parseSimpleToml ( tomlText : string ) : Record < string , any > {
12+ const result : Record < string , any > = { } ;
13+ const lines = tomlText . split ( '\n' ) ;
14+
15+ for ( const line of lines ) {
16+ const trimmed = line . trim ( ) ;
17+ if ( trimmed && ! trimmed . startsWith ( '#' ) && trimmed . includes ( '=' ) ) {
18+ const [ key , value ] = trimmed . split ( '=' , 2 ) ;
19+ const cleanKey = key . trim ( ) ;
20+ const cleanValue = value . trim ( ) . replace ( / [ ' " ] / g, '' ) ;
21+
22+ if ( / ^ 0 x [ a - f A - F 0 - 9 ] { 40 } $ / . test ( cleanValue ) ) {
23+ result [ cleanKey ] = cleanValue ;
24+ }
25+ }
26+ }
27+
28+ return result ;
29+ }
30+
31+ function extractAddresses ( obj : Record < string , any > ) : TableAddresses {
32+ const addresses : TableAddresses = { } ;
33+ for ( const [ key , value ] of Object . entries ( obj ) ) {
34+ if ( typeof value === 'string' && / ^ 0 x [ a - f A - F 0 - 9 ] { 40 } $ / . test ( value ) ) {
35+ let newKey = key ;
36+ if ( key === 'protocol_versions_addr' ) newKey = 'ProtocolVersions' ;
37+ if ( key === 'superchain_config_addr' ) newKey = 'SuperchainConfig' ;
38+ if ( key === 'op_contracts_manager_proxy_addr' ) newKey = 'OPContractsManagerProxy' ;
39+ addresses [ newKey ] = value ;
40+ }
41+ }
42+ return addresses ;
43+ }
744
845export function SuperchainContractTable ( {
946 chain,
@@ -19,12 +56,14 @@ export function SuperchainContractTable({
1956 useEffect ( ( ) => {
2057 async function fetchAddresses ( ) {
2158 try {
22- const response = await fetch ( CONFIG_URL ) ;
59+ const configUrl = getConfigUrl ( chain ) ;
60+ const response = await fetch ( configUrl ) ;
2361 if ( ! response . ok ) {
24- throw new Error ( ' Failed to fetch config' ) ;
62+ throw new Error ( ` Failed to fetch config from ${ configUrl } ` ) ;
2563 }
2664 const text = await response . text ( ) ;
27- const data = toml . parse ( text ) ;
65+
66+ const data = parseSimpleToml ( text ) ;
2867 setConfig ( data ) ;
2968 } catch ( err ) {
3069 setError ( err . message ) ;
@@ -35,7 +74,7 @@ export function SuperchainContractTable({
3574 }
3675
3776 fetchAddresses ( ) ;
38- } , [ ] ) ;
77+ } , [ chain ] ) ;
3978
4079 if ( loading ) {
4180 return < div > Loading...</ div > ;
@@ -45,25 +84,6 @@ export function SuperchainContractTable({
4584 return < div > Error: { error } </ div > ;
4685 }
4786
48- // Helper function to recursively extract Ethereum addresses with renamed keys
49- function extractAddresses ( obj : Record < string , any > , prefix = '' ) : TableAddresses {
50- const addresses : TableAddresses = { } ;
51- for ( const [ key , value ] of Object . entries ( obj ) ) {
52- if ( typeof value === 'string' && / ^ 0 x [ a - f A - F 0 - 9 ] { 40 } $ / . test ( value ) ) {
53- // Rename specific keys
54- let newKey = `${ prefix } ${ key } ` ;
55- if ( key === 'protocol_versions_addr' ) newKey = 'ProtocolVersions' ;
56- if ( key === 'superchain_config_addr' ) newKey = 'SuperchainConfig' ;
57- if ( key === 'op_contracts_manager_proxy_addr' ) newKey = 'OPContractsManagerProxy' ;
58- addresses [ newKey ] = value ;
59- } else if ( typeof value === 'object' && value !== null ) {
60- Object . assign ( addresses , extractAddresses ( value , `${ key } .` ) ) ; // Recurse into nested objects
61- }
62- }
63- return addresses ;
64- }
65-
66-
6787 const addresses = extractAddresses ( config || { } ) ;
6888
6989 return (
0 commit comments