|
| 1 | +--- |
| 2 | +title: Comptime Selectors |
| 3 | +description: Compile-time function selectors and event topics -- a key differentiator of eth.zig. |
| 4 | +--- |
| 5 | + |
| 6 | +One of eth.zig's most powerful features is **comptime-first design**. Function selectors and event topics are computed at compile time, eliminating runtime hashing entirely. |
| 7 | + |
| 8 | +## Function Selectors |
| 9 | + |
| 10 | +A Solidity function selector is the first 4 bytes of the Keccak-256 hash of the function signature. In eth.zig, this is done at compile time: |
| 11 | + |
| 12 | +```zig |
| 13 | +const eth = @import("eth"); |
| 14 | +
|
| 15 | +// Computed at compile time -- zero runtime cost |
| 16 | +const transfer_sel = eth.abi_comptime.comptimeSelector("transfer(address,uint256)"); |
| 17 | +// transfer_sel == [4]u8{ 0xa9, 0x05, 0x9c, 0xbb } |
| 18 | +
|
| 19 | +const approve_sel = eth.abi_comptime.comptimeSelector("approve(address,uint256)"); |
| 20 | +// approve_sel == [4]u8{ 0x09, 0x5e, 0xa7, 0xb3 } |
| 21 | +``` |
| 22 | + |
| 23 | +The Zig compiler evaluates `comptimeSelector` during compilation. The resulting binary contains only the precomputed 4-byte selectors -- no hashing at runtime. |
| 24 | + |
| 25 | +## Event Topics |
| 26 | + |
| 27 | +Event topics (used for log filtering) work the same way: |
| 28 | + |
| 29 | +```zig |
| 30 | +const eth = @import("eth"); |
| 31 | +
|
| 32 | +const transfer_topic = eth.abi_comptime.comptimeTopic("Transfer(address,address,uint256)"); |
| 33 | +// transfer_topic == keccak256("Transfer(address,address,uint256)") |
| 34 | +
|
| 35 | +const approval_topic = eth.abi_comptime.comptimeTopic("Approval(address,address,uint256)"); |
| 36 | +``` |
| 37 | + |
| 38 | +## Why This Matters |
| 39 | + |
| 40 | +In other Ethereum libraries, function selectors are typically computed at runtime: |
| 41 | + |
| 42 | +| Approach | When It Runs | Cost | |
| 43 | +|----------|-------------|------| |
| 44 | +| eth.zig `comptimeSelector` | Compile time | 0 ns at runtime | |
| 45 | +| Runtime `keccak256(signature)` | Every call | ~128 ns per hash | |
| 46 | +| Cached/lazy hash | First call | ~128 ns once, then lookup | |
| 47 | + |
| 48 | +For hot paths (MEV bots, indexers, high-frequency DeFi), eliminating per-call hashing overhead adds up. |
| 49 | + |
| 50 | +## ERC-20 Precomputed Selectors |
| 51 | + |
| 52 | +The ERC-20 wrapper provides all standard selectors as comptime constants: |
| 53 | + |
| 54 | +```zig |
| 55 | +const selectors = eth.erc20.selectors; |
| 56 | +
|
| 57 | +selectors.name; // name() |
| 58 | +selectors.symbol; // symbol() |
| 59 | +selectors.decimals; // decimals() |
| 60 | +selectors.totalSupply; // totalSupply() |
| 61 | +selectors.balanceOf; // balanceOf(address) |
| 62 | +selectors.transfer; // transfer(address,uint256) |
| 63 | +selectors.approve; // approve(address,uint256) |
| 64 | +selectors.allowance; // allowance(address,address) |
| 65 | +selectors.transferFrom; // transferFrom(address,address,uint256) |
| 66 | +``` |
| 67 | + |
| 68 | +## Custom Selectors |
| 69 | + |
| 70 | +Define selectors for any contract function: |
| 71 | + |
| 72 | +```zig |
| 73 | +const eth = @import("eth"); |
| 74 | +
|
| 75 | +// UniswapV2 Router |
| 76 | +const swap_sel = eth.abi_comptime.comptimeSelector( |
| 77 | + "swapExactTokensForTokens(uint256,uint256,address[],address,uint256)" |
| 78 | +); |
| 79 | +
|
| 80 | +// Aave V3 Pool |
| 81 | +const supply_sel = eth.abi_comptime.comptimeSelector( |
| 82 | + "supply(address,uint256,address,uint16)" |
| 83 | +); |
| 84 | +
|
| 85 | +// Any custom function |
| 86 | +const my_sel = eth.abi_comptime.comptimeSelector( |
| 87 | + "myFunction(uint256,bytes32,bool)" |
| 88 | +); |
| 89 | +``` |
0 commit comments