119 lines
3.9 KiB
TypeScript
119 lines
3.9 KiB
TypeScript
|
|
/**
|
|||
|
|
* 单账户买入测试脚本
|
|||
|
|
* 使用主账户直接执行买入,不需要分发 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)
|