How to differentiate between AddLiquidity and SwapEthForExactTokens on contract function execution on Uniswap Fork

We are trying to process a fee when tokens are "Sold" on PancakeSwap. For now "Sold" is when the user swaps their tokens for BNB. Initially our current logic would also charge a fee when adding to the LP. (Which is not considered a sell in our usecase)

We noticed Pancake uses AddLiquidityETH to transfer tokens to the LP, and SwapETHForExactTokens also performs the same action during execution.

How can we differentiate during execution time between AddLiquidtyETH or SwapETHForExactTokens?

Our current _transfer function looks as follows:

    /// @dev overrides transfer function to meet tokenomics of UFF
    function _transfer(address sender, address recipient, uint256 amount) internal virtual override antiWhale(sender, recipient, amount) {
        // If not modified, full ammount is transfer
        uint256 sendAmount = amount;
        // Buy action?
        if(lpToken != address(0) && msg.sender == lpToken && recipient != PCSRouter){
            // default buy fee is 5% / Token OPs Team (dev and marketing)
            uint256 buyFeeAmount = amount.mul(maxBuyFee).div(10000);
            sendAmount = amount.sub(buyFeeAmount);
            require(amount == sendAmount + buyFeeAmount, "UFF::transfer: Buy value invalid");
            super._transfer(sender, team, buyFeeAmount);

        // Sell Action?
        if(lpToken != address(0) && recipient == lpToken && msg.sender != PCSRouter){
            uint256 sellFeeAmount = amount.mul(maxSellFee).div(10000);
            sendAmount = amount.sub(sellFeeAmount);
            require(amount == sendAmount + sellFeeAmount, "UFF::transfer: Sell value invalid");
            super._transfer(sender, team, sellFeeAmount);
        // default 100% of transfer sent to recipient
        super._transfer(sender, recipient, sendAmount);

Thank you in advance

Hi, if you want to charge fee when swap tokens, I think you should call function that supports fee, such as swapExactTokensForTokensSupportingFeeOnTransferTokens

Emmm, I think when you call AddLiquidtyETH, the token will transfer to the swap, and when you call SwapETHForExactTokens, the token will transfer from the swap.

1 Like

Thank you Skyge. My mistake, I made a typo. The router we are using uses AddLiquidity and SwapExactTokensForETH.

The issue is both functions transfer tokens to the LP. But they have diferent purposes. AddLiquidity is to receive LP tokens, and SwapExactTokensForETH outputs ETH.

If so, sorry, I do not know. But I think you should call SwapExactTokensForETH at first and then try to call AddLiquidity

1 Like

IIRC for uniswap v2, if you interact through the router, the funds don't actually go to the router address. It goes directly into the pair address but you'll need to confirm this for PCS.

Assuming that is true, you just need to add an additional check to see if either sender or recipient is the pair address, if so then you just deduct the fee (assuming fee is same on both sides).

1 Like

When users addLiquidity, the router is also who execute TransferFrom , as you say he uses the previously set allowance on behalf of msg.sender.

Problem with this is , to my _transfer context router is the msg.sender and I dont know if user called AddLiquidityEth or SwapExactTokensForETH from the PancakeSwap frontend.

If I knew, I could set my fee at the right time.


Ah okay, I misunderstood your question. What if you get the lp token balance before the transfer and compare it with the lp token balance after the transfer? If there are any changes, it means it is either an add / remove liquidity. If they are the same (including 0) then it is either a buy or sell (depending on whether or not the from / to address is the pcs pair address)