117 lines
3.3 KiB
JavaScript
117 lines
3.3 KiB
JavaScript
|
|
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 USER = getAddress('0xa013422A5918CD099C63c8CC35283EACa99a705d')
|
|||
|
|
|
|||
|
|
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'
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
inputs: [],
|
|||
|
|
name: 'getBorrowRate',
|
|||
|
|
outputs: [{ name: '', type: 'uint64' }],
|
|||
|
|
stateMutability: 'view',
|
|||
|
|
type: 'function'
|
|||
|
|
}
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
async function main() {
|
|||
|
|
console.log('\n=== 验证当前借款状态 ===\n')
|
|||
|
|
console.log('用户:', USER)
|
|||
|
|
console.log('Lending 合约:', LENDING_PROXY)
|
|||
|
|
console.log()
|
|||
|
|
|
|||
|
|
// 获取账户余额
|
|||
|
|
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]
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
// 获取借款利率
|
|||
|
|
const borrowRate = await client.readContract({
|
|||
|
|
address: LENDING_PROXY,
|
|||
|
|
abi: LENDING_ABI,
|
|||
|
|
functionName: 'getBorrowRate'
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
console.log('=== 账户状态 ===')
|
|||
|
|
console.log()
|
|||
|
|
console.log('getBalance() 返回值:', balance.toString())
|
|||
|
|
if (balance > 0) {
|
|||
|
|
console.log(' → 用户有存款:', Number(balance) / 1e6, 'USDC')
|
|||
|
|
} else if (balance < 0) {
|
|||
|
|
console.log(' → 用户有负余额(债务):', Number(-balance) / 1e6, 'USDC')
|
|||
|
|
} else {
|
|||
|
|
console.log(' → 用户余额为 0')
|
|||
|
|
}
|
|||
|
|
console.log()
|
|||
|
|
|
|||
|
|
console.log('borrowBalanceOf() 返回值:', borrowBalance.toString())
|
|||
|
|
console.log(' → 借款金额:', Number(borrowBalance) / 1e6, 'USDC')
|
|||
|
|
console.log()
|
|||
|
|
|
|||
|
|
console.log('=== 利息信息 ===')
|
|||
|
|
console.log()
|
|||
|
|
console.log('借款年化利率 (APY):', (Number(borrowRate) / 1e18 * 100).toFixed(2), '%')
|
|||
|
|
console.log()
|
|||
|
|
|
|||
|
|
// 分析结果
|
|||
|
|
console.log('=== 分析 ===')
|
|||
|
|
console.log()
|
|||
|
|
|
|||
|
|
if (borrowBalance > 0n) {
|
|||
|
|
console.log('✓ 用户确实有借款!')
|
|||
|
|
console.log()
|
|||
|
|
console.log('借款金额:', Number(borrowBalance) / 1e6, 'USDC')
|
|||
|
|
console.log()
|
|||
|
|
|
|||
|
|
if (balance === 0n) {
|
|||
|
|
console.log('余额状态: balance = 0,这意味着:')
|
|||
|
|
console.log(' - 用户之前可能有存款')
|
|||
|
|
console.log(' - withdraw 金额超过了存款')
|
|||
|
|
console.log(' - 超出部分形成了债务')
|
|||
|
|
console.log()
|
|||
|
|
console.log('这正是 Compound V3 的设计:')
|
|||
|
|
console.log(' withdraw(amount) 会:')
|
|||
|
|
console.log(' 1. 优先从存款中扣除')
|
|||
|
|
console.log(' 2. 存款不足时,自动创建债务')
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
console.log()
|
|||
|
|
console.log('用户需要归还的总额:', Number(borrowBalance) / 1e6, 'USDC')
|
|||
|
|
console.log('(注意:每秒都在计息,实际金额会略高)')
|
|||
|
|
} else {
|
|||
|
|
console.log('✗ 用户当前没有借款')
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
main()
|