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