use anyhow;
use tracing::debug;
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");
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
);
}
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
);
}
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
);
}
anyhow::anyhow!("Failed to send transaction: {}", error_str)
}