I have a simple swap function in Solidity which can set custom slippage for the token amount. My problem is it doesn't work with tokens which has sell taxes. For instance, if a token has a 5% percent sell tax, the below code will fail and it doesn't matter what value I set for percent. I am using the return value of the getAmountsOut function of Uniswap V2, which returns the minimum available tokens to get out, and I go even below of this value with 30%. Other thing what confuses me that I can set a 30% slippage by using 70 as a value for percentage, but it won't work. However when I try to sell the same tokens manually on the Pancakeswap website, they can be sold at 25% or much less slippage. So 30% slippage is more than 25%, I don't understand why the tx with the smaller slippage goes through and the bigger value fails.

With tokens without tax fees, the code works properly. My first idea was that the slippage calculation is incorrect, but I get the value straight from the DEX's smartcontract and go under with minus 30%, so I can't imagine why it doesn't work. Maybe my implementation has a trivial bug which I couldn't identify.

function swapTokensWithSlippagePercent(address tokenToSellAddr, 
                                address ethAddr,
                                uint deadline,
                                uint percent) 
                                external {

    IBEP20 tokenContractObj = IBEP20(tokenToSellAddr);
    uint256 myBalance = tokenContractObj.balanceOf(address(this));
    tokenContractObj.approve(_pancakeSwapRouterAddress, myBalance);
    require(myBalance > 0);
    address[] memory path = new address[](2);
    path[0] = tokenToSellAddr; 
    path[1] = ethAddr;
    uint[] memory tokenAmountToSwap;
    uint[] memory amounts;        
    tokenAmountToSwap = _pancakeRouter.getAmountsOut(myBalance, path);
    uint toGet;  
    toGet = tokenAmountToSwap[1].div(100).mul(percent);      
    amounts = _pancakeRouter.swapExactTokensForETH(myBalance, toGet, path, address(this), deadline);


1 Answer 1


That's because for these tokens you need to call swapExactTokensForETHSupportingFeeOnTransferTokens or swapExactETHForTokensSupportingFeeOnTransferTokens.

You can always use these method instead of swapExactTokensForETH to support both kind of tokens, just keep in mind that an extra fee may be deducted if so.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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