I have implemented the Uniswap v2 router function called
function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts);
I implemented it like this…
function swapTokensForExactEthTest(
uint256 _amountOut,
address _tokenIn,
uint256 _amountInMax,
address _to
) external {
IERC20(_tokenIn).transferFrom(msg.sender, address(this), _amountInMax);
IERC20(_tokenIn).approve(UNISWAP_ROUTER_ADDRESS, _amountInMax);
address[] memory path;
path = new address[](2);
path[0] = _tokenIn;
path[1] = WETH;
IUniswapV2Router01(UNISWAP_ROUTER_ADDRESS).swapTokensForExactETH(
_amountOut,
_amountInMax,
path,
_to,
block.timestamp
);
}
and testing it like this…
//WBTC_WHALE=0xF977814e90dA44bFA03b6295A0616a897441aceC
const WHALE = process.env.WBTC_WHALE;
const AMOUNT_OUT_MIN = 1;
const TOKEN_IN = WBTC; //0x2260fac5e5542a773aa44fbcfedf7c193bc2c599
const AMOUNT_IN = 100000000;
it('should swap tokens for exact eth', async()=>{
tokenIn = await IERC20.at(TOKEN_IN);
testUniswap = await TestUniswap.new();
await testUniswap.swapTokensForExactEthTest(
AMOUNT_OUT_MIN,
tokenIn.address,
AMOUNT_IN,
accounts[0],
{
from: WHALE
}
);
console.log(`sent ${balanceOf(accounts[0])}`);
});
My problem is when I’m testing it like the testing code above, I’m getting…
Error: Returned error: VM Exception while processing transaction: revert
I caught the error using remix. The error is due to gas limit and says…
Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? Gas estimation: 0x2dc6c0 Gas limit: 0x2dc6c0
more specifically…
code -3200 "message": "VM Exception while processing transaction: revert"
I can’t understand how to solve it. Anyone please help.
Anyone please help.