Adding liquidity/swapping tokens for BNB not working (uniswap/pancakeswap testnet)

I am attempting something quite simple, using the safemoon contract swapping functions as reference, with reflections removed.

This code, however, is not working, for some reason. I am using the Pancakeswap testnet router/factory addresses (https://twitter.com/pancakeswap/status/1369547285160370182) when initializing my uniswap variables, and I have confirmed that the .WETH() method returns the address for BNB (0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd).

It is also quite confusing, as to how this is supposed to work. When I deploy my contract, it lacks any BNB/liquidity pool, therefore the swapping functions can't work, because there is no price, right?

But I have tried adding liquidity manually, through the pancakeswap testnet router contract (as done here: Can't add liquidity on PancakeSwap with burnable ERC20 token - #10 by Yoshiko) and I got errors as well.

Very confusing how to proceed. The error, I must note, is TRANSFER_FROM_FAILED. It stems from swapTokensToEth.

`uint256 numTokensSellToAddToLiquidity = 10000;
uint256 _maxTxAmount = 100000;
bool inSwapAndLiquify = false;
bool swapAndLiquifyEnabled = false;
function _transferStandard(address sender, address recipient, uint256 tAmount) private {
require(balanceOf(msg.sender) >= tAmount, "Insuficient balance for transaction.");
(uint256 tTransferAmount, uint256 liqfee, uint256 azfee) = _getValues(tAmount);

    uint256 contractTokenBalance = balanceOf_token(address(this));

    if(contractTokenBalance >= _maxTxAmount)
    {
        contractTokenBalance = _maxTxAmount;
    }


    bool overMinTokenBalance = contractTokenBalance >= numTokensSellToAddToLiquidity;
    if (
        overMinTokenBalance &&
        !inSwapAndLiquify &&
        sender != uniswapV2Pair &&
        swapAndLiquifyEnabled
    ) {
        _tOwned[address(this)] = numTokensSellToAddToLiquidity;
        //add liquidity - this function runs
        swapAndLiquify(_tOwned[address(this)]);
    }

    _tOwned[msg.sender] -= tAmount;
    _tOwned[recipient] = _tOwned[recipient].add(tTransferAmount);
    _tOwned[devWallet] = _tOwned[devWallet].add(azfee);
    _tOwned[address(this)] = _tOwned[address(this)].add(liqfee);
    emit Transfer(sender, recipient, tTransferAmount);
}

function swapAndLiquify(uint256 contractTokenBalance) private {
    uint256 half = contractTokenBalance.div(2);
    uint256 otherHalf = contractTokenBalance.sub(half);
    //Function below, balanceOf_token retrieves tokens mapped to the address.
    uint256 initialBalance = balanceOf_token(address(this));
    //Function below results in error
   swapTokensForEth(half);

    /*uint256 newBalance = _tOwned[address(this)].sub(initialBalance);
    addLiquidity(otherHalf, newBalance);
    emit SwapAndLiquify(half, newBalance, otherHalf);*/
}

receive() external payable {}

function setSwapAndLiquifyEnabled(bool _enabled) public onlyOwner {
    swapAndLiquifyEnabled = _enabled;
    emit SwapAndLiquifyEnabledUpdated(_enabled);
}

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
    // Pools for each token of the pair must exist and have liquidity
    uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
        tokenAmount,
        0, // accept any amount of ETH
        path,
        address(this),
        block.timestamp
    );
}

function addLiquidity(uint256 tokenAmount, uint256 ethAmount) private {
    // approve token transfer to cover all possible scenarios
    _approve(address(this), address(uniswapV2Router), tokenAmount);

    // add the liquidity
    uniswapV2Router.addLiquidityETH{value: ethAmount}(
        address(this),
        tokenAmount,
        0, // slippage is unavoidable
        0, // slippage is unavoidable
        myWallet,
        block.timestamp
    );
}`
1 Like