Skip to main content

Try it Live

Run Transaction examples in the interactive playground

format

Format transaction for human-readable display.

Format Components

The formatted output includes:
  1. Transaction Type - Legacy, EIP-2930, EIP-1559, EIP-4844, or EIP-7702
  2. Operation - Transfer or contract creation
  3. Recipient - Address (shortened) or “contract creation”
  4. Value - ETH amount if non-zero
  5. Nonce - Transaction sequence number
  6. Data - Byte length if present

Format Patterns

Standard Transfer

"[Type] tx to [address], value: [ETH], nonce: [n]"
Example: "EIP-1559 tx to 0x742d35Cc..., value: 1 ETH, nonce: 5"

Contract Creation

"[Type] contract creation, data: [bytes] bytes, nonce: [n]"
Example: "EIP-1559 contract creation, data: 1234 bytes, nonce: 0"

Zero Value Transfer

"[Type] tx to [address], nonce: [n]"
Example: "Legacy tx to 0x..., nonce: 10"

With Data

"[Type] tx to [address], value: [ETH], data: [bytes] bytes, nonce: [n]"
Example: "EIP-1559 tx to 0x..., value: 0.1 ETH, data: 68 bytes, nonce: 2"

Usage Patterns

Logging

import { format } from 'tevm/Transaction'

function logTransaction(tx: Transaction.Any) {
  console.log(`Processing: ${format(tx)}`)
}

// Usage
logTransaction(tx)
// "Processing: EIP-1559 tx to 0x742d35Cc..., value: 1 ETH, nonce: 5"

User Notifications

import { format } from 'tevm/Transaction'

function notifyUser(tx: Transaction.Any) {
  const description = format(tx)
  alert(`Transaction sent: ${description}`)
}

Debug Output

import { format, hash } from 'tevm/Transaction'

function debugTransaction(tx: Transaction.Any) {
  const txHash = hash(tx)
  const description = format(tx)

  console.group('Transaction Debug')
  console.log('Description:', description)
  console.log('Hash:', Hex(txHash))
  console.log('Type:', tx.type)
  console.log('Gas Limit:', tx.gasLimit)
  console.groupEnd()
}

Transaction History

import { format, hash } from 'tevm/Transaction'

interface TransactionRecord {
  hash: string
  description: string
  timestamp: number
}

function recordTransaction(tx: Transaction.Any): TransactionRecord {
  return {
    hash: Hex(hash(tx)),
    description: format(tx),
    timestamp: Date.now()
  }
}

// Usage
const records: TransactionRecord[] = []
records.push(recordTransaction(tx))

// Display history
records.forEach(record => {
  console.log(`${record.description} at ${new Date(record.timestamp)}`)
})

Error Messages

import { format } from 'tevm/Transaction'
import { TransactionError } from 'tevm/errors'

// Use built-in TransactionError with context
throw new TransactionError('Insufficient balance', {
  code: 'INSUFFICIENT_BALANCE',
  context: { tx: format(tx) }
})
// Error: "Insufficient balance: EIP-1559 tx to 0x..., value: 10 ETH, nonce: 3"

Batch Processing

import { format } from 'tevm/Transaction'

function processBatch(transactions: Transaction.Any[]) {
  console.log(`Processing ${transactions.length} transactions:`)

  transactions.forEach((tx, i) => {
    console.log(`${i + 1}. ${format(tx)}`)
  })
}

// Output:
// Processing 3 transactions:
// 1. EIP-1559 tx to 0x..., value: 1 ETH, nonce: 0
// 2. Legacy tx to 0x..., value: 0.5 ETH, nonce: 1
// 3. EIP-1559 contract creation, data: 5000 bytes, nonce: 2

Custom Formatting

For more control, access fields directly:
import * as Address from 'tevm/Address'
import * as Hex from 'tevm/Hex'

function customFormat(tx: Transaction.Any): string {
  const type = tx.type === Transaction.Type.Legacy ? 'Legacy' :
               tx.type === Transaction.Type.EIP1559 ? 'EIP-1559' :
               `Type ${tx.type}`

  const to = tx.to ? Address.toChecksummed(tx.to) : 'Contract Creation'

  const value = tx.value > 0n ?
    ` | Value: ${tx.value / 10n**18n} ETH` : ''

  const data = tx.data.length > 0 ?
    ` | Data: ${Hex(tx.data).slice(0, 10)}...` : ''

  return `[${type}] To: ${to} | Nonce: ${tx.nonce}${value}${data}`
}

// Usage
console.log(customFormat(tx))
// "[EIP-1559] To: 0x742d35Cc... | Nonce: 5 | Value: 1 ETH"

Comparison with toString

Unlike JSON.stringify, format() provides human-readable output:
import { format } from 'tevm/Transaction'

// format() - human readable
console.log(format(tx))
// "EIP-1559 tx to 0x742d35Cc..., value: 1 ETH, nonce: 5"

// JSON.stringify() - machine readable
console.log(JSON.stringify(tx, null, 2))
// {
//   "type": 2,
//   "chainId": "1",
//   "nonce": "5",
//   ...
// }

See Also

References