Skip to main content
added 6 characters in body
Source Link
natewelch_
  • 12.2k
  • 1
  • 30
  • 43

// 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;

// 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);
    }


}

}

// 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);
}

}

// 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);
    }


}
Source Link

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);
}

}