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