import { createPublicClient, http, parseAbi } from 'viem'; import { arbitrumSepolia } from 'viem/chains'; // 合约地址 const CONFIGURATOR_ADDRESS = '0x488409CE9A3Fcd8EbD373dCb7e025cF8AB96fcdc'; const YT_A_ADDRESS = '0x97204190B35D9895a7a47aa7BaC61ac08De3cF05'; const USER_ADDRESS = '0xa013422A5918CD099C63c8CC35283EACa99a705d'; const CONFIGURATOR_ABI = parseAbi([ 'function owner() view returns (address)', 'function collateralConfigs(address) view returns (bool isActive, uint256 collateralFactor, uint256 liquidationThreshold, uint256 liquidationBonus)', 'function setCollateralConfig(address _asset, uint256 _collateralFactor, uint256 _liquidationThreshold, uint256 _liquidationBonus)', 'function setCollateralActive(address _asset, bool _isActive)' ]); const publicClient = createPublicClient({ chain: arbitrumSepolia, transport: http('https://sepolia-rollup.arbitrum.io/rpc') }); async function diagnose() { console.log('🔍 Configurator 诊断工具\n'); try { // 1. 检查 owner console.log('1️⃣ 检查合约 Owner:'); const owner = await publicClient.readContract({ address: CONFIGURATOR_ADDRESS, abi: CONFIGURATOR_ABI, functionName: 'owner' }); console.log(` Owner: ${owner}`); console.log(` 你的地址: ${USER_ADDRESS}`); const isOwner = owner.toLowerCase() === USER_ADDRESS.toLowerCase(); console.log(` 匹配: ${isOwner ? '✅' : '❌'}\n`); if (!isOwner) { console.log(' ⚠️ 你不是 owner!这就是交易失败的原因。\n'); return; } // 2. 检查当前配置 console.log('2️⃣ 检查 YT-A 当前配置:'); try { const config = await publicClient.readContract({ address: CONFIGURATOR_ADDRESS, abi: CONFIGURATOR_ABI, functionName: 'collateralConfigs', args: [YT_A_ADDRESS] }); console.log(` isActive: ${config[0]}`); console.log(` collateralFactor: ${config[1]}`); console.log(` liquidationThreshold: ${config[2]}`); console.log(` liquidationBonus: ${config[3]}\n`); } catch (error) { console.log(` ❌ 读取失败: ${error.message}\n`); } // 3. 尝试模拟调用 setCollateralConfig console.log('3️⃣ 模拟调用 setCollateralConfig(YT-A, 7500, 8500, 1000):'); try { const result = await publicClient.simulateContract({ address: CONFIGURATOR_ADDRESS, abi: CONFIGURATOR_ABI, functionName: 'setCollateralConfig', args: [YT_A_ADDRESS, 7500n, 8500n, 1000n], account: USER_ADDRESS }); console.log(' ✅ 模拟成功!交易应该可以执行\n'); console.log(' 模拟结果:', result); } catch (error) { console.log(' ❌ 模拟失败!'); console.log(` 错误类型: ${error.name}`); console.log(` 错误信息: ${error.shortMessage || error.message}`); if (error.cause) { console.log(` 底层原因: ${JSON.stringify(error.cause, null, 2)}`); } if (error.details) { console.log(` 详情: ${error.details}`); } if (error.metaMessages) { console.log(` 元信息: ${error.metaMessages.join(', ')}`); } console.log('\n'); } // 4. 检查合约代码 console.log('4️⃣ 检查合约代码:'); const bytecode = await publicClient.getBytecode({ address: CONFIGURATOR_ADDRESS }); console.log(` 代码大小: ${bytecode ? bytecode.length : 0} bytes`); console.log(` 合约已部署: ${bytecode && bytecode.length > 2 ? '✅' : '❌'}\n`); // 5. 建议 console.log('💡 诊断建议:'); console.log(' 1. 检查合约是否有访问控制(如 Ownable, AccessControl)'); console.log(' 2. 参数可能有范围限制(如 collateralFactor 必须 <= 10000)'); console.log(' 3. 可能需要先调用其他初始化函数'); console.log(' 4. 查看合约源码了解具体的 require 条件'); console.log('\n📊 在 Arbiscan 查看合约:'); console.log(` https://sepolia.arbiscan.io/address/${CONFIGURATOR_ADDRESS}#code`); } catch (error) { console.error('❌ 诊断过程出错:', error); } } diagnose();