Tokenomics / does buying&selling tokens trigger the _transfer function?

good morning.

yes, my main question is about tokenomics- however, one thing confuse me profusely!
Is buying a token from DEX/CEX the same as transfer? and if so or not so, does buying tokens from DEX/CEX invoke the transfer function?

anyways, if any one is able to provide some clarity with the question above I will appreciate it with much gratitude.

okay, so the main question of this post:
I am trying to add tokenomics to my token (a 10% tax for buying, 20% tax for selling). I have already added taxing to my transfer function (I will paste the code at the end of this post), however, I am not sure how to add this to buying and selling of my token. ( Like when someone buys from pancakeswap )
I've scanned safemoon's code on their GitHub and found no buying/selling function(s) that was using tokenomics- however, I've noticed transfer functions ( which has me confused as to what actually happens 'under-the-hood' when tokens are bought. hint, my question above ).

//Adding Tax To Transfer(s)
    function transfer(address recipient, uint256 amount) public override returns(bool){
        
        //conditions
        require(recipient != bankerContract, "Cannot Send To Bank");
        require(recipient != burnerContract, "Cannot Send To Burner");
        
        
        address sender = msg.sender;

        //Checking if no tax is applied
        if (excludedFromTax[msg.sender] == true) {
            _transfer(sender, recipient, amount);
        }
        else{
            uint256 bankAmount = amount.mul(taxFee) / 100;

            //Sending to bank (tax)
            _transfer(_msgSender(), bankerContract, bankAmount);
            //Sending To Address
            _transfer(_msgSender(), recipient, amount.sub(bankAmount));

        }
        return true;
    }

the token is ERC20, and openzeppelin's library is installed. I am using truffle for developing this token and my text editor is VSC.

I did notice one method- or I think it's a hook of sorts... called _beforetokentransfer . It is an empty function, but do I create the taxing inside this function?
I think there is also an _aftertokentransfer which- I'm guessing- is where I would implement burn functionality if I had something like 'auto burn after every X amount of coins bought'

:1234: Code to reproduce

Hey @XskodeMedia ,
when you buy, you will trigger the transfer() method while when you make a sell, the DEX will call the transferFrom() method. Btw in both cases the internal function called is the _transfer(). If you want to add fees for buy and sell the easiest way to know the type of the trade is this:

BUY:

if(from == pair)

SELL

if(to == pair)

The process of buying and selling tokens on a DEX or CEX involves transferring the tokens between different Ethereum addresses. When you buy a token on a DEX or CEX, you are sending a certain amount of Ether to a contract address in exchange for the token. It involves the transfer function being called in the smart contract of the token.

In order to apply your tokenomics (i.e., the tax on buying and selling) to the process of buying and selling your token on a DEX or CEX, you will need to modify the transfer function to add tax on buying and selling.

We need to check whether the sender or the recipient is a DEX or CEX contract address. and if so, apply the appropriate tax.

Sample code:

if (recipient == dexContractAddress) {
  // Apply 10% tax on buying
  uint256 taxAmount = amount.mul(10) / 100;
  _transfer(sender, bankerContract, taxAmount);
  // Send the remaining amount to the recipient
  _transfer(sender, recipient, amount.sub(taxAmount));
}
else if (sender == dexContractAddress) {
  // Apply 20% tax on selling
  uint256 taxAmount = amount.mul(20) / 100;
  _transfer(sender, bankerContract, taxAmount);
  // Send the remaining amount to the recipient
  _transfer(sender, recipient, amount.sub(taxAmount));
}
else {
  // No tax applied
  _transfer(sender, recipient, amount);
}

This code will apply a 10% tax on buying and a 20% tax on selling when the token is transferred to or from a DEX or CEX contract address. You will need to replace dexContractAddress with the actual contract address of the DEX or CEX that you are using.