You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Uniswap SwapRouter V3 Gas估算失败问题求助(Hardhat Fork环境)

问题排查与解决方向

1. 用错Uniswap V3核心函数(最关键问题)

你当前调用的exactInputSingle是代币兑换接口,完全不是用来添加流动性的。Uniswap V3添加流动性必须用NonfungiblePositionManager合约的mint方法,SwapRouter只负责代币交换,不处理流动性部署。

主网的NonfungiblePositionManager合约地址是0xC36442b4a4522E871399CD717aBDD847Ab11FE88,需要单独引入它的ABI。

2. 参数逻辑存在致命错误

你的参数列表有多处问题:

  • 滑点计算错误:slippageTolerance = 1相当于允许100%滑点,直接导致amountB.sub(amountB.mul(slippageTolerance))结果为0,交易必然失败。改成0.01(1%)才合理,还要修正精度问题:
    const slippageTolerance = 0.01; // 1%
    const minAmountB = amountB.sub(amountB.mul(Math.floor(slippageTolerance * 100)).div(100));
    
  • 参数结构不匹配:就算换对了函数,你当前的参数顺序和含义也和Uniswap V3的mint接口完全不符。mint需要的参数是token0、token1、fee、tickLower、tickUpper、amount0Desired、amount1Desired、amount0Min、amount1Min、recipient、deadline,得重新对应调整。

3. 缺失代币授权步骤

在调用流动性添加函数前,必须先授权合约(比如NonfungiblePositionManager)转移你的代币,不然合约无法获取代币,交易必然失败,gas自然无法估算。补充这段代码:

// 授权tokenA给PositionManager
await tokenA.approve(nonfungiblePositionManagerAddress, amountA);
// 授权tokenB给PositionManager
await tokenB.approve(nonfungiblePositionManagerAddress, amountB);

4. Gas设置方式错误

手动设置gasLimit时,要放在交易的overrides参数里,不是塞进函数参数:

const tx = await nonfungiblePositionManagerContract.mint(mintParams, {
  gasLimit: ethers.utils.hexlify(2000000) // 手动指定gas上限,可根据实际情况调整
});

5. 基础配置验证

  • 把tokenAAddress和tokenBAddress替换为真实有效的代币地址,同时确认对应fee层级的Pool合约已经在链上存在(比如你用的100 fee层级,要确保链上有这个Pool)。
  • 检查Hardhat Fork的区块高度是否正确,测试账户是否有足够ETH支付gas,以及对应代币余额是否充足。

内容的提问来源于stack exchange,提问作者jayyyo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 22:42:45