主要更新: - 金库交易动态读取: 从合约动态获取所有金库地址和名称 - 边界值测试优化: 修复 buy_exceed_hardcap 计算逻辑和错误处理 - 新增 ErrorBoundary 组件: 全局错误边界处理 - vite 配置优化: 添加 optimizeDeps 解决动态导入问题 - 交易历史和连接按钮改进 技术改进: - 使用 useReadContract/useReadContracts 批量读取合约数据 - 改进 parseError 函数处理 ccip 模块加载失败 - 添加测试状态管理 (isTestRunning, testTypeRef) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
118 lines
3.8 KiB
TypeScript
118 lines
3.8 KiB
TypeScript
import { useState, useEffect } from 'react'
|
|
|
|
export type TransactionType = 'mint' | 'burn' | 'buy' | 'sell' | 'approve' | 'create_vault' | 'update_price' | 'test' | 'addLiquidity' | 'removeLiquidity' | 'swap'
|
|
|
|
export interface TransactionRecord {
|
|
id: string
|
|
type: TransactionType
|
|
hash: string
|
|
timestamp: number
|
|
status: 'pending' | 'success' | 'failed'
|
|
amount?: string
|
|
token?: string
|
|
vault?: string
|
|
error?: string
|
|
}
|
|
|
|
const STORAGE_KEY = 'yt_asset_tx_history'
|
|
const MAX_RECORDS = 50
|
|
|
|
export function useTransactionHistory() {
|
|
const [transactions, setTransactions] = useState<TransactionRecord[]>([])
|
|
|
|
// 从 localStorage 加载历史记录
|
|
useEffect(() => {
|
|
try {
|
|
const stored = localStorage.getItem(STORAGE_KEY)
|
|
if (stored) {
|
|
const parsed = JSON.parse(stored)
|
|
// 过滤并修复损坏的记录
|
|
const valid = Array.isArray(parsed)
|
|
? parsed
|
|
.filter((tx: any) => tx && typeof tx === 'object' && tx.id && typeof tx.id === 'string')
|
|
.map((tx: any) => ({
|
|
...tx,
|
|
// 确保所有字段都是正确类型
|
|
id: String(tx.id),
|
|
type: String(tx.type || 'test'),
|
|
hash: (typeof tx.hash === 'string' && tx.hash.startsWith('0x')) ? tx.hash : '',
|
|
timestamp: Number(tx.timestamp) || Date.now(),
|
|
status: ['pending', 'success', 'failed'].includes(tx.status) ? tx.status : 'failed',
|
|
amount: tx.amount ? String(tx.amount) : undefined,
|
|
token: tx.token ? String(tx.token) : undefined,
|
|
vault: tx.vault ? String(tx.vault) : undefined,
|
|
error: tx.error ? (typeof tx.error === 'string' ? tx.error : JSON.stringify(tx.error)) : undefined,
|
|
}))
|
|
: []
|
|
setTransactions(valid)
|
|
}
|
|
} catch (e) {
|
|
console.error('Failed to load transaction history:', e)
|
|
// 清除损坏的数据
|
|
localStorage.removeItem(STORAGE_KEY)
|
|
}
|
|
}, [])
|
|
|
|
// 保存到 localStorage
|
|
const saveToStorage = (records: TransactionRecord[]) => {
|
|
try {
|
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(records.slice(0, MAX_RECORDS)))
|
|
} catch (e) {
|
|
console.error('Failed to save transaction history:', e)
|
|
}
|
|
}
|
|
|
|
// 添加新交易
|
|
const addTransaction = (tx: Omit<TransactionRecord, 'id' | 'timestamp'>) => {
|
|
const newTx: TransactionRecord = {
|
|
type: tx.type,
|
|
hash: (typeof tx.hash === 'string' && tx.hash.startsWith('0x')) ? tx.hash : '',
|
|
status: tx.status,
|
|
amount: tx.amount ? String(tx.amount) : undefined,
|
|
token: tx.token ? String(tx.token) : undefined,
|
|
vault: tx.vault ? String(tx.vault) : undefined,
|
|
error: tx.error ? (typeof tx.error === 'string' ? tx.error : JSON.stringify(tx.error)) : undefined,
|
|
id: `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
|
|
timestamp: Date.now(),
|
|
}
|
|
setTransactions(prev => {
|
|
const updated = [newTx, ...prev].slice(0, MAX_RECORDS)
|
|
saveToStorage(updated)
|
|
return updated
|
|
})
|
|
return newTx.id
|
|
}
|
|
|
|
// 更新交易状态
|
|
const updateTransaction = (id: string, updates: Partial<TransactionRecord>) => {
|
|
// 确保 hash 是字符串
|
|
if (updates.hash && typeof updates.hash !== 'string') {
|
|
updates.hash = ''
|
|
}
|
|
// 确保 error 是字符串
|
|
if (updates.error && typeof updates.error !== 'string') {
|
|
updates.error = JSON.stringify(updates.error)
|
|
}
|
|
setTransactions(prev => {
|
|
const updated = prev.map(tx =>
|
|
tx.id === id ? { ...tx, ...updates } : tx
|
|
)
|
|
saveToStorage(updated)
|
|
return updated
|
|
})
|
|
}
|
|
|
|
// 清空历史记录
|
|
const clearHistory = () => {
|
|
setTransactions([])
|
|
localStorage.removeItem(STORAGE_KEY)
|
|
}
|
|
|
|
return {
|
|
transactions,
|
|
addTransaction,
|
|
updateTransaction,
|
|
clearHistory,
|
|
}
|
|
}
|