Overview
Opcode:0x46
Introduced: Istanbul (EIP-1344)
CHAINID retrieves the unique identifier for the current blockchain network. This enables contracts to implement replay protection and chain-specific behavior, preventing transactions from one chain being replayed on another.
Specification
Stack Input:Behavior
CHAINID pushes the chain identifier onto the stack as a 256-bit unsigned integer:Examples
Basic Usage
Pre-Istanbul Error
Chain Detection
Gas Cost
Cost: 2 gas (GasQuickStep) CHAINID is one of the cheapest operations in the EVM. Comparison:CHAINID: 2 gasNUMBER,TIMESTAMP,GASLIMIT: 2 gasCOINBASE: 2 gasSELFBALANCE: 5 gas
Common Usage
Chain-Specific Token Addresses
Cross-Chain Message Verification
Replay Protection
Chain-Specific Configuration
Multi-Chain Deployment Detection
Security Considerations
EIP-155 Replay Protection
CHAINID enables EIP-155 replay protection in transactions:Fork Safety
During chain forks, chainid prevents replay:Testnet vs Mainnet Safety
Cross-Chain Attack Prevention
Pre-Istanbul Compatibility
Implementation
- TypeScript
Edge Cases
Pre-Istanbul Execution
Uncommon Chain IDs
Maximum Chain ID
Known Chain IDs
Benchmarks
Performance:- Hardfork check: O(1)
- Stack push: O(1)
- 2 gas per query
- ~500,000 queries per million gas
Related Instructions
- COINBASE (0x41) - Get block producer
- NUMBER (0x43) - Get block number
- SELFBALANCE (0x47) - Get balance (also Istanbul+)
References
- EIP-1344 - ChainID opcode
- EIP-155 - Simple replay attack protection
- Yellow Paper - Section 9.3
- EVM Codes - CHAINID
- Chainlist - Comprehensive chain ID registry

