import { useState, useEffect } from 'react' export type TransactionType = 'mint' | 'burn' | 'buy' | 'sell' | 'approve' | 'create_vault' | 'update_price' | 'test' 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([]) // 从 localStorage 加载历史记录 useEffect(() => { try { const stored = localStorage.getItem(STORAGE_KEY) if (stored) { setTransactions(JSON.parse(stored)) } } catch (e) { console.error('Failed to load transaction history:', e) } }, []) // 保存到 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) => { const newTx: TransactionRecord = { ...tx, 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) => { 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, } }