This directory contains practical examples demonstrating how to use the Uniswap V4 SDK for Rust to interact with Uniswap V4 protocols.
- Rust 1.88 or later
- A mainnet RPC URL (for forking)
- Create a
.envfile in the project root:
MAINNET_RPC_URL=https://your-ethereum-mainnet-rpc-url- Build the project with extensions feature:
cargo build --features extensions- mint_position_basic.rs - Demonstrates minting a liquidity position in an existing ETH-USDC V4 pool
- increase_liquidity.rs - Shows how to add more tokens to an existing position, demonstrating the complete workflow from initial mint to liquidity increase
- mint_position_permit2.rs - Demonstrates using Permit2 for gasless token approvals when minting positions
Each example can be run independently:
# Run the basic minting example
cargo run --example mint_position_basic --features extensions
# Run the liquidity increase example
cargo run --example increase_liquidity --features extensions
# Run the permit2 example
cargo run --example mint_position_permit2 --features extensionsNote: Examples require the extensions feature for V4 functionality.
- Hooks: V4 introduces hooks that can customize pool behavior
- Currencies: V4 uses
Currencyinstead ofTokento support native ETH - Position Manager: New position manager contract with different interface
- Pool Keys: V4 pools are identified by pool keys containing currency pair, fee, tick spacing, and hooks
- Create or reference a pool with the desired currency pair, fee tier, and hooks
- Define position parameters including tick range and liquidity amount
- Prepare transaction using
add_call_parameters()with appropriate options - Execute transaction through the V4 Position Manager contract
All examples use Anvil forking to create a local testnet that mirrors the mainnet state:
- Fork from a recent mainnet block
- Create test accounts with ETH balances
- Set up token balances and approvals
- Execute transactions in the forked environment
- Use
uniswap_v4_sdk::prelude::*for easy imports - Use shared utilities from
examples/commonmodule - Set up Anvil provider for local testing
- Handle both native ETH and ERC20 tokens as currencies
- Use appropriate slippage tolerance and deadline parameters