I am trying to build a bot that can perform tri-arbitrage trades and I ran into an issue that I don't really understand when trying to interact with the Uniswap v2 router in my contract to estimate a trade.
I am running this on a hardhat forked Ethereum mainnet and this is the error I'm currently getting;
In my terminal
Error: call revert exception [ See: https://links.ethers.org/v5-errors-CALL_EXCEPTION ] (method="estimateTriDexTrade(address,address,address,address,address,address,uint256)", data="0x", errorArgs=null, errorName=null, errorSignature=null, reason=null, code=CALL_EXCEPTION, version=abi/5.7.0)
In the hardhat node console
Error: Transaction reverted without a reason string
at <UnrecognizedContract>.<unknown> (0xd9e1ce17f2641f24ae83637ab66a2cca9c378b9f)
at FlashLoanAb.getAmountOutMin (contracts/FlashLoanAb.sol:93)
at FlashLoanAb.estimateTriDexTrade (contracts/FlashLoanAb.sol:110)
This the UniswapRouter interfaces I'm using in my contract
interface IUniswapV2Router {
function getAmountsOut(
uint256 amountIn,
address[] memory path
) external view returns (uint256[] memory amounts);
function swapExactTokensForTokens(
uint256 amountIn,
uint256 amountOutMin,
address[] calldata path,
address to,
uint256 deadline
) external returns (uint256[] memory amounts);
}
interface IUniswapV2Pair {
function token0() external view returns (address);
function token1() external view returns (address);
function swap(
uint256 amount0Out,
uint256 amount1Out,
address to,
bytes calldata data
) external;
}
These are the function calls
function getAmountOutMin(
address router,
address _tokenIn,
address _tokenOut,
uint256 _amount
) public view returns (uint256) {
address[] memory path;
path = new address[](2);
path[0] = _tokenIn;
path[1] = _tokenOut;
uint256[] memory amountOutMins = IUniswapV2Router(router).getAmountsOut(
_amount,
path
);
return amountOutMins[path.length - 1];
}
function estimateTriDexTrade(
address _router1,
address _router2,
address _router3,
address _token1,
address _token2,
address _token3,
uint256 _amount
) external view returns (uint256) {
console.log(_amount);
uint amtBack1 = getAmountOutMin(_router1, _token1, _token2, _amount);
uint amtBack2 = getAmountOutMin(_router2, _token2, _token3, amtBack1);
uint amtBack3 = getAmountOutMin(_router3, _token3, _token1, amtBack2);
return amtBack3;
}
These are the router addresses I'm
"routers": [
{
"dex": "1inch",
"address": "0x1111111254EEB25477B68fb85Ed929f73A960582"
},
{
"dex": "sushiswap",
"address": "0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F"
},
{
"dex": "uniswap",
"address": "0x7a250d5630b4cf539739df2c5dacb4c659f2488d"
}
],
I followed the link suggested in the error logs and read about the CALL_EXCEPTION error code in the ethers documentation but the common causes don't seem to apply here, tried the suggested debugging solution and I can access other functions in the contract just these that are throwing error.
I'm still kinda wet behind the ears when it comes to solidity and blockchain so I don't really understand what it is that I'm doing wrong. Am I interacting with the contract wrongly, or passing the wrong variables? Can someone please enlighten me, thanks.