OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-wallet / cmd / history.js

113 lines · javascript ref:
// cmd/history.js - Transaction History Command

/**
 * History command handler
 * @param {WalletContext} context - Wallet context
 * @param {Object} enabledProtocols - Enabled protocol objects
 * @returns {Array} - Command definition
 */
export default function (context, enabledProtocols) {
  return [
    async function handler(args) {
      // Parse arguments
      // Format: history [protocol] [chain] [limit]
      // Examples:
      //   history
      //   history wdk
      //   history wdk ethereum
      //   history wdk ethereum 10

      let protocol, chain, limit = 50

      if (args.length >= 3) {
        protocol = args[0]
        chain = args[1]
        limit = parseInt(args[2]) || 50
      } else if (args.length === 2) {
        protocol = args[0]
        chain = args[1]
      } else if (args.length === 1) {
        protocol = args[0]
      }

      console.log('')
      console.log('📜 Transaction History')

      if (protocol) {
        // Get history from specific protocol
        const protocolHandler = context.protocols[protocol]
        if (!protocolHandler) {
          console.error(`❌ Protocol "${protocol}" not available`)
          console.error(`   Available protocols: ${Object.keys(context.protocols).join(', ')}`)
          process.exit(1)
        }

        const txs = await protocolHandler.history({ limit, chain })

        if (txs.length === 0) {
          console.log('')
          console.log('   No transactions found')
          console.log('')
          return
        }

        console.log(`   Protocol: ${protocol}`)
        if (chain) console.log(`   Chain: ${chain}`)
        console.log(`   Showing ${txs.length} transaction(s)`)
        console.log('')

        // Display transactions
        txs.forEach((tx, index) => {
          const date = new Date(tx.timestamp).toLocaleString()
          const type = tx.type === 'send' ? '📤' : '📥'
          console.log(`   ${index + 1}. ${type} ${tx.type.toUpperCase()}`)
          console.log(`      Date: ${date}`)
          console.log(`      Chain: ${tx.chain}`)
          console.log(`      Amount: ${tx.amount}`)
          if (tx.to) console.log(`      To: ${tx.to}`)
          if (tx.from) console.log(`      From: ${tx.from}`)
          if (tx.hash) console.log(`      Hash: ${tx.hash}`)
          if (tx.memo) console.log(`      Memo: ${tx.memo}`)
          console.log('')
        })
      } else {
        // Get history from all protocols
        let totalTxs = 0

        for (const [name, protocolHandler] of Object.entries(context.protocols)) {
          const txs = await protocolHandler.history({ limit })

          if (txs.length > 0) {
            console.log(`   Protocol: ${name}`)
            console.log(`   Transactions: ${txs.length}`)
            console.log('')

            txs.slice(0, 5).forEach((tx) => {
              const date = new Date(tx.timestamp).toLocaleString()
              const type = tx.type === 'send' ? '📤' : '📥'
              console.log(`     ${type} ${date} - ${tx.amount} (${tx.chain})`)
            })

            if (txs.length > 5) {
              console.log(`     ... and ${txs.length - 5} more`)
            }

            console.log('')
            totalTxs += txs.length
          }
        }

        if (totalTxs === 0) {
          console.log('')
          console.log('   No transactions found')
          console.log('')
        } else {
          console.log(`   Total: ${totalTxs} transaction(s)`)
          console.log('')
          console.log('   Run "hyper-wallet history <protocol>" for details')
        }
      }
    }
  ]
}