Files
Heyue_test/frontend/scripts/simulate-deposit.js
Sofio 5cb64f881e feat: 添加多链支持和 Lending 借贷系统
- 新增 ARB Sepolia + BNB Testnet 多链支持
- 添加 LendingPanel 借贷系统组件
- 添加 LendingAdminPanel 管理面板
- 添加 USDCPanel USDC 操作组件
- 添加 HoldersPanel 持有人信息组件
- 添加 AutoTestPanel 自动化测试组件
- 重构 LP 组件为模块化结构 (LP/)
- 添加多个调试和测试脚本
- 修复 USDC 精度动态配置
- 优化合约配置支持多链切换

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-26 23:32:29 +08:00

135 lines
4.6 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { createPublicClient, http, encodeFunctionData, formatUnits } from 'viem'
import { arbitrumSepolia } from 'viem/chains'
const client = createPublicClient({
chain: arbitrumSepolia,
transport: http()
})
const LENDING_PROXY = '0xCb4E7B1069F6C26A1c27523ce4c8dfD884552d1D'
const YT_A = '0x97204190B35D9895a7a47aa7BaC61ac08De3cF05'
const USER = '0xa013422a5918cd099c63c8cc35283eaca99a705d'
const CONFIGURATOR = '0x488409CE9A3Fcd8EbD373dCb7e025cF8AB96fcdc'
const LENDING_ABI = [
{
inputs: [
{ internalType: 'address', name: '_collateralAsset', type: 'address' },
{ internalType: 'uint256', name: '_amount', type: 'uint256' }
],
name: 'deposit',
outputs: [],
stateMutability: 'nonpayable',
type: 'function'
}
]
const CONFIGURATOR_ABI = [
{
inputs: [{ internalType: 'address', name: '_asset', type: 'address' }],
name: 'getCollateralConfig',
outputs: [
{ internalType: 'bool', name: 'isActive', type: 'bool' },
{ internalType: 'uint256', name: 'collateralFactor', type: 'uint256' },
{ internalType: 'uint256', name: 'liquidationThreshold', type: 'uint256' },
{ internalType: 'uint256', name: 'liquidationBonus', type: 'uint256' }
],
stateMutability: 'view',
type: 'function'
},
{
inputs: [{ internalType: 'address', name: '', type: 'address' }],
name: 'collateralConfigs',
outputs: [
{ internalType: 'bool', name: 'isActive', type: 'bool' },
{ internalType: 'uint256', name: 'collateralFactor', type: 'uint256' },
{ internalType: 'uint256', name: 'liquidationThreshold', type: 'uint256' },
{ internalType: 'uint256', name: 'liquidationBonus', type: 'uint256' }
],
stateMutability: 'view',
type: 'function'
}
]
async function simulateDeposit() {
console.log('🔍 模拟存款操作...\n')
console.log('合约地址:', LENDING_PROXY)
console.log('抵押品地址:', YT_A)
console.log('用户地址:', USER)
console.log('存款金额: 10 YT-A\n')
// 1. 检查 Configurator 中的配置
console.log('=== 检查 Configurator 配置 ===')
try {
const config = await client.readContract({
address: CONFIGURATOR,
abi: CONFIGURATOR_ABI,
functionName: 'getCollateralConfig',
args: [YT_A]
})
console.log('✅ 通过 getCollateralConfig 读取:')
console.log(' - 是否激活:', config[0])
console.log(' - 抵押率:', Number(config[1]) / 100 + '%')
console.log(' - 清算阈值:', Number(config[2]) / 100 + '%')
console.log(' - 清算奖励:', Number(config[3]) / 100 + '%')
} catch (error) {
console.log('❌ getCollateralConfig 失败:', error.message.split('\n')[0])
// 尝试直接读取mapping
try {
const config = await client.readContract({
address: CONFIGURATOR,
abi: CONFIGURATOR_ABI,
functionName: 'collateralConfigs',
args: [YT_A]
})
console.log('✅ 通过 collateralConfigs mapping 读取:')
console.log(' - 是否激活:', config[0])
console.log(' - 抵押率:', Number(config[1]) / 100 + '%')
console.log(' - 清算阈值:', Number(config[2]) / 100 + '%')
console.log(' - 清算奖励:', Number(config[3]) / 100 + '%')
if (!config[0]) {
console.log('\n⚠ 警告: 抵押品未激活!这就是存款失败的原因。')
}
} catch (error2) {
console.log('❌ collateralConfigs mapping 也失败:', error2.message.split('\n')[0])
}
}
// 2. 模拟调用 deposit
console.log('\n=== 模拟存款调用 ===')
const depositAmount = 10n * 10n ** 18n // 10 YT-A
try {
await client.simulateContract({
address: LENDING_PROXY,
abi: LENDING_ABI,
functionName: 'deposit',
args: [YT_A, depositAmount],
account: USER
})
console.log('✅ 模拟存款成功!交易应该可以执行。')
} catch (error) {
console.log('❌ 模拟存款失败')
console.log('\n详细错误信息:')
console.log(error.message)
// 尝试解析错误原因
if (error.message.includes('Collateral not active')) {
console.log('\n💡 原因: 抵押品未激活')
console.log(' 需要管理员通过 Configurator 激活此抵押品')
} else if (error.message.includes('insufficient')) {
console.log('\n💡 原因: 余额或授权不足')
} else if (error.message.includes('paused')) {
console.log('\n💡 原因: 合约已暂停')
} else {
console.log('\n💡 这可能是由于抵押品未在借贷合约中配置')
}
}
console.log('\n=== 诊断完成 ===\n')
}
simulateDeposit().catch(console.error)