How the exchange calls my Smart Contract methods?

I’m new to the smart contract world and there are many things that I still don’t get how works.

I’ve created a simple contract with the following methods:

constructor()
    {
        tokenCreator = msg.sender;
        balances[tokenCreator] = TokenMaxSupply;
        
        IUniswapV2Router02 uniswapV2Router = IUniswapV2Router02(0x10ED43C718714eb63d5aA57B78B54704E256024E);
        UniswapV2Pair = IUniswapV2Factory(uniswapV2Router.factory()).createPair(address(this), uniswapV2Router.WETH());
        UniswapV2Router = uniswapV2Router;
    }
    
    /**
    * @dev Returns the total supply of this token
    */
    function TotalSupply() public view returns (uint256)
    {
        return TokenMaxSupply;
    }
    
    /**
    * @dev Check the number of tokens owned by an address including holder reflections
    */
    function CheckAddressBalance(address addressToCheck) public view returns (uint256)
    {
        return balances[addressToCheck];
    }
    
    /**
    * @dev Check the allowance between addresses
    */
    function CheckAllowance(address from, address to) public view returns (uint256)
    {
        return allowances[from][to];
    }
    
    /**
    * @dev Transfers tokens from one address to another.
    */
    function TransferTokens(address sendingAddress, address addressToSend, uint256 amount) public returns (bool)
    {
        require(addressToSend != address(0), 'Invalid Address.');
        require(sendingAddress != address(0), 'Invalid sending Address.');
        require(balances[sendingAddress] >= amount, 'Not enough tokens to transfer.');
        
        //Decrease sender balance
        balances[sendingAddress] = balances[sendingAddress].sub(amount);
        
        //Add the new amount to receiver address
        balances[addressToSend] = balances[addressToSend].add(amount);
        
        emit Transfer(sendingAddress, addressToSend, amount);
        return true;
    }

My goal is to deploy the contract and add it to pancakeswap.

My question is: When someone wants to buy my token in pancakeswap, the method TransferTokens should be called to add/subtract the tokens. How pancakeswap knows that it needs to call this function?

Or do pancakeswap calls a specific method and I need to change the name of the method?

In reality the transfer function is called automatically. I mean when you send a token implicity call its transfer function

You mean that methods from interface IERC20 (totalSupply(), balanceOf(), transfer(), etc.) are called automatically from exchanges or wallets?
So I need to implement those methods obligatorily or my token won’t work?

If that’s the case, do I need to implement the interface and inherit those methods in my token code, or can I add directly those methods to my token contract?

Hi, welcome! :wave:

I think pancake swap will call the function transferFrom() to transfer tokens, and there is a modifier override for the function transferFrom, so you can override it to add your logic.

Ok. It seem that I need to implement the IERC20 interface becuse those will be the methods that will be called by exchanges/wallets.