# How to tell from inside the contract exclusively using Pancakeswap if a transfer represents a sell or a buy

**URL:** <https://forum.openzeppelin.com/t/how-to-tell-from-inside-the-contract-exclusively-using-pancakeswap-if-a-transfer-represents-a-sell-or-a-buy/7187>\
**Category:** Support\
**Created:** [April 21, 2021, 7:49am UTC](https://forum.openzeppelin.com/t/how-to-tell-from-inside-the-contract-exclusively-using-pancakeswap-if-a-transfer-represents-a-sell-or-a-buy/7187 "2021-04-21T07:49:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![LSparkzwz](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/lsparkzwz/32/4979_2.png) [@LSparkzwz](https://forum.openzeppelin.com/u/LSparkzwz)\
**Post date:** [April 21, 2021, 7:49am UTC](https://forum.openzeppelin.com/t/how-to-tell-from-inside-the-contract-exclusively-using-pancakeswap-if-a-transfer-represents-a-sell-or-a-buy/7187/1 "2021-04-21T07:49:58Z")

</div>

I have a token contract that will exclusively use Pancakeswap as trading platform. How can I tell in the contract if a transfer represents someone buying the token or someone selling it?

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?

---

<div class="post-metadata">

**Author:** ![Yoshiko](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/yoshiko/32/6858_2.png) [@Yoshiko](https://forum.openzeppelin.com/u/Yoshiko)\
**Post date:** [April 23, 2021, 7:22am UTC](https://forum.openzeppelin.com/t/how-to-tell-from-inside-the-contract-exclusively-using-pancakeswap-if-a-transfer-represents-a-sell-or-a-buy/7187/2 "2021-04-23T07:22:32Z")

</div>

> 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](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.

 ![transf](https://us1.discourse-cdn.com/flex022/uploads/zeppelin/original/2X/c/cde25a9cc72c66729994dccc70a61d0a1a85a20c.png)

Let's look at another transaction from this same project.  
[https://bscscan.com/tx/0xfc22ec34a3bb36af831b98ef48fb48fe7d59fbc5b8b83aec889dba14b35da6e7](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.

 ![tofrom](https://us1.discourse-cdn.com/flex022/uploads/zeppelin/original/2X/8/87b432ce44f72efffeeefb4bb2b072e403f85365.png)

Let's look at some Transfer Code.

```auto
    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.

```auto
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.

```auto
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.
