Selector
A Selector is a 4-byte identifier used to uniquely identify Ethereum function calls. It’s computed as the first 4 bytes of the Keccak-256 hash of the function signature.Type Definition
Creating Selectors
From Signature String
The most common way to create a selector is from a function signature:From Hex String
From Bytes
Operations
Convert to Hex
Compare Selectors
Common Selectors
ERC-20
transfer(address,uint256)→0xa9059cbbapprove(address,uint256)→0x095ea7b3balanceOf(address)→0x70a08231totalSupply()→0x18160ddd
ERC-721
transferFrom(address,address,uint256)→0x23b872ddsafeTransferFrom(address,address,uint256)→0x42842e0eownerOf(uint256)→0x6352211e
Signature Format
Function signatures must use canonical type names: ✅ Correct:transfer(address,uint256)swap(uint256,uint256,address,bytes)execute((address,uint256,bytes)[])
transfer(address, uint256)(has spaces)transfer(address,uint)(should be uint256)Transfer(address,uint256)(wrong capitalization)
How Selectors Work
- Function Signature: Start with the canonical function signature
- Hash: Compute
keccak256(signature) - Truncate: Take the first 4 bytes
Selector Collisions
While rare, different function signatures can theoretically produce the same selector (collision). In practice, this is extremely unlikely due to the cryptographic properties of Keccak-256. If a collision occurs:- One function will override the other in the contract
- The EVM will use whichever function is defined first
- This is considered a critical contract bug
Usage in Contracts
Selectors are used in:- Function Calls: First 4 bytes of transaction data identify the function
- Low-level Calls:
address.call(abi.encodeWithSelector(selector, args)) - Function Routing: Contracts use selectors to route calls to the correct function
API Reference
Constructors
fromBytes(bytes: []const u8) !Selector- Create from raw bytesfromHex(hex: []const u8) !Selector- Create from hex stringfromSignature(signature: []const u8) Selector- Compute from function signature
Operations
toHex(selector: Selector) [10]u8- Convert to hex string (0x + 8 hex chars)equals(a: Selector, b: Selector) bool- Compare selectors
See Also
- FunctionSignature - Extended selector with metadata
- EventSignature - 32-byte event topic
- ErrorSignature - 4-byte error selector
- ABI Encoding - Encoding function calls

