ive been working on my dapp, fairly new to solidity, im trying to make a smart contract that provides a interface to buy tokens from Uniswap using ETH and then send the tokens back to my smart contract for staking. Problem is when i set the swapExactETHForTokens address_to parameter to address(this) it fails to swap. But when i set it to an external address, it swaps successfully. How can i work around this?
function _buyGuns() public payable returns (uint256) {
address[] memory path = new address[](2);
path[0] = _router.WETH();//sell
path[1] = address(this);
uint deadline = block.timestamp + 300; //consider passing from frontend
address addr = payable(address(this));
uint[] memory tokenAmount_ = _router.swapExactETHForTokens{value: msg.value}(
0, //always succeeds
path,
addr, //address to
deadline
);
uint256 outputTokenCount = uint256(tokenAmount_[tokenAmount_.length - 1]);
return (outputTokenCount);
}
error message on remix when i try swap:> Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? The execution failed due to an exception. Reverted>