import { createPublicClient, http } from 'viem'
import { bscTestnet } from 'viem/chains'

const lendingProxyAddress = '0xCb4E7B1069F6C26A1c27523ce4c8dfD884552d1D'
const userAddress = '0xa013422A5918CD099C63c8CC35283EACa99a705d'

const client = createPublicClient({
  chain: bscTestnet,
  transport: http('https://data-seed-prebsc-1-s1.binance.org:8545'),
})

console.log('🔍 测试 lendingProxy 合约...\n')

try {
  // 检查合约代码
  const code = await client.getBytecode({ address: lendingProxyAddress })
  console.log('✅ 合约代码长度:', code ? code.length : 0, '字节')
  
  if (!code || code === '0x') {
    console.error('❌ 合约未部署！')
    process.exit(1)
  }

  // 调用 supplyBalanceOf
  const balance = await client.readContract({
    address: lendingProxyAddress,
    abi: [{
      type: 'function',
      name: 'supplyBalanceOf',
      inputs: [{ name: 'account', type: 'address' }],
      outputs: [{ name: '', type: 'uint256' }],
      stateMutability: 'view',
    }],
    functionName: 'supplyBalanceOf',
    args: [userAddress],
  })

  console.log('✅ 供应余额:', balance.toString())
  console.log('格式化:', (Number(balance) / 1e18).toFixed(2), 'USDC')

} catch (error) {
  console.error('❌ 错误:', error.shortMessage || error.message)
}
