Skip to main content

Try it Live

Run Transaction examples in the interactive playground

hasAccessList

Check if transaction has an access list.

    Transaction Types with Access Lists

    TypeHas Access ListNotes
    Legacy (0x00)NoPre-EIP-2930
    EIP-2930 (0x01)YesIntroduced access lists
    EIP-1559 (0x02)YesInherits from EIP-2930
    EIP-4844 (0x03)YesBlob transactions include access lists
    EIP-7702 (0x04)YesAuthorization transactions include access lists

    Usage Patterns

    Conditional Access List Processing

    import { hasAccessList, getAccessList } from 'tevm/Transaction'
    
    function processTransaction(tx: Transaction.Any) {
      if (hasAccessList(tx)) {
        const accessList = getAccessList(tx)
        prewarmStorage(accessList)
      }
    
      executeTransaction(tx)
    }
    

    Type-Safe Access List Access

    import { hasAccessList } from 'tevm/Transaction'
    
    function analyzeAccessList(tx: Transaction.Any) {
      if (!hasAccessList(tx)) {
        return { addresses: 0, storageKeys: 0 }
      }
    
      // Safe to access accessList
      const list = getAccessList(tx)
      return {
        addresses: list.length,
        storageKeys: list.reduce(
          (sum, item) => sum + item.storageKeys.length,
          0
        )
      }
    }
    

    Transaction Classification

    import { hasAccessList } from 'tevm/Transaction'
    
    function classifyTransaction(tx: Transaction.Any): string {
      if (!hasAccessList(tx)) {
        return 'legacy'
      }
    
      switch (tx.type) {
        case Transaction.Type.EIP2930:
          return 'eip2930-access-list'
        case Transaction.Type.EIP1559:
          return 'eip1559-dynamic-fee'
        case Transaction.Type.EIP4844:
          return 'eip4844-blob'
        case Transaction.Type.EIP7702:
          return 'eip7702-authorization'
        default:
          return 'unknown'
      }
    }
    

    Gas Cost Analysis

    import { hasAccessList, getAccessList } from 'tevm/Transaction'
    
    function calculateAccessListGasCost(tx: Transaction.Any): bigint {
      if (!hasAccessList(tx)) {
        return 0n
      }
    
      const accessList = getAccessList(tx)
      let cost = 0n
    
      for (const item of accessList) {
        cost += 2400n  // Per address
        cost += BigInt(item.storageKeys.length) * 1900n  // Per storage key
      }
    
      return cost
    }
    

    Filtering Transactions

    import { hasAccessList } from 'tevm/Transaction'
    
    function filterTransactionsWithAccessLists(
      transactions: Transaction.Any[]
    ): Transaction.Any[] {
      return transactions.filter(hasAccessList)
    }
    
    // Get only legacy transactions
    function filterLegacyTransactions(
      transactions: Transaction.Any[]
    ): Transaction.Legacy[] {
      return transactions.filter(
        (tx): tx is Transaction.Legacy => !hasAccessList(tx)
      )
    }
    

    Type Guard Pattern

    The hasAccessList function works well as a type guard:
    import { hasAccessList, getAccessList } from 'tevm/Transaction'
    
    function processWithAccessList(tx: Transaction.Any) {
      // Type guard narrows tx type
      if (hasAccessList(tx)) {
        // TypeScript knows tx has accessList field
        const list = tx.accessList
        // or use helper
        const accessList = getAccessList(tx)
      }
    }
    

    See Also

    References