0

Deployed on Polygon Mumbai. Code compiles fine on REMIX and is meant to perform multihop swaps on Polygon Mumbai, but it returns error after function is called.

Deployed Contract always funded with USDC to initiate swaps.

Need quickfix.

// SPDX-License-Identifier: MIT
pragma solidity >0.7.6;
pragma abicoder v2;

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

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

contract SwapExamples {

    address private owner;

    address public constant WETH = 0xA6FA4fB5f76172d178d61B04b0ecd319C5d1C0aa;
    address public constant WETH9 = 0x9c3C9283D3e44854697Cd22D3Faa240Cfb032889;
    address public constant USDC = 0x0FA8781a83E46826621b3BC094Ea2A0212e71B23;
    ISwapRouter public immutable swapRouter = ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564);

    // For this example, we will set the pool fee to 0.3%.
    uint24 public constant poolFee = 3000;

    constructor() {
        
        owner = payable(msg.sender);

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


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

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

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

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


        ISwapRouter.ExactInputParams memory params =
            ISwapRouter.ExactInputParams({
                path: abi.encodePacked(USDC, poolFee, WETH9, poolFee, WETH),
                recipient: msg.sender,
                deadline: block.timestamp,
                amountIn: amountIn,
                amountOutMinimum: 0
            });

        // Executes the swap.
        amountOut = swapRouter.exactInput(params);
    }    

1 Answer 1

0

found workaround just need to call the add approve tokens as a separate function then call the approve function through the swapExactInputMultihop function

// SPDX-License-Identifier: MIT
pragma solidity >=0.7.6;
pragma abicoder v2;

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

contract FlashSwap {
    address private owner;

    // This example swaps DAI/WETH9 for single path swaps and DAI/USDC/WETH9 for multi path swaps.

    address public constant WETH = 0xA6FA4fB5f76172d178d61B04b0ecd319C5d1C0aa;
    address public constant WETH9 = 0x9c3C9283D3e44854697Cd22D3Faa240Cfb032889;
    address public constant USDC = 0x0FA8781a83E46826621b3BC094Ea2A0212e71B23;
    ISwapRouter public immutable swapRouter =
        ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564);

    // For this example, we will set the pool fee to 0.3%.
    uint24 public constant poolFee = 3000;

    constructor() {
        owner = payable(msg.sender);
    }

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

    function Approve () private {

        uint256 amountIn= 1000000000000000000000000000000000000000000000000000000000;
        
        TransferHelper.safeApprove(USDC, address(swapRouter), amountIn);
    
        TransferHelper.safeApprove(WETH9, address(swapRouter), amountIn);

        



    }

    function swapExactInputMultihop(
        uint256 amountToken
    ) external returns (uint256 amountOut) {

        Approve();

        bytes memory path = abi.encodePacked(
            USDC,
            poolFee,
            WETH9,
            poolFee,
            USDC
        );
        // Transfer `amountIn` of DAI to this contract.
       
        

       
        

        ISwapRouter.ExactInputParams memory params = ISwapRouter
            .ExactInputParams({
                path: path,
                recipient: msg.sender,
                deadline: block.timestamp,
                amountIn: amountToken,
                amountOutMinimum: 0
            });

        
        
        amountOut = swapRouter.exactInput(params);
    }


}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.