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

Polygon测试网UniswapV3 Solidity兑换WMATIC到USDC报错排查

Polygon测试网UniswapV3 WMATIC转USDC兑换报错问题

我在Polygon测试网用Solidity实现UniswapV3的exactInputSingle兑换(WMATIC转USDC),已经授权合约使用WMATIC,但Remix仍显示错误。

合约代码

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity =0.8.6;
pragma abicoder v2;

import '@uniswap/v3-periphery/contracts/libraries/TransferHelper.sol';
import '@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol';

contract SingleSwap {

    ISwapRouter public immutable swapRouter = ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564);

    address public constant WMATIC = 0x9c3C9283D3e44854697Cd22D3Faa240Cfb032889;
    address public constant USDC = 0xE097d6B3100777DC31B34dC2c58fB524C2e76921;

    uint24 public constant poolFee = 3000;

    function swapExactInputSingle(uint256 amountIn) external returns (uint256 amountOut) {

        TransferHelper.safeTransferFrom(WMATIC, msg.sender, address(this), amountIn);

        TransferHelper.safeApprove(WMATIC, address(swapRouter), amountIn);

        ISwapRouter.ExactInputSingleParams memory params =
            ISwapRouter.ExactInputSingleParams({
                tokenIn: WMATIC,
                tokenOut: USDC,
                fee: poolFee,
                recipient: msg.sender,
                deadline: block.timestamp,
                amountIn: amountIn,
                amountOutMinimum: 0,
                sqrtPriceLimitX96: 0
            });

        amountOut = swapRouter.exactInputSingle(params);
    }
}

Remix错误截图

Remix错误截图

可能的报错原因及解决思路

  • 依赖导入问题:Remix直接导入@uniswap/v3-periphery可能无法正确拉取依赖,建议改用完整GitHub路径导入,比如:
    import "https://github.com/Uniswap/v3-periphery/blob/main/contracts/libraries/TransferHelper.sol";
    import "https://github.com/Uniswap/v3-periphery/blob/main/contracts/interfaces/ISwapRouter.sol";
    
  • 授权金额不足:确认给合约的WMATIC授权金额≥调用swapExactInputSingle时传入的amountIn,授权不足会触发safeTransferFrom失败。
  • Deadline设置过短:用block.timestamp作为deadline容易因区块打包延迟导致交易过期,建议改为block.timestamp + 600(延长10分钟)。
  • 链上环境配置:检查Remix是否已切换到Polygon Mumbai测试网,钱包是否连接正确,账户是否有足够MATIC支付Gas。
  • 流动性池存在性:确认Polygon Mumbai上WMATIC/USDC的0.3%(3000)费率池已创建且有流动性,否则兑换会失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 13:35:23