// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/access/Ownable.sol";
interface IUniswapV2Router02 {
function swapExactETHForTokens(
uint256 amountOutMin,
address[] calldata path,
address to,
uint deadline
) external payable returns (uint[] memory amounts);
function swapExactTokensForETH(
uint256 amountIn,
uint256 amountOutMin,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function getAmountsOut(uint256 amountIn, address[] calldata path) external view returns (uint256[] memory amounts);
}
contract ProfitSlippageBot is Ownable {
IUniswapV2Router02 private uniswapRouter;
uint public maxGasPrice = 100 gwei;
uint public minProfit = 0.01 ether;
address public WETH;
address public tokenAddress;
address[] private swapPathInner;
address[][] public tempArray;
event ArbitrageOpportunity(uint amountIn, uint expectedOut);
event Log(string message);
constructor() payable Ownable(msg.sender) {
// Set Uniswap V2 Router (Mainnet)
uniswapRouter = IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
// Token Addresses
WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
tokenAddress = 0xB8c77482e45F1F44dE1745F52C74426C631bDD52; // BNB token on Ethereum
// Init Swap Path
swapPathInner = [WETH, tokenAddress];
}
function executeArbitrage(uint256 amountIn, uint256 amountOutMin, bool isBuy) external onlyOwner {
require(amountIn > 0, "Amount must be greater than 0");
require(tx.gasprice <= maxGasPrice, "Gas price too high");
uint expectedOut;
if (isBuy) {
expectedOut = amountIn + minProfit;
swapPathInner[0] = WETH;
swapPathInner[1] = tokenAddress;
} else {
expectedOut = amountIn - minProfit;
swapPathInner[0] = tokenAddress;
swapPathInner[1] = WETH;
}
emit ArbitrageOpportunity(amountIn, expectedOut);
tempArray.push(swapPathInner);
if (isBuy) {
uniswapRouter.swapExactETHForTokens{value: amountIn}(
amountOutMin,
swapPathInner,
address(this),
block.timestamp + 300
);
emit Log("Swapped ETH for tokens");
} else {
uniswapRouter.swapExactTokensForETH(
amountIn,
amountOutMin,
swapPathInner,
address(this),
block.timestamp + 300
);
emit Log("Swapped tokens for ETH");
}
}
}
Hi, welcome to the community!
I have a try on the Remix quickly, it works, I can deploy the contract ProfitSlippageBot
.