import { createPublicClient, http, formatUnits } from 'viem'; import { arbitrumSepolia } from 'viem/chains'; const RPC_URL = 'https://sepolia-rollup.arbitrum.io/rpc'; const LENDING_PROXY = '0xCb4E7B1069F6C26A1c27523ce4c8dfD884552d1D'; const TEST_USER = '0xa013422A5918CD099C63c8CC35283EACa99a705d'; // 有抵押品的用户 const YT_A = '0x97204190B35D9895a7a47aa7BaC61ac08De3cF05'; const client = createPublicClient({ chain: arbitrumSepolia, transport: http(RPC_URL) }); const testResults = { success: [], failed: [] }; async function testFunction(name, abi, params = []) { try { const result = await client.readContract({ address: LENDING_PROXY, abi: [abi], functionName: name, args: params }); testResults.success.push(name); return { success: true, value: result }; } catch (error) { testResults.failed.push(name); return { success: false, error: error.message.split('\n')[0] }; } } async function runAllTests() { console.log('\n=== 验证所有前端 LENDING ABI 函数 ===\n'); // 1. getCollateral - 用户查询 console.log('1️⃣ getCollateral(account, asset):'); const r1 = await testFunction('getCollateral', { inputs: [ { internalType: 'address', name: 'account', type: 'address' }, { internalType: 'address', name: 'asset', type: 'address' } ], name: 'getCollateral', outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], stateMutability: 'view', type: 'function' }, [TEST_USER, YT_A]); console.log(r1.success ? ` ✅ 成功 - 抵押量: ${formatUnits(r1.value, 18)}` : ` ❌ 失败 - ${r1.error}`); // 2. getBalance - 用户余额 console.log('\n2️⃣ getBalance(account):'); const r2 = await testFunction('getBalance', { inputs: [{ internalType: 'address', name: 'account', type: 'address' }], name: 'getBalance', outputs: [{ internalType: 'int256', name: '', type: 'int256' }], stateMutability: 'view', type: 'function' }, [TEST_USER]); console.log(r2.success ? ` ✅ 成功 - 余额: ${r2.value.toString()}` : ` ❌ 失败 - ${r2.error}`); // 3. borrowBalanceOf - 借款余额 console.log('\n3️⃣ borrowBalanceOf(account):'); const r3 = await testFunction('borrowBalanceOf', { inputs: [{ internalType: 'address', name: 'account', type: 'address' }], name: 'borrowBalanceOf', outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], stateMutability: 'view', type: 'function' }, [TEST_USER]); console.log(r3.success ? ` ✅ 成功 - 借款: ${formatUnits(r3.value, 6)} USDC` : ` ❌ 失败 - ${r3.error}`); // 4. getTotalBorrow - 系统总借款 console.log('\n4️⃣ getTotalBorrow():'); const r4 = await testFunction('getTotalBorrow', { inputs: [], name: 'getTotalBorrow', outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], stateMutability: 'view', type: 'function' }); console.log(r4.success ? ` ✅ 成功 - 总借款: ${formatUnits(r4.value, 6)} USDC` : ` ❌ 失败 - ${r4.error}`); // 5. getTotalSupply - 系统总供应 console.log('\n5️⃣ getTotalSupply():'); const r5 = await testFunction('getTotalSupply', { inputs: [], name: 'getTotalSupply', outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], stateMutability: 'view', type: 'function' }); console.log(r5.success ? ` ✅ 成功 - 总供应: ${formatUnits(r5.value, 6)} USDC` : ` ❌ 失败 - ${r5.error}`); // 6. getUtilization - 资金利用率 console.log('\n6️⃣ getUtilization():'); const r6 = await testFunction('getUtilization', { inputs: [], name: 'getUtilization', outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], stateMutability: 'view', type: 'function' }); console.log(r6.success ? ` ✅ 成功 - 利用率: ${Number(r6.value) / 1e16}%` : ` ❌ 失败 - ${r6.error}`); // 7. getBorrowRate - 借款利率 console.log('\n7️⃣ getBorrowRate():'); const r7 = await testFunction('getBorrowRate', { inputs: [], name: 'getBorrowRate', outputs: [{ internalType: 'uint64', name: '', type: 'uint64' }], stateMutability: 'view', type: 'function' }); console.log(r7.success ? ` ✅ 成功 - 借款利率: ${Number(r7.value) / 1e16}%` : ` ❌ 失败 - ${r7.error}`); // 8. getSupplyRate - 存款利率 console.log('\n8️⃣ getSupplyRate():'); const r8 = await testFunction('getSupplyRate', { inputs: [], name: 'getSupplyRate', outputs: [{ internalType: 'uint64', name: '', type: 'uint64' }], stateMutability: 'view', type: 'function' }); console.log(r8.success ? ` ✅ 成功 - 存款利率: ${Number(r8.value) / 1e16}%` : ` ❌ 失败 - ${r8.error}`); // 9. owner - 管理员 console.log('\n9️⃣ owner():'); const r9 = await testFunction('owner', { inputs: [], name: 'owner', outputs: [{ internalType: 'address', name: '', type: 'address' }], stateMutability: 'view', type: 'function' }); console.log(r9.success ? ` ✅ 成功 - Owner: ${r9.value}` : ` ❌ 失败 - ${r9.error}`); // 10. paused - 暂停状态 console.log('\n🔟 paused():'); const r10 = await testFunction('paused', { inputs: [], name: 'paused', outputs: [{ internalType: 'bool', name: '', type: 'bool' }], stateMutability: 'view', type: 'function' }); console.log(r10.success ? ` ✅ 成功 - Paused: ${r10.value}` : ` ❌ 失败 - ${r10.error}`); // 汇总 console.log('\n' + '='.repeat(60)); console.log('📊 测试结果汇总:'); console.log('='.repeat(60)); console.log(`✅ 成功: ${testResults.success.length}/10`); console.log(` ${testResults.success.join(', ')}`); if (testResults.failed.length > 0) { console.log(`❌ 失败: ${testResults.failed.length}/10`); console.log(` ${testResults.failed.join(', ')}`); } console.log('='.repeat(60) + '\n'); // 对比测试脚本中的错误函数 console.log('⚠️ check-lending-setup.js 中使用的错误函数:'); console.log(' getTotalLiquidity() - 不存在,应改为 getTotalSupply()'); console.log(''); } runAllTests().catch(console.error);