DeFiDuck: Volatility-Aware TWAP Execution Hook
DeFiDuck is a v4 Hook that is creative in handling DeFi trading by helping users "duck" three major problems: market volatility, MEV exploitation, and high fees. It achieves this through intelligent multi-venue TWAP (Time-Weighted Average Price) coordination powered by real-time Pyth oracle data.
Core Innovation: DeFiDuck implements a volatility-aware decision engine that automatically adapts trading strategies based on real-time market conditions. When users attempt swaps, the Hook intercepts transactions and makes intelligent routing decisions:
Low Volatility (<2%): Normal Uniswap execution with dynamically reduced fees (0.25% vs standard 0.30%)
Medium Volatility (2-5%): Large swaps (>50 ETH) are automatically redirected to TWAP execution across multiple venues
High Volatility (5-10%): Lower threshold for TWAP activation (>25 ETH), enhanced fees for protection
Extreme Volatility (>10%): Circuit breaker activation - all direct swaps blocked, only TWAP orders accepted
Multi-Venue TWAP Coordination: The system coordinates execution across Uniswap v4 pool slices and 1inch limit orders. Large trades are automatically split into smaller chunks and executed over time to minimize price impact and slippage. For example, a 100 ETH swap becomes 10 chunks of 10 ETH executed over 5 minutes.
Anti-MEV Protection: Integration with Pyth Entropy provides on-chain randomness to randomize TWAP execution timing, preventing MEV bots from front-running predictable execution patterns.
Technical Architecture:
- VolatilityGuardHook: Core Uniswap v4 Hook contract with beforeSwap() logic
- 1inchTWAPCoordinator: Manages multi-venue order coordination and 1inch limit order creation
- SwapRouter: Custom router for executing swaps through the Hook system
- Real-time Oracle Integration: Pyth Network price feeds for volatility calculation
DeFiDuck is built as a Uniswap v4 Hook that intercepts and analyzes every swap attempt to make intelligent routing decisions based on real-time market volatility via Pyth Price Feeds and limit order protocol trading through 1inch.
The system consists of three main components working in concert:
- VolatilityGuardHook Contract:
- Implements the Uniswap v4 Hook interface as the primary entry point
- Integrates Pyth Network's pull oracle system for real-time price data
- Calculates volatility by comparing current Pyth prices against historical averages
- Uses a decision tree algorithm to determine execution strategy based on volatility thresholds
- Implements dynamic fee adjustment (0.25% low volatility, 0.30% normal, 0.50% high volatility)
- 1nchTWAPCoordinator Contract:
- Manages multi-venue execution coordination
- Creates 1inch limit orders using their Order struct format with proper salt generation
- Implements TWAP chunking logic (e.g., 100 ETH → 10 chunks of 10 ETH over 5 minutes)
- Handles cross-venue settlement and order tracking
- SwapRouter Contract:
- Custom router implementing Uniswap v4's unlock pattern with IUnlockCallback
- Manages token transfers and pool manager interactions
- Provides clean interface for executing swaps through the Hook system
Sponsor Partner Integration:
- Uniswap v4 Hooks:
- Used V4 hooks,
- Implemented proper Hook flags for beforeSwap functionality
- Leveraged PoolSwapTest and PoolModifyLiquidityTest for testing infrastructure
- Used CREATE2 deployment pattern for Hook address validation
- 1inch Protocol:
- Integrated 1inch Limit Order Protocol v3 structures
- Implemented proper Order struct with maker/taker assets, amounts, and traits
- Used 1inch router address 0x111111125421cA6dc452d289314280a0f8842A65 for order hashing
- Created production-ready limit order creation with proper salt and signature handling
- Pyth Network:
- Integrated Pyth pull oracle for real-time ETH/USD price feeds
- Implemented Pyth Entropy integration for anti-MEV randomization
- Price feed ID: 0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace
Notable Technical Achievements
- Hook Address Validation: The biggest technical challenge was Uniswap v4's strict Hook address validation. Hooks must be deployed at addresses with specific bit patterns. We solved this by:
- Using vm.etch() in tests to deploy Hook at correct addresses
- Implementing proper CREATE2 deployment scripts for production
- Creating comprehensive test suite that validates Hook logic without pool initialization
- Multi-Venue TWAP Algorithm: Implemented sophisticated TWAP execution logic:
- Real Transaction Demonstration: Created SwapScenarioDemo.s.sol that executes 8 real SwapRouter.swap() function calls on Unichain Sepolia, demonstrating:
- Small swaps with dynamic fees
- Large swaps triggering TWAP coordination
- Multiple TWAP chunks execution
- Circuit breaker activation
- High volatility protection
- Production-Ready Error Handling