Unisswap router V2 error when call inside _transfer

Hi my friends. I got a problem:

I create an IBEP20 token, in _stransfer, I check if user is selling token in Pancake, I will send 5% token from user to token address. Now, what I want is: I use swapExactTokensForETHSupportingFeeOnTransferTokens to swap this token to another wallet (I call marketing wallet).

That's weird: If I run function swapTokenToBNB_to_marketingTeam (line 736), it will be successful to swap toekn in Token address to BNB and send to marketing wallet.

Buy the way, I did approve address token (address.this), for spender is: router address, pair address already.

But, when I sell token in Pancake,then in _transfer, it is error with swapTokenToBNB_to_marketingTeam (line 726).

What is wrong I am? Thank you guys.

Hello @Loli_Pop

Its difficult to know what exactly happens without the trace of a reverting transaction. I'm wondering if the fee might cause DEX to not the the amount of tokens its expecting, and consequently rejecting the trade.

Please note that your approveMyself(address,uint256) is a CRITICAL security issue.

Anyone can call it, putting their own address in the dex param, with any amount ... and that will allow them to drain any token owned by address(this) ... which I understand is all the fees that you are trying to get.

Thank you, I understand. This function just for test, I use to make sure Token Address did approve itselft to Router address.

Please, I need to know that: In _transfer (of ERC20), could I call swapExactTokensForETHSupportingFeeOnTransferTokens, is that possible. my friend?

Well, you have to make sure you don't create an infinite loop.

If _transfer calls swapExactTokensForETHSupportingFeeOnTransferTokens ... and then swapExactTokensForETHSupportingFeeOnTransferTokens trigger a transfer on the same token ... then you are in trouble.

Also, the fact that is possible doesn't mean it a good idea. A swap is an expensive operation ... and doing that on (any?) transfer will cause tremendous cost to your users. I would recommend not doing to. You can keep you fees in the token ... and only swap when you want to withdraw the fees.

Thank you for your advice. I did check line 719: sender!=address(this), mean if this address (mean token) call swapExactTokensForETHSupportingFeeOnTransferTokens, it will not be count fee.

But, I dont know why: I can run funtion line 736 alone is success, but when sell token in Pancake is error, too weird...

When you swap, is there any change a uniswap pair might trigger a transfer? What is you uniswap path ?

Yes, when I buy token on Pancake (Uniswap ROuter v2), token will transfer from user to pair address.
Then I call swapExactTokensForETHSupportingFeeOnTransferTokens in _transfer.

I dont know why Pancake how red alert.

and that will trigger what ? Is there any chance it trigger a transfer on your token that will then trigger another swapExactTokensForETHSupportingFeeOnTransferTokens ... and so on ?