1

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.

Transfer:

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);
        return;
    }


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

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

    super._transfer(from, to, amount);


}

sellForBNBAndSendToDev:

function sellForBNBAndSendToDev() public  {
    
    swappingToBnb = true;
    swapTokensForEth(balanceOf(address(this)));
    uint256 bnbToSend = address(this).balance;
    _devWallet.transfer(bnbToSend);
    swappingToBnb = false;
}

SwapTokensForEth:

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
    uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
        tokenAmount,
        0, // accept any amount of ETH
        path,
        address(this),
        block.timestamp
    );

}

-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):

https://testnet.bscscan.com/tx/0x1ca182c630a186d9f1f4b0d564d9de1703e694520e8eb0718b4dba68a6a78744

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

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

https://testnet.bscscan.com/tx/0x1fb50a5c87ec05dc018a5ce87437a28c0bcf91800a0bf7e6005d307d946a3ba7

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

https://testnet.bscscan.com/tx/0xce768de8dd9304845df1826a4e22f96200e3b6185db74aa62c9adc96dbc8db12

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.

10
  • Does it throw error before sending transaction or not?! Also, is there liquidity or not?! Sep 8 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 at 17:55
  • What is the "trick"?! Sep 8 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 at 18:38
  • Why don't you dedicate some initial balance to the contract?! So it fails even it has balance?! Sep 8 at 19:03
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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