# EVM Gas & Transaction Patterns

Reference for understanding gas mechanics and common transaction patterns on EVM chains.

## Gas Basics

- **Gas Limit** — Maximum gas units you're willing to pay. Set by the user.
- **Gas Used** — Actual gas consumed by the transaction.
- **Gas Price** — Price per gas unit (in Gwei). Higher = faster confirmation.
- **Max Fee** (EIP-1559) — Maximum total fee per gas unit.
- **Priority Fee** (EIP-1559) — Tip to the validator for faster inclusion.
- **Base Fee** — Minimum gas price set by the network (burns).

## Common Gas Costs

| Operation | Approximate Gas |
|-----------|-----------------|
| Simple ETH transfer | 21,000 |
| ERC-20 transfer | 45,000-65,000 |
| ERC-20 approve | 45,000-50,000 |
| Uniswap V2 swap | 120,000-180,000 |
| Uniswap V3 swap | 100,000-150,000 |
| Multi-hop swap (3 pools) | 250,000-400,000 |
| NFT mint (ERC-721) | 80,000-150,000 |
| Aave supply | 200,000-300,000 |
| Aave borrow | 300,000-500,000 |
| Flash loan | 300,000-800,000 |
| Contract deployment | 500,000-5,000,000+ |

## Gas Utilization Thresholds

- **< 80%** — Normal, healthy margin.
- **80-95%** — Tight, consider increasing gas limit.
- **95-99%** — Dangerously close, likely to fail on slight changes.
- **≥ 99%** — Almost certainly ran out of gas. Transaction failed.

## Transaction Status Codes

- **0x1** — Success.
- **0x0** — Failed (reverted).
- **Pending** — Not yet mined.

## Function Selectors (4-byte prefixes)

Common DeFi function signatures and their 4-byte selectors:

| Selector | Function |
|----------|----------|
| 0xa9059cbb | transfer(address,uint256) |
| 0x23b872dd | transferFrom(address,address,uint256) |
| 0x095ea7b3 | approve(address,uint256) |
| 0x38ed1739 | swapExactTokensForTokens |
| 0x7ff36ab5 | swapExactETHForTokens |
| 0x18cbafe5 | swapExactTokensForETH |
| 0x5c11d795 | swapExact..FeeOnTransfer |
| 0x3593564c | execute (Universal Router) |
| 0x04e45aaf | exactInputSingle (V3) |
| 0xe8e33700 | addLiquidity |
| 0xf305d719 | addLiquidityETH |
| 0x2e1a7d4d | withdraw(uint256) |
| 0xd0e30db0 | deposit() |

## Event Topics (first 32 bytes of keccak256)

| Topic | Event |
|-------|-------|
| 0xddf252ad… | Transfer(address,address,uint256) |
| 0x8c5be1e5… | Approval(address,address,uint256) |
| 0xd78ad95f… | Swap (Uniswap V2) |
| 0xc42079f9… | Swap (Uniswap V3) |
| 0x1c411e9a… | Sync (Uniswap V2) |

## EIP-1559 Transaction Types

- **Type 0** — Legacy (gas price only).
- **Type 1** — Access list (EIP-2930).
- **Type 2** — EIP-1559 (maxFeePerGas + maxPriorityFeePerGas).
