I have this token test contract: The contract has a transaction tax of 1%. The transaction fee is converted to (myContractToken to bnb) and sent to a wallet. It is used the balance of the token that the contract already had before the current transaction.


function _transfer(
    address from,
    address to,
    uint256 amount
) internal override {
    require(from != address(0), "ERC20: transfer from the zero address");
    require(to != address(0), "ERC20: transfer to the zero address");

    if(amount == 0) {
        super._transfer(from, to, 0);

    bool takeFee = !swappingToBnb && !_isExcludedFromFees[from] && !_isExcludedFromFees[to];

     if(takeFee) {
        uint256 contractTokenBalance = balanceOf(address(this));
        uint256 fees = amount.mul(devFee).div(100);
        amount = amount.sub(fees);
        super._transfer(from, address(this), fees);

    super._transfer(from, to, amount);



function sellForBNBAndSendToDev() public  {
    swappingToBnb = true;
    uint256 bnbToSend = address(this).balance;
    swappingToBnb = false;


function swapTokensForEth(uint256 tokenAmount) private {

    // generate the uniswap pair path of token -> weth
    address[] memory path = new address[](2);
    path[0] = address(this);
    path[1] = uniswapV2Router.WETH();

    _approve(address(this), address(uniswapV2Router), tokenAmount);

    // make the swap
        0, // accept any amount of ETH


-If I call with a wallet that is not excluded from the fee from the PancakeSwap interface the transaction fails (it is not slipage problem):


-If I delete call to sellForBNBAndSendToDev(); the transaction does not fail

-If I call sellForBNBAndSendToDev directly, the transaction doesn't fail:


-If instead of using the PancakeSwap interface, I do a "normal" transaction by sending tokens from one wallet to another. sellForBNBAndSendToDev() runs without fail:


That is, sellForBNBAndSendToDev () only fails when doing a swap with the PancaKswap interface.

On the other hand, I have tried to debug the transaction that fails, but I can't because I get this error: The method 'debug_traceTransaction' does not exist / is not available. Remix in BSC test net not-available-remix-in

---------New test----------

I have used this tool to debug failed transaction:

The problem is in

"sellForBNBAndSendToDev" -> "swapTokensForEth" -> "swapExactTokensForETHSupportingFeeOnTransferTokens" -> "pair.swap"

enter image description here

inside PancakeSwap lib.

Although it is useful information, I do not know what the problem is.

  • Does it throw error before sending transaction or not?! Also, is there liquidity or not?! Sep 8, 2021 at 17:52
  • Before sending the transaction, PancakeSwap locks the transaction. It tells me that a problem has occurred with the tokens and does not send the transaction to the metamask. I have the failed transaction executed because I have used a "trick" to submit the transaction even though PancakeSwap knows in advance that it is going to fail. Yes token have liquidity. Sep 8, 2021 at 17:55
  • What is the "trick"?! Sep 8, 2021 at 18:17
  • The first transaction does not fail, because the contract does not yet have the tokens in the balance. Therefore, sellForBNBAndSendToDev() is not executed in the first transaction. The second transaction "if (contractTokenBalance> 0)" is the one that fails. Therefore the "trick" so that the PancekeSwap interface does not stop the second transaction is to send the two transactions at the same time in a period of 1-3 seconds. If I wait a little longer, PancakeSwap will notice and stop the second transaction before it fails. Sep 8, 2021 at 18:38
  • Why don't you dedicate some initial balance to the contract?! So it fails even it has balance?! Sep 8, 2021 at 19:03

1 Answer 1


I have been analyzing the code of other tokens in production that use the call to "swapExactTokensForETHSupportingFeeOnTransferTokens" during a transaction. I have found a pattern, all tokens only use this call in sells, that is, when the contract token is "sold" for BNB.

I have verified that this problem does not occur in sells. Therefore I have added an if so that it only executes "swapExactTokensForETHSupportingFeeOnTransferTokens" on sells transactions and the problem does not occur.

  • Hello.... were you able to fix the problem? Could you share the code fixed? I am having this same problem even if I am swaping only for transaction from uniswapV2Router. Feb 24, 2022 at 12:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.