My FlokiTokenTrader.sol is primarily written for Arbitrage trading. All functions are working properly but transaction is not succeeding in trade function.
I used georli ethereum network USDC token instead of Floki token. Can someone review my code and tell me why the error is coming in the trade function.
Trade Function Transaction: https://goerli.etherscan.io/tx/0xd96f50f71f79a518ffe2a9c4a48284b6ec82df74347119ac1b3273445675b2bc
pragma solidity ^0.8.18;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
contract FlokiTokenTrader is Ownable {
receive() payable external{}
using SafeERC20 for IERC20;
address public constant UNISWAP_ROUTER_ADDRESS = 0x4648a43B2C14Da09FdF82B161150d3F634f40491;
address public constant SUSHISWAP_ROUTER_ADDRESS = 0x1b02dA8Cb0d097eB8D57A175b88c7D8b47997506;
IUniswapV2Router02 public uniswapRouter;
IUniswapV2Router02 public sushiSwapRouter;
constructor() {
uniswapRouter = IUniswapV2Router02(UNISWAP_ROUTER_ADDRESS);
sushiSwapRouter = IUniswapV2Router02(SUSHISWAP_ROUTER_ADDRESS);
}
function trade(address token, uint flokiAmount, uint uniswapMinAmount, uint sushiswapMinAmount, address to, uint deadline) external onlyOwner {
address[] memory path = new address[](2);
path[0] = uniswapRouter.WETH();
path[1] = token;
// Get the expected amounts for trading on Uniswap and Sushiswap
uint[] memory uniswapAmounts = uniswapRouter.getAmountsOut(flokiAmount, path);
path[0] = sushiSwapRouter.WETH();
uint[] memory sushiSwapAmounts = sushiSwapRouter.getAmountsOut(flokiAmount, path);
// If the Floki price on Uniswap is lower, trade on Uniswap
if (uniswapAmounts[1] > sushiSwapAmounts[1]) {
uint[] memory amounts = uniswapRouter.swapExactTokensForTokens(flokiAmount, uniswapMinAmount, path, address(this), deadline);
IERC20(token).safeApprove(SUSHISWAP_ROUTER_ADDRESS, amounts[1]);
sushiSwapRouter.swapExactTokensForTokens(amounts[1], sushiswapMinAmount, path, to, deadline);
}
// If the Floki price on Sushiswap is lower, trade on Sushiswap
else {
uint[] memory amounts = sushiSwapRouter.swapExactTokensForTokens(flokiAmount, sushiswapMinAmount, path, address(this), deadline);
IERC20(token).safeApprove(UNISWAP_ROUTER_ADDRESS, amounts[1]);
uniswapRouter.swapExactTokensForTokens(amounts[1], uniswapMinAmount, path, to, deadline);
}
}
function EthBalance() public view returns (uint256) {
return address(this).balance;
}
function TokenBalance(address token) public view returns (uint256) {
return IERC20(token).balanceOf(address(this));
}
function withdrawTokens(address token, uint amount, address to) external onlyOwner {
IERC20(token).safeTransfer(to, amount);
}
function withdrawETH(uint amount, address payable to) external onlyOwner {
require(address(this).balance >= amount, "Insufficient ETH balance in the contract");
to.transfer(amount);
}
}