> ## Documentation Index
> Fetch the complete documentation index at: https://voltaire.tevm.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# Transaction.getChainId

> Extract chain ID from transaction

<Card title="Try it Live" icon="play" href="https://playground.tevm.sh?example=primitives/transaction.ts">
  Run Transaction examples in the interactive playground
</Card>

# getChainId

Extract chain ID from transaction.

<Tabs />

## Chain ID Extraction

### Typed Transactions

For typed transactions (EIP-2930, EIP-1559, EIP-4844, EIP-7702), chain ID is explicitly included:

```typescript theme={null}
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:

```typescript theme={null}
// EIP-155 formula: v = chainId * 2 + 35 + yParity
// Reverse: chainId = (v - 35 - yParity) / 2

// Example: Mainnet (chainId = 1)
v = 37 → chainId = (37 - 35) / 2 = 1  // yParity = 0
v = 38 → chainId = (38 - 35 - 1) / 2 = 1  // yParity = 1

// Example: Polygon (chainId = 137)
v = 309 → chainId = (309 - 35) / 2 = 137  // yParity = 0
v = 310 → chainId = (310 - 35 - 1) / 2 = 137  // yParity = 1
```

### Pre-EIP-155 Transactions

Pre-EIP-155 legacy transactions use simple v values (27 or 28):

```typescript theme={null}
const preEip155: Transaction.Legacy = {
  type: Transaction.Type.Legacy,
  v: 27n,  // yParity = 0, no chain ID
  // ...
}

getChainId(preEip155) // undefined
```

## Usage Patterns

### Replay Protection

```typescript theme={null}
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

```typescript theme={null}
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

```typescript theme={null}
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](/primitives/transaction/hashing) - Transaction and signing hash computation
* [Signing](/primitives/transaction/signing) - Signature verification and sender recovery
* [getGasPrice](/primitives/transaction/get-gas-price) - Get effective gas price
* [format](/primitives/transaction/format) - Format transaction for display

## References

* [EIP-155: Simple replay attack protection](https://eips.ethereum.org/EIPS/eip-155)
* [EIP-2718: Typed Transaction Envelope](https://eips.ethereum.org/EIPS/eip-2718)
* [Chain IDs](https://chainlist.org/) - Comprehensive list of EVM chain IDs
