blockchain - Facing error(Execution revert: STF) while swapping the USDC into wAVAX Tokens in Avalanche C chain - Stack Overflow

admin2025-04-18  2

I'm facing the Error(execution revert: STF) while swapping USDC into wAVAX

I’m encountering the following error message when attempting to swap USDC for AVAX using the Uniswap V3 exactInputSingle protocol:

Error: execution revert: STF

I’m running the contract on the Avalanche C-Chain, and I have followed the necessary steps to approve the transfer and fund the swapping contract. However, I’m still unable to complete the swap successfully.

Steps taken so far:

Approval:
    I’ve granted the appropriate approval to the Uniswap V3 Router contract to spend USDC on my behalf.
    The approval is done via TransferHelper.safeApprove and the approval amount matches the swap amount.

Fund Transfer:
    I’ve transferred the USDC from my wallet to the Swapping contract using the safeTransferFrom method.

Uniswap V3 Swap:
    I am using the exactInputSingle function of the Uniswap V3 Router to execute the swap.
    The parameters used in the swap include:
        Token In: USDC (the stablecoin)
        Token Out: AVAX (wrapped as wAVAX)
        Pool Fee: 0.3% (3000)
        Amount In: [specified amount]
        Amount Out Minimum: 0 (no slippage tolerance set)

Error Encounter:
    When attempting to execute the swap, the transaction reverts with the error message execution revert: STF.
    This error message is not very descriptive, and I’m unable to pinpoint what’s causing the revert.

Environment:

Network: Avalanche C-Chain
Uniswap Version: V3
Tokens: USDC (ERC-20), wAVAX (wrapped AVAX)
Contract Setup: Custom swapping contract using Uniswap V3 router
Approval Mechanism: TransferHelper.safeApprove to the Uniswap Router
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;
pragma abicoder v2;

import "@uniswap/v3-periphery/contracts/libraries/TransferHelper.sol";
import "@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol";
import "@uniswap/v3-core/contracts/libraries/LowGasSafeMath.sol";
// import "@openzeppelin/contracts/token/ERC20/IERC20.sol";


interface IWAVAX {
    function deposit() external payable;
    function withdraw(uint256 wad) external;
}

contract StablecoinToAVAXSwapper {
    using LowGasSafeMath for uint256;
    ISwapRouter public immutable swapRouter;
    address public immutable stablecoin;
    address public immutable wavax;
    address private owner;
    uint24 public constant poolFee = 3000; // Example pool fee for USDC -> wAVAX in Uniswap V3 (can vary)

    constructor(ISwapRouter _swapRouter, address _stablecoin, address _wavax) {
        swapRouter = _swapRouter;
        stablecoin = _stablecoin;
        wavax = _wavax;
        owner = msg.sender;
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "Not the owner");
        _;
    }

    function swap(uint256 amountIn) external returns (uint256 amountOut) {
        // Approve this contract to spend stablecoin
        TransferHelper.safeApprove(stablecoin, address(this), amountIn);

        // Transfer stablecoin from the user to this contract
        TransferHelper.safeTransferFrom(stablecoin, msg.sender, address(this), amountIn);

        // Approve Uniswap router to spend stablecoin
        TransferHelper.safeApprove(stablecoin, address(swapRouter), amountIn);

        // Prepare the ExactInputSingleParams struct
        ISwapRouter.ExactInputSingleParams memory params =
            ISwapRouter.ExactInputSingleParams({
                tokenIn: stablecoin,          // TokenIn (USDC)
                tokenOut: wavax,              // TokenOut (wAVAX)
                fee: poolFee,                 // Pool fee (typically 0.3%)
                recipient: msg.sender,        // Recipient
                deadline: block.timestamp + 300, // 5-minute deadline
                amountIn: amountIn,           // Amount in (USDC)
                amountOutMinimum: 0,          // Minimum amount out (wAVAX)
                sqrtPriceLimitX96: 0         // No price limit (0 means no limit)
            });

        // Execute the swap using Uniswap V3
        amountOut = swapRouter.exactInputSingle(params);
    }

    // Withdraw wrapped AVAX (wAVAX) to owner
    function withdraw(uint256 amount) external onlyOwner {
        IWAVAX(wavax).withdraw(amount);
        payable(owner).transfer(amount);
    }

    // Generic function to check balance of any token (AVAX or ERC20 tokens)
    function balanceOf(address token) public view returns (uint256) {
        if (token == address(0)) {
            // If the address is 0, return the contract's AVAX balance
            return address(this).balance;
        } else {
            // Otherwise, it's an ERC20 token, so call balanceOf for that token
            return IERC20(token).balanceOf(address(this));
        }
    }

    // Receive AVAX directly (in case AVAX is sent to the contract)
    receive() external payable {}
}
转载请注明原文地址:http://anycun.com/QandA/1744957117a90014.html