Hook Contracts Developer Guide

Hook Contracts

Few Protocol provides specialized Uniswap V4 Hook contracts that enable seamless integration between Few Wrapped Tokens and Uniswap V4 pools. These hooks automatically handle token wrapping/unwrapping during trading operations.

Overview

Hook contracts act as middleware between Uniswap V4 pools and Few Protocol's wrapped tokens, providing:

  • Automatic Token Wrapping: Converts underlying tokens to Few Wrapped Tokens during liquidity provision

  • Seamless Trading: Users can trade with underlying tokens while the pool operates with wrapped tokens

  • Gas Optimization: Reduces transaction costs by batching operations

  • Enhanced Liquidity: Enables deeper liquidity pools with wrapped token benefits

Available Hook Contracts

FewTokenHook

Purpose: Universal hook for ERC20 tokens (USDC, DAI, CBBTC, etc.)

Features:

  • Supports all standard ERC20 tokens except USDT

  • 1:1 wrapping/unwrapping ratio

  • Automatic approval management

  • Compatible with most modern tokens

Use Cases:

  • USDC ↔ fwUSDC pools

  • DAI ↔ fwDAI pools

  • CBBTC ↔ fwCBBTC pools

  • Any standard ERC20 ↔ Few Wrapped Token pools

FewETHHook

Purpose: Specialized hook for ETH/WETH integration

Features:

  • Native ETH support

  • WETH compatibility

  • Optimized for Ethereum's native currency

  • Gas-efficient ETH handling

Use Cases:

  • ETH ↔ fwETH pools

  • WETH ↔ fwETH pools

  • Native Ethereum trading pairs

FewUSDTHook

Purpose: Specialized hook for USDT integration

Features:

  • SafeERC20 compatibility for USDT's non-standard approve function

  • Handles USDT's unique implementation quirks

  • Secure approval management

Use Cases:

  • USDT ↔ fwUSDT pools

  • Legacy token compatibility

How Hook Contracts Work

1. Pool Initialization

When a Uniswap V4 pool is created with a Hook contract:

Pool: Token A / Few Wrapped Token B (with Hook)

2. Liquidity Provision

Users provide liquidity with underlying tokens:

User provides: Token A + Token B
Hook converts: Token A β†’ Few Wrapped Token A, Token B β†’ Few Wrapped Token B
Pool receives: Few Wrapped Token A + Few Wrapped Token B

3. Trading Operations

Users trade with underlying tokens seamlessly:

User swaps: Token A β†’ Token B
Hook converts: Token A β†’ Few Wrapped Token A (input conversion)
Pool executes: Few Wrapped Token A β†’ Few Wrapped Token B
Hook converts: Few Wrapped Token B β†’ Token B (output conversion)
User receives: Token B

4. Liquidity Removal

Users withdraw underlying tokens:

Pool returns: Few Wrapped Token A + Few Wrapped Token B
Hook converts: Few Wrapped Token A β†’ Token A, Few Wrapped Token B β†’ Token B
User receives: Token A + Token B

Integration Benefits

For Traders

  • Familiar Interface: Trade with standard tokens (USDC, ETH, etc.)

  • Enhanced Features: Access Few Protocol benefits transparently

  • Lower Costs: Optimized gas usage through hook automation

  • Better Rates: Deeper liquidity from wrapped token pools

For Liquidity Providers

  • Dual Benefits: Earn trading fees + Few Protocol rewards

  • Risk Management: Maintain exposure to underlying assets

  • Simplified Operations: Automatic wrapping/unwrapping

  • Capital Efficiency: Single-sided liquidity provision support

For Developers

  • Easy Integration: Standard Uniswap V4 interface

  • Flexible Architecture: Support for various token types

  • Extensible Design: Custom hooks for specific needs

  • Battle-tested Code: Audited and production-ready

Contract Addresses

Technical Specifications

Hook Flags

All Few Protocol hooks implement the following Uniswap V4 flags:

  • BEFORE_SWAP_FLAG: Pre-swap token conversion

  • BEFORE_ADD_LIQUIDITY_FLAG: Pre-liquidity token wrapping

  • BEFORE_SWAP_RETURNS_DELTA_FLAG: Delta handling for conversions

  • BEFORE_INITIALIZE_FLAG: Pool initialization setup

Security Features

  • Address Validation: Ensures correct token pairing

  • Overflow Protection: SafeMath operations throughout

  • Reentrancy Guards: Protection against reentrancy attacks

  • Access Controls: Proper permission management

Gas Optimization

  • Batch Operations: Multiple actions in single transaction

  • Efficient Storage: Optimized state variable usage

  • Minimal External Calls: Reduced cross-contract interactions

  • Assembly Optimizations: Critical path optimizations

Integration Guide

For Pool Creators

  1. Choose the Right Hook: Select based on your token type

  2. Deploy Pool: Use Uniswap V4 PoolManager with hook address

  3. Initialize: Set initial price and liquidity

  4. Test: Verify wrapping/unwrapping functionality

For Frontend Developers

  1. Standard Interface: Use normal Uniswap V4 SDK calls

  2. Token Display: Show underlying tokens to users

  3. Transaction Handling: Let hooks manage conversions automatically

  4. Error Handling: Implement proper error catching

Example Integration

// Create pool with FewTokenHook
IPoolManager.PoolKey memory key = IPoolManager.PoolKey({
    currency0: Currency.wrap(USDC_ADDRESS),
    currency1: Currency.wrap(FW_USDC_ADDRESS), 
    fee: 3000,
    tickSpacing: 60,
    hooks: IHooks(FEW_TOKEN_HOOK_ADDRESS)
});

poolManager.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);

Note: Hook contracts are immutable once deployed. Always verify contract addresses and test thoroughly on testnets before mainnet integration.

Last updated