Skip to main content
added 116 characters in body
Source Link

I'm trying to create a split swap smart contract. What is wrong with my codeThis contract works fine on the BSC chain, but on ETH I got the error "TransferHelper: TRANSFER_FROM_FAILED", why?

IPancakeswapV2RouterSplitSwap.sol

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;
 
interface IPancakeswapV2Router {
    function swapExactTokensForTokens(uint256 amountIn, uint256 amountOutMin, address[] memory path, address to) external payable returns (uint256 amountOut);   import 
}

SplitSwap.sol

/'@uniswap/ SPDX-License-Identifier: GPLv2-3.0

pragma solidity >=0.8.2 <0.9core/contracts/interfaces/IERC20.0;
sol';
import '.'@uniswap/v2-periphery/contracts/interfaces/IPancakeswapV2RouterIUniswapV2Router02.sol';

contract SplitSwap {

    function splitSwap(address token0, address token1, address router, uintuint256 amount, uintuint256 size) public { 

        IERC20(token0).transferFrom(msg.sender, address(this), amount);

        IERC20(token0).approve(router, amount);
        
        address[] memory pathpaths = new address[](2);

        path[0]paths[0] = token0;
        path[1]paths[1] = token1;

        while (amount > 0) {
            
            uint amountIn = amount < size ? amount : size;
            uint amountOut = 0;

            IPancakeswapV2RouterIUniswapV2Router02(router).swapExactTokensForTokens(
                amountIn, 
                amountOut, path
                paths, 
                msg.sender,
                block.timestamp + 120);

            amount = amount - amountIn;
        }  
    }
}

I'm trying to create a split swap smart contract. What is wrong with my code?

IPancakeswapV2Router.sol

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;
 
interface IPancakeswapV2Router {
    function swapExactTokensForTokens(uint256 amountIn, uint256 amountOutMin, address[] memory path, address to) external payable returns (uint256 amountOut);    
}

SplitSwap.sol

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;

import './interfaces/IPancakeswapV2Router.sol';

contract SplitSwap {

    function splitSwap(address token0, address token1, address router, uint amount, uint size) public {
        
        address[] memory path = new address[](2);

        path[0] = token0;
        path[1] = token1;

        while (amount > 0) {
            
            uint amountIn = amount < size ? amount : size;
            uint amountOut = 0;

            IPancakeswapV2Router(router).swapExactTokensForTokens(amountIn, amountOut, path, msg.sender);

            amount = amount - amountIn;
        }  
    }
}

I'm trying to create a split swap smart contract. This contract works fine on the BSC chain, but on ETH I got the error "TransferHelper: TRANSFER_FROM_FAILED", why?

SplitSwap.sol

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;

import '@uniswap/v2-core/contracts/interfaces/IERC20.sol';
import '@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol';

contract SplitSwap {

    function splitSwap(address token0, address token1, address router, uint256 amount, uint256 size) public { 

        IERC20(token0).transferFrom(msg.sender, address(this), amount);

        IERC20(token0).approve(router, amount);
        
        address[] memory paths = new address[](2);

        paths[0] = token0;
        paths[1] = token1;

        while (amount > 0) {

            uint amountIn = amount < size ? amount : size;
            uint amountOut = 0;

            IUniswapV2Router02(router).swapExactTokensForTokens(
                amountIn, 
                amountOut, 
                paths, 
                msg.sender,
                block.timestamp + 120);

            amount = amount - amountIn;
        }  
    }
}
Source Link

Create SplitSwap smart contract

I'm trying to create a split swap smart contract. What is wrong with my code?

IPancakeswapV2Router.sol

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;

interface IPancakeswapV2Router {
    function swapExactTokensForTokens(uint256 amountIn, uint256 amountOutMin, address[] memory path, address to) external payable returns (uint256 amountOut);    
}

SplitSwap.sol

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;

import './interfaces/IPancakeswapV2Router.sol';

contract SplitSwap {

    function splitSwap(address token0, address token1, address router, uint amount, uint size) public {
        
        address[] memory path = new address[](2);

        path[0] = token0;
        path[1] = token1;

        while (amount > 0) {
            
            uint amountIn = amount < size ? amount : size;
            uint amountOut = 0;

            IPancakeswapV2Router(router).swapExactTokensForTokens(amountIn, amountOut, path, msg.sender);

            amount = amount - amountIn;
        }  
    }
}