54 lines
2.1 KiB
JavaScript
54 lines
2.1 KiB
JavaScript
|
|
// 验证 APY 计算逻辑
|
|||
|
|
|
|||
|
|
const borrowRatePerSecond = 14999999976144000n
|
|||
|
|
const secondsPerYear = BigInt(365 * 24 * 60 * 60) // 31536000
|
|||
|
|
|
|||
|
|
console.log('\n=== APY 计算验证 ===\n')
|
|||
|
|
|
|||
|
|
console.log('原始数据:')
|
|||
|
|
console.log(' borrowRatePerSecond:', borrowRatePerSecond.toString())
|
|||
|
|
console.log(' secondsPerYear:', secondsPerYear.toString())
|
|||
|
|
console.log()
|
|||
|
|
|
|||
|
|
// 方法1:错误的计算(之前的方式)
|
|||
|
|
const wrongAPY = Number(borrowRatePerSecond) / 10000
|
|||
|
|
console.log('❌ 错误计算(除以10000):')
|
|||
|
|
console.log(' APY:', wrongAPY.toFixed(2), '%')
|
|||
|
|
console.log(' = 1,499,999,997,614.40 %(明显错误)')
|
|||
|
|
console.log()
|
|||
|
|
|
|||
|
|
// 方法2:正确的计算
|
|||
|
|
// APY% = (ratePerSecond / 1e18) × secondsPerYear × 100
|
|||
|
|
const borrowAPY = (borrowRatePerSecond * secondsPerYear * BigInt(100)) / BigInt(10 ** 18)
|
|||
|
|
console.log('✅ 正确计算(Compound V3 格式):')
|
|||
|
|
console.log(' 每秒利率:', Number(borrowRatePerSecond) / 1e18)
|
|||
|
|
console.log(' 年化倍数 (1 + rate)^seconds ≈ rate × seconds:')
|
|||
|
|
console.log(' ', Number(borrowRatePerSecond) / 1e18, '× 31,536,000 =', Number(borrowRatePerSecond) * 31536000 / 1e18)
|
|||
|
|
console.log(' APY:', Number(borrowAPY).toFixed(2), '%')
|
|||
|
|
console.log()
|
|||
|
|
|
|||
|
|
// 详细计算步骤
|
|||
|
|
console.log('计算步骤:')
|
|||
|
|
console.log(' 1. borrowRatePerSecond × secondsPerYear × 100')
|
|||
|
|
console.log(' =', borrowRatePerSecond.toString(), '×', secondsPerYear.toString(), '× 100')
|
|||
|
|
const step1 = borrowRatePerSecond * secondsPerYear * BigInt(100)
|
|||
|
|
console.log(' =', step1.toString())
|
|||
|
|
console.log()
|
|||
|
|
console.log(' 2. 结果 ÷ 10^18')
|
|||
|
|
console.log(' =', step1.toString(), '÷ 1000000000000000000')
|
|||
|
|
console.log(' =', borrowAPY.toString())
|
|||
|
|
console.log()
|
|||
|
|
|
|||
|
|
console.log('最终结果: ', Number(borrowAPY).toFixed(2), '%')
|
|||
|
|
console.log()
|
|||
|
|
|
|||
|
|
// 验证使用率计算
|
|||
|
|
console.log('=== 使用率计算验证 ===\n')
|
|||
|
|
console.log('如果 utilizationRate = 0(当前没有借款)')
|
|||
|
|
const utilizationRate = 0n
|
|||
|
|
const utilizationPercent = utilizationRate > 0n
|
|||
|
|
? (utilizationRate * BigInt(100)) / BigInt(10 ** 18)
|
|||
|
|
: 0n
|
|||
|
|
console.log(' 使用率:', Number(utilizationPercent).toFixed(2), '%')
|
|||
|
|
console.log()
|