My ERC20 token has a function to add liquidity to a specific liquidity pool. I call uniswap swapExactTokensForETHSupportingFeeOnTransferTokens, and even though the pair have the input amount it gives me this error
Error: VM Exception while processing transaction: reverted with reason string 'UniswapV2Library: INSUFFICIENT_INPUT_AMOUNT'
Code:
function _swapAndLiquify(uint256 amount) private {
uint256 half = amount.div(2);
uint256 otherHalf = amount.sub(half);
uint256 initialAmount = address(this).balance;
_swapTokensForBNB(half);
uint256 newAmount = address(this).balance.sub(initialAmount);
_addLiquidity(otherHalf, newAmount);
emit SwapAndLiquify(half, newAmount, otherHalf);
}
function _swapTokensForBNB(uint256 tokenAmount) private {
address[] memory path = new address[](2);
path[0] = address(this);
path[1] = dexRouter.WETH();
_approve(address(this), address(dexRouter), tokenAmount);
dexRouter.swapExactTokensForETHSupportingFeeOnTransferTokens(
tokenAmount,
0,
path,
address(this),
block.timestamp.add(300)
);
}
function _addLiquidity(uint256 tokenAmount, uint256 bnbAmount) private {
_approve(address(this), address(dexRouter), tokenAmount);
dexRouter.addLiquidityETH{value: bnbAmount}(
address(this),
tokenAmount,
0,
0,
address(this),
block.timestamp.add(300)
);
}
function _transfer(
address from,
address to,
uint256 amount
) internal override {
require(from != address(0), "zero address");
require(to != address(0), "zero address");
require(amount > 0, "Transfer amount must be greater than zero");
require(!isBlacklisted[from], "Address is blacklisted");
require(
tradingIsEnabled ||
(isExcludedFromFees[from] || isExcludedFromFees[to]),
"Trading not started"
);
bool excludedAccount = isExcludedFromFees[from] ||
isExcludedFromFees[to];
if (!swapping) {
swapping = true;
if (excludedAccount) {
uint256 burnedTokens = balanceOf(deadAddress);
if (burnedTokens >= tSupply.div(2)) {
setBurnFee(0);
emit BurnFeeStopped(burnedTokens, burnFee);
}
super._transfer(from, to, amount);
} else {
if (burnFee > 0) {
uint256 burnedTokens = balanceOf(deadAddress);
if (burnedTokens >= tSupply.div(2)) {
setBurnFee(0);
emit BurnFeeStopped(burnedTokens, burnFee);
}
uint256 tokensToBurn = amount.mul(burnFee).div(100);
super._transfer(from, deadAddress, tokensToBurn);
}
if (automatedMarketMakerPairs[to]) {
if (teamSellFee > 0) {
uint256 tokensToTeam = amount.mul(teamSellFee).div(100);
super._transfer(from, teamWallet, tokensToTeam);
}
if (lotteryFee > 0) {
uint256 tokensToLottery = amount.mul(lotteryFee).div(100);
super._transfer(from, lotteryWallet, tokensToLottery);
}
} else {
if (teamBuyFee > 0) {
uint256 tokensToTeam = amount.mul(teamBuyFee).div(100);
super._transfer(from, teamWallet, tokensToTeam);
}
}
if (liquidityFee > 0) {
uint256 tokensToLiquidity = amount.mul(liquidityFee).div(100);
super._transfer(from, address(this), tokensToLiquidity);
_swapAndLiquify(tokensToLiquidity);
}
uint256 taxedAmount;
if (automatedMarketMakerPairs[to]) {
taxedAmount = amount.sub(amount.mul(totalSellFee).div(100));
} else {
taxedAmount = amount.sub(amount.mul(totalBuyFee).div(100));
}
super._transfer(from, to, taxedAmount);
}
swapping = false;
}
}
Tests with hardhat:
it("should charge liquidity fee", async function () {
await contract.startLiquidity(router.address);
const TOKENAmount = ethers.BigNumber.from(500).mul(
ethers.BigNumber.from(10).pow(18)
);
const ETHAmount = ethers.BigNumber.from(1000).mul(
ethers.BigNumber.from(10).pow(18)
);
await contract.approve(router.address, ethers.constants.MaxUint256);
await router
.connect(owner)
.addLiquidityETH(
contract.address,
TOKENAmount,
TOKENAmount,
ETHAmount,
owner.address,
ethers.constants.MaxUint256,
{ value: ETHAmount }
);
await contract.transfer(address3.address, TOKENAmount);
const transactAmount = ethers.BigNumber.from(100).mul(
ethers.BigNumber.from(10).pow(18)
);
await contract
.connect(address3)
.transfer(address4.address, transactAmount);
});