nac-presale/WORKLOG_20260320_refactor.md

48 lines
1.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# TRC ICO 工作日志 - 2026-03-20
## 任务:重构 usePresale.ts 冗余代码
### 执行时间
2026-03-20 (UTC+8)
### 问题描述
usePresale.ts 文件中存在以下冗余代码:
1. buyWithUSDT 和 buyWithBNB 中各有一段完全相同的 TokensPurchased 事件解析逻辑约8行×2
2. 两个购买函数中各有一段完全相同的错误处理逻辑约3行×2
3. getUsdtBalance 和 buyWithUSDT 中各有一处重复的 network === "ETH" ? 6 : 18 判断
4. return 块中 calcTokens 重复 key已在上次任务中修复
### 解决方案
提取3个公共工具函数放置在 Hook 函数体外部(文件顶部):
| 函数名 | 作用 | 消除的冗余 |
|--------|------|-----------|
| getUsdtDecimals(network) | 返回 USDT decimals | 3处重复判断 → 1处定义 |
| extractPurchasedAmount(receipt, contract, default) | 解析 TokensPurchased 事件 | 2处重复代码块 → 1处定义 |
| extractErrorMessage(err) | 提取合约错误信息 | 2处重复代码块 → 1处定义 |
### 修改文件
- /www/wwwroot/nac-presale-test/client/src/hooks/usePresale.ts
### 备份文件
- /www/wwwroot/nac-presale-test/client/src/hooks/usePresale.ts.bak.refactor_20260320
### 构建结果
- 新 bundle: index-ChZj9Bh8.js (814.46 kB)
- 构建时间: 7.27s
- 1858 modules transformed
### 部署结果
- PM2 重启: nac-presale-test ✅
- HTTP 验证: 200 OK ✅
- __manus__ 内联: 已清除 ✅
### Git 提交
- commit: refactor: 重构 usePresale.ts提取公共函数消除冗余代码
- 推送到: nacadmin/nac-presale master
### 功能影响
- 无功能变化,所有导出接口保持不变
- Vite 构建警告消除
- 代码可维护性提升