0

anothe rissue presents itself! My problem is with the swapback function: (the function is public because i was testing it, it should be internal)

function swapBack() public swapping {
    uint256 amountToSwap = IBEP20(address(this)).balanceOf(address(this));
    require(amountToSwap > 0, "Nothing to swap");
    
    address[] memory path = new address[](2);
    path[0] = address(this);
    path[1] = WBNB;

    uint256 balanceBefore = address(this).balance;

    router.swapExactTokensForETHSupportingFeeOnTransferTokens(
        amountToSwap,
        0,
        path,
        address(this),
        block.timestamp
    );

    uint256 amountBNB = address(this).balance.sub(balanceBefore); // total

    // calculate relational dividends
    uint256 divTax = amountBNB.div(totalFee.sub(burnTax));
    
    // spread the pool costs relative to tax values
    uint256 amountBNBLiquidity = divTax.mul(liquidityFee);
    uint256 amountBNBMarketing = divTax.mul(marketingFee);
    uint256 amountBNBDev = divTax.mul(devFee);

    (bool tmpSuccess,) = payable(marketingFeeReceiver).call{value: amountBNBMarketing}("");
    (tmpSuccess,) = payable(devFeeReceiver).call{value: amountBNBDev}("");
    require(tmpSuccess, "Tax went unpaid to receivers");
    
    // Supress warning msg
    tmpSuccess = false;

    if(amountBNBLiquidity > 0){
        router.addLiquidityETH{value: amountBNBLiquidity}(
            address(this),
            amountBNBLiquidity,
            0,
            0,
            autoLiquidityReceiver,
            block.timestamp
        );
        emit AutoLiquify(amountBNBLiquidity, amountBNBLiquidity);
    }

}

the idea is that when the token amount reaches a certain threshold, the tokens that have been sent to the contract would be converted into busdT, and then split up to the accounts with their respective tax % amount defined here:

uint256 public liquidityFee    = 10;
uint256 public burnTax         = 10;
uint256 public marketingFee    = 5;
uint256 public devFee          = 5;
uint256 public totalFee        = marketingFee + liquidityFee + devFee + burnTax; // total 3%
uint256 public feeDenominator  = 1000;

but for some reason, the transaction fails with "Eip838ExecutionError: execution reverted: TransferHelper: TRANSFER_FROM_FAILED"

there is liquidity in the LP and there are tokens in the contract but for some reason it doesnt work. could someome please have a look and see what could be the issue?

thanks!

1 Answer 1

0

it looks like you might be missing an approval step before calling swapExactTokensForETHSupportingFeeOnTransferTokens. Try adding this line of code to approve the router to spend the tokens on behalf of the contract:

_approve(address(this), address(router), amountToSwap);

Incorporate this line before the swap function call in your swapBack function. This should resolve the "TRANSFER_FROM" error.

Please let me know if this works for you or if you have any other questions!

1
  • Thank you for your help, adding a similarly formulated line worked
    – CFC
    Commented Jun 12 at 4:13

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.