Overview
Accurate gas estimation is crucial for successful transaction execution. The OKX DEX SDK provides methods to estimate gas limits and retrieve current gas prices, including EIP-1559 support.Gas Price Estimation
Get current gas prices for any supported chain.Basic Gas Price
EIP-1559 Gas Pricing
For chains supporting EIP-1559 (Ethereum, Polygon, Base, etc.):EIP-1559 uses a base fee (burned) + priority fee (to validators) model. The
maxFeePerGas includes both components.Gas Limit Estimation
Estimate the gas required for a specific transaction.Simple ETH Transfer
Contract Interaction
Complete Gas Estimation Example
Using Swap Data Gas Estimates
ThegetSwapData method already includes gas estimates:
The SDK automatically includes appropriate gas estimates in swap transaction data. You can use these values directly or verify them with
getGasLimit.Gas Optimization Tips
Add Safety Buffer
Always add a buffer to gas estimates:EIP-1559 Priority Fees
Adjust priority fees based on urgency:Monitor Gas Prices
For time-sensitive transactions, monitor gas prices:Chain-Specific Considerations
Ethereum Mainnet
- High gas costs during peak hours
- Use EIP-1559 for better estimates
- Consider L2 alternatives
Layer 2 Networks (Base, Arbitrum, Optimism)
- Significantly lower gas costs
- Faster confirmation times
- Still use EIP-1559
BSC and Polygon
- Lower gas costs than Ethereum
- May use legacy gas pricing or EIP-1559
API Reference
getGasPrice
Parameters:chainIndex- Chain identifier (e.g., ‘1’ for Ethereum, ‘8453’ for Base)
baseFee- Current base fee (EIP-1559 chains)maxFeePerGas- Maximum fee per gas unitmaxPriorityFeePerGas- Maximum priority fee per gasgasPrice- Legacy gas price (non-EIP-1559 chains)
getGasLimit
Parameters:chainIndex- Chain identifierfromAddress- Sender addresstoAddress- Recipient or contract addresstxAmount- Transaction value in weiextJson- Extended JSON with optionalinputDatafor contract calls
gasLimit- Estimated gas limit for the transaction
Next Steps
Transaction Broadcasting
Broadcast transactions with optimal gas settings
Executing Swaps
Execute swaps with proper gas configuration