import { createPublicClient, http } from 'viem';
import { base } from 'viem/chains';

const client = createPublicClient({ chain: base, transport: http('https://mainnet.base.org') });

const sleep = (ms) => new Promise(r => setTimeout(r, ms));
const safe = async (fn) => { try { return await fn(); } catch (e) { return `ERR ${(e.shortMessage||e.message).slice(0,100)}`; } };

// MultisigGovernance — threshold + signers[]
const M = '0x34a19C7298aCA59e185F1ecfB352AC566bB4B4A9';
const u256 = [{ name:'threshold', type:'function', stateMutability:'view', inputs:[], outputs:[{type:'uint256'}] }];
console.log('=== MultisigGovernance', M);
console.log('threshold:', await safe(() => client.readContract({ address: M, abi: u256, functionName:'threshold' })));
await sleep(300);
// Try common signer-list patterns
const sigArr = [{ name:'getSigners', type:'function', stateMutability:'view', inputs:[], outputs:[{type:'address[]'}] }];
console.log('getSigners():', await safe(() => client.readContract({ address: M, abi: sigArr, functionName:'getSigners' })));
await sleep(300);
const sigIdx = [{ name:'signers', type:'function', stateMutability:'view', inputs:[{type:'uint256'}], outputs:[{type:'address'}] }];
const signers = [];
for (let i = 0; i < 10; i++) {
  const v = await safe(() => client.readContract({ address: M, abi: sigIdx, functionName:'signers', args:[BigInt(i)] }));
  if (typeof v === 'string' && v.startsWith('ERR')) break;
  signers.push(v);
  await sleep(250);
}
console.log('signers iterated:', signers);

// DilithiumAttestor
const D = '0xef703bBe9b21960ede3e96abBB0Bbd0c525Fe050';
console.log('\n=== DilithiumAttestor', D);
await sleep(300);
console.log('origin:', await safe(() => client.readContract({ address: D, abi: [{name:'origin',type:'function',stateMutability:'view',inputs:[],outputs:[{type:'address'}]}], functionName:'origin' })));
await sleep(300);
console.log('zkEnabled:', await safe(() => client.readContract({ address: D, abi: [{name:'zkEnabled',type:'function',stateMutability:'view',inputs:[],outputs:[{type:'bool'}]}], functionName:'zkEnabled' })));
await sleep(300);
console.log('verifierAddress:', await safe(() => client.readContract({ address: D, abi: [{name:'verifierAddress',type:'function',stateMutability:'view',inputs:[],outputs:[{type:'address'}]}], functionName:'verifierAddress' })));

// VaultfireTeleporterBridge
const T = '0x62edf0cBc8Fa3d98B6A07197DFE9cb1c128a7eC7';
console.log('\n=== VaultfireTeleporterBridge', T);
await sleep(300);
console.log('teleporterMessenger:', await safe(() => client.readContract({ address: T, abi: [{name:'teleporterMessenger',type:'function',stateMutability:'view',inputs:[],outputs:[{type:'address'}]}], functionName:'teleporterMessenger' })));
await sleep(300);
console.log('requiredGasLimit:', await safe(() => client.readContract({ address: T, abi: [{name:'requiredGasLimit',type:'function',stateMutability:'view',inputs:[],outputs:[{type:'uint256'}]}], functionName:'requiredGasLimit' })));

// ProductionBeliefAttestationVerifier — get riscZeroVerifier
const P = '0x2a46B5932e9559176241056A04a608B8fEbCc5A7';
console.log('\n=== ProductionBeliefAttestationVerifier', P);
await sleep(300);
console.log('riscZeroVerifier:', await safe(() => client.readContract({ address: P, abi: [{name:'riscZeroVerifier',type:'function',stateMutability:'view',inputs:[],outputs:[{type:'address'}]}], functionName:'riscZeroVerifier' })));
