swap doesn't working and giving error "Transaction failed"

43 Views Asked by At

I am currently working on a project involving token swapping. swap doesn't working . Is there any mistake in my code ? I am using Ethereum sepolia test net. but the swap not woring and giving an error that transation failed. Please help if any one knows.

How can I perform ERC20 to ETH swaps in Uniswap V3?

   // SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.20;
pragma abicoder v2;

import "https://github.com/Uniswap/v3-periphery/blob/main/contracts/interfaces/ISwapRouter.sol";


interface IERC20 {
    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
    function approve(address spender, uint256 amount) external returns (bool);
}

contract SingleSwap {
    address public constant routerAddress = 0x3bFA4769FB09eefC5a80d6E87c3B9C650f7Ae48E;
    ISwapRouter public immutable swapRouter = ISwapRouter(routerAddress);
    
    address public constant LINK = 0x779877A7B0D9E8603169DdbD7836e478b4624789;
    address public constant WETH = 0xfFf9976782d46CC05630D1f6eBAb18b2324d6B14;
    address public constant USD = 0x6f14C02Fc1F78322cFd7d707aB90f18baD3B54f5;
    
    IERC20 public WETHToken = IERC20(WETH);
    
    uint24 public constant poolFee = 3000;

    constructor() {}

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

        WETHToken.approve(address(swapRouter), amountIn);

        ISwapRouter.ExactInputSingleParams memory params =
            ISwapRouter.ExactInputSingleParams({
                tokenIn: WETH,
                tokenOut: USD,
                fee: poolFee,
                recipient: address(this),
                deadline: block.timestamp,
                amountIn: amountIn,
                amountOutMinimum: 0,
                sqrtPriceLimitX96: 0
            });

        amountOut = swapRouter.exactInputSingle(params);
    }

    function swapExactOutputSingle(uint256 amountOut, uint256 amountInMaximum) external returns (uint256 amountIn) {

        WETHToken.approve(address(swapRouter), amountInMaximum);

        ISwapRouter.ExactOutputSingleParams memory params =
            ISwapRouter.ExactOutputSingleParams({
                tokenIn: WETH,
                tokenOut: USD,
                fee: poolFee,
                recipient: address(this),
                deadline: block.timestamp,
                amountOut: amountOut,
                amountInMaximum: amountInMaximum,
                sqrtPriceLimitX96: 0
            });

        amountIn = swapRouter.exactOutputSingle(params);

        if (amountIn < amountInMaximum) {
            WETHToken.approve(address(swapRouter), 0);
            WETHToken.transfer(address(this), amountInMaximum - amountIn);
        }
    }
}

please help if you guys know.

0

There are 0 best solutions below