← Back to home

DeFiDuck

DeFiDuck: Helping users duck volatility, MEV, and high fees since ETH Global New Delhi 2025!

Problem Statement

DeFiDuck: Volatility-Aware TWAP Execution HookDeFiDuck 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 acceptedMulti-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() logic1inchTWAPCoordinator: Manages multi-venue order coordination and 1inch limit order creationSwapRouter: Custom router for executing swaps through the Hook systemReal-time Oracle Integration: Pyth Network price feeds for volatility calculation

Solution

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 pointIntegrates Pyth Network's pull oracle system for real-time price dataCalculates volatility by comparing current Pyth prices against historical averagesUses a decision tree algorithm to determine execution strategy based on volatility thresholdsImplements dynamic fee adjustment (0.25% low volatility, 0.30% normal, 0.50% high volatility)1nchTWAPCoordinator Contract:Manages multi-venue execution coordinationCreates 1inch limit orders using their Order struct format with proper salt generationImplements TWAP chunking logic (e.g., 100 ETH → 10 chunks of 10 ETH over 5 minutes)Handles cross-venue settlement and order trackingSwapRouter Contract:Custom router implementing Uniswap v4's unlock pattern with IUnlockCallbackManages token transfers and pool manager interactionsProvides clean interface for executing swaps through the Hook systemSponsor Partner Integration:Uniswap v4 Hooks:Used V4 hooks,Implemented proper Hook flags for beforeSwap functionalityLeveraged PoolSwapTest and PoolModifyLiquidityTest for testing infrastructureUsed CREATE2 deployment pattern for Hook address validation1inch Protocol:Integrated 1inch Limit Order Protocol v3 structuresImplemented proper Order struct with maker/taker assets, amounts, and traitsUsed 1inch router address 0x111111125421cA6dc452d289314280a0f8842A65 for order hashingCreated production-ready limit order creation with proper salt and signature handlingPyth Network:Integrated Pyth pull oracle for real-time ETH/USD price feedsImplemented Pyth Entropy integration for anti-MEV randomizationPrice feed ID: 0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0aceNotable Technical AchievementsHook 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 addressesImplementing proper CREATE2 deployment scripts for productionCreating comprehensive test suite that validates Hook logic without pool initializationMulti-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 feesLarge swaps triggering TWAP coordinationMultiple TWAP chunks executionCircuit breaker activationHigh volatility protectionProduction-Ready Error Handling

Hackathon

ETHGlobal New Delhi

2025

Prizes

  • 🏆

    Extend Limit Order Protocol1st place

    1inch

Contributors