Unable to swap tax amount to ETH when someone is buying token

50 Views Asked by At

I am working on an erc20 token which deducts tax on buying and selling of token, the problem i have been tackling with is when a person buying a token the transaction is getting failed, but when someone is selling the token it's working fine, swapping the tax amount into ETH, I am using Uniswap V2, please help me out in this issue, looking forward for your responses, Thanks in advance.

function _update(address from, address to, uint256 value) internal override {
        if(!tradingEnabled && from != address(0)) revert TradingNotEnabledYet();
        if(value > maxTxAmount && !_isExcludedFromFees[from] && !_isExcludedFromFees[to]) revert ExceededMaxTxLimit(value);
        address pairAddr = _helperFactory.getPair(address(this), WETH_ADDRESS);
        uint256 taxAmount;
      if(to == pairAddr && !_isExcludedFromFees[from]) {
            taxAmount = (value * sellTax) / BASE;
            value = value - taxAmount;
        }
      else if(from == pairAddr && !_isExcludedFromFees[to]) {
            taxAmount = (value * buyTax) / BASE;
            value = value - taxAmount;
        }
      if(taxAmount > 0) {
        super._update(from, address(this), taxAmount);
        if(to == pairAddr) _swapTokensForETH();
        //if(from == pairAddr) _swapTokensForETH(); Error Here

        }
        super._update(from, to, value);
    }

function _swapTokensForETH() private {
     if (allowance(address(this), UNISWAP_V2_ROUTER) == 0) IERC20(address(this)).approve(UNISWAP_V2_ROUTER, type(uint256).max);
        address[] memory path = new address[](2);
        path[0] = address(this);
        path[1] = WETH_ADDRESS;
         uint256 amount = balanceOf(address(this));
        _helperRouter.swapExactTokensForETHSupportingFeeOnTransferTokens(
            amount,
            0,
            path,
            adminWallet,
            block.timestamp
        );
    }

Error: VM Exception while processing transaction: reverted with reason string 'UniswapV2: TRANSFER_FAILED'

0

There are 0 best solutions below