waterpump-evm-pool-sdk 0.1.0

EVM pool SDK — viewers, infusers, harvesters, swappers for Uniswap V3/V4, PancakeSwap, Slipstream, Shadow, Algebra
Documentation
use anyhow;
use tracing::debug;

/// Handle Uniswap V3 transaction errors and provide user-friendly error
/// messages
///
/// This function decodes common Uniswap V3 revert errors and provides
/// actionable error messages to help diagnose transaction failures.
pub fn handle_uniswap_v3_error(error: impl std::fmt::Display) -> anyhow::Error {
    let error_str = format!("{}", error);
    debug!(error = %error_str, "Handling Uniswap V3 error");

    // Check for "Too little received" error
    if error_str.contains("Too little received") {
        return anyhow::anyhow!(
            "Transaction reverted: Too little received\n\nThis typically means the swap output \
             was lower than the minimum\namount specified (amountOutMinimum/slippage \
             settings).\nTry increasing your slippage tolerance or reducing amountIn.\n\nOriginal \
             error: {}",
            error_str
        );
    }

    // Check for "Too much requested" error
    // This error occurs when the amount requested exceeds available liquidity
    if error_str.contains("Too much requested") || error_str.contains("0x08c379a0") {
        return anyhow::anyhow!(
            "Transaction reverted: Too much requested\n\nThis error typically means:\n1. The \
             amount requested exceeds available pool liquidity\n2. Insufficient balance or \
             allowance\n3. Slippage tolerance too tight\n\nOriginal error: {}",
            error_str
        );
    }

    // Check for other common Uniswap V3 errors
    if error_str.contains("STF") || error_str.contains("SPL") {
        return anyhow::anyhow!(
            "Transaction reverted: Price limit exceeded\n\nThis error occurs when the swap would \
             move the price beyond the\nspecified sqrtPriceLimitX96. Try adjusting your price \
             limit.\n\nOriginal error: {}",
            error_str
        );
    }

    if error_str.contains("execution reverted") {
        return anyhow::anyhow!(
            "Transaction reverted: {}\n\nCommon causes:\n1. Insufficient balance or allowance\n2. \
             Amount exceeds pool liquidity\n3. Slippage tolerance too tight\n4. Price moved \
             unfavorably\n5. Invalid swap parameters\n\nOriginal error: {}",
            if error_str.len() > 200 { &error_str[..200] } else { &error_str },
            error_str
        );
    }

    // Generic error fallback
    anyhow::anyhow!("Failed to send transaction: {}", error_str)
}