Skip to main content

Try it Live

Run Transaction examples in the interactive playground

getChainId

Extract chain ID from transaction.

    Chain ID Extraction

    Typed Transactions

    For typed transactions (EIP-2930, EIP-1559, EIP-4844, EIP-7702), chain ID is explicitly included:
    const tx: Transaction.EIP1559 = {
      type: Transaction.Type.EIP1559,
      chainId: 1n,  // Explicit field
      // ...
    }
    
    getChainId(tx) // 1n
    

    Legacy Transactions

    For legacy transactions, chain ID is encoded in the v value per EIP-155:
    // EIP-155 formula: v = chainId * 2 + 35 + yParity
    // Reverse: chainId = (v - 35 - yParity) / 2
    
    // Example: Mainnet (chainId = 1)
    v = 37chainId = (37 - 35) / 2 = 1  // yParity = 0
    v = 38chainId = (38 - 35 - 1) / 2 = 1  // yParity = 1
    
    // Example: Polygon (chainId = 137)
    v = 309chainId = (309 - 35) / 2 = 137  // yParity = 0
    v = 310chainId = (310 - 35 - 1) / 2 = 137  // yParity = 1
    

    Pre-EIP-155 Transactions

    Pre-EIP-155 legacy transactions use simple v values (27 or 28):
    const preEip155: Transaction.Legacy = {
      type: Transaction.Type.Legacy,
      v: 27n,  // yParity = 0, no chain ID
      // ...
    }
    
    getChainId(preEip155) // undefined
    

    Usage Patterns

    Replay Protection

    import { getChainId } from 'tevm/Transaction'
    import { TransactionError } from 'tevm/errors'
    
    function validateChainId(tx: Transaction.Any, expectedChainId: bigint) {
      const txChainId = getChainId(tx)
    
      if (txChainId !== undefined && txChainId !== expectedChainId) {
        throw new TransactionError(
          `Wrong chain: expected ${expectedChainId}, got ${txChainId}`,
          {
            code: 'WRONG_CHAIN_ID',
            context: { expected: expectedChainId, actual: txChainId }
          }
        )
      }
    }
    
    // Usage
    validateChainId(tx, 1n) // Mainnet
    

    Cross-Chain Detection

    import { getChainId } from 'tevm/Transaction'
    
    function detectChain(tx: Transaction.Any): string {
      const chainId = getChainId(tx)
    
      switch (chainId) {
        case 1n: return 'Ethereum Mainnet'
        case 137n: return 'Polygon'
        case 42161n: return 'Arbitrum One'
        case 10n: return 'Optimism'
        default: return chainId ? `Chain ${chainId}` : 'Pre-EIP-155'
      }
    }
    

    Transaction Filtering

    import { getChainId } from 'tevm/Transaction'
    
    function filterByChain(
      transactions: Transaction.Any[],
      targetChainId: bigint
    ): Transaction.Any[] {
      return transactions.filter(tx => {
        const chainId = getChainId(tx)
        return chainId === targetChainId
      })
    }
    
    // Get all mainnet transactions
    const mainnetTxs = filterByChain(allTransactions, 1n)
    

    See Also

    • Hashing - Transaction and signing hash computation
    • Signing - Signature verification and sender recovery
    • getGasPrice - Get effective gas price
    • format - Format transaction for display

    References