Skip to content

UniswapV2/V3 router ABI decoding #15

@koko1123

Description

@koko1123

Summary

Add pre-built ABI decoders for the most common DEX router contracts (UniswapV2Router02, UniswapV3SwapRouter, Universal Router).

Motivation

The overwhelming majority of MEV opportunities come from DEX swaps. Searchers decode every pending transaction to check if it's a swap, extract the parameters (token pair, amount, slippage), and evaluate whether an arb or sandwich is profitable.

Having optimized, pre-built decoders for the most common routers eliminates boilerplate and leverages eth.zig's fast ABI decoding (1.77x faster than Alloy) where it matters most.

Proposed API

const eth = @import("eth");

// Decode a pending transaction
const decoded = eth.dex.decode(tx.data) orelse continue;

switch (decoded) {
    .v2_swap_exact_tokens_for_tokens => |swap| {
        // swap.amountIn, swap.amountOutMin, swap.path, swap.to, swap.deadline
    },
    .v3_exact_input_single => |swap| {
        // swap.tokenIn, swap.tokenOut, swap.fee, swap.recipient, swap.amountIn, swap.sqrtPriceLimitX96
    },
    .universal_router_execute => |cmd| {
        // Decode Universal Router commands
    },
}

Scope

Phase 1: Core routers

  • UniswapV2Router02: swapExactTokensForTokens, swapTokensForExactTokens, swapExactETHForTokens, swapTokensForExactETH
  • UniswapV3SwapRouter: exactInputSingle, exactInput, exactOutputSingle, exactOutput
  • Universal Router: execute command decoding

Phase 2: Additional DEXs

  • SushiSwap (V2-compatible)
  • PancakeSwap (V2/V3-compatible)
  • Aerodrome/Velodrome (Base/OP)
  • Camelot (Arbitrum)

Implementation Notes

  • Use comptime function selectors for zero-cost selector matching
  • Struct types for each decoded swap variant
  • Should compose with pending tx subscription (Flashbots / MEV bundle submission #13) for a complete mempool monitoring pipeline
  • The Universal Router uses a command-based encoding that needs special handling

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is neededl2L2 chain supportmevMEV/searcher tooling

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions