import { createPublicClient, http, getAddress } from 'viem' import { arbitrumSepolia } from 'viem/chains' const client = createPublicClient({ chain: arbitrumSepolia, transport: http('https://api.zan.top/node/v1/arb/sepolia/baf84c429d284bb5b676cb8c9ca21c07') }) const LENDING_PROXY = getAddress('0xCb4E7B1069F6C26A1c27523ce4c8dfD884552d1D') const USDC = getAddress('0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d') const USER = getAddress('0xa013422A5918CD099C63c8CC35283EACa99a705d') const TRANSFER_EVENT = { type: 'event', name: 'Transfer', inputs: [ { indexed: true, name: 'from', type: 'address' }, { indexed: true, name: 'to', type: 'address' }, { indexed: false, name: 'value', type: 'uint256' } ] } async function main() { console.log('\n=== 检查借款历史(USDC 从 Lending 转出给用户)===\n') console.log('用户:', USER) console.log('Lending 合约:', LENDING_PROXY) console.log('USDC:', USDC) console.log() const latestBlock = await client.getBlockNumber() console.log('最新区块:', latestBlock) console.log('查询范围: 最近 10000 个区块\n') try { // 查找 USDC 从 Lending 转给用户的记录(这才是真正的借款/提取) const logs = await client.getLogs({ address: USDC, event: TRANSFER_EVENT, args: { from: LENDING_PROXY, to: USER }, fromBlock: latestBlock - 10000n, toBlock: latestBlock }) if (logs.length > 0) { console.log(`✓ 找到 ${logs.length} 笔 USDC 转出记录:\n`) let totalWithdrawn = 0n for (let i = 0; i < logs.length; i++) { const log = logs[i] const { value } = log.args totalWithdrawn += value console.log(`${i + 1}. 区块 ${log.blockNumber}`) console.log(` 交易: ${log.transactionHash}`) console.log(` 数量: ${Number(value) / 1e6} USDC`) // 获取交易详情以确认是 withdraw 还是其他操作 try { const tx = await client.getTransaction({ hash: log.transactionHash }) const selector = tx.input.slice(0, 10) // withdraw(uint256) = 0x2e1a7d4d // withdrawFrom(...) = ... if (selector === '0x2e1a7d4d') { console.log(` 函数: withdraw (借款/提取)`) } else { console.log(` 函数选择器: ${selector}`) } } catch (error) { console.log(` (无法获取交易详情)`) } console.log() } console.log(`总计: ${Number(totalWithdrawn) / 1e6} USDC`) } else { console.log('✗ 未找到任何 USDC 转出记录') console.log() console.log('这意味着:') console.log(' - 用户从未真正从 Lending 提取或借款 USDC') console.log(' - 如果用户看到"借款成功"的消息,那些都是前端误报') } console.log('\n=== 对比:USDC 转入 Lending(存款)===\n') const supplyLogs = await client.getLogs({ address: USDC, event: TRANSFER_EVENT, args: { from: USER, to: LENDING_PROXY }, fromBlock: latestBlock - 10000n, toBlock: latestBlock }) if (supplyLogs.length > 0) { console.log(`找到 ${supplyLogs.length} 笔 USDC 存入记录:\n`) let totalSupplied = 0n supplyLogs.forEach((log, i) => { const { value } = log.args totalSupplied += value console.log(`${i + 1}. 区块 ${log.blockNumber}`) console.log(` 交易: ${log.transactionHash}`) console.log(` 数量: ${Number(value) / 1e6} USDC`) console.log() }) console.log(`总计存入: ${Number(totalSupplied) / 1e6} USDC`) } else { console.log('未找到 USDC 存入记录') } // 检查当前余额 console.log('\n=== 当前账户状态 ===\n') const LENDING_ABI = [ { inputs: [{ name: 'account', type: 'address' }], name: 'getBalance', outputs: [{ name: '', type: 'int256' }], stateMutability: 'view', type: 'function' }, { inputs: [{ name: 'account', type: 'address' }], name: 'borrowBalanceOf', outputs: [{ name: '', type: 'uint256' }], stateMutability: 'view', type: 'function' } ] const balance = await client.readContract({ address: LENDING_PROXY, abi: LENDING_ABI, functionName: 'getBalance', args: [USER] }) const borrowBalance = await client.readContract({ address: LENDING_PROXY, abi: LENDING_ABI, functionName: 'borrowBalanceOf', args: [USER] }) console.log('USDC 余额(存款):', balance > 0 ? `${Number(balance) / 1e6} USDC` : '0 USDC') console.log('借款余额:', Number(borrowBalance) / 1e6, 'USDC') console.log() if (balance > 0) { console.log('⚠️ 用户当前有 USDC 存款在 Lending 中') console.log(` 存款金额: ${Number(balance) / 1e6} USDC`) console.log() console.log('根据 Compound V3 设计:') console.log(' - withdraw() 会先从存款中扣除') console.log(` - 只有 withdraw 金额 > ${Number(balance) / 1e6} USDC 时,才会产生真正的借款`) } } catch (error) { console.error('查询失败:', error.message) } } main()