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 SwapExactTokensForETH also performs the same action during execution.
How can we differentiate during execution time between AddLiquidtyETH or SwapExactTokensForETH?
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.add(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.add(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.
Best Answer
Unfortunately, this is not possible if you don't know when your tokens are added to liquidity. Why?
Here is the "sell" function of pancakeSwap's router:
As you can see, the tokens are transferred using
TransferHelper.safeTransferFrom(path[0], msg.sender, PancakeLibrary.pairFor(factory, path[0], path[1]), amountIn);
Which is a wrapper for the
transfer()
function:Same goes with
addLiquidityEth()
:You can only modify the behavior of the transfer function by adding a hook which would trigger depending of one of the 3 inputs:
from
,to
andvalue
.However,
from
andto
andvalue
are the same for both functions and are thus useless to detect which function is used, preventing you in effect from creating a hook to differentiate functions.