After deploying Simple Arbitrage Contract I am trying to deposit eth but deposit is not happening and Debug is showing value 0 wei. While depositing only transaction fee is deducted from metamask not showing eth send separately.
Can you help me?
This is the smart contract. Is there a mistake in this contract?,
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../interfaces/UniswapV2Router.sol";
import "../interfaces/Erc20.sol";
contract SimpleArbitrage {
address public owner;
address public wethAddress;
address public daiAddress;
address public uniswapRouterAddress;
address public sushiswapRouterAddress;
uint256 public arbitrageAmount;
enum Exchange {
UNI,
SUSHI,
NONE
}
constructor(
address _uniswapRouterAddress,
address _sushiswapRouterAddress,
address _weth,
address _dai
) {
uniswapRouterAddress = _uniswapRouterAddress;
sushiswapRouterAddress = _sushiswapRouterAddress;
owner = msg.sender;
wethAddress = _weth;
daiAddress = _dai;
}
modifier onlyOwner() {
require(msg.sender == owner, "only owner can call this");
_;
}
function deposit(uint256 amount) public onlyOwner {
require(amount > 0, "Deposit amount must be greater than 0");
IERC20(wethAddress).transferFrom(msg.sender, address(this), amount);
arbitrageAmount += amount;
}
function withdraw(uint256 amount) public onlyOwner {
require(amount <= arbitrageAmount, "Not enough amount deposited");
IERC20(wethAddress).transferFrom(address(this), msg.sender, amount);
arbitrageAmount -= amount;
}
function makeArbitrage() public {
uint256 amountIn = arbitrageAmount;
Exchange result = _comparePrice(amountIn);
if (result == Exchange.UNI) {
// sell ETH in uniswap for DAI with high price and buy ETH from sushiswap with lower price
uint256 amountOut = _swap(
amountIn,
uniswapRouterAddress,
wethAddress,
daiAddress
);
uint256 amountFinal = _swap(
amountOut,
sushiswapRouterAddress,
daiAddress,
wethAddress
);
arbitrageAmount = amountFinal;
} else if (result == Exchange.SUSHI) {
// sell ETH in sushiswap for DAI with high price and buy ETH from uniswap with lower price
uint256 amountOut = _swap(
amountIn,
sushiswapRouterAddress,
wethAddress,
daiAddress
);
uint256 amountFinal = _swap(
amountOut,
uniswapRouterAddress,
daiAddress,
wethAddress
);
arbitrageAmount = amountFinal;
}
}
function _swap(
uint256 amountIn,
address routerAddress,
address sell_token,
address buy_token
) internal returns (uint256) {
IERC20(sell_token).approve(routerAddress, amountIn);
uint256 amountOutMin = (_getPrice(
routerAddress,
sell_token,
buy_token,
amountIn
) * 95) / 100;
address[] memory path = new address[](2);
path[0] = sell_token;
path[1] = buy_token;
uint256 amountOut = IUniswapV2Router02(routerAddress)
.swapExactTokensForTokens(
amountIn,
amountOutMin,
path,
address(this),
block.timestamp
)[1];
return amountOut;
}
function _comparePrice(uint256 amount) internal view returns (Exchange) {
uint256 uniswapPrice = _getPrice(
uniswapRouterAddress,
wethAddress,
daiAddress,
amount
);
uint256 sushiswapPrice = _getPrice(
sushiswapRouterAddress,
wethAddress,
daiAddress,
amount
);
// we try to sell ETH with higher price and buy it back with low price to make profit
if (uniswapPrice > sushiswapPrice) {
require(
_checkIfArbitrageIsProfitable(
amount,
uniswapPrice,
sushiswapPrice
),
"Arbitrage not profitable"
);
return Exchange.UNI;
} else if (uniswapPrice < sushiswapPrice) {
require(
_checkIfArbitrageIsProfitable(
amount,
sushiswapPrice,
uniswapPrice
),
"Arbitrage not profitable"
);
return Exchange.SUSHI;
} else {
return Exchange.NONE;
}
}
function _checkIfArbitrageIsProfitable(
uint256 amountIn,
uint256 higherPrice,
uint256 lowerPrice
) internal pure returns (bool) {
// uniswap & sushiswap have 0.3% fee for every exchange
// so gain made must be greater than 2 * 0.3% * arbitrage_amount
// difference in ETH
uint256 difference = ((higherPrice - lowerPrice) * 10**18) /
higherPrice;
uint256 payed_fee = (2 * (amountIn * 3)) / 1000;
if (difference > payed_fee) {
return true;
} else {
return false;
}
}
function _getPrice(
address routerAddress,
address sell_token,
address buy_token,
uint256 amount
) internal view returns (uint256) {
address[] memory pairs = new address[](2);
pairs[0] = sell_token;
pairs[1] = buy_token;
uint256 price = IUniswapV2Router02(routerAddress).getAmountsOut(
amount,
pairs
)[1];
return price;
}
}