A sophisticated Uniswap V4 hook that automatically routes swaps to the most cost-effective blockchain using Across Protocol, maximizing user savings through intelligent gas price optimization and cross-chain execution.
DeFi users lose significant value to gas fees when swapping on expensive chains. A typical 1 ETH swap on Ethereum mainnet can cost $50+ in gas fees, while the identical swap on Arbitrum costs ~$2. Our hook eliminates this inefficiency by automatically detecting optimal execution chains and seamlessly bridging assets for maximum cost savings.
graph TB
A[User Initiates Swap] --> B[Gas Optimization Hook]
B --> C[Gas Price Oracle]
B --> D[Cost Calculator]
D --> E{Savings > Threshold?}
E -->|Yes| F[Cross-Chain Manager]
E -->|No| G[Execute Local Swap]
F --> H[Across Protocol Bridge]
H --> I[Execute Swap on Target Chain]
I --> J[Bridge Back if Needed]
J --> K[Return Assets to User]
crosschain-gas-optimization-hook/
βββ src/
β βββ hooks/
β β βββ GasOptimizationHook.sol # Main hook implementation
β β βββ base/
β β βββ OptimizedBaseHook.sol # Enhanced base hook with gas optimizations
β βββ core/
β β βββ CrossChainManager.sol # Cross-chain execution coordinator
β β βββ GasPriceOracle.sol # Multi-chain gas price aggregator
β β βββ CostCalculator.sol # Total cost analysis engine
β β βββ HookConfiguration.sol # Configuration and governance
β βββ integrations/
β β βββ AcrossIntegration.sol # Across Protocol interface
β β βββ ChainlinkIntegration.sol # Chainlink price feed integration
β β βββ UniswapV4Integration.sol # Enhanced V4 pool interactions
β βββ libraries/
β β βββ ChainUtils.sol # Chain-specific utilities
β β βββ GasCalculations.sol # Gas estimation algorithms
β β βββ SafeBridging.sol # Bridge safety mechanisms
β β βββ TokenMapping.sol # Cross-chain token address mapping
β βββ interfaces/
β β βββ IGasOptimizationHook.sol # Main hook interface
β β βββ ICrossChainManager.sol # Cross-chain manager interface
β β βββ IGasPriceOracle.sol # Gas oracle interface
β β βββ ICostCalculator.sol # Cost calculator interface
β β βββ external/
β β βββ IAcrossProtocol.sol # Across Protocol interfaces
β β βββ IChainlinkAggregator.sol # Chainlink aggregator interfaces
β βββ storage/
β β βββ GasOptimizationStorage.sol # Hook storage layout
β β βββ CrossChainStorage.sol # Cross-chain state management
β βββ utils/
β βββ Errors.sol # Custom error definitions
β βββ Events.sol # Event definitions
β βββ Constants.sol # System constants
βββ test/
β βββ unit/
β β βββ GasOptimizationHook.t.sol # Hook unit tests
β β βββ GasPriceOracle.t.sol # Oracle unit tests
β β βββ CostCalculator.t.sol # Calculator unit tests
β β βββ CrossChainManager.t.sol # Manager unit tests
β βββ integration/
β β βββ CrossChainFlow.t.sol # End-to-end integration tests
β β βββ AcrossIntegration.t.sol # Across Protocol integration tests
β β βββ MultiChainScenarios.t.sol # Multi-chain test scenarios
β βββ fork/
β β βββ MainnetFork.t.sol # Mainnet fork tests
β β βββ ArbitrumFork.t.sol # Arbitrum fork tests
β β βββ CrossChainFork.t.sol # Cross-chain fork tests
β βββ mocks/
β β βββ MockSpokePool.sol # Mock Across spoke pool
β β βββ MockChainlinkOracle.sol # Mock Chainlink price feed
β β βββ MockPoolManager.sol # Mock Uniswap pool manager
β βββ utils/
β βββ TestConstants.sol # Test configuration constants
β βββ TestHelpers.sol # Common test utilities
β βββ ForkHelpers.sol # Fork testing utilities
βββ script/
β βββ deployment/
β β βββ Deploy.s.sol # Main deployment script
β β βββ DeployHook.s.sol # Hook-specific deployment
β β βββ ConfigureChains.s.sol # Multi-chain configuration
β β βββ VerifyContracts.s.sol # Contract verification
β βββ management/
β β βββ UpdateGasPrices.s.sol # Gas price update script
β β βββ ConfigureThresholds.s.sol # Threshold configuration
β β βββ EmergencyPause.s.sol # Emergency controls
β βββ utils/
β βββ NetworkConfig.s.sol # Network-specific configurations
β βββ DeploymentHelpers.s.sol # Deployment utilities
βββ docs/
β βββ architecture/
β β βββ ARCHITECTURE.md # System architecture documentation
β β βββ FLOW_DIAGRAMS.md # Process flow documentation
β β βββ SECURITY.md # Security considerations
β βββ integration/
β β βββ ACROSS_INTEGRATION.md # Across Protocol integration guide
β β βββ UNISWAP_V4.md # Uniswap V4 integration details
β β βββ CHAINLINK_ORACLES.md # Oracle integration guide
β βββ api/
β βββ HOOK_API.md # Hook API documentation
β βββ EXTERNAL_APIS.md # External API documentation
βββ config/
β βββ chains/
β β βββ ethereum.json # Ethereum configuration
β β βββ arbitrum.json # Arbitrum configuration
β β βββ optimism.json # Optimism configuration
β β βββ polygon.json # Polygon configuration
β β βββ base.json # Base configuration
β βββ tokens/
β β βββ token-mapping.json # Cross-chain token mappings
β βββ deployment/
β βββ testnet-config.json # Testnet deployment configuration
β βββ mainnet-config.json # Mainnet deployment configuration
βββ lib/ # Forge dependencies
βββ remappings.txt # Import path mappings
βββ foundry.toml # Foundry configuration
βββ package.json # Node.js dependencies
βββ .env.example # Environment variables template
βββ .gitignore
βββ LICENSE
βββ README.md
The primary hook contract that integrates with Uniswap V4's pool manager to intercept swaps and apply gas optimization logic.
Key Features:
- Implements
beforeSwaphook to analyze and potentially redirect transactions - Manages user preferences and optimization thresholds
- Coordinates with other system components for decision making
- Handles fallback mechanisms for failed cross-chain operations
Core Functions:
function beforeSwap(address sender, PoolKey calldata key, IPoolManager.SwapParams calldata params, bytes calldata hookData) external override returns (bytes4);
function getOptimizationQuote(IPoolManager.SwapParams calldata params) external view returns (OptimizationQuote memory);
function setUserPreferences(UserPreferences calldata preferences) external;Enhanced base hook implementation with gas optimizations and additional utility functions.
Key Features:
- Gas-optimized hook permission management
- Enhanced error handling and revert reasons
- Transient storage utilization for temporary state
- Batch operation support
Central coordinator for all cross-chain operations, managing the lifecycle of cross-chain swaps from initiation to completion.
Key Features:
- Cross-chain swap lifecycle management
- Bridge operation coordination with Across Protocol
- State synchronization across chains
- Recovery mechanisms for failed operations
Core Functions:
function initiateCrossChainSwap(CrossChainSwapParams calldata params) external returns (bytes32 swapId);
function handleDestinationSwap(bytes32 swapId, bytes calldata swapData) external;
function emergencyRecovery(bytes32 swapId) external;Multi-chain gas price aggregation system that provides real-time gas price data across supported networks.
Key Features:
- Simple on-chain gas price tracking with keeper updates
- Chainlink integration for USD value calculations
- Fallback price mechanisms
- Historical gas price tracking for trend analysis
Core Functions:
function getGasPrice(uint256 chainId) external view returns (uint256 gasPrice, uint256 timestamp);
function updateGasPrices(uint256[] calldata chainIds, uint256[] calldata gasPrices) external onlyKeeper;
function getGasPriceTrend(uint256 chainId, uint256 timeWindow) external view returns (GasTrend memory);Sophisticated cost analysis engine that determines the total cost of executing swaps on different chains.
Key Features:
- Total cost calculation (gas + bridge fees + slippage)
- USD value calculations using Chainlink price feeds
- Dynamic threshold adjustment based on market conditions
- MEV cost consideration
Core Functions:
function calculateTotalCost(CostParams calldata params) external view returns (TotalCost memory);
function findOptimalChain(OptimizationParams calldata params) external view returns (uint256 chainId, uint256 expectedSavingsUSD);
function updateCostParameters(CostParameters calldata newParams) external onlyOwner;Comprehensive integration with Across Protocol for seamless cross-chain asset bridging.
Key Features:
- Spoke pool interaction management
- Bridge fee calculation and optimization
- Relayer selection and management
- Bridge transaction monitoring and status tracking
Core Functions:
function bridgeTokens(BridgeParams calldata params) external returns (bytes32 bridgeId);
function getBridgeFee(address token, uint256 amount, uint256 destinationChain) external view returns (uint256 fee);
function getBridgeStatus(bytes32 bridgeId) external view returns (BridgeStatus memory);Integration with Chainlink's decentralized oracle network for reliable USD price feeds.
Key Features:
- ETH/USD, USDC/USD, and other token price feeds
- Multi-chain price feed support
- Price feed health monitoring
- Fallback price mechanisms for USD value calculations
Core Functions:
function getTokenPriceUSD(address token) external view returns (uint256 priceUSD);
function calculateSavingsUSD(uint256 gasSavingsWei) external view returns (uint256 savingsUSD);
function validatePriceFeed(address feed) external view returns (bool isValid);Enhanced integration utilities for Uniswap V4 pool interactions.
Key Features:
- Pool state analysis
- Liquidity depth assessment
- Slippage calculation
- Pool fee optimization
Advanced gas estimation and calculation algorithms using simple Solidity math.
Key Features:
- EIP-1559 gas price prediction
- Transaction cost modeling using basis points
- Gas usage optimization
- Network congestion impact analysis
Example Implementation:
function calculateSavingsPercent(uint256 originalCost, uint256 optimizedCost)
external pure returns (uint256) {
if (optimizedCost >= originalCost) return 0;
return ((originalCost - optimizedCost) * 10000) / originalCost; // basis points
}Chain-specific utilities and helper functions.
Key Features:
- Chain ID validation and mapping
- Block time and finality calculations
- Network-specific parameter management
- Chain health monitoring
Security-focused bridging utilities and safety mechanisms.
Key Features:
- Bridge transaction validation
- Anti-MEV protection mechanisms
- Bridge limit enforcement
- Emergency circuit breakers
Cross-chain token address mapping and validation.
Key Features:
- Token address resolution across chains
- Token metadata validation
- Supported token registry
- Token bridge compatibility checks
Centralized configuration management with governance controls.
Key Features:
- Parameter management with timelock
- Multi-signature governance integration
- Emergency parameter updates
- Configuration validation
Configuration Parameters:
struct HookConfig {
uint256 minSavingsThresholdBPS; // Minimum savings threshold (basis points)
uint256 minAbsoluteSavingsUSD; // Minimum absolute savings in USD
uint256 maxBridgeTimeSeconds; // Maximum acceptable bridge time
uint256 gasPriceUpdateInterval; // Gas price update frequency
uint256 emergencyPauseFlag; // Emergency pause mechanism
mapping(uint256 => ChainConfig) chainConfigs; // Per-chain configurations
}Optimized storage layouts using storage slots and packed structs for gas efficiency.
Key Features:
- Packed storage structures
- Storage slot optimization
- State transition tracking
- Historical data retention
- Real-time Analysis: Continuous monitoring of gas prices across 5+ major chains
- USD Value Display: Show users exact dollar savings using Chainlink price feeds
- Threshold Management: Configurable savings thresholds (percentage + absolute USD amounts)
- Across Protocol Integration: Leverage Across's fast, secure bridging infrastructure
- Automatic Asset Management: Handle token bridging and return flows transparently
- Fallback Mechanisms: Robust error handling and recovery procedures
- Multi-signature Governance: Decentralized parameter management
- Emergency Controls: Circuit breakers and pause mechanisms
- Bridge Security: Integration with battle-tested Across Protocol
- MEV Protection: Built-in protection against sandwich attacks
- USD Cost Tracking: Comprehensive cost analysis with Chainlink USD pricing
- Savings Reporting: Real-time and historical savings tracking in USD
- Performance Metrics: Bridge success rates, execution times, and user satisfaction
- Transparent Operations: Clear cost breakdowns and USD savings projections
- Flexible Configuration: User-customizable optimization preferences
- Mobile-Friendly: Optimized for mobile wallet integration
# Clone the repository
git clone https://github.com/your-org/crosschain-gas-optimization-hook
cd crosschain-gas-optimization-hook
# Install dependencies
forge install
# Install development dependencies (if using frontend)
npm install
# Copy environment configuration
cp .env.example .env
# Edit .env with your configuration# Essential dependencies
forge install Uniswap/v4-periphery --no-commit
forge install across-protocol/contracts --no-commit
forge install OpenZeppelin/openzeppelin-contracts --no-commit
forge install smartcontractkit/chainlink-brownie-contracts --no-commit
# Development & Testing
forge install foundry-rs/forge-std --no-commit
# Optional: Gas-optimized utilities
forge install transmissions11/solmate --no-commit# Build all contracts
forge build
# Run unit tests
forge test
# Run integration tests with mainnet fork
forge test --fork-url $ETHEREUM_RPC_URL --match-path "test/integration/*"
# Generate gas report
forge test --gas-report
# Coverage analysis
forge coverage# Deploy to testnet
forge script script/deployment/Deploy.s.sol --rpc-url $SEPOLIA_RPC_URL --broadcast --verify
# Deploy to mainnet (with additional confirmations)
forge script script/deployment/Deploy.s.sol --rpc-url $ETHEREUM_RPC_URL --broadcast --verify --confirmations 3π° User Action: Swap 10 ETH β USDC
π Analysis:
β’ Ethereum: 180 gwei ($450 gas cost)
β’ Arbitrum: 0.1 gwei ($2 gas cost)
β’ Bridge Cost: $8 via Across
π― Result: Net Savings: $440 (97.7% reduction)
β‘ Execution: Auto-route to Arbitrum
πΈ Display: "You saved $440.00! (0.22 ETH at $2,000/ETH)"
π° User Action: Swap 1000 USDC β ETH
π Optimal Route: Polygon (lowest gas) β Bridge back to Ethereum
π― Total Savings: $125 including bridge fees
β‘ Execution Time: 3 minutes via Across fast bridge
πΈ Display: "You saved $125.00 compared to Ethereum execution"
π° User Action: Swap $50 worth of LINK
π Analysis: 15% savings available but only $7.50 absolute
π― Decision: Execute locally (below $10 USD threshold)
πΈ Display: "Local execution selected - savings below threshold"
# Network RPC URLs
ETHEREUM_RPC_URL=https://eth-mainnet.g.alchemy.com/v2/YOUR_KEY
ARBITRUM_RPC_URL=https://arb-mainnet.g.alchemy.com/v2/YOUR_KEY
OPTIMISM_RPC_URL=https://opt-mainnet.g.alchemy.com/v2/YOUR_KEY
POLYGON_RPC_URL=https://polygon-mainnet.g.alchemy.com/v2/YOUR_KEY
BASE_RPC_URL=https://base-mainnet.g.alchemy.com/v2/YOUR_KEY
# API Keys
ETHERSCAN_API_KEY=your_etherscan_key
ARBISCAN_API_KEY=your_arbiscan_key
POLYGONSCAN_API_KEY=your_polygonscan_key
# Deployment
PRIVATE_KEY=your_private_key
DEPLOYER_ADDRESS=your_deployer_address
# Chainlink Price Feeds
CHAINLINK_ETH_USD_FEED=0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419
CHAINLINK_USDC_USD_FEED=0x8fFfFfd4AfB6115b954Bd326cbe7B4BA576818f6
GAS_PRICE_UPDATE_INTERVAL=300 # 5 minutesstruct SystemConfig {
uint256 minSavingsThresholdBPS; // 500 (5%)
uint256 minAbsoluteSavingsUSD; // $10 USD
uint256 maxBridgeTimeSeconds; // 1800 (30 minutes)
uint256 emergencyPauseDelay; // 86400 (24 hours)
bool enableCrossChainArbitrage; // true
bool enableUSDDisplayMode; // true (show USD savings)
}// Ethereum Mainnet
mapping(address => address) public priceFeeds;
constructor() {
// ETH/USD
priceFeeds[0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2] = 0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419;
// USDC/USD
priceFeeds[0xA0b86a33E6c4b4c2CC6C1C4CdBbD0D8C7b4e5d2A] = 0x8fFfFfd4AfB6115b954Bd326cbe7B4BA576818f6;
// LINK/USD
priceFeeds[0x514910771AF9Ca656af840dff83E8264EcF986CA] = 0x2c1d072e956AFFC0D435Cb7AC38EF18d24d9127c;
}- Contract Logic: Individual contract functionality
- Library Functions: Mathematical calculations using basis points
- Integration Points: Interface compatibility testing
- Cross-Chain Flows: End-to-end cross-chain swap execution
- Chainlink Integration: Price feed reliability and USD calculations
- Error Handling: Failure scenarios and recovery mechanisms
- Mainnet Simulation: Real-world conditions with actual liquidity
- Multi-Chain Scenarios: Cross-chain interactions with live protocols
- Gas Optimization: Actual gas usage analysis and optimization
- Gas Efficiency: Optimize contract gas usage
- Throughput: Handle multiple concurrent operations
- Latency: Minimize execution delays
- Total USD Saved: Cumulative dollar savings across all users
- User Adoption: Percentage of eligible swaps using optimization
- Bridge Success Rate: Successful cross-chain executions
- Average Savings: Mean percentage and absolute USD savings per swap
- Real-time Gas Prices: Live monitoring across all supported chains
- USD Conversion Rates: Chainlink price feed status and health
- Bridge Status: Across Protocol bridge health and performance
- User Activity: Swap volume and optimization usage patterns
- System Health: Contract status, oracle feeds, and error rates
- Basic hook functionality
- Simple gas price oracle with keeper updates
- Across Protocol bridging
- Cost calculation engine with USD display
- MEV protection mechanisms
- Batch optimization for multiple users
- Enhanced gas price prediction
- Advanced user preference management
- Integration with major wallet providers
- Mobile application support
- Additional bridge protocol support
- Governance token and incentive mechanisms
- Layer 2 expansion (zkSync, Starknet)
- Advanced arbitrage strategies
- Institutional-grade features
- White-label solution for other protocols
We welcome contributions from the community! Please see our Contributing Guidelines for details on:
- Code style and standards
- Testing requirements
- Pull request process
- Issue reporting
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Write tests for your changes
- Ensure all tests pass (
forge test) - Run linting and formatting (
forge fmt) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Uniswap Labs for the innovative V4 hook architecture
- Across Protocol for secure and efficient cross-chain infrastructure
- Chainlink for reliable USD price feeds and oracle services
- OpenZeppelin for security-focused smart contract libraries
- Foundry for excellent development tooling
Built with β€οΈ for the Uniswap Hook Incubator Hackathon π¦β‘
Revolutionizing DeFi through intelligent cross-chain optimization