This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.
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:
Stack Output:
Gas Cost: 2 (GasQuickStep)
Operation:
Hardfork: Available from Istanbul onwards
Behavior
CHAINID pushes the chain identifier onto the stack as a 256-bit unsigned integer:
If called before Istanbul hardfork, the instruction is invalid and returns an error.
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 gas
NUMBER, TIMESTAMP, GASLIMIT: 2 gas
COINBASE: 2 gas
SELFBALANCE: 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
Edge Cases
Pre-Istanbul Execution
Uncommon Chain IDs
Maximum Chain ID
Known Chain IDs
Benchmarks
Performance:
- Hardfork check: O(1)
- Stack push: O(1)
Gas efficiency:
- 2 gas per query
- ~500,000 queries per million gas
References