Gas optimizer

From XSpoonAi/spoon-awesome-skill · View interactive version

Gas optimizer is an AI agent skill from the GitHub repository XSpoonAi/spoon-awesome-skill. Advanced gas optimization tool for Ethereum and EVM-compatible chains. Analyzes transactions, simulates gas usage, suggests optimal gas prices, batches transactions, and implements gas-saving patterns for smart contract interactions.

How to use this skill

Skill file

Usage Examples

1. Real-Time Gas Price Monitoring

from scripts.gas_estimator import GasEstimator

estimator = GasEstimator()
prices = estimator.get_gas_prices(chain_id=1)

print(f"Fast: {prices['fast']} Gwei")
print(f"Standard: {prices['standard']} Gwei")
print(f"Economical: {prices['economical']} Gwei")
print(f"Base Fee: {prices['base_fee']} Gwei")

Output:

Fast: 0.05 Gwei
Standard: 0.04 Gwei
Economical: 0.04 Gwei
Base Fee: 0.03 Gwei

2. Estimate Transaction Cost

estimate = estimator.estimate_gas(
    to_address="0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D",
    data="0xa9059cbb...",
    value=0,
    chain_id=1
)

print(f"Gas Estimate: {estimate['gas_estimate']} units")
print(f"Cost: {estimate['cost_usd']} USD")

Output:

Gas Estimate: 52108 units
Cost: 0.01 USD

3. Batch Multiple Transactions

from scripts.transaction_batcher import TransactionBatcher

