I need some understanding why I get errors in the following code. I try to call function getAmountOutMin to get the amount of particular token(_tokenOut) I can buy (amountOutMins) on _amountIn of token _tokenIn. But regardless of amount I specify in _amountIn(for instance, I specify 10000000000000000 WEI, that's about 0.01 WETH) I get the error.
call to Uniswap.getAmountOutMin errored: execution reverted
As I token_in I specify 0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6 address(WETH in Goerli), token_out = 0x78c26A88c19ae0311e0121f7350735b842AFFd41.
I do need some help to understand what I'm doing wrong
pragma solidity ^0.8.7;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "hardhat/console.sol";
interface IUniswapV2Router {
function getAmountsOut(uint256 amountIn, address[] memory path)
external
view
returns (uint256[] memory amounts);
function swapExactTokensForTokens(
uint256 amountIn,
uint256 amountOutMin,
address[] calldata path,
uint256 deadline
) external returns (uint256[] memory amounts);
}
contract Uniswap {
address private constant uniswap_goerli = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;
address private constant WETH_Goerli = 0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6;
address private constant ETH_Goerli = 0x8d27431c473E83611847D195d325972e80D1F4c1;
///call via web3
function approve_for_uniswap(uint _amount_to_send) public payable{
// IERC20(ETH_Goerli).approve(address(this),_amount_to_send);
// IERC20(ETH_Goerli).transfer(address(this),_amount_to_send);
IERC20(ETH_Goerli).approve(uniswap_goerli,_amount_to_send);
}
function _swap_token(address _token_in,address _token_out,uint _amountIn,uint _amountOutMin,address _to) external{
address[] memory path;
if (_token_in==WETH_Goerli || _token_out == WETH_Goerli){
path = new address[](2);
path[0]=_token_in;
path[1] = _token_out;}
else {
path = new address[](3);
path[0] = _token_in;
path[1] = WETH_Goerli;
path[2] = _token_out;
}
IUniswapV2Router(uniswap_goerli).swapExactTokensForTokens(_amountIn, _amountOutMin, path, _to, block.timestamp);
}
function getAmountOutMin(address _tokenIn, address _tokenOut, uint256 _amountIn) external view returns (uint256) {
address[] memory path;
if (_tokenIn == WETH_Goerli || _tokenOut == WETH_Goerli) {
path = new address[](2);
path[0] = _tokenIn;
path[1] = _tokenOut;
} else {
path = new address[](3);
path[0] = _tokenIn;
path[1] = WETH_Goerli;
path[2] = _tokenOut;
}
uint256[] memory amountOutMins = IUniswapV2Router(uniswap_goerli).getAmountsOut(_amountIn, path);
return amountOutMins[path.length -1];}```