import { createWalletClient, http, createPublicClient } from 'viem' import { privateKeyToAccount } from 'viem/accounts' import { arbitrumSepolia } from 'viem/chains' // 从环境变量或命令行参数获取私钥 const PRIVATE_KEY = process.env.PRIVATE_KEY || process.argv[2] if (!PRIVATE_KEY || !PRIVATE_KEY.startsWith('0x')) { console.error('❌ 请提供私钥:') console.error(' 方式1: export PRIVATE_KEY=0x...') console.error(' 方式2: node configure-collateral.js 0x...') process.exit(1) } const account = privateKeyToAccount(PRIVATE_KEY) const publicClient = createPublicClient({ chain: arbitrumSepolia, transport: http() }) const walletClient = createWalletClient({ account, chain: arbitrumSepolia, transport: http() }) const CONFIGURATOR = '0x488409CE9A3Fcd8EbD373dCb7e025cF8AB96fcdc' const LENDING_PROXY = '0xCb4E7B1069F6C26A1c27523ce4c8dfD884552d1D' const COLLATERAL_ASSETS = [ { name: 'YT-A', address: '0x97204190B35D9895a7a47aa7BaC61ac08De3cF05', collateralFactor: 7500, // 75% liquidationThreshold: 8500, // 85% liquidationBonus: 1000 // 10% }, { name: 'YT-B', address: '0x181ef4011c35C4a2Fda08eBC5Cf509Ef58E553fF', collateralFactor: 7500, liquidationThreshold: 8500, liquidationBonus: 1000 }, { name: 'YT-C', address: '0xE9A5b9f3a2Eda4358f81d4E2eF4f3280A664e5B0', collateralFactor: 7500, liquidationThreshold: 8500, liquidationBonus: 1000 } ] // Configurator ABI const CONFIGURATOR_ABI = [ { inputs: [ { internalType: 'address', name: '_asset', type: 'address' }, { internalType: 'uint256', name: '_collateralFactor', type: 'uint256' }, { internalType: 'uint256', name: '_liquidationThreshold', type: 'uint256' }, { internalType: 'uint256', name: '_liquidationBonus', type: 'uint256' } ], name: 'setCollateralConfig', outputs: [], stateMutability: 'nonpayable', type: 'function' }, { inputs: [ { internalType: 'address', name: '_asset', type: 'address' }, { internalType: 'bool', name: '_isActive', type: 'bool' } ], name: 'setCollateralActive', outputs: [], stateMutability: 'nonpayable', type: 'function' }, { inputs: [], name: 'owner', outputs: [{ internalType: 'address', name: '', type: 'address' }], stateMutability: 'view', type: 'function' } ] async function configureCollateral() { console.log('\n🔧 配置借贷抵押品\n') console.log('Configurator:', CONFIGURATOR) console.log('操作者:', account.address) console.log('') // 检查权限 try { const owner = await publicClient.readContract({ address: CONFIGURATOR, abi: CONFIGURATOR_ABI, functionName: 'owner' }) console.log('Configurator Owner:', owner) if (owner.toLowerCase() !== account.address.toLowerCase()) { console.error('\n❌ 错误: 当前账户不是 Configurator 的 owner') console.error(' 需要使用 owner 账户的私钥') process.exit(1) } } catch (error) { console.warn('⚠️ 无法检查owner,继续尝试配置...\n') } // 配置每个抵押品 for (const asset of COLLATERAL_ASSETS) { console.log(`\n📝 配置 ${asset.name} (${asset.address})`) console.log(` - 抵押率: ${asset.collateralFactor / 100}%`) console.log(` - 清算阈值: ${asset.liquidationThreshold / 100}%`) console.log(` - 清算奖励: ${asset.liquidationBonus / 100}%`) try { // 1. 设置抵押品参数 console.log(' → 设置参数...') const hash1 = await walletClient.writeContract({ address: CONFIGURATOR, abi: CONFIGURATOR_ABI, functionName: 'setCollateralConfig', args: [ asset.address, asset.collateralFactor, asset.liquidationThreshold, asset.liquidationBonus ] }) console.log(' ✅ 参数设置交易:', hash1) // 等待确认 await publicClient.waitForTransactionReceipt({ hash: hash1 }) console.log(' ✅ 交易已确认') // 2. 激活抵押品 console.log(' → 激活抵押品...') const hash2 = await walletClient.writeContract({ address: CONFIGURATOR, abi: CONFIGURATOR_ABI, functionName: 'setCollateralActive', args: [asset.address, true] }) console.log(' ✅ 激活交易:', hash2) // 等待确认 await publicClient.waitForTransactionReceipt({ hash: hash2 }) console.log(' ✅ 交易已确认') console.log(` ✅ ${asset.name} 配置完成!`) } catch (error) { console.error(` ❌ 配置失败:`, error.message.split('\n')[0]) // 继续处理下一个 continue } } console.log('\n✅ 所有抵押品配置完成!\n') console.log('现在可以尝试存入抵押品了。') } configureCollateral().catch((error) => { console.error('\n❌ 配置过程出错:', error.message) process.exit(1) })