i’m trying to add liquidity in uniswap v2 router from a solidity smart contract, but it seems to fail even though the allowance have been given.
pragma solidity ^0.8.0;
import "./IUniswapV2Router01.sol";
import "./IERC20.sol"; // Import ERC20 interface if needed
contract MyContract {
IUniswapV2Router01 public uniswapRouter;
address public tokenAddress;
constructor(address _router, address _tokenAddress) {
uniswapRouter = IUniswapV2Router01(_router);
tokenAddress = _tokenAddress;
}
function addLiquidityETH(uint amountTokenDesired, uint amountTokenMin, uint amountETHMin, uint deadline) external payable {
// Approve the router to spend the token on behalf of this contract
IERC20 token = IERC20(tokenAddress);
token.approve(address(uniswapRouter), amountTokenDesired);
// Call addLiquidityETH function
uniswapRouter.addLiquidityETH{value: msg.value}(
tokenAddress,
amountTokenDesired,
amountTokenMin,
amountETHMin,
address(this),
deadline
);
}
}
Can someone explain me what’s the mistake i’m doing ? Or could help me with the actual solidity code to call addLiquidityETH function in uniswapv2router.
New contributor
Infected Mice is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.