Overview
Gas optimization in Sardis:- Multi-Chain Gas Estimation: Real-time gas price tracking across all supported chains
- Intelligent Route Finding: Automatic selection of cheapest chain for transfers
- Batch Transactions: Combine multiple operations to reduce overhead
- Congestion Detection: Avoid high-gas periods automatically
packages/sardis-chain/src/sardis_chain/gas_optimizer.py
Gas Optimizer
Quick Start
Cost Comparison by Chain
Typical ERC-20 Transfer Costs
65,000 gas units (standard USDC transfer):| Chain | Gas Price | Cost (USD) | Time | Congestion |
|---|---|---|---|---|
| Base | 0.003 gwei | $0.0001 | 6s | Low |
| Optimism | 0.003 gwei | $0.0001 | 6s | Low |
| Arbitrum | 0.2 gwei | $0.001 | 3s | Low |
| Polygon | 50 gwei | $0.003 | 6s | Medium |
| Ethereum | 25 gwei | $0.05 | 36s | Medium |
examples/gas_optimizer_demo.py:15-40
Real-Time Cost Estimation
Intelligent Route Finding
Find Cheapest Chain
examples/gas_optimizer_demo.py:42-66):
Route Selection API
packages/sardis-chain/src/sardis_chain/gas_optimizer.py:33-43
Gas Price Monitoring
Track All Chains
examples/gas_optimizer_demo.py:69-90):
Congestion Detection
gas_optimizer.py:128-135):
Batch Transactions
Why Batch?
Single Transaction Overhead:- Base cost: 21,000 gas
- ERC-20 transfer: 65,000 gas
- Total: 86,000 gas
- Base cost: 21,000 gas (once)
- 3x ERC-20 transfers: 195,000 gas
- Total: 216,000 gas
- Savings: ~30% (258k → 216k)
Safe Batch API
Policy Module Considerations
Batched transactions are checked individually:Chain Selection Strategies
By Transaction Size
Microtransactions (< $10):- Best: Base, Optimism, Arbitrum
- Why: Sub-cent gas fees
- Cost: < 0.1% of transaction value
- Best: Base, Polygon, Optimism
- Why: Low gas, good token availability
- Cost: < 0.01% of transaction value
- Best: Ethereum
- Why: Maximum security, liquidity
- Cost: < 0.001% of transaction value
By Frequency
High-Frequency (> 100 tx/day):- Best: Arbitrum, Base
- Why: Fastest blocks (1-2s), lowest gas
- Example: AI agent making frequent small purchases
- Best: Base, Polygon
- Why: Good balance of cost and speed
- Example: E-commerce checkout agent
- Best: Any chain (optimize per transaction)
- Why: Gas costs negligible relative to transaction value
- Example: Monthly payroll distribution
By Token Availability
USDC-Only Operations:- Best: Base, Arc Testnet
- Why: Optimized for USDC (Arc uses USDC as gas token)
- Best: Polygon, Ethereum
- Why: Broadest token support (USDC, USDT, EURC, PYUSD)
Caching Strategy
Gas Price Cache
TTL: 30 seconds (configurable)gas_optimizer.py:296-323):
Cache Expiration
examples/gas_optimizer_demo.py:93-117
Production Integration
Automatic Chain Selection
Override Chain Selection
Congestion Avoidance
Cost Estimation Parameters
Gas Price Estimates
Realistic ranges by chain (gas_optimizer.py:60-71):
Block Times
Confirmation time = block_time × 3 blocksBridge Fees (Future)
Cross-chain transfer estimates:Gas Estimation API Reference
GasOptimizer Class
Data Classes
Example: Complete Demo
Run the full gas optimizer demo:examples/gas_optimizer_demo.py):
- Gas estimation for all chains (lines 15-40)
- Route finding and ranking (lines 42-66)
- Real-time price monitoring (lines 69-90)
- Cache behavior demonstration (lines 93-117)
Next Steps
Supported Chains
Chain configurations and RPC endpoints
Supported Tokens
Token availability per chain
Smart Contracts
Policy enforcement and batch transactions