Files
Heyue_test/frontend/scripts/single-buy-test.ts
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

119 lines
3.9 KiB
TypeScript
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.

/**
* 单账户买入测试脚本
* 使用主账户直接执行买入,不需要分发 ETH
*/
import {
createPublicClient,
createWalletClient,
http,
parseUnits,
formatUnits,
type Address,
type Hex
} from 'viem'
import { arbitrumSepolia } from 'viem/chains'
import { privateKeyToAccount } from 'viem/accounts'
const CONTRACTS = {
WUSD: '0x6d2bf81a631dFE19B2f348aE92cF6Ef41ca2DF98' as Address,
VAULT_YT_A: '0x0cA35994F033685E7a57ef9bc5d00dd3cf927330' as Address,
}
const MAIN_PRIVATE_KEY = '0xa082a7037105ebd606bee80906687e400d89899bbb6ba0273a61528c2f5fab89' as Hex
const WUSD_ABI = [
{ inputs: [{ name: 'spender', type: 'address' }, { name: 'amount', type: 'uint256' }], name: 'approve', outputs: [{ type: 'bool' }], stateMutability: 'nonpayable', type: 'function' },
{ inputs: [{ name: 'account', type: 'address' }], name: 'balanceOf', outputs: [{ type: 'uint256' }], stateMutability: 'view', type: 'function' },
] as const
const VAULT_ABI = [
{ inputs: [{ name: '_wusdAmount', type: 'uint256' }], name: 'depositYT', outputs: [], stateMutability: 'nonpayable', type: 'function' },
{ inputs: [{ name: '_wusdAmount', type: 'uint256' }], name: 'previewBuy', outputs: [{ type: 'uint256' }], stateMutability: 'view', type: 'function' },
{ inputs: [{ name: 'account', type: 'address' }], name: 'balanceOf', outputs: [{ type: 'uint256' }], stateMutability: 'view', type: 'function' },
{ inputs: [], name: 'symbol', outputs: [{ type: 'string' }], stateMutability: 'view', type: 'function' },
] as const
async function main() {
console.log('🚀 单账户买入测试\n')
const publicClient = createPublicClient({
chain: arbitrumSepolia,
transport: http('https://sepolia-rollup.arbitrum.io/rpc'),
})
const account = privateKeyToAccount(MAIN_PRIVATE_KEY)
const walletClient = createWalletClient({
account,
chain: arbitrumSepolia,
transport: http('https://sepolia-rollup.arbitrum.io/rpc'),
})
console.log(`📍 账户: ${account.address}`)
// 检查余额
const ethBalance = await publicClient.getBalance({ address: account.address })
const wusdBalance = await publicClient.readContract({
address: CONTRACTS.WUSD,
abi: WUSD_ABI,
functionName: 'balanceOf',
args: [account.address],
})
console.log(`⛽ ETH 余额: ${formatUnits(ethBalance, 18)} ETH`)
console.log(`💰 WUSD 余额: ${formatUnits(wusdBalance, 18)} WUSD`)
// 获取金库 symbol
const vaultSymbol = await publicClient.readContract({
address: CONTRACTS.VAULT_YT_A,
abi: VAULT_ABI,
functionName: 'symbol',
})
console.log(`🏦 目标金库: ${vaultSymbol}`)
// 买入金额
const buyAmount = parseUnits('50', 18) // 50 WUSD
console.log(`\n📝 买入金额: ${formatUnits(buyAmount, 18)} WUSD`)
// 预览
const previewYT = await publicClient.readContract({
address: CONTRACTS.VAULT_YT_A,
abi: VAULT_ABI,
functionName: 'previewBuy',
args: [buyAmount],
})
console.log(`📊 预计获得: ${formatUnits(previewYT, 18)} ${vaultSymbol}`)
// 执行买入
console.log('\n1⃣ 授权 WUSD...')
const approveHash = await walletClient.writeContract({
address: CONTRACTS.WUSD,
abi: WUSD_ABI,
functionName: 'approve',
args: [CONTRACTS.VAULT_YT_A, buyAmount],
})
await publicClient.waitForTransactionReceipt({ hash: approveHash })
console.log(` ✓ 授权成功: ${approveHash}`)
console.log('\n2⃣ 执行买入...')
const buyHash = await walletClient.writeContract({
address: CONTRACTS.VAULT_YT_A,
abi: VAULT_ABI,
functionName: 'depositYT',
args: [buyAmount],
})
await publicClient.waitForTransactionReceipt({ hash: buyHash })
console.log(` ✓ 买入成功: ${buyHash}`)
// 检查结果
const ytBalance = await publicClient.readContract({
address: CONTRACTS.VAULT_YT_A,
abi: VAULT_ABI,
functionName: 'balanceOf',
args: [account.address],
})
console.log(`\n✅ ${vaultSymbol} 余额: ${formatUnits(ytBalance, 18)}`)
}
main().catch(console.error)