2

I'm trying to perform a Uniswap V3 swap on this contract from WETH to DAI. I'm using a local environment that consists of: Solidity extension, VS Code, and Python framework Brownie-eth.

The contract deploys without issues, however calling swapExactInputSingle or swapExactInputSingle02 will result in the following error:

ValueError: Gas estimation failed: 'execution reverted: STF'. This transaction will likely revert. If you wish to broadcast, you must set the gas limit manually.

Deploying and interacting with this contract on Remix IDE returns the following error:

status  false Transaction mined but execution failed
transaction hash    0x8762f246bd2e2a2ddf10c7b499c349a961a9a3f47a349376d6516a40e9275561
from    0xF24B09E697A390F86791E161C834bC9928209E75
to  Swap.swapExactInputSingle(uint256) 0xdE802C558a02e9a2A5cD43d8f8Dc2784583ef056
gas 3000000 gas
transaction cost    31396 gas 
input   0x73b...10000
decoded input   {
    "uint256 amountIn": "10000000000000000"
}
decoded output   - 
logs    []
val 0 wei

There is enough WETH balance in the wallet and the token addresses are correct. I am at a bit of a loss as to why it keeps failing.

//SPDX-License-Identifier: Unlicense
pragma solidity >=0.7.5;
pragma abicoder v2;

import "@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol";
import "@uniswap/v3-periphery/contracts/libraries/TransferHelper.sol";
import "@uniswap/swap-router-contracts/contracts/interfaces/IV3SwapRouter.sol";

contract Swap {

    address private constant SWAP_ROUTER =
        0xE592427A0AEce92De3Edee1F18E0157C05861564;
    address private constant SWAP_ROUTER_02 =
        0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45;

    address private constant WETH = 0xc778417E063141139Fce010982780140Aa0cD5Ab; // rinkeby
    address public constant DAI = 0xc7AD46e0b8a400Bb3C915120d284AafbA8fc4735; // rinkeby

    ISwapRouter public immutable swapRouter = ISwapRouter(SWAP_ROUTER);
    IV3SwapRouter public immutable swapRouter02 = IV3SwapRouter(SWAP_ROUTER_02);

    function safeTransferWithApprove(uint256 amountIn, address routerAddress)
        internal
    {
        TransferHelper.safeTransferFrom(
            DAI,
            msg.sender,
            address(this),
            amountIn
        );

        TransferHelper.safeApprove(DAI, routerAddress, amountIn);
    }

    function swapExactInputSingle(uint256 amountIn)
        external
        returns (uint256 amountOut)
    {
        safeTransferWithApprove(amountIn, address(swapRouter));

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

        amountOut = swapRouter.exactInputSingle(params);
    }

    function swapExactInputSingle02(uint256 amountIn)
        external
        returns (uint256 amountOut)
    {
        safeTransferWithApprove(amountIn, address(swapRouter02));

        IV3SwapRouter.ExactInputSingleParams memory params = IV3SwapRouter
            .ExactInputSingleParams({
                tokenIn: DAI,
                tokenOut: WETH,
                fee: 3000,
                recipient: msg.sender,
                amountIn: amountIn,
                amountOutMinimum: 0,
                sqrtPriceLimitX96: 0
            });

        amountOut = swapRouter02.exactInputSingle(params);
    }
}

0 Answers0