Edit: I see contracts have a Pancakeswap/Uniswap pair address created by the factory address and use that to interact, so is it safe to say that if the sender is that address, then it’s a sell and viceversa? Or are there case in which you don’t really interact with the router?
TL;DR: Yes. Check for the Token LP Pair Address to be the "From", that means it's a buy of your token from Pancake Swap. If the "From" is not the Token LP Pair Address then it's a sell.
You won't interact with the router if you are using a different exchange than Pancake Swap.
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Full Explanation:
You're on the right track..... To truly understand this let's look at two PancakeSwap/UniSwap transactions. You'll see that in both Transactions the To and From are the "same".
https://bscscan.com/tx/0x11f0ae8ead81d75254f0761674034376b78e4da7639795e9d8412ecdc13f4f20
In this transaction we can see that a user swapped BNB for PIT.
This would be a "buy" for the User, and a "sell" for the PancakeSwap LP token.
It all depends on how you view it, but what's important is that we see it is interacting with three different addresses.
PancakeSwap Router: 0x05ff2b0db69458a0750badebc4f9e13add608c7f
PancakeSwap PIT LP Pair Address: 0xB450CBF17F6723Ef9c1bf3C3f0e0aBA368D09bF5
Buyer of PIT, Seller of BNB: 0x109e4b85f59f9e035e379b235233daec93bafbac
In the Tokens Transferred section we see that it goes From the PancakeSwap Router and the LP Pair.
We also see that specifically, 1 transfer is from the PIT LP token to the Buyer of PIT, which interacts with our Smart Contract.
This means our true From is the PancakeSwap LP Pair.
Check the screenshot below for guidance.
Let's look at another transaction from this same project.
https://bscscan.com/tx/0xfc22ec34a3bb36af831b98ef48fb48fe7d59fbc5b8b83aec889dba14b35da6e7
In this tx we have a seller of PIT and a buyer of BNB. But we have the same two addresses for the Router and LP Pair, but we have a different User.
Buyer of BNB, Seller of PIT: 0x3252fd862dcc05d870d45ee843ab044c56245574
This means that in our smart contract we see the User as the seller, our true From.
See the below screenshot for guidance.
Let's look at some Transfer Code.
function transferTokens(address sender, address recipient, uint256 transferAmount, bool takeFee) private {
if (!takeFee) {
removeAllFee();
}
_takeLiquidity(taxLiquidity);
takeTeamFee(taxTeamFee);
takeReflectFee(reflectFee, taxFee);
amountTransferedWithinOneDay[sender] += transferAmount;
emit Transfer(sender, recipient, taxTransferAmount);
if (!takeFee){
restoreAllFee();
}
}
If we want to check and see if this is a buy or a sell, we would check the
sender
parameter.
We could add an if statement like so to check the sender.
if(sender == LPtokenAddress ){
// do functionality if the sender is the LP Token in Pancake Swap.
}
The question you might be asking yourself right now is "how do I know the address of a token's LP pair, before I deploy the contract?"
In safemoon, they have some code in the constructor.
uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory()).createPair(address(this), _uniswapV2Router.WETH());
The variable uniswapV2Pair
is the LPtokenAddress.
You will need to come up with your own ways of what to do with this variable and how you use it in your code.
If you have any questions let me know.