How to distinguish between adding liquidity and swapping tokens

Hi,

I'm adding fees to a token and I want to distinguish buys and sells when swapping.
I don't want to include fees when the token is transferred or liquidity is added. While testing, I realized that adding liquidity takes fees from the user as well.

How can I make sure fees are only accounted for when swapping and not when adding liquidity?

Some code:


function _transfer(address sender, address recipient, uint amount) internal override virtual{
        requires(...)

        variables(...)

        if (isExcludedFromFee[sender]) {
            takeFee = false;
        }

        bool Buy = getLPStatus(sender) || getRouterStatus(sender);
        bool Sell = getLPStatus(recipient) || getRouterStatus(recipient);

        taxes = (Buy) ? amount.mul(buyFeePercentage).div(10000) : taxes;
        taxes = (Sell) ? amount.mul(sellFeePercentage).div(10000) : taxes;

        if (!takeFee || taxes == 0){
            _realTransfer(sender, recipient, amount);
        } else {

            amount = amount.sub(taxes);
            _realTransfer(sender, recipient, amount);

            uint taxD = taxAmount.mul(devpercent).div(10000);
            uint taxBurn = taxAmount.sub(taxD).mul(burnpercent).div(10000);
            uint taxlps = taxAmount.sub(taxD).sub(taxBurn);
            _realTransfers(...)
        }
    }

Also, I'm making transfers to another smart contract/wallet because I'm not sure how to prevent sandwich attacks unless I hardcode a low slippage but sounds like a pretty bad choice. Sending the funds to another smart contract will allow me to change it in the future once it's properly optimized.

Thank you,

This is an internal function, you could create different external functions that call this one for each case, like a addLiquidity function that sends a parameter to _transfer indicating if a fee is to be charge.

I'm assuming you mean uniswap or pancakeswap. I think the router handles the liquidity events and only calls transfer on the token contract. So i dont think its possible to distinguish between a normal transfer and a liquidity event. You could exclude the router address from the tax event, but that would probably also exclude all swap events.