diff --git a/docs/base-chain/network-information/gas-optimization b/docs/base-chain/network-information/gas-optimization new file mode 100644 index 000000000..dfe8cafe8 --- /dev/null +++ b/docs/base-chain/network-information/gas-optimization @@ -0,0 +1,74 @@ +--- +title: Gas Optimization on Base +description: Practical techniques to minimize costs and improve UX when building on Base. +--- + +# Gas Optimization on Base + +Base offers significantly lower fees than Ethereum L1, but optimizing further improves user experience, especially for high-volume apps, agents, or mini-apps. Focus on happy-path patterns with real examples. + +## Core Principles + +- **Batch where possible**: Reduce per-transaction overhead. +- **Use efficient data types and patterns**: Avoid unnecessary storage writes. +- **Leverage Base-specific features**: Flashblocks for faster/previewable inclusion, accurate fee estimation. +- **Client-side estimation**: Always simulate before sending. + +Fees are dynamic. Always estimate on-chain; Base's block production differs from L1. See [Network Fees](/base-chain/network-information/network-fees). + +## Step-by-Step Techniques + +### 1. Accurate Gas Estimation + +Use `eth_estimateGas` or libraries with Base RPCs. Prefer `base` public endpoints or providers. + +```typescript +// filename: estimate-gas.ts +import { createPublicClient, http, parseEther } from 'viem' +import { base } from 'viem/chains' + +const client = createPublicClient({ + chain: base, + transport: http() +}) + +async function estimateTransfer(to: `0x${string}`, value: bigint) { + const gas = await client.estimateGas({ + account: '0xYourAddress', + to, + value + }) + console.log(`Estimated gas: ${gas}`) + return gas +} +``` + +Combine with `eth_feeHistory` for priority fees. See [RPC API](/base-chain/api-reference/rpc-overview) + +### 2. Batching Transactions + +Use multicall or account abstractions (Base Account / Smart Wallets) for batched ops. + +**Example with viem + Base Account patterns** (link to sub-accounts/spend permissions docs). + +### 3. Contract-Level Optimizations + +- Use `immutable`/`constant` where possible. + +- Minimize storage slots (pack variables). + +- Prefer events over storage for offchain indexing. + +- For agents: Use efficient loops and avoid reverts in hot paths. + +### 4. Flashblocks-Aware Patterns + +Preview transactions or react to partial blocks for better UX in high-throughput scenarios. (Link to Flashblocks section). + +## Common Pitfalls & Troubleshooting + +- **Nonce gaps**: Handle carefully with replacements. See [Troubleshooting Transactions](/base-chain/network-information/troubleshooting-transactions). + +- **Over-estimation**: Add small buffer but avoid excess. + +- **Legacy vs EIP-1559**: Base supports modern types; use `maxFeePerGas`/`maxPriorityFeePerGas`.