batcher = TransactionBatcher()
transactions = [
    {"to": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", "data": "0x095ea7b3..."},
    {"to": "0xdAC17F958D2ee523a2206206994597C13D831ec7", "data": "0x095ea7b3..."},
    {"to": "0x6B175474E89094C44Da98b954EedeAC495271d0F", "data": "0x095ea7b3..."}
]

result = batcher.batch_transactions(transactions, chain_id=1)
print(f"Transactions batched: {result['transactions_batched']}")
print(f"Gas savings: {result['gas_estimates']['savings_percentage']}%")

Output:

Transactions batched: 3
Gas savings: 30.77%

4. Compare L2 Network Costs

from scripts.l2_comparison import L2GasComparison

l2_comp = L2GasComparison()
comparison = l2_comp.compare_transaction_costs(
    transaction_type='swap',
    networks=['ethereum', 'arbitrum', 'optimism', 'polygon']
)

for network in comparison['comparison']:
    print(f"{network['network']}: ${network['total_cost_usd']}")

Output:

Polygon: $0.01
Ethereum Mainnet: $0.02
Arbitrum One: $0.23
Optimism: $0.35

5. Optimize Contract Call

from scripts.gas_optimizer import GasOptimizer

optimizer = GasOptimizer()
optimization = optimizer.optimize_contract_call(
    contract_address="0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984",
    function_signature="transfer(address,uint256)",
    params=["0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb", 1000000]
)

print(f"Estimated gas: {optimization['analysis']['estimated_gas']}")
print(f"Potential savings: {optimization['total_potential_savings']} gas")

6. Analyze Transaction History

analysis = optimizer.analyze_transaction_pattern(
    address="0x28C6c06298d514Db089934071355E5743bf21d60",
    num_transactions=100
)

print(f"Average gas used: {analysis['statistics']['average_gas_used']}")
print(f"Failed transactions: {analysis['statistics']['failed_transactions']}")
print(f"Total potential savings: {analysis['total_potential_savings_gas']} gas")

7. Get Smart Gas Strategy

strategy = optimizer.suggest_optimal_gas_strategy(
    transaction_type='swap',
    urgency='medium'
)

print(f"Recommended gas price: {strategy['recommended_strategy']['total_gas_price_gwei']} Gwei")
print(f"Estimated cost: {strategy['recommended_strategy']['estimated_cost_eth']} ETH")
print(f"Wait time: {strategy['recommended_strategy']['estimated_wait_time']}")

8. Calculate Bridge Costs

bridge_cost = l2_comp.get_bridge_costs(
    from_network='ethereum',
    to_network='arbitrum',
    amount_usd=1000.0
)

print(f"Bridge fee: ${bridge_cost['costs']['total_cost_usd']}")
print(f"Estimated time: {bridge_cost['estimated_time']}")

9. Batch Token Approvals

tokens = [
    "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",  # USDC
    "0xdAC17F958D2ee523a2206206994597C13D831ec7",  # USDT
    "0x6B175474E89094C44Da98b954EedeAC495271d0F"   # DAI
]

result = batcher.create_token_approval_batch(
    token_addresses=tokens,
    spender="0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D",
    amount=2**256 - 1,
    chain_id=1
)

print(f"Approvals batched: {result['transactions_batched']}")
print(f"Gas savings: {result['gas_estimates']['savings_percentage']}%")

10. Check Multicall Support

is_supported = batcher.is_multicall_supported(chain_id=1)
print(f"Multicall3 supported: {is_supported}")

Output Format

All modules return structured JSON with consistent format:

{
  "success": true,
  "timestamp": "2026-02-10T14:25:30Z",
  "chain_id": 1,
  "gas_estimate": 52108,
  "gas_price_gwei": 0.04,
  "cost_eth": "0.00000208",
  "cost_usd": "0.01",
  "optimization_level": "standard",
  "gas_saved": 60000,
  "savings_percentage": 30.77,
  "recommendations": [
    "Gas prices are historically low in 2026",
    "Consider batching multiple transactions"
  ],
  "execution_time_ms": 1250
}

Error Handling

All errors return structured format:

{
  "success": false,
  "error": "Failed to fetch gas prices",
  "error_code": "API_ERROR",
  "timestamp": "2026-02-10T14:25:30Z",
  "retry_after": 60
}

Common Error Codes:

Network Support

| Network | Chain ID | Status | RPC | Gas Token |

|---------|----------|--------|-----|-----------|

| Ethereum | 1 | ✅ Active | Infura/Alchemy | ETH |

| Polygon | 137 | ✅ Active | Public RPC | MATIC |

| Arbitrum | 42161 | ✅ Active | Public RPC | ETH |

| Optimism | 10 | ✅ Active | Public RPC | ETH |

| Base | 8453 | ✅ Active | Public RPC | ETH |

| zkSync Era | 324 | ✅ Active | Public RPC | ETH |

Gas Savings Table

| Operation | Individual Gas | Batched Gas | Savings |

|-----------|----------------|-------------|---------|

| 2 Token Approvals | 130,000 | 95,000 | 26.9% |

| 3 Token Approvals | 195,000 | 135,000 | 30.8% |

| 5 Token Approvals | 325,000 | 215,000 | 33.8% |

| 10 Token Approvals | 650,000 | 415,000 | 36.2% |

| 2 ERC20 Transfers | 130,000 | 98,000 | 24.6% |

| 5 ERC20 Transfers | 325,000 | 230,000 | 29.2% |

Transaction Types Gas Estimates

| Type | Base Gas | Complexity | Use Case |

|------|----------|------------|----------|

| ETH Transfer | 21,000 | Simple | Wallet to wallet |

| ERC20 Transfer | 65,000 | Low | Token transfer |

| Uniswap Swap | 150,000 | Medium | DEX trading |

| NFT Mint | 100,000 | Medium | NFT creation |

| Aave Deposit | 200,000 | High | DeFi lending |

| Complex DeFi | 300,000+ | Very High | Multi-protocol |

Severity Levels

| Level | Gas Price Range | Cost Impact | Recommendation |

|-------|-----------------|-------------|----------------|

| OPTIMAL | < 5 Gwei | Very Low ($0.01-0.05) | Execute immediately |

| LOW | 5-20 Gwei | Low ($0.05-0.20) | Good time to execute |

| MEDIUM | 20-50 Gwei | Moderate ($0.20-0.50) | Consider waiting |

| HIGH | 50-100 Gwei | High ($0.50-1.00) | Wait for better price |

| CRITICAL | > 100 Gwei | Very High ($1.00+) | Delay if not urgent |

2026 Context: Gas prices are historically low (0.04-0.05 Gwei) due to EIP-4844 blob transactions and improved network efficiency.

Best Practices

Gas Optimization Strategies

  1. Batch Transactions: Use Multicall3 to combine multiple calls (25-35% savings)
  2. Off-Peak Timing: Execute during weekends or early UTC hours
  3. L2 Networks: Use Arbitrum, Optimism, or Polygon for 95%+ savings
  4. EIP-1559: Set appropriate base fee + priority fee for faster inclusion
  5. Simulation: Always simulate transactions before execution
  6. Monitoring: Set up alerts for optimal gas price windows

Security Best Practices

  1. Never expose private keys or sensitive data
  2. Validate all transaction data before signing
  3. Use secure RPC endpoints (Infura, Alchemy)
  4. Implement maximum gas price limits
  5. Monitor for front-running on large transactions
  6. Use multicall's allowFailure flag for non-critical operations
  7. Verify contract addresses before interaction
  8. Test on testnets before mainnet deployment

Integration Guidelines

  1. Cache gas prices for 30-60 seconds to reduce API calls
  2. Implement retry logic with exponential backoff
  3. Use multiple RPC providers for failover
  4. Handle rate limits gracefully
  5. Log all optimization decisions for auditing
  6. Set realistic timeout values (10-30 seconds)
  7. Validate API responses before processing

Configuration

Recommended Settings

# Gas Optimizer Configuration
CONFIG = {
    "cache_ttl": 60,  # seconds
    "max_retries": 3,
    "timeout": 10,  # seconds
    "default_slippage": 0.5,  # percentage
    "max_gas_price_gwei": 100,
    "priority_fee_percentile": 50,
    "enable_multicall": True,
    "enable_l2_comparison": True
}

Network-Specific Settings

NETWORK_CONFIG = {
    1: {  # Ethereum
        "rpc": "https://eth.llamarpc.com",
        "gas_buffer": 1.1,  # 10% buffer
        "confirmation_blocks": 1
    },
    137: {  # Polygon
        "rpc": "https://polygon-rpc.com",
        "gas_buffer": 1.2,  # 20% buffer
        "confirmation_blocks": 3
    },
    42161: {  # Arbitrum
        "rpc": "https://arb1.arbitrum.io/rpc",
        "gas_buffer": 1.05,  # 5% buffer
        "confirmation_blocks": 1
    }
}

Version & Support

Changelog

v1.0.0 (2026-02-10)

Known Limitations

  1. API Dependencies: Relies on Etherscan and RPC provider availability
  2. Rate Limits: Free tier API keys have call limitations
  3. Estimation Accuracy: 95% accuracy, can vary with complex contracts
  4. Network Latency: 1-3 second response time depending on network
  5. Chain Support: Limited to EVM-compatible chains
  6. Historical Data: Etherscan API limited to 10,000 transactions per query

Troubleshooting

Common Issues

Issue: "API rate limit exceeded"

Issue: "RPC connection failed"

Issue: "Gas estimation failed"

Issue: "Multicall not supported"

Issue: "Price data unavailable"

Support & Community

Testing

Unit Tests

pytest tests/test_gas_estimator.py -v
pytest tests/test_transaction_batcher.py -v
pytest tests/test_gas_optimizer.py -v
pytest tests/test_l2_comparison.py -v

Integration Tests

# Requires valid API keys
export ETHERSCAN_API_KEY="your_key"
export INFURA_KEY="your_key"
pytest tests/integration/ -v

Performance Tests

pytest tests/performance/test_response_time.py -v

Contributors

References

Last Updated: February 10, 2026

Maintainer: SpoonOS Community

Status: ✅ Production Ready

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